Address reviewer feedback: check XDG mode properly and make lazy
Co-authored-by: MikeMcQuaid <125011+MikeMcQuaid@users.noreply.github.com>
This commit is contained in:
parent
3a358905ac
commit
040fd9cd91
@ -24,16 +24,22 @@ module Homebrew
|
|||||||
else
|
else
|
||||||
raise "'HOMEBREW_BUNDLE_FILE' cannot be specified with '--global'" if env_bundle_file.present?
|
raise "'HOMEBREW_BUNDLE_FILE' cannot be specified with '--global'" if env_bundle_file.present?
|
||||||
|
|
||||||
if user_config_home
|
# Determine if we're in XDG mode by checking if user_config_home is XDG-based
|
||||||
xdg_path = "#{user_config_home}/Brewfile"
|
# In bin/brew: XDG_CONFIG_HOME set -> HOMEBREW_USER_CONFIG_HOME="${XDG_CONFIG_HOME}/homebrew"
|
||||||
legacy_path = Bundle.exchange_uid_if_needed! { "#{Dir.home}/.Brewfile" }
|
# XDG_CONFIG_HOME not set -> HOMEBREW_USER_CONFIG_HOME="${HOME}/.homebrew"
|
||||||
|
using_xdg = user_config_home&.end_with?("/homebrew")
|
||||||
# Prefer existing file to maintain backwards compatibility
|
|
||||||
if File.exist?(xdg_path) || !File.exist?(legacy_path)
|
if using_xdg && user_config_home
|
||||||
xdg_path
|
# XDG mode: check both XDG and legacy locations, preferring existing files
|
||||||
|
xdg_brewfile = "#{user_config_home}/Brewfile"
|
||||||
|
if File.exist?(xdg_brewfile)
|
||||||
|
xdg_brewfile
|
||||||
else
|
else
|
||||||
legacy_path
|
legacy_brewfile = Bundle.exchange_uid_if_needed! { "#{Dir.home}/.Brewfile" }
|
||||||
|
File.exist?(legacy_brewfile) ? legacy_brewfile : xdg_brewfile
|
||||||
end
|
end
|
||||||
|
elsif user_config_home && File.exist?("#{user_config_home}/Brewfile")
|
||||||
|
"#{user_config_home}/Brewfile"
|
||||||
else
|
else
|
||||||
Bundle.exchange_uid_if_needed! do
|
Bundle.exchange_uid_if_needed! do
|
||||||
"#{Dir.home}/.Brewfile"
|
"#{Dir.home}/.Brewfile"
|
||||||
|
@ -26,8 +26,11 @@ RSpec.describe Homebrew::Bundle::Brewfile do
|
|||||||
|
|
||||||
allow(ENV).to receive(:fetch).with("HOMEBREW_USER_CONFIG_HOME", any_args)
|
allow(ENV).to receive(:fetch).with("HOMEBREW_USER_CONFIG_HOME", any_args)
|
||||||
.and_return(env_user_config_home_value)
|
.and_return(env_user_config_home_value)
|
||||||
|
allow(ENV).to receive(:[]).with("XDG_CONFIG_HOME").and_return(nil)
|
||||||
allow(File).to receive(:exist?).with("/Users/username/.homebrew/Brewfile")
|
allow(File).to receive(:exist?).with("/Users/username/.homebrew/Brewfile")
|
||||||
.and_return(config_dir_brewfile_exist)
|
.and_return(config_dir_brewfile_exist)
|
||||||
|
# Allow any other File.exist? calls to return false by default
|
||||||
|
allow(File).to receive(:exist?).and_return(false)
|
||||||
end
|
end
|
||||||
|
|
||||||
context "when `file` is specified with a relative path" do
|
context "when `file` is specified with a relative path" do
|
||||||
@ -175,14 +178,27 @@ RSpec.describe Homebrew::Bundle::Brewfile do
|
|||||||
context "when HOMEBREW_USER_CONFIG_HOME is set but neither Brewfile exists" do
|
context "when HOMEBREW_USER_CONFIG_HOME is set but neither Brewfile exists" do
|
||||||
let(:config_dir_brewfile_exist) { false }
|
let(:config_dir_brewfile_exist) { false }
|
||||||
|
|
||||||
before do
|
context "when XDG_CONFIG_HOME is set" do
|
||||||
# Mock that both XDG and legacy Brewfiles don't exist
|
before do
|
||||||
allow(File).to receive(:exist?).with("/Users/username/.homebrew/Brewfile").and_return(false)
|
allow(ENV).to receive(:[]).with("XDG_CONFIG_HOME").and_return("/custom/xdg")
|
||||||
allow(File).to receive(:exist?).with("#{Dir.home}/.Brewfile").and_return(false)
|
# Mock that both XDG and legacy Brewfiles don't exist
|
||||||
|
allow(File).to receive(:exist?).with("/Users/username/.homebrew/Brewfile").and_return(false)
|
||||||
|
allow(File).to receive(:exist?).with("#{Dir.home}/.Brewfile").and_return(false)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns the XDG path for initial Brewfile creation" do
|
||||||
|
expect(path).to eq(Pathname.new("#{env_user_config_home_value}/Brewfile"))
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it "returns the XDG path for initial Brewfile creation" do
|
context "when XDG_CONFIG_HOME is not set" do
|
||||||
expect(path).to eq(Pathname.new("#{env_user_config_home_value}/Brewfile"))
|
before do
|
||||||
|
allow(ENV).to receive(:[]).with("XDG_CONFIG_HOME").and_return(nil)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns the legacy path" do
|
||||||
|
expect(path).to eq(Pathname.new("#{Dir.home}/.Brewfile"))
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user