summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/rubygems/commands/install_command.rb4
-rw-r--r--lib/rubygems/config_file.rb12
-rw-r--r--lib/rubygems/update_suggestion.rb70
-rw-r--r--test/rubygems/helper.rb1
-rw-r--r--test/rubygems/test_gem_commands_install_command.rb19
-rw-r--r--test/rubygems/test_gem_update_suggestion.rb137
6 files changed, 243 insertions, 0 deletions
diff --git a/lib/rubygems/commands/install_command.rb b/lib/rubygems/commands/install_command.rb
index 071687c63f..c04c01f258 100644
--- a/lib/rubygems/commands/install_command.rb
+++ b/lib/rubygems/commands/install_command.rb
@@ -5,6 +5,7 @@ require_relative "../dependency_installer"
require_relative "../local_remote_options"
require_relative "../validator"
require_relative "../version_option"
+require_relative "../update_suggestion"
##
# Gem installer command line tool
@@ -17,6 +18,7 @@ class Gem::Commands::InstallCommand < Gem::Command
include Gem::VersionOption
include Gem::LocalRemoteOptions
include Gem::InstallUpdateOptions
+ include Gem::UpdateSuggestion
def initialize
defaults = Gem::DependencyInstaller::DEFAULT_OPTIONS.merge({
@@ -168,6 +170,8 @@ You can use `i` command instead of `install`.
show_installed
+ say update_suggestion if eglible_for_update?
+
terminate_interaction exit_code
end
diff --git a/lib/rubygems/config_file.rb b/lib/rubygems/config_file.rb
index c53e209ae8..b18f4115cc 100644
--- a/lib/rubygems/config_file.rb
+++ b/lib/rubygems/config_file.rb
@@ -371,6 +371,18 @@ if you believe they were disclosed to a third party.
@backtrace || $DEBUG
end
+ # Check config file is writable. Creates empty file if not present to ensure we can write to it.
+ def config_file_writable?
+ if File.exist?(config_file_name)
+ File.writable?(config_file_name)
+ else
+ require "fileutils"
+ FileUtils.mkdir_p File.dirname(config_file_name)
+ File.open(config_file_name, "w") {}
+ true
+ end
+ end
+
# The name of the configuration file.
def config_file_name
@config_file_name || Gem.config_file
diff --git a/lib/rubygems/update_suggestion.rb b/lib/rubygems/update_suggestion.rb
new file mode 100644
index 0000000000..71c44af3af
--- /dev/null
+++ b/lib/rubygems/update_suggestion.rb
@@ -0,0 +1,70 @@
+# frozen_string_literal: true
+
+##
+# Mixin methods for Gem::Command to promote available RubyGems update
+
+module Gem::UpdateSuggestion
+ # list taken from https://github.com/watson/ci-info/blob/7a3c30d/index.js#L56-L66
+ CI_ENV_VARS = [
+ "CI", # Travis CI, CircleCI, Cirrus CI, Gitlab CI, Appveyor, CodeShip, dsari
+ "CONTINUOUS_INTEGRATION", # Travis CI, Cirrus CI
+ "BUILD_NUMBER", # Jenkins, TeamCity
+ "CI_APP_ID", "CI_BUILD_ID", "CI_BUILD_NUMBER", # Applfow
+ "RUN_ID" # TaskCluster, dsari
+ ].freeze
+
+ ONE_WEEK = 7 * 24 * 60 * 60
+
+ ##
+ # Message to promote available RubyGems update with related gem update command.
+
+ def update_suggestion
+ <<-MESSAGE
+
+A new release of RubyGems is available: #{Gem.rubygems_version} → #{Gem.latest_rubygems_version}!
+Run `gem update --system #{Gem.latest_rubygems_version}` to update your installation.
+
+ MESSAGE
+ end
+
+ ##
+ # Determines if current environment is eglible for update suggestion.
+
+ def eglible_for_update?
+ # explicit opt-out
+ return false if Gem.configuration[:prevent_update_suggestion]
+ return false if ENV["RUBYGEMS_PREVENT_UPDATE_SUGGESTION"]
+
+ # focus only on human usage of final RubyGems releases
+ return false unless Gem.ui.tty?
+ return false if Gem.rubygems_version.prerelease?
+ return false if Gem.disable_system_update_message
+ return false if ci?
+
+ # check makes sense only when we can store of last try
+ # otherwise we will not be able to prevent annoying update message
+ # on each command call
+ return unless Gem.configuration.config_file_writable?
+
+ # load time of last check, ensure the difference is enough to repeat the suggestion
+ check_time = Time.now.to_i
+ last_update_check = Gem.configuration[:last_update_check] || 0
+ return false if (check_time - last_update_check) < ONE_WEEK
+
+ # compare current and latest version, this is the part where
+ # latest rubygems spec is fetched from remote
+ (Gem.rubygems_version < Gem.latest_rubygems_version).tap do |eglible|
+ if eglible
+ # store the time of last successful check into config file
+ Gem.configuration[:last_update_check] = check_time
+ Gem.configuration.write
+ end
+ end
+ rescue # don't block install command on any problem
+ false
+ end
+
+ def ci?
+ CI_ENV_VARS.any? {|var| ENV.include?(var) }
+ end
+end
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