diff --git a/bundler/lib/bundler/cli.rb b/bundler/lib/bundler/cli.rb index 0f234f26b956..6bb1f561c28d 100644 --- a/bundler/lib/bundler/cli.rb +++ b/bundler/lib/bundler/cli.rb @@ -185,6 +185,12 @@ def help(cli = nil) end def self.handle_no_command_error(command, has_namespace = $thor_runner) + if command == "upgrade" + command = Bundler.ui.add_color("bundle update ", :bold, :cyan) + Bundler.ui.info("Please use #{command} to update gems in your bundle.") + exit(1) + end + if Bundler.settings[:plugins] && Bundler::Plugin.command?(command) return Bundler::Plugin.exec_command(command, ARGV[1..-1]) end diff --git a/bundler/spec/other/cli_dispatch_spec.rb b/bundler/spec/other/cli_dispatch_spec.rb index a2c745b0703d..bc0a3016dd19 100644 --- a/bundler/spec/other/cli_dispatch_spec.rb +++ b/bundler/spec/other/cli_dispatch_spec.rb @@ -17,4 +17,10 @@ bundle "in", raise_on_error: false expect(err).to eq("Ambiguous command in matches [info, init, install]") end + + it "prints a helpful message for 'upgrade'" do + bundle "upgrade", raise_on_error: false + expect(out).to eq("Please use bundle update to update gems in your bundle.") + expect(exitstatus).to eq(1) + end end diff --git a/lib/rubygems/exceptions.rb b/lib/rubygems/exceptions.rb index 40485bbadff8..2f100cd35f13 100644 --- a/lib/rubygems/exceptions.rb +++ b/lib/rubygems/exceptions.rb @@ -26,6 +26,44 @@ def self.attach_correctable DidYouMean.correct_error(Gem::UnknownCommandError, Gem::UnknownCommandSpellChecker) end end + + def detailed_message(highlight: true, **) + msg = super(highlight: highlight) + + case unknown_command + when "upgrade" + command = "bundle update " + + bold = "\e[1m" + cyan = "\e[36m" + reset = "\e[0m" + + command = if highlight && can_display_colors? + "#{bold}#{cyan}#{command}#{reset}" + else + "`#{command}`" + end + + msg + "\nPlease use #{command} to update gems in your bundle." + + else + msg + end + end + + private + + def can_display_colors? + are_colors_supported? && !are_colors_disabled? + end + + def are_colors_supported? + stdout.tty? && ENV["TERM"] != "dumb" + end + + def are_colors_disabled? + !ENV["NO_COLOR"].nil? && !ENV["NO_COLOR"].empty? + end end class Gem::DependencyError < Gem::Exception; end diff --git a/test/rubygems/test_gem_command_manager.rb b/test/rubygems/test_gem_command_manager.rb index 889d5ce9e66a..55d4d573e37c 100644 --- a/test/rubygems/test_gem_command_manager.rb +++ b/test/rubygems/test_gem_command_manager.rb @@ -91,6 +91,24 @@ def test_find_command_unknown_suggestions assert_equal message, actual_message end + def test_upgrade_points_users_to_update + e = assert_raise Gem::UnknownCommandError do + @command_manager.find_command "upgrade" + end + + assert_equal <<~MSG.chomp, e.detailed_message(highlight: false) + Unknown command upgrade (Gem::UnknownCommandError) + Please use `bundle update ` to update gems in your bundle. + MSG + + def e.can_display_colors? = true + + assert_equal <<~MSG.chomp, e.detailed_message(highlight: true) + \e[1mUnknown command upgrade (\e[1;4mGem::UnknownCommandError\e[m\e[1m)\e[m + Please use \e[1m\e[36mbundle update \e[0m to update gems in your bundle. + MSG + end + def test_run_interrupt old_load_path = $:.dup $: << File.expand_path("test/rubygems", PROJECT_DIR)