From 9242457df5af7b05adbda1439904010e23ead1e2 Mon Sep 17 00:00:00 2001 From: Mike McQuaid Date: Fri, 7 Jun 2024 10:14:39 +0100 Subject: [PATCH] Fix `brew bundle --cleanup` from `stdin` This was previously failing to read a valid `Brewfile` on repeated reads so attempting to cleanup everything. Instead, cache the `Brewfile` DSL in the `install` command and pass it to the `cleanup` command. Fixes #1378 --- cmd/bundle.rb | 1 + lib/bundle/commands/cleanup.rb | 4 +++- lib/bundle/commands/exec.rb | 4 ++-- lib/bundle/commands/install.rb | 8 ++++++-- spec/bundle/commands/install_command_spec.rb | 12 ++++++++++++ 5 files changed, 24 insertions(+), 5 deletions(-) diff --git a/cmd/bundle.rb b/cmd/bundle.rb index 4ad8f5d2c..652632fd0 100755 --- a/cmd/bundle.rb +++ b/cmd/bundle.rb @@ -129,6 +129,7 @@ def run file: args.file, force: true, zap: args.zap?, + dsl: Bundle::Commands::Install.dsl, ) end when "dump" diff --git a/lib/bundle/commands/cleanup.rb b/lib/bundle/commands/cleanup.rb index 777c4569a..31e75b0b6 100644 --- a/lib/bundle/commands/cleanup.rb +++ b/lib/bundle/commands/cleanup.rb @@ -18,7 +18,9 @@ def reset! Bundle::BrewServices.reset! end - def run(global: false, file: nil, force: false, zap: false) + def run(global: false, file: nil, force: false, zap: false, dsl: nil) + @dsl ||= dsl + casks = casks_to_uninstall(global:, file:) formulae = formulae_to_uninstall(global:, file:) taps = taps_to_untap(global:, file:) diff --git a/lib/bundle/commands/exec.rb b/lib/bundle/commands/exec.rb index 4e42841eb..a546da6fa 100644 --- a/lib/bundle/commands/exec.rb +++ b/lib/bundle/commands/exec.rb @@ -25,12 +25,12 @@ def run(*args, global: false, file: nil) command_path = command_path.dirname.to_s end - brewfile = Brewfile.read(global:, file:) + @dsl = Brewfile.read(global:, file:) require "formula" require "formulary" - ENV.deps = brewfile.entries.map do |entry| + ENV.deps = @dsl.entries.map do |entry| next if entry.type != :brew f = Formulary.factory(entry.name) diff --git a/lib/bundle/commands/install.rb b/lib/bundle/commands/install.rb index 8227f4e2d..de9d917d5 100644 --- a/lib/bundle/commands/install.rb +++ b/lib/bundle/commands/install.rb @@ -6,12 +6,16 @@ module Install module_function def run(global: false, file: nil, no_lock: false, no_upgrade: false, verbose: false, force: false, quiet: false) - parsed_entries = Brewfile.read(global:, file:).entries + @dsl = Brewfile.read(global:, file:) Bundle::Installer.install( - parsed_entries, + @dsl.entries, global:, file:, no_lock:, no_upgrade:, verbose:, force:, quiet:, ) || exit(1) end + + def dsl + @dsl + end end end end diff --git a/spec/bundle/commands/install_command_spec.rb b/spec/bundle/commands/install_command_spec.rb index 41f5227bc..0975a2f06 100644 --- a/spec/bundle/commands/install_command_spec.rb +++ b/spec/bundle/commands/install_command_spec.rb @@ -38,6 +38,18 @@ expect { described_class.run }.not_to raise_error end + it "#dsl returns a valid DSL" do + allow(Bundle::TapInstaller).to receive(:preinstall).and_return(false) + allow(Bundle::WhalebrewInstaller).to receive(:preinstall).and_return(false) + allow(Bundle::VscodeExtensionInstaller).to receive(:preinstall).and_return(false) + allow(Bundle::BrewInstaller).to receive_messages(preinstall: true, install: true) + allow(Bundle::CaskInstaller).to receive_messages(preinstall: true, install: true) + allow(Bundle::MacAppStoreInstaller).to receive_messages(preinstall: true, install: true) + allow_any_instance_of(Pathname).to receive(:read).and_return(brewfile_contents) + described_class.run + expect(described_class.dsl.entries.first.name).to eql("phinze/cask") + end + it "does not raise an error when skippable" do expect(Bundle::BrewInstaller).not_to receive(:install)