Skip to content

Commit

Permalink
Merge pull request #17007 from Homebrew/remote-tap-tests
Browse files Browse the repository at this point in the history
Clean up args_parse tests
  • Loading branch information
MikeMcQuaid committed Apr 2, 2024
2 parents 21dcf3c + 8ebcadd commit f0e442d
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 32 deletions.
34 changes: 18 additions & 16 deletions Library/Homebrew/test/cmd/bundle_spec.rb
@@ -1,23 +1,25 @@
# frozen_string_literal: true

RSpec.describe "brew bundle", :integration_test do
describe "check" do
it "checks if a Brewfile's dependencies are satisfied", :needs_network do
setup_remote_tap "homebrew/bundle"
require "cmd/shared_examples/args_parse"

HOMEBREW_REPOSITORY.cd do
system "git", "init"
system "git", "commit", "--allow-empty", "-m", "This is a test commit"
end
RSpec.describe "Homebrew::Cmd::BundleCmd", :integration_test, :needs_network do
before { setup_remote_tap "homebrew/bundle" }

it_behaves_like "parseable arguments", command_name: "bundle"

it "checks if a Brewfile's dependencies are satisfied" do
HOMEBREW_REPOSITORY.cd do
system "git", "init"
system "git", "commit", "--allow-empty", "-m", "This is a test commit"
end

mktmpdir do |path|
FileUtils.touch "#{path}/Brewfile"
path.cd do
expect { brew "bundle", "check" }
.to output("The Brewfile's dependencies are satisfied.\n").to_stdout
.and not_to_output.to_stderr
.and be_a_success
end
mktmpdir do |path|
FileUtils.touch "#{path}/Brewfile"
path.cd do
expect { brew "bundle", "check" }
.to output("The Brewfile's dependencies are satisfied.\n").to_stdout
.and not_to_output.to_stderr
.and be_a_success
end
end
end
Expand Down
10 changes: 7 additions & 3 deletions Library/Homebrew/test/cmd/services_spec.rb
@@ -1,9 +1,13 @@
# frozen_string_literal: true

RSpec.describe "brew services", :integration_test, :needs_network do
it "allows controlling services" do
setup_remote_tap "homebrew/services"
require "cmd/shared_examples/args_parse"

RSpec.describe "Homebrew::Cmd::Services", :integration_test, :needs_network do
before { setup_remote_tap "homebrew/services" }

it_behaves_like "parseable arguments", command_name: "services"

it "allows controlling services" do
expect { brew "services", "list" }
.to not_to_output.to_stderr
.and not_to_output.to_stdout
Expand Down
22 changes: 10 additions & 12 deletions Library/Homebrew/test/cmd/shared_examples/args_parse.rb
@@ -1,22 +1,20 @@
# frozen_string_literal: true

RSpec.shared_examples "parseable arguments" do |argv: nil|
subject(:method_name) { "#{command_name.tr("-", "_")}_args" }

let(:command_name) do |example|
example.metadata[:example_group][:parent_example_group][:description].delete_prefix("brew ")
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
argv ||= described_class.parser.instance_variable_get(:@min_named_args)&.times&.map { "argument" }
argv ||= []
cmd = described_class.new(argv)
expect(cmd.args).to be_a Homebrew::CLI::Args
klass = described_class
else
require "dev-cmd/#{command_name}" unless require? "cmd/#{command_name}"
parser = Homebrew.public_send(method_name)
expect(parser).to respond_to(:parse)
# for tests of remote taps, we need to load the command class
require(Commands.external_ruby_v2_cmd_path(command_name))
klass = Object.const_get(command)
end
argv = klass.parser.instance_variable_get(:@min_named_args)&.times&.map { "argument" } || []
cmd = klass.new(argv)
expect(cmd.args).to be_a Homebrew::CLI::Args
end
end
2 changes: 1 addition & 1 deletion Library/Homebrew/test/dev-cmd/prof_spec.rb
Expand Up @@ -4,7 +4,7 @@
require "dev-cmd/prof"

RSpec.describe Homebrew::DevCmd::Prof do
it_behaves_like "parseable arguments", argv: ["--", "help"]
it_behaves_like "parseable arguments"

describe "integration tests", :integration_test, :needs_network do
after do
Expand Down

0 comments on commit f0e442d

Please sign in to comment.