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

audit: audit deprecate/disable reasons #18463

Merged
merged 5 commits into from
Oct 1, 2024
Merged
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
6 changes: 6 additions & 0 deletions Library/Homebrew/cask/audit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,12 @@
add_error "Cask should be located in '#{expected_path}'"
end

sig { void }
def audit_deprecate_disable
error = SharedAudits.check_deprecate_disable_reason(cask)

Check warning on line 951 in Library/Homebrew/cask/audit.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/cask/audit.rb#L951

Added line #L951 was not covered by tests
add_error error if error
end

sig {
params(
url_to_check: T.any(String, URL),
Expand Down
5 changes: 5 additions & 0 deletions Library/Homebrew/formula_auditor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,11 @@ def audit_prefix_has_contents
EOS
end

def audit_deprecate_disable
error = SharedAudits.check_deprecate_disable_reason(formula)
problem error if error
end

def quote_dep(dep)
dep.is_a?(Symbol) ? dep.inspect : "'#{dep}'"
end
Expand Down
47 changes: 47 additions & 0 deletions Library/Homebrew/test/cask/audit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1142,5 +1142,52 @@ def tmp_cask(name, text)
it { is_expected.to error_with(/a homepage stanza is required/) }
end
end

describe "checking deprecate/disable" do
let(:only) { ["deprecate_disable"] }
let(:cask_token) { "deprecated-cask" }

context "when deprecate/disable is used with a valid reason" do
let(:cask) do
tmp_cask cask_token.to_s, <<~RUBY
cask '#{cask_token}' do
version "1.0"
sha256 "8dd95daa037ac02455435446ec7bc737b34567afe9156af7d20b2a83805c1d8a"
url "https://brew.sh/foo.zip"
name "Audit"
desc "Cask Auditor"
homepage "https://brew.sh/"
app "Audit.app"
deprecate! date: "2021-01-01", because: :foobar
end
RUBY
end

it "fails" do
expect(run).to error_with(/foobar is not a valid deprecate! or disable! reason/)
end
end

context "when deprecate/disable is used with an invalid reason" do
let(:cask) do
tmp_cask cask_token.to_s, <<~RUBY
cask '#{cask_token}' do
version "1.0"
sha256 "8dd95daa037ac02455435446ec7bc737b34567afe9156af7d20b2a83805c1d8a"
url "https://brew.sh/foo.zip"
name "Audit"
desc "Cask Auditor"
homepage "https://brew.sh/"
app "Audit.app"
disable! date: "2021-01-01", because: :discontinued
end
RUBY
end

it "passes" do
expect(run).to pass
end
end
end
end
end
31 changes: 31 additions & 0 deletions Library/Homebrew/test/formula_auditor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1312,4 +1312,35 @@ class FooAT11 < Formula
.to match("Formula foo should also have a conflict declared with bar")
end
end

describe "#audit_deprecate_disable" do
specify "it warns when deprecate/disable reason is invalid" do
fa = formula_auditor "foo", <<~RUBY
class Foo < Formula
url "https://brew.sh/foo-1.0.tgz"

deprecate! date: "2021-01-01", because: :foobar
end
RUBY

mkdir_p fa.formula.prefix
fa.audit_deprecate_disable
expect(fa.problems.first[:message])
.to match("foobar is not a valid deprecate! or disable! reason")
end

specify "it does not warn when deprecate/disable reason is valid" do
fa = formula_auditor "foo", <<~RUBY
class Foo < Formula
url "https://brew.sh/foo-1.0.tgz"

deprecate! date: "2021-01-01", because: :repo_archived
end
RUBY

mkdir_p fa.formula.prefix
fa.audit_deprecate_disable
expect(fa.problems).to be_empty
end
end
end
16 changes: 16 additions & 0 deletions Library/Homebrew/utils/shared_audits.rb
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,20 @@ def self.github_tag_from_url(url)
def self.gitlab_tag_from_url(url)
url[%r{^https://gitlab\.com/(?:\w[\w.-]*/){2,}-/archive/([^/]+)/}, 1]
end

sig { params(formula_or_cask: T.any(Formula, Cask::Cask)).returns(T.nilable(String)) }
def self.check_deprecate_disable_reason(formula_or_cask)
return if !formula_or_cask.deprecated? && !formula_or_cask.disabled?

reason = formula_or_cask.deprecated? ? formula_or_cask.deprecation_reason : formula_or_cask.disable_reason
return unless reason.is_a?(Symbol)

reasons = if formula_or_cask.is_a?(Formula)
DeprecateDisable::FORMULA_DEPRECATE_DISABLE_REASONS
else
DeprecateDisable::CASK_DEPRECATE_DISABLE_REASONS
end

"#{reason} is not a valid deprecate! or disable! reason" unless reasons.include?(reason)
end
end
Loading