Skip to content

Commit bcffc2b

Browse files
committed
Store last check even when upgrade is not available and fix test.
1 parent 0fbc4ac commit bcffc2b

File tree

3 files changed

+77
-5
lines changed

3 files changed

+77
-5
lines changed

lib/rubygems/config_file.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ def config_file_name
392392

393393
# The name of the state file.
394394
def state_file_name
395-
@state_file_name || Gem.state_file
395+
Gem.state_file
396396
end
397397

398398
# Reads time of last update check from state file

lib/rubygems/update_suggestion.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,11 @@ def eglible_for_update?
5353

5454
# compare current and latest version, this is the part where
5555
# latest rubygems spec is fetched from remote
56-
if (Gem.rubygems_version < Gem.latest_rubygems_version)
56+
(Gem.rubygems_version < Gem.latest_rubygems_version).tap do |eglible|
5757
# store the time of last successful check into state file
5858
Gem.configuration.last_update_check = check_time
59-
return true
59+
60+
return eglible
6061
end
6162
rescue # don't block install command on any problem
6263
false

test/rubygems/test_gem_update_suggestion.rb

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ def setup
99

1010
@cmd = Gem::Command.new "dummy", "dummy"
1111
@cmd.extend Gem::UpdateSuggestion
12+
@start_time = 1_000_000
13+
@minute = 60 * 60
14+
@week = 7 * 24 * @minute
1215
end
1316

1417
def with_eglible_environment(**params)
@@ -22,12 +25,13 @@ def self.with_eglible_environment(
2225
rubygems_version: Gem::Version.new("1.2.3"),
2326
latest_rubygems_version: Gem::Version.new("2.0.0"),
2427
ci: false,
28+
reset_last_update_check: true,
2529
cmd:
2630
)
2731
original_config, Gem.configuration[:prevent_update_suggestion] = Gem.configuration[:prevent_update_suggestion], nil
2832
original_env, ENV["RUBYGEMS_PREVENT_UPDATE_SUGGESTION"] = ENV["RUBYGEMS_PREVENT_UPDATE_SUGGESTION"], nil
2933
original_disable, Gem.disable_system_update_message = Gem.disable_system_update_message, nil
30-
Gem.configuration.last_update_check = 0
34+
Gem.configuration.last_update_check = 0 if reset_last_update_check
3135

3236
Gem.ui.stub :tty?, tty do
3337
Gem.stub :rubygems_version, rubygems_version do
@@ -69,6 +73,73 @@ def test_eglible_for_update
6973
end
7074
end
7175

76+
def test_eglible_for_update_is_not_annoying_when_new_version_is_released
77+
current_version = Gem::Version.new("1.2.0")
78+
latest_version = current_version
79+
80+
# checking for first time, it is not eglible since new version
81+
# is not released yet and stored
82+
with_eglible_environment(cmd: @cmd, rubygems_version: current_version, latest_rubygems_version: latest_version) do
83+
Time.stub :now, @start_time do
84+
refute @cmd.eglible_for_update?
85+
assert_equal Gem.configuration.last_update_check, @start_time
86+
end
87+
end
88+
89+
# checking next week, it is not eglible since new version
90+
# is not released yet and timestamp is stored
91+
with_eglible_environment(
92+
cmd: @cmd,
93+
rubygems_version: current_version,
94+
latest_rubygems_version: latest_version,
95+
reset_last_update_check: false
96+
) do
97+
Time.stub :now, @start_time + @week do
98+
refute @cmd.eglible_for_update?
99+
assert_equal Gem.configuration.last_update_check, @start_time + @week
100+
end
101+
end
102+
103+
# pretend new version is released
104+
latest_version = Gem::Version.new("1.3.0")
105+
106+
# checking later same next week, it is not eglible even new version
107+
# is released and timestamp is not stored
108+
with_eglible_environment(
109+
cmd: @cmd,
110+
rubygems_version: current_version,
111+
latest_rubygems_version: latest_version,
112+
reset_last_update_check: false
113+
) do
114+
Time.stub :now, @start_time + @week + @minute do
115+
refute @cmd.eglible_for_update?
116+
assert_equal Gem.configuration.last_update_check, @start_time + @week
117+
end
118+
end
119+
end
120+
121+
def test_eglible_for_update_is_not_annoying_when_not_upgraded
122+
with_eglible_environment(cmd: @cmd) do
123+
# checking for first time, it is eglible and stored
124+
Time.stub :now, @start_time do
125+
assert @cmd.eglible_for_update?
126+
assert_equal Gem.configuration.last_update_check, @start_time
127+
end
128+
129+
# checking minute later is not eglible and not stored
130+
Time.stub :now, @start_time + @minute do
131+
refute @cmd.eglible_for_update?
132+
assert_equal Gem.configuration.last_update_check, @start_time
133+
end
134+
135+
# checking week later is eglible again and stored
136+
Time.stub :now, @start_time + @week do
137+
assert @cmd.eglible_for_update?
138+
assert_equal Gem.configuration.last_update_check, @start_time + @week
139+
end
140+
end
141+
end
142+
72143
def test_eglible_for_update_prevent_config
73144
with_eglible_environment(cmd: @cmd) do
74145
begin
@@ -121,7 +192,7 @@ def test_eglible_for_update_on_ci
121192
end
122193

123194
def test_eglible_for_update_unwrittable_config
124-
with_eglible_environment(ci: true, cmd: @cmd) do
195+
with_eglible_environment(cmd: @cmd) do
125196
Gem.configuration.stub :state_file_writable?, false do
126197
refute @cmd.eglible_for_update?
127198
end

0 commit comments

Comments
 (0)