Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add audit for Ruby files in taps. #17574

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 52 additions & 32 deletions Library/Homebrew/tap_auditor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,59 +4,67 @@
module Homebrew
# Auditor for checking common violations in {Tap}s.
class TapAuditor
attr_reader :name, :path, :formula_names, :formula_aliases, :formula_renames, :cask_tokens,
:tap_audit_exceptions, :tap_style_exceptions, :tap_pypi_formula_mappings, :problems
sig { returns(Tap) }
attr_reader :tap

attr_reader :problems

sig { params(tap: Tap, strict: T.nilable(T::Boolean)).void }
def initialize(tap, strict:)
Homebrew.with_no_api_env do
@name = tap.name
@path = tap.path
@tap_audit_exceptions = tap.audit_exceptions
@tap_style_exceptions = tap.style_exceptions
@tap_pypi_formula_mappings = tap.pypi_formula_mappings
@problems = []

@cask_tokens = tap.cask_tokens.map do |cask_token|
cask_token.split("/").last
end
@formula_aliases = tap.aliases.map do |formula_alias|
formula_alias.split("/").last
end
@formula_renames = tap.formula_renames
@formula_names = tap.formula_names.map do |formula_name|
formula_name.split("/").last
end
end
def initialize(tap, strict: nil)
@tap = tap
@problems = []
end

sig { void }
def audit
audit_json_files
audit_tap_formula_lists
audit_aliases_renames_duplicates
Homebrew.with_no_api_env do
audit_json_files
audit_ruby_files
audit_tap_formula_lists
audit_aliases_renames_duplicates
end
end

sig { void }
def audit_json_files
json_patterns = Tap::HOMEBREW_TAP_JSON_FILES.map { |pattern| @path/pattern }
json_patterns = Tap::HOMEBREW_TAP_JSON_FILES.map { |pattern| tap.path/pattern }
Pathname.glob(json_patterns).each do |file|
JSON.parse file.read
rescue JSON::ParserError
problem "#{file.to_s.delete_prefix("#{@path}/")} contains invalid JSON"
problem "#{file.to_s.delete_prefix("#{tap.path}/")} contains invalid JSON"
end
end

sig { void }
def audit_ruby_files
stray_ruby_files = tap.path.glob("**/*.rb", File::FNM_DOTMATCH)

stray_ruby_files -= tap.command_files
stray_ruby_files.reject! { _1.ascend.any?(tap.command_dir) }

# Allow mixed formula/cask taps except for core taps.
stray_ruby_files -= tap.cask_files unless tap.core_tap?
stray_ruby_files -= tap.formula_files unless tap.core_cask_tap?

return if stray_ruby_files.none?

problem "Ruby files in wrong location:\n#{stray_ruby_files.map(&:to_s).join("\n")}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should say what location they should be in. Even from reading the code: I don't really understand what would be the right/wrong location here. Can you elaborate?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct would be *.rb (if Formula/ doesn't exist), cmd/**/*.rb, Formula/**/*.rb and Casks/**/*.rb, anything else incorrect.

Additionally, Formula/**/*.rb is incorrect in homebrew/cask and cask/**/*.rb is incorrect in homebrew/core.

Copy link
Member

@Bo98 Bo98 Jun 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Homebrew/test-bot is a tap with formulae but also uses lib/**/*.rb and spec/**/*.rb.
Homebrew/portable-ruby has Abstract/*.rb.

Stray files detection here seems somewhat far reaching.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additionally, Formula/**/*.rb is incorrect in homebrew/cask and cask/**/*.rb is incorrect in homebrew/core.

This seems reasonable and a good add. Could also look for Ruby files that look like formulae or casks that aren't in a directory that Homebrew will ever find

Correct would be *.rb (if Formula/ doesn't exist), cmd/**/*.rb, Formula/**/*.rb and Casks/**/*.rb, anything else incorrect.

Agreed with @Bo98: this does not and feels like it's going to blow up on a lot of taps, official and otherwise.

What's the motivation here?

Copy link
Member Author

@reitermarkus reitermarkus Jun 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The motivation is to move the logic from the cask CI workflow into brew audit. I'm also fine with simly removing this check altogether.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which part of the cask CI currently checks this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I think either:

  • this check can be removed entirely
  • if we want it to be in Homebrew/brew: it should check for files than e.g. contain class .+ < Formula or cask ".+" do that aren't in potential_formula_dirs
    sig { returns(T::Array[Pathname]) }
    def potential_formula_dirs
    @potential_formula_dirs ||= [path/"Formula", path/"HomebrewFormula", path].freeze
    end
    or cask_dir
    sig { returns(Pathname) }
    def cask_dir
    @cask_dir ||= path/"Casks"
    end
    and leave all other random Ruby files alone

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Matching class .+ < Formula would still include the Abstract/*.rb mentioned above. Then again, I'm not a fan of subclassing Formula that way in the first place.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then: remove it entirely, I think.

end

sig { void }
def audit_tap_formula_lists
check_formula_list_directory "audit_exceptions", @tap_audit_exceptions
check_formula_list_directory "style_exceptions", @tap_style_exceptions
check_formula_list "pypi_formula_mappings", @tap_pypi_formula_mappings
check_formula_list_directory "audit_exceptions", tap.audit_exceptions
check_formula_list_directory "style_exceptions", tap.style_exceptions
check_formula_list "pypi_formula_mappings", tap.pypi_formula_mappings
end

sig { void }
def audit_aliases_renames_duplicates
duplicates = formula_aliases & formula_renames.keys
formula_aliases = tap.aliases.map do |formula_alias|
formula_alias.split("/").last
end

duplicates = formula_aliases & tap.formula_renames.keys
return if duplicates.none?

problem "The following should either be an alias or a rename, not both: #{duplicates.to_sentence}"
Expand All @@ -79,6 +87,18 @@ def check_formula_list(list_file, list)
return
end

cask_tokens = tap.cask_tokens.map do |cask_token|
cask_token.split("/").last
end

formula_aliases = tap.aliases.map do |formula_alias|
formula_alias.split("/").last
end

formula_names = tap.formula_names.map do |formula_name|
formula_name.split("/").last
end

list = list.keys if list.is_a? Hash
invalid_formulae_casks = list.select do |formula_or_cask_name|
formula_names.exclude?(formula_or_cask_name) &&
Expand All @@ -90,7 +110,7 @@ def check_formula_list(list_file, list)

problem <<~EOS
#{list_file}.json references
formulae or casks that are not found in the #{@name} tap.
formulae or casks that are not found in the #{tap.name} tap.
Invalid formulae or casks: #{invalid_formulae_casks.join(", ")}
EOS
end
Expand Down
79 changes: 79 additions & 0 deletions Library/Homebrew/test/tap_auditor_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# frozen_string_literal: true

require "tap_auditor"

RSpec.describe Homebrew::TapAuditor do
subject(:tap_auditor) { described_class.new(tap) }

let(:tap) { Tap.fetch("user", "repo") }
let(:ruby_files) { [] }

before do
tap.path.mkpath

ruby_files.each do |ruby_file|
ruby_file.dirname.mkpath
FileUtils.touch ruby_file
end
end

after do
tap.path.dirname.rmtree
end

context "when Ruby files are in a wrong location" do
let(:ruby_files) do
[
tap.path/"wrong"/"file.rb",
]
end

it "fails" do
tap_auditor.audit
expect(tap_auditor.problems.first[:message]).to match "Ruby files in wrong location"
end
end

context "when formula files are in homebrew/cask" do
let(:tap) { CoreCaskTap.instance }
let(:ruby_files) do
[
tap.path/"Formula"/"formula.rb",
]
end

it "fails" do
tap_auditor.audit
expect(tap_auditor.problems.first[:message]).to match "Ruby files in wrong location"
end
end

context "when cask files are in homebrew/core" do
let(:tap) { CoreTap.instance }
let(:ruby_files) do
[
tap.path/"Casks"/"cask.rb",
]
end

it "fails" do
tap_auditor.audit
expect(tap_auditor.problems.first[:message]).to match "Ruby files in wrong location"
end
end

context "when Ruby files are in correct locations" do
let(:ruby_files) do
[
tap.path/"cmd"/"cmd.rb",
tap.path/"Formula"/"formula.rb",
tap.path/"Casks"/"cask.rb",
]
end

it "passes" do
tap_auditor.audit
expect(tap_auditor.problems).to be_empty
end
end
end
Loading