Skip to content

Commit 3a35890

Browse files
CopilotMikeMcQuaid
andcommitted
Address backwards compatibility concerns and use Pathname#mkpath
- Restore backwards compatibility by checking both XDG and legacy Brewfile paths - Use existing file when available, only use XDG path for new file creation - Replace FileUtils.mkdir_p with Pathname#mkpath as requested - Add comprehensive test coverage for all scenarios Co-authored-by: MikeMcQuaid <[email protected]>
1 parent fb65fa9 commit 3a35890

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

Library/Homebrew/bundle/brewfile.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,15 @@ def self.path(dash_writes_to_stdout: false, global: false, file: nil)
2525
raise "'HOMEBREW_BUNDLE_FILE' cannot be specified with '--global'" if env_bundle_file.present?
2626

2727
if user_config_home
28-
"#{user_config_home}/Brewfile"
28+
xdg_path = "#{user_config_home}/Brewfile"
29+
legacy_path = Bundle.exchange_uid_if_needed! { "#{Dir.home}/.Brewfile" }
30+
31+
# Prefer existing file to maintain backwards compatibility
32+
if File.exist?(xdg_path) || !File.exist?(legacy_path)
33+
xdg_path
34+
else
35+
legacy_path
36+
end
2937
else
3038
Bundle.exchange_uid_if_needed! do
3139
"#{Dir.home}/.Brewfile"

Library/Homebrew/bundle/dumper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def self.brewfile_path(global: false, file: nil)
8181
sig { params(file: Pathname, content: String).void }
8282
def self.write_file(file, content)
8383
Bundle.exchange_uid_if_needed! do
84-
FileUtils.mkdir_p file.parent
84+
file.parent.mkpath
8585
file.open("w") { |io| io.write content }
8686
end
8787
end

Library/Homebrew/test/bundle/brewfile_spec.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# typed: strict
21
# frozen_string_literal: true
32

43
require "bundle"
@@ -173,10 +172,16 @@
173172
end
174173
end
175174

176-
context "when HOMEBREW_USER_CONFIG_HOME is set but Brewfile does not exist" do
175+
context "when HOMEBREW_USER_CONFIG_HOME is set but neither Brewfile exists" do
177176
let(:config_dir_brewfile_exist) { false }
178177

179-
it "returns the XDG-compliant path when HOMEBREW_USER_CONFIG_HOME is set" do
178+
before do
179+
# Mock that both XDG and legacy Brewfiles don't exist
180+
allow(File).to receive(:exist?).with("/Users/username/.homebrew/Brewfile").and_return(false)
181+
allow(File).to receive(:exist?).with("#{Dir.home}/.Brewfile").and_return(false)
182+
end
183+
184+
it "returns the XDG path for initial Brewfile creation" do
180185
expect(path).to eq(Pathname.new("#{env_user_config_home_value}/Brewfile"))
181186
end
182187
end

0 commit comments

Comments
 (0)