Skip to content

Commit

Permalink
Merge pull request #18463 from bevanjkay/deprecate-disable-reasons
Browse files Browse the repository at this point in the history
audit: audit deprecate/disable reasons
  • Loading branch information
MikeMcQuaid authored Oct 1, 2024
2 parents 419f3b3 + 53e8739 commit 3a9f952
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 0 deletions.
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 @@ def audit_cask_path
add_error "Cask should be located in '#{expected_path}'"
end

sig { void }
def audit_deprecate_disable
error = SharedAudits.check_deprecate_disable_reason(cask)
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

0 comments on commit 3a9f952

Please sign in to comment.