From c7d043065c058f20ce30c61bb3ce127cb15cc0a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josef=20=C5=A0im=C3=A1nek?= Date: Sun, 11 Sep 2022 05:40:55 +0200 Subject: [rubygems/rubygems] Add 'call for update' to RubyGems install command. https://github.com/rubygems/rubygems/commit/05811f8248 --- test/rubygems/helper.rb | 1 + test/rubygems/test_gem_commands_install_command.rb | 19 +++ test/rubygems/test_gem_update_suggestion.rb | 137 +++++++++++++++++++++ 3 files changed, 157 insertions(+) create mode 100644 test/rubygems/test_gem_update_suggestion.rb (limited to 'test') diff --git a/test/rubygems/helper.rb b/test/rubygems/helper.rb index ae89d669fe..43423dc101 100644 --- a/test/rubygems/helper.rb +++ b/test/rubygems/helper.rb @@ -309,6 +309,7 @@ class Gem::TestCase < Test::Unit::TestCase ENV["XDG_DATA_HOME"] = nil ENV["SOURCE_DATE_EPOCH"] = nil ENV["BUNDLER_VERSION"] = nil + ENV["RUBYGEMS_PREVENT_UPDATE_SUGGESTION"] = "true" @current_dir = Dir.pwd @fetcher = nil diff --git a/test/rubygems/test_gem_commands_install_command.rb b/test/rubygems/test_gem_commands_install_command.rb index 7a58bcd7cb..14bddec485 100644 --- a/test/rubygems/test_gem_commands_install_command.rb +++ b/test/rubygems/test_gem_commands_install_command.rb @@ -1,5 +1,6 @@ # frozen_string_literal: true require_relative "helper" +require_relative "test_gem_update_suggestion" require "rubygems/commands/install_command" require "rubygems/request_set" require "rubygems/rdoc" @@ -1550,4 +1551,22 @@ ERROR: Possible alternatives: non_existent_with_hint assert_equal " a-3", out.shift assert_empty out end + + def test_suggest_update_if_enabled + TestUpdateSuggestion.with_eglible_environment(cmd: @cmd) do + spec_fetcher do |fetcher| + fetcher.gem "a", 2 + end + + @cmd.options[:args] = %w[a] + + use_ui @ui do + assert_raise Gem::MockGemUi::SystemExitException, @ui.error do + @cmd.execute + end + end + + assert_includes @ui.output, "A new release of RubyGems is available: 1.2.3 → 2.0.0!" + end + end end diff --git a/test/rubygems/test_gem_update_suggestion.rb b/test/rubygems/test_gem_update_suggestion.rb new file mode 100644 index 0000000000..aefebf41cf --- /dev/null +++ b/test/rubygems/test_gem_update_suggestion.rb @@ -0,0 +1,137 @@ +# frozen_string_literal: true +require_relative "helper" +require "rubygems/command" +require "rubygems/update_suggestion" + +class TestUpdateSuggestion < Gem::TestCase + def setup + super + + @cmd = Gem::Command.new "dummy", "dummy" + @cmd.extend Gem::UpdateSuggestion + end + + def with_eglible_environment(**params) + self.class.with_eglible_environment(**params) do + yield + end + end + + def self.with_eglible_environment( + tty: true, + rubygems_version: Gem::Version.new("1.2.3"), + latest_rubygems_version: Gem::Version.new("2.0.0"), + ci: false, + cmd: + ) + original_config, Gem.configuration[:prevent_update_suggestion] = Gem.configuration[:prevent_update_suggestion], nil + original_env, ENV["RUBYGEMS_PREVENT_UPDATE_SUGGESTION"] = ENV["RUBYGEMS_PREVENT_UPDATE_SUGGESTION"], nil + original_disable, Gem.disable_system_update_message = Gem.disable_system_update_message, nil + Gem.configuration[:last_update_check] = nil + + Gem.ui.stub :tty?, tty do + Gem.stub :rubygems_version, rubygems_version do + Gem.stub :latest_rubygems_version, latest_rubygems_version do + cmd.stub :ci?, ci do + yield + end + end + end + end + ensure + Gem.configuration[:prevent_update_suggestion] = original_config + ENV["RUBYGEMS_PREVENT_UPDATE_SUGGESTION"] = original_env + Gem.disable_system_update_message = original_disable + end + + def test_update_suggestion + Gem.stub :rubygems_version, Gem::Version.new("1.2.3") do + Gem.stub :latest_rubygems_version, Gem::Version.new("2.0.0") do + assert_equal @cmd.update_suggestion, <<~SUGGESTION + + A new release of RubyGems is available: 1.2.3 → 2.0.0! + Run `gem update --system 2.0.0` to update your installation. + + SUGGESTION + end + end + end + + def test_eglible_for_update + with_eglible_environment(cmd: @cmd) do + Time.stub :now, 123456789 do + assert @cmd.eglible_for_update? + assert_equal Gem.configuration[:last_update_check], 123456789 + + # test last check is written to config file + assert File.read(Gem.configuration.config_file_name).match("last_update_check: 123456789") + end + end + end + + def test_eglible_for_update_prevent_config + with_eglible_environment(cmd: @cmd) do + begin + original_config, Gem.configuration[:prevent_update_suggestion] = Gem.configuration[:prevent_update_suggestion], true + refute @cmd.eglible_for_update? + ensure + Gem.configuration[:prevent_update_suggestion] = original_config + end + end + end + + def test_eglible_for_update_prevent_env + with_eglible_environment(cmd: @cmd) do + begin + original_env, ENV["RUBYGEMS_PREVENT_UPDATE_SUGGESTION"] = ENV["RUBYGEMS_PREVENT_UPDATE_SUGGESTION"], "yes" + refute @cmd.eglible_for_update? + ensure + ENV["RUBYGEMS_PREVENT_UPDATE_SUGGESTION"] = original_env + end + end + end + + def test_eglible_for_update_non_tty + with_eglible_environment(tty: false, cmd: @cmd) do + refute @cmd.eglible_for_update? + end + end + + def test_eglible_for_update_for_prerelease + with_eglible_environment(rubygems_version: Gem::Version.new("1.0.0-rc1"), cmd: @cmd) do + refute @cmd.eglible_for_update? + end + end + + def test_eglible_for_update_disabled_update + with_eglible_environment(cmd: @cmd) do + begin + original_disable, Gem.disable_system_update_message = Gem.disable_system_update_message, "disabled" + refute @cmd.eglible_for_update? + ensure + Gem.disable_system_update_message = original_disable + end + end + end + + def test_eglible_for_update_on_ci + with_eglible_environment(ci: true, cmd: @cmd) do + refute @cmd.eglible_for_update? + end + end + + def test_eglible_for_update_unwrittable_config + with_eglible_environment(ci: true, cmd: @cmd) do + Gem.configuration.stub :config_file_writable?, false do + refute @cmd.eglible_for_update? + end + end + end + + def test_eglible_for_update_notification_delay + with_eglible_environment(cmd: @cmd) do + Gem.configuration[:last_update_check] = Time.now.to_i + refute @cmd.eglible_for_update? + end + end +end -- cgit v1.2.3