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

Remove OpenStruct from Uses cmd #18806

Merged
merged 2 commits into from
Nov 25, 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
8 changes: 4 additions & 4 deletions Library/Homebrew/cli/named_args.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ def to_formulae
sig {
params(
only: T.nilable(Symbol),
ignore_unavailable: T.nilable(T::Boolean),
ignore_unavailable: T::Boolean,
method: T.nilable(Symbol),
uniq: T::Boolean,
warn: T::Boolean,
).returns(T::Array[T.any(Formula, Keg, Cask::Cask)])
}
def to_formulae_and_casks(
only: parent&.only_formula_or_cask,
ignore_unavailable: nil,
ignore_unavailable: false,
method: T.unsafe(nil),
uniq: true,
warn: T.unsafe(nil)
Expand Down Expand Up @@ -367,10 +367,10 @@ def to_kegs
end

sig {
params(only: T.nilable(Symbol), ignore_unavailable: T.nilable(T::Boolean), all_kegs: T.nilable(T::Boolean))
params(only: T.nilable(Symbol), ignore_unavailable: T::Boolean, all_kegs: T.nilable(T::Boolean))
.returns([T::Array[Keg], T::Array[Cask::Cask]])
}
def to_kegs_to_casks(only: parent&.only_formula_or_cask, ignore_unavailable: nil, all_kegs: nil)
def to_kegs_to_casks(only: parent&.only_formula_or_cask, ignore_unavailable: false, all_kegs: nil)
method = all_kegs ? :kegs : :default_kegs
@to_kegs_to_casks ||= {}
@to_kegs_to_casks[method] ||=
Expand Down
22 changes: 14 additions & 8 deletions Library/Homebrew/cmd/uses.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
require "formula"
require "cask/caskroom"
require "dependencies_helpers"
require "ostruct"

module Homebrew
module Cmd
Expand All @@ -15,6 +14,11 @@ module Cmd
class Uses < AbstractCommand
include DependenciesHelpers

class UnavailableFormula < T::Struct
const :name, String
const :full_name, String
end

cmd_args do
description <<~EOS
Show formulae and casks that specify <formula> as a dependency; that is, show dependents
Expand Down Expand Up @@ -64,10 +68,7 @@ def run
opoo e
used_formulae_missing = true
# If the formula doesn't exist: fake the needed formula object name.
# This is a legacy use of OpenStruct that should be refactored.
# rubocop:disable Style/OpenStructUse
args.named.map { |name| OpenStruct.new name:, full_name: name }
# rubocop:enable Style/OpenStructUse
args.named.map { |name| UnavailableFormula.new name:, full_name: name }
dduugg marked this conversation as resolved.
Show resolved Hide resolved
end

use_runtime_dependents = args.installed? &&
Expand All @@ -87,14 +88,19 @@ def run

private

sig { params(use_runtime_dependents: T::Boolean, used_formulae: T::Array[Formula]).returns(T::Array[Formula]) }
sig {
params(use_runtime_dependents: T::Boolean, used_formulae: T::Array[T.any(Formula, UnavailableFormula)])
.returns(T::Array[Formula])
}
def intersection_of_dependents(use_runtime_dependents, used_formulae)
recursive = args.recursive?
show_formulae_and_casks = !args.formula? && !args.cask?
includes, ignores = args_includes_ignores(args)

deps = []
if use_runtime_dependents
# We can only get here if `used_formulae_missing` is false, thus there are no UnavailableFormula.
used_formulae = T.cast(used_formulae, T::Array[Formula])
if show_formulae_and_casks || args.formula?
deps += used_formulae.map(&:runtime_installed_formula_dependents)
.reduce(&:&)
Expand Down Expand Up @@ -140,8 +146,8 @@ def intersection_of_dependents(use_runtime_dependents, used_formulae)

sig {
params(
dependents: T::Array[Formula], used_formulae: T::Array[Formula], recursive: T::Boolean,
includes: T::Array[Symbol], ignores: T::Array[Symbol]
dependents: T::Array[Formula], used_formulae: T::Array[T.any(Formula, UnavailableFormula)],
recursive: T::Boolean, includes: T::Array[Symbol], ignores: T::Array[Symbol]
).returns(
T::Array[Formula],
)
Expand Down
8 changes: 1 addition & 7 deletions Library/Homebrew/formula.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2886,7 +2886,7 @@ def run_test(keep_tmp: false)

sig { returns(T::Boolean) }
def test_defined?
false
method(:test).owner != Formula
reitermarkus marked this conversation as resolved.
Show resolved Hide resolved
end

def test; end
Expand Down Expand Up @@ -3348,12 +3348,6 @@ def inherited(child)
end
end

def method_added(method)
super

define_method(:test_defined?) { true } if method == :test
end

def freeze
specs.each(&:freeze)
@livecheck.freeze
Expand Down
3 changes: 0 additions & 3 deletions Library/Homebrew/formula.rbi
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# typed: strict

# This file provides definitions for Forwardable#delegate, which is currently not supported by Sorbet.

class Formula
def self.on_system_blocks_exist?; end
# This method is included by `OnSystem`
def self.on_macos(&block); end
end
19 changes: 19 additions & 0 deletions Library/Homebrew/test/cmd/uses_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require "cli/named_args"
require "cmd/shared_examples/args_parse"
require "cmd/uses"
require "fileutils"
Expand Down Expand Up @@ -44,4 +45,22 @@
.and not_to_output.to_stderr
.and be_a_success
end

it "handles unavailable formula", :integration_test do
setup_test_formula "foo"
setup_test_formula "bar"
setup_test_formula "optional", <<~RUBY
url "https://brew.sh/optional-1.0"
depends_on "bar" => :optional
RUBY

expect_any_instance_of(Homebrew::CLI::NamedArgs)
.to receive(:to_formulae)
.and_raise(FormulaUnavailableError, "foo")
cmd = described_class.new(%w[foo --eval-all --include-optional --recursive])
expect { cmd.run }
.to output(/^(bar\noptional|optional\nbar)$/).to_stdout
.and output(/Error: Missing formulae should not have dependents!\n/).to_stderr
.and raise_error SystemExit
end
end
Loading