Skip to content

Commit 821ff0f

Browse files
Fix and simplify cache tests on existing URL.
* Rely on mocking :within_timeframe? instead of time travel (previous dates were not consistent) and fix expectations on :add. * Also fix the log for the recheck failure test: it did not contain the actual URL, which was then of added as new URL => This reveals a bug in handling failure re-checks.
1 parent 1dce201 commit 821ff0f

File tree

2 files changed

+21
-24
lines changed

2 files changed

+21
-24
lines changed

spec/html-proofer/cache_spec.rb

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -112,38 +112,36 @@ def read_cache(cache_file)
112112
let(:cache_file_name) { '.within_date.log' }
113113

114114
it 'does not write file if timestamp is within date' do
115-
# this is frozen to within 7 days of the log
116-
new_time = Time.local(2015, 10, 20, 12, 0, 0)
117-
Timecop.freeze(new_time)
118-
119-
log = read_cache(cache_file)
120-
current_time = log.values.first['time']
121115

122116
expect_any_instance_of(HTMLProofer::Cache).to receive(:write)
123-
run_proofer(['www.github.com'], :links, cache: { timeframe: '30d', cache_file: cache_file_name }.merge(default_cache_options))
124117

125-
# note that the timestamp did not change
126-
log = read_cache(cache_file)
127-
new_time = log.values.first['time']
128-
expect(current_time).to eq(new_time)
118+
# we expect no add...
119+
expect_any_instance_of(HTMLProofer::Cache).to_not receive(:add)
120+
# ...since we are mocking within_timeframe? to be true
121+
within = true
122+
expect_any_instance_of(HTMLProofer::Cache).to receive(:within_timeframe?).and_return(within)
123+
124+
run_proofer(['www.github.com'], :links, cache: { timeframe: '30d', cache_file: cache_file_name }.merge(default_cache_options))
129125

130-
Timecop.return
131126
end
132127
end
133128

134129
context 'not within date' do
135130
let(:cache_file_name) { '.not_within_date.log' }
136131

137132
it 'does write file if timestamp is not within date' do
138-
# this is frozen to within 20 days after the log
139-
new_time = Time.local(2014, 10, 21, 12, 0, 0)
140-
Timecop.travel(new_time)
141133

142-
# since the timestamp changed, we expect an add
143-
expect_any_instance_of(HTMLProofer::Cache).to receive(:add)
134+
# mock within_timeframe
135+
expect_any_instance_of(HTMLProofer::Cache).to receive(:write)
136+
137+
# we expect an add...
138+
expect_any_instance_of(HTMLProofer::Cache).to receive(:add).with('www.github.com', nil, 200)
139+
# ...since we are mocking within_timeframe? to be true
140+
within = false
141+
expect_any_instance_of(HTMLProofer::Cache).to receive(:within_timeframe?).and_return(within)
142+
144143
run_proofer(['www.github.com'], :links, cache: { timeframe: '4d', cache_file: cache_file_name }.merge(default_cache_options))
145144

146-
Timecop.return
147145
end
148146
end
149147

@@ -170,18 +168,17 @@ def read_cache(cache_file)
170168
let(:cache_file_name) { '.recheck_failure.log' }
171169

172170
it 'does recheck failures, regardless of cache' do
173-
# this is frozen to within 7 days of the log
174-
new_time = Time.local(2015, 10, 20, 12, 0, 0)
175-
Timecop.freeze(new_time)
176171

177172
expect_any_instance_of(HTMLProofer::Cache).to receive(:write)
173+
178174
# we expect the same link to be readded...
179175
expect_any_instance_of(HTMLProofer::Cache).to receive(:add)
176+
# ...even though we are within the time frame
177+
within = true
178+
expect_any_instance_of(HTMLProofer::Cache).to receive(:within_timeframe?).and_return(within)
180179

181-
# ...even though we are within the 30d time frame, because it's a failure
182180
run_proofer(['http://www.foofoofoo.biz'], :links, cache: { timeframe: '30d', cache_file: cache_file_name }.merge(default_cache_options))
183181

184-
Timecop.return
185182
end
186183
end
187184
end
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"www.foofoofoo.biz":{"time":"2015-10-20 12:00:00 -0700","filenames":null,"status":0,"message":"External link www.foofoofoo.biz failed: 0 "}}
1+
{"http://www.foofoofoo.biz":{"time":"2015-10-20 12:00:00 -0700","filenames":null,"status":0,"message":"External link www.foofoofoo.biz failed: 0 "}}

0 commit comments

Comments
 (0)