From cf8835eff77a12029133615b817cd1cfeae5d27f Mon Sep 17 00:00:00 2001 From: Mike McQuaid Date: Tue, 1 Apr 2025 14:36:43 +0100 Subject: [PATCH] test/bundle: add additional test coverage. Add missing test coverage, requires, fix a TODO and remove `needs_macos` from a cask test. --- Library/Homebrew/bundle/brew_installer.rb | 3 +- .../test/bundle/brew_installer_spec.rb | 34 +++++++++++++++++++ .../test/bundle/commands/check_spec.rb | 17 ++++++++-- Library/Homebrew/test/bundle/skipper_spec.rb | 9 ++--- 4 files changed, 53 insertions(+), 10 deletions(-) diff --git a/Library/Homebrew/bundle/brew_installer.rb b/Library/Homebrew/bundle/brew_installer.rb index 83338ef85e..0d5ae35f7c 100644 --- a/Library/Homebrew/bundle/brew_installer.rb +++ b/Library/Homebrew/bundle/brew_installer.rb @@ -67,8 +67,7 @@ module Homebrew else Formula[@full_name].version end.to_s - version_path = Pathname.new(@version_file) - version_path.write("#{version}\n") + File.write(@version_file, "#{version}\n") puts "Wrote #{@name} version #{version} to #{@version_file}" if verbose end diff --git a/Library/Homebrew/test/bundle/brew_installer_spec.rb b/Library/Homebrew/test/bundle/brew_installer_spec.rb index 40edf2c12d..ec6b0f738e 100644 --- a/Library/Homebrew/test/bundle/brew_installer_spec.rb +++ b/Library/Homebrew/test/bundle/brew_installer_spec.rb @@ -260,6 +260,40 @@ RSpec.describe Homebrew::Bundle::BrewInstaller do end end end + + context "when the version_file option is provided" do + before do + Homebrew::Bundle.reset! + + allow_any_instance_of(described_class).to receive(:install_change_state!).and_return(true) + allow_any_instance_of(described_class).to receive(:installed?).and_return(true) + allow_any_instance_of(described_class).to receive(:linked?).and_return(true) + end + + let(:version_file) { "version.txt" } + let(:version) { "1.0" } + + context "when formula versions are changed and specified by the environment" do + before do + allow_any_instance_of(described_class).to receive(:changed?).and_return(false) + ENV["HOMEBREW_BUNDLE_EXEC_FORMULA_VERSION_#{formula_name.upcase}"] = version + end + + it "writes the version to the file" do + expect(File).to receive(:write).with(version_file, "#{version}\n") + described_class.preinstall(formula_name, version_file:) + described_class.install(formula_name, version_file:) + end + end + + context "when using the latest formula" do + it "writes the version to the file" do + expect(File).to receive(:write).with(version_file, "#{version}\n") + described_class.preinstall(formula_name, version_file:) + described_class.install(formula_name, version_file:) + end + end + end end context "when a formula isn't installed" do diff --git a/Library/Homebrew/test/bundle/commands/check_spec.rb b/Library/Homebrew/test/bundle/commands/check_spec.rb index 158d481816..2e62f9383c 100644 --- a/Library/Homebrew/test/bundle/commands/check_spec.rb +++ b/Library/Homebrew/test/bundle/commands/check_spec.rb @@ -3,6 +3,7 @@ require "bundle" require "bundle/commands/check" require "bundle/brew_checker" +require "bundle/cask_checker" require "bundle/mac_app_store_checker" require "bundle/vscode_extension_checker" require "bundle/brew_installer" @@ -56,11 +57,23 @@ RSpec.describe Homebrew::Bundle::Commands::Check do end context "when formulae are not installed" do - it "raises an error" do + let(:verbose) { true } + + it "raises an error and outputs to stdout" do allow(Homebrew::Bundle::CaskDumper).to receive(:casks).and_return([]) allow(Homebrew::Bundle::BrewInstaller).to receive(:upgradable_formulae).and_return([]) allow_any_instance_of(Pathname).to receive(:read).and_return("brew 'abc'") - expect { do_check }.to raise_error(SystemExit) + expect { do_check }.to raise_error(SystemExit).and \ + output(/brew bundle can't satisfy your Brewfile's dependencies/).to_stdout + end + + it "partially outputs when HOMEBREW_BUNDLE_CHECK_ALREADY_OUTPUT_FORMULAE_ERRORS is set" do + allow(Homebrew::Bundle::CaskDumper).to receive(:casks).and_return([]) + allow(Homebrew::Bundle::BrewInstaller).to receive(:upgradable_formulae).and_return([]) + allow_any_instance_of(Pathname).to receive(:read).and_return("brew 'abc'") + ENV["HOMEBREW_BUNDLE_CHECK_ALREADY_OUTPUT_FORMULAE_ERRORS"] = "abc" + expect { do_check }.to raise_error(SystemExit).and \ + output("Satisfy missing dependencies with `brew bundle install`.\n").to_stdout end it "does not raise error on skippable formula" do diff --git a/Library/Homebrew/test/bundle/skipper_spec.rb b/Library/Homebrew/test/bundle/skipper_spec.rb index 1e0049b1ff..379d0aadb0 100644 --- a/Library/Homebrew/test/bundle/skipper_spec.rb +++ b/Library/Homebrew/test/bundle/skipper_spec.rb @@ -27,19 +27,16 @@ RSpec.describe Homebrew::Bundle::Skipper do end end - context "with an unbottled formula on ARM", :needs_macos do + context "with an unbottled formula on ARM" do let(:entry) { Homebrew::Bundle::Dsl::Entry.new(:brew, "mysql") } - # TODO: remove OpenStruct usage - # rubocop:todo Style/OpenStructUse it "returns true" do allow(Hardware::CPU).to receive(:arm?).and_return(true) - allow_any_instance_of(Formula).to receive(:stable).and_return(OpenStruct.new(bottled?: false, - bottle_defined?: true)) + allow(Homebrew).to receive(:default_prefix?).and_return(true) + stub_formula_loader formula("mysql") { url "mysql-1.0" } expect(skipper.skip?(entry)).to be true end - # rubocop:enable Style/OpenStructUse end context "with an unlisted cask", :needs_macos do