Files
brew/Library/Homebrew/test/cmd/shared_examples/args_parse.rb
T
Mike McQuaid 56f42e0e1d Make tests use public APIs and enforce via RuboCop
- Tests reached into private APIs with `send` and poked internal
  state via `instance_variable_get`/`instance_variable_set`, hiding
  which interfaces specs really exercise and letting visibility rot.
- Make every statically-poked method public and call it directly,
  keeping `public_send` only for dynamically-named public methods.
- Read and write state through public `attr_*` accessors instead of
  instance variable reflection.
- Add `Homebrew/NoSendInTests` and
  `Homebrew/NoInstanceVariableAccessInTests` cops scoped to
  `Library/Homebrew/test` so neither pattern creeps back in.
- Keep rare justified disables, e.g. `Module#remove_const` is
  private core Ruby and raw memoisation state has no accessor.
2026-07-24 19:55:53 +01:00

25 lines
816 B
Ruby

# typed: false
# frozen_string_literal: true
RSpec.shared_examples "parseable arguments" do |command_name: nil|
let(:command) do |example|
example.metadata.dig(:example_group, :parent_example_group, :description)
end
it "can parse arguments" do
if described_class
klass = described_class
else
# for tests of remote taps, we need to load the command class
require(Commands.external_ruby_v2_cmd_path(command_name))
# The command class name is only known at runtime.
# rubocop:disable Sorbet/ConstantsFromStrings
klass = Object.const_get(command)
# rubocop:enable Sorbet/ConstantsFromStrings
end
argv = klass.parser.min_named_args&.times&.map { "argument" } || []
cmd = klass.new(argv)
expect(cmd.args).to be_a Homebrew::CLI::Args
end
end