diff options
author | Ellen Marie Dash <[email protected]> | 2023-11-29 18:49:08 -0500 |
---|---|---|
committer | git <[email protected]> | 2023-11-30 19:58:40 +0000 |
commit | 7008d97b76649928cd5552eb0f44a496d468daf0 (patch) | |
tree | 5dfe0a5e2ab0ccd82c9da6302ab58200706cff06 | |
parent | 060f14bf62ad3f426a6666901c45b82d4334fa26 (diff) |
[rubygems/rubygems] Only show "Defaulting to user installation" message when it matters.
https://github.com/rubygems/rubygems/commit/61b0947225
-rw-r--r-- | lib/rubygems/command.rb | 12 | ||||
-rw-r--r-- | test/rubygems/test_gem_command.rb | 62 |
2 files changed, 73 insertions, 1 deletions
diff --git a/lib/rubygems/command.rb b/lib/rubygems/command.rb index fd2cf61a05..c3a448bc21 100644 --- a/lib/rubygems/command.rb +++ b/lib/rubygems/command.rb @@ -23,6 +23,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. attr_reader :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 diff --git a/test/rubygems/test_gem_command.rb b/test/rubygems/test_gem_command.rb index 3d0f04830e..d50f3d2c79 100644 --- a/test/rubygems/test_gem_command.rb +++ b/test/rubygems/test_gem_command.rb @@ -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 |