Skip to content

Commit

Permalink
Merge pull request #1549 from rrotter/user_config_home
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeMcQuaid authored Jan 27, 2025
2 parents ef706d4 + 369ddcf commit e6dfce5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
5 changes: 3 additions & 2 deletions cmd/bundle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ class BundleCmd < AbstractCommand
flag "--file=",
description: "Read the `Brewfile` from this location. Use `--file=-` to pipe to stdin/stdout."
switch "--global",
description: "Read the `Brewfile` from `~/.Brewfile` or " \
"the `HOMEBREW_BUNDLE_FILE_GLOBAL` environment variable, if set."
description: "Read the `Brewfile` from `~/.Brewfile`, " \
"in the `HOMEBREW_USER_CONFIG_HOME` directory, " \
"or the `HOMEBREW_BUNDLE_FILE_GLOBAL` environment variable, if set."
switch "-v", "--verbose",
description: "`install` prints output from commands as they are run. " \
"`check` lists all missing dependencies."
Expand Down
9 changes: 7 additions & 2 deletions lib/bundle/brewfile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,20 @@ module Brewfile
def path(dash_writes_to_stdout: false, global: false, file: nil)
env_bundle_file_global = ENV.fetch("HOMEBREW_BUNDLE_FILE_GLOBAL", nil)
env_bundle_file = ENV.fetch("HOMEBREW_BUNDLE_FILE", nil)
user_config_home = ENV.fetch("HOMEBREW_USER_CONFIG_HOME", nil)

filename = if global
if env_bundle_file_global.present?
env_bundle_file_global
else
raise "'HOMEBREW_BUNDLE_FILE' cannot be specified with '--global'" if env_bundle_file.present?

Bundle.exchange_uid_if_needed! do
"#{Dir.home}/.Brewfile"
if user_config_home && File.exist?("#{user_config_home}/Brewfile")
"#{user_config_home}/Brewfile"
else
Bundle.exchange_uid_if_needed! do
"#{Dir.home}/.Brewfile"
end
end
end
elsif file.present?
Expand Down
16 changes: 16 additions & 0 deletions spec/bundle/brewfile_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,22 @@
let(:dash_writes_to_stdout) { false }
let(:env_bundle_file_global_value) { nil }
let(:env_bundle_file_value) { nil }
let(:env_user_config_home_value) { "/Users/username/.homebrew" }
let(:file_value) { nil }
let(:has_global) { false }
let(:config_dir_brewfile_exist) { false }

before do
allow(ENV).to receive(:fetch).and_return(nil)
allow(ENV).to receive(:fetch).with("HOMEBREW_BUNDLE_FILE_GLOBAL", any_args)
.and_return(env_bundle_file_global_value)
allow(ENV).to receive(:fetch).with("HOMEBREW_BUNDLE_FILE", any_args)
.and_return(env_bundle_file_value)

allow(ENV).to receive(:fetch).with("HOMEBREW_USER_CONFIG_HOME", any_args)
.and_return(env_user_config_home_value)
allow(File).to receive(:exist?).with("/Users/username/.homebrew/Brewfile")
.and_return(config_dir_brewfile_exist)
end

context "when `file` is specified with a relative path" do
Expand Down Expand Up @@ -154,6 +161,15 @@
expect(path).to eq(expected_pathname)
end
end

context "when HOMEBREW_USER_CONFIG_HOME/Brewfile exists" do
let(:config_dir_brewfile_exist) { true }
let(:expected_pathname) { Pathname.new("#{env_user_config_home_value}/Brewfile") }

it "returns the expected path" do
expect(path).to eq(expected_pathname)
end
end
end

context "when HOMEBREW_BUNDLE_FILE has a value" do
Expand Down

0 comments on commit e6dfce5

Please sign in to comment.