[前][次][番号順一覧][スレッド一覧]

ruby-changes:74449

From: Josef <ko1@a...>
Date: Fri, 11 Nov 2022 17:24:29 +0900 (JST)
Subject: [ruby-changes:74449] de159c5a85 (master): [rubygems/rubygems] Store last check even when upgrade is not available and fix test.

https://git.ruby-lang.org/ruby.git/commit/?id=de159c5a85

From de159c5a855dd53bfd9ce284c9099306724560a7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Josef=20=C5=A0im=C3=A1nek?= <josef.simanek@g...>
Date: Fri, 4 Nov 2022 22:56:31 +0100
Subject: [rubygems/rubygems] Store last check even when upgrade is not
 available and fix test.

https://github.com/rubygems/rubygems/commit/bcffc2b0a5
---
 lib/rubygems/config_file.rb                 |  2 +-
 lib/rubygems/update_suggestion.rb           |  5 +-
 test/rubygems/test_gem_update_suggestion.rb | 75 ++++++++++++++++++++++++++++-
 3 files changed, 77 insertions(+), 5 deletions(-)

diff --git a/lib/rubygems/config_file.rb b/lib/rubygems/config_file.rb
index f2abc7f2af..4aa8b4d33a 100644
--- a/lib/rubygems/config_file.rb
+++ b/lib/rubygems/config_file.rb
@@ -392,7 +392,7 @@ if you believe they were disclosed to a third party. https://github.com/ruby/ruby/blob/trunk/lib/rubygems/config_file.rb#L392
 
   # The name of the state file.
   def state_file_name
-    @state_file_name || Gem.state_file
+    Gem.state_file
   end
 
   # Reads time of last update check from state file
diff --git a/lib/rubygems/update_suggestion.rb b/lib/rubygems/update_suggestion.rb
index d9ac517e49..c2e81b2374 100644
--- a/lib/rubygems/update_suggestion.rb
+++ b/lib/rubygems/update_suggestion.rb
@@ -53,10 +53,11 @@ Run `gem update --system #{Gem.latest_rubygems_version}` to update your installa https://github.com/ruby/ruby/blob/trunk/lib/rubygems/update_suggestion.rb#L53
 
     # compare current and latest version, this is the part where
     # latest rubygems spec is fetched from remote
-    if (Gem.rubygems_version < Gem.latest_rubygems_version)
+    (Gem.rubygems_version < Gem.latest_rubygems_version).tap do |eglible|
       # store the time of last successful check into state file
       Gem.configuration.last_update_check = check_time
-      return true
+
+      return eglible
     end
   rescue # don't block install command on any problem
     false
diff --git a/test/rubygems/test_gem_update_suggestion.rb b/test/rubygems/test_gem_update_suggestion.rb
index ba54059ee7..520a69ac69 100644
--- a/test/rubygems/test_gem_update_suggestion.rb
+++ b/test/rubygems/test_gem_update_suggestion.rb
@@ -9,6 +9,9 @@ class TestUpdateSuggestion < Gem::TestCase https://github.com/ruby/ruby/blob/trunk/test/rubygems/test_gem_update_suggestion.rb#L9
 
     @cmd = Gem::Command.new "dummy", "dummy"
     @cmd.extend Gem::UpdateSuggestion
+    @start_time = 1_000_000
+    @minute = 60 * 60
+    @week = 7 * 24 * @minute
   end
 
   def with_eglible_environment(**params)
@@ -22,12 +25,13 @@ class TestUpdateSuggestion < Gem::TestCase https://github.com/ruby/ruby/blob/trunk/test/rubygems/test_gem_update_suggestion.rb#L25
     rubygems_version: Gem::Version.new("1.2.3"),
     latest_rubygems_version: Gem::Version.new("2.0.0"),
     ci: false,
+    reset_last_update_check: true,
     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 = 0
+    Gem.configuration.last_update_check = 0 if reset_last_update_check
 
     Gem.ui.stub :tty?, tty do
       Gem.stub :rubygems_version, rubygems_version do
@@ -69,6 +73,73 @@ class TestUpdateSuggestion < Gem::TestCase https://github.com/ruby/ruby/blob/trunk/test/rubygems/test_gem_update_suggestion.rb#L73
     end
   end
 
+  def test_eglible_for_update_is_not_annoying_when_new_version_is_released
+    current_version = Gem::Version.new("1.2.0")
+    latest_version = current_version
+
+    # checking for first time, it is not eglible since new version
+    # is not released yet and stored
+    with_eglible_environment(cmd: @cmd, rubygems_version: current_version, latest_rubygems_version: latest_version) do
+      Time.stub :now, @start_time do
+        refute @cmd.eglible_for_update?
+        assert_equal Gem.configuration.last_update_check, @start_time
+      end
+    end
+
+    # checking next week, it is not eglible since new version
+    # is not released yet and timestamp is stored
+    with_eglible_environment(
+      cmd: @cmd,
+      rubygems_version: current_version,
+      latest_rubygems_version: latest_version,
+      reset_last_update_check: false
+    ) do
+      Time.stub :now, @start_time + @week do
+        refute @cmd.eglible_for_update?
+        assert_equal Gem.configuration.last_update_check, @start_time + @week
+      end
+    end
+
+    # pretend new version is released
+    latest_version = Gem::Version.new("1.3.0")
+
+    # checking later same next week, it is not eglible even new version
+    # is released and timestamp is not stored
+    with_eglible_environment(
+      cmd: @cmd,
+      rubygems_version: current_version,
+      latest_rubygems_version: latest_version,
+      reset_last_update_check: false
+    ) do
+      Time.stub :now, @start_time + @week + @minute do
+        refute @cmd.eglible_for_update?
+        assert_equal Gem.configuration.last_update_check, @start_time + @week
+      end
+    end
+  end
+
+  def test_eglible_for_update_is_not_annoying_when_not_upgraded
+    with_eglible_environment(cmd: @cmd) do
+      # checking for first time, it is eglible and stored
+      Time.stub :now, @start_time do
+        assert @cmd.eglible_for_update?
+        assert_equal Gem.configuration.last_update_check, @start_time
+      end
+
+      # checking minute later is not eglible and not stored
+      Time.stub :now, @start_time + @minute do
+        refute @cmd.eglible_for_update?
+        assert_equal Gem.configuration.last_update_check, @start_time
+      end
+
+      # checking week later is eglible again and stored
+      Time.stub :now, @start_time + @week do
+        assert @cmd.eglible_for_update?
+        assert_equal Gem.configuration.last_update_check, @start_time + @week
+      end
+    end
+  end
+
   def test_eglible_for_update_prevent_config
     with_eglible_environment(cmd: @cmd) do
       begin
@@ -121,7 +192,7 @@ class TestUpdateSuggestion < Gem::TestCase https://github.com/ruby/ruby/blob/trunk/test/rubygems/test_gem_update_suggestion.rb#L192
   end
 
   def test_eglible_for_update_unwrittable_config
-    with_eglible_environment(ci: true, cmd: @cmd) do
+    with_eglible_environment(cmd: @cmd) do
       Gem.configuration.stub :state_file_writable?, false do
         refute @cmd.eglible_for_update?
       end
-- 
cgit v1.2.3


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]