Skip to content

Commit

Permalink
Merge pull request #18968 from Homebrew/deprecate_disable-type
Browse files Browse the repository at this point in the history
deprecate_disable: `typed: strict`
  • Loading branch information
MikeMcQuaid authored Dec 23, 2024
2 parents d5e1b51 + eebcd60 commit 60f781f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
3 changes: 2 additions & 1 deletion Library/Homebrew/cask/installer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,12 @@ def install
raise
end

sig { void }
def check_deprecate_disable
deprecate_disable_type = DeprecateDisable.type(@cask)
return if deprecate_disable_type.nil?

message = DeprecateDisable.message(@cask)
message = DeprecateDisable.message(@cask).to_s
message_full = "#{@cask.token} has been #{message}"

case deprecate_disable_type
Expand Down
21 changes: 13 additions & 8 deletions Library/Homebrew/deprecate_disable.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# typed: true # rubocop:todo Sorbet/StrictSigil
# typed: strict
# frozen_string_literal: true

# Helper module for handling `disable!` and `deprecate!`.
# @api internal
module DeprecateDisable
module_function

FORMULA_DEPRECATE_DISABLE_REASONS = {
FORMULA_DEPRECATE_DISABLE_REASONS = T.let({
does_not_build: "does not build",
no_license: "has no license",
repo_archived: "has an archived upstream repository",
Expand All @@ -19,27 +19,29 @@ module DeprecateDisable
"a different checksum than the current one. " \
"Upstream's repository might have been compromised. " \
"We can re-package this once upstream has confirmed that they retagged their release",
}.freeze
}.freeze, T::Hash[Symbol, String])

CASK_DEPRECATE_DISABLE_REASONS = {
CASK_DEPRECATE_DISABLE_REASONS = T.let({
discontinued: "is discontinued upstream",
moved_to_mas: "is now exclusively distributed on the Mac App Store",
no_longer_available: "is no longer available upstream",
no_longer_meets_criteria: "no longer meets the criteria for acceptable casks",
unmaintained: "is not maintained upstream",
unsigned: "is unsigned or does not meet signature requirements",
}.freeze
}.freeze, T::Hash[Symbol, String])

# One year when << or >> to Date.today.
REMOVE_DISABLED_TIME_WINDOW = 12
REMOVE_DISABLED_BEFORE = (Date.today << REMOVE_DISABLED_TIME_WINDOW).freeze
REMOVE_DISABLED_BEFORE = T.let((Date.today << REMOVE_DISABLED_TIME_WINDOW).freeze, Date)

sig { params(formula_or_cask: T.any(Formula, Cask::Cask)).returns(T.nilable(Symbol)) }
def type(formula_or_cask)
return :deprecated if formula_or_cask.deprecated?

:disabled if formula_or_cask.disabled?
end

sig { params(formula_or_cask: T.any(Formula, Cask::Cask)).returns(T.nilable(String)) }
def message(formula_or_cask)
return if type(formula_or_cask).blank?

Expand Down Expand Up @@ -92,9 +94,12 @@ def message(formula_or_cask)
message
end

sig { params(string: T.nilable(String), type: Symbol).returns(T.nilable(T.any(String, Symbol))) }
def to_reason_string_or_symbol(string, type:)
if (type == :formula && FORMULA_DEPRECATE_DISABLE_REASONS.key?(string&.to_sym)) ||
(type == :cask && CASK_DEPRECATE_DISABLE_REASONS.key?(string&.to_sym))
return if string.nil?

if (type == :formula && FORMULA_DEPRECATE_DISABLE_REASONS.key?(string.to_sym)) ||
(type == :cask && CASK_DEPRECATE_DISABLE_REASONS.key?(string.to_sym))
return string.to_sym
end

Expand Down
23 changes: 23 additions & 0 deletions Library/Homebrew/test/deprecate_disable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,24 @@
require "deprecate_disable"

RSpec.describe DeprecateDisable do
let(:deprecate_date) { Date.parse("2020-01-01") }
let(:disable_date) { deprecate_date >> DeprecateDisable::REMOVE_DISABLED_TIME_WINDOW }
let(:deprecated_formula) do
instance_double(Formula, deprecated?: true, disabled?: false, deprecation_reason: :does_not_build,
deprecation_replacement: nil, deprecation_date: nil, disable_date: nil)
end
let(:deprecated_formula_with_date) do
instance_double(Formula, deprecated?: true, disabled?: false, deprecation_reason: :does_not_build,
deprecation_replacement: nil, deprecation_date: deprecate_date, disable_date: nil)
end
let(:disabled_formula) do
instance_double(Formula, deprecated?: false, disabled?: true, disable_reason: "is broken",
disable_replacement: nil, deprecation_date: nil, disable_date: nil)
end
let(:disabled_formula_with_date) do
instance_double(Formula, deprecated?: false, disabled?: true, disable_reason: :does_not_build,
disable_replacement: nil, deprecation_date: nil, disable_date: disable_date)
end
let(:deprecated_cask) do
instance_double(Cask::Cask, deprecated?: true, disabled?: false, deprecation_reason: :discontinued,
deprecation_replacement: nil, deprecation_date: nil, disable_date: nil)
Expand Down Expand Up @@ -39,7 +49,9 @@
before do
formulae = [
deprecated_formula,
deprecated_formula_with_date,
disabled_formula,
disabled_formula_with_date,
deprecated_formula_with_replacement,
disabled_formula_with_replacement,
]
Expand Down Expand Up @@ -86,11 +98,22 @@
.to eq "deprecated because it does not build!"
end

it "returns a deprecation message with disable date" do
allow(Date).to receive(:today).and_return(deprecate_date + 1)
expect(described_class.message(deprecated_formula_with_date))
.to eq "deprecated because it does not build! It will be disabled on #{disable_date}."
end

it "returns a disable message with a custom reason" do
expect(described_class.message(disabled_formula))
.to eq "disabled because it is broken!"
end

it "returns a disable message with disable date" do
expect(described_class.message(disabled_formula_with_date))
.to eq "disabled because it does not build! It was disabled on #{disable_date}."
end

it "returns a deprecation message with a preset cask reason" do
expect(described_class.message(deprecated_cask))
.to eq "deprecated because it is discontinued upstream!"
Expand Down

0 comments on commit 60f781f

Please sign in to comment.