[rubygems/rubygems] Only show "Defaulting to user installation" message when it matters.

https://github.com/rubygems/rubygems/commit/61b0947225
This commit is contained in:
Ellen Marie Dash 2023-11-29 18:49:08 -05:00 committed by git
parent 060f14bf62
commit 7008d97b76
2 changed files with 73 additions and 1 deletions

View File

@ -22,6 +22,15 @@ class Gem::Command
Gem::OptionParser.accept Symbol, &:to_sym
##
# Names of commands that should print "Defaulting to user installation"
# warning.
COMMANDS_WITH_AUTO_INSTALL_DIR_WARNING = [
"install",
"update",
].freeze
##
# The name of the command.
@ -323,7 +332,8 @@ class Gem::Command
elsif @when_invoked
@when_invoked.call options
else
if Gem.paths.auto_user_install && !options[:install_dir] && !options[:user_install]
if COMMANDS_WITH_AUTO_INSTALL_DIR_WARNING.include?(@command) && \
Gem.paths.auto_user_install && !options[:install_dir] && !options[:user_install]
self.ui.say "Defaulting to user installation because default installation directory (#{Gem.default_dir}) is not writable."
end

View File

@ -399,4 +399,66 @@ ERROR: Possible alternatives: non_existent_with_hint
assert_equal expected, @ui.error
end
def test_show_defaulting_to_user_install_when_appropriate
omit "this test doesn't work with ruby-core setup" if ruby_repo?
Gem.stub(:default_dir, "/this-directory-does-not-exist") do
# Replace `Gem.paths` with a new instance, so `Gem.paths.auto_user_install`
# is accurate.
Gem.stub(:paths, Gem::PathSupport.new(ENV)) do
output_regex = "Defaulting to user installation"
test_command = Class.new(Gem::Command) do
def initialize
# "gem install" should ALWAYS print the warning.
super("install", "Gem::Command instance for testing")
end
def execute
true
end
end
cmd = test_command.new
use_ui @ui do
cmd.invoke
assert_match output_regex, @ui.output,
"Gem.default_dir = #{Gem.default_dir.inspect}\n" \
"Gem.paths.auto_user_install = #{Gem.paths.auto_user_install.inspect}"
end
end
end
end
def test_dont_show_defaulting_to_user_install_when_appropriate
Gem.stub(:default_dir, "/this-directory-does-not-exist") do
# Replace `Gem.paths` with a new instance, so `Gem.paths.auto_user_install`
# is accurate.
Gem.stub(:paths, Gem::PathSupport.new(ENV)) do
output_regex = /^Defaulting to user installation/
test_command = Class.new(Gem::Command) do
def initialize
# "gem blargh" should NEVER print the warning.
super("blargh", "Gem::Command instance for testing")
end
def execute
true
end
end
cmd = test_command.new
use_ui @ui do
cmd.invoke
assert_no_match output_regex, @ui.output,
"Gem.default_dir = #{Gem.default_dir.inspect}\n" \
"Gem.paths.auto_user_install = #{Gem.paths.auto_user_install.inspect}"
end
end
end
end
end