Skip to content

Commit

Permalink
Fix and simplify cache tests on existing URL.
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
riccardoporreca committed Feb 7, 2020
1 parent 1dce201 commit 821ff0f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 24 deletions.
43 changes: 20 additions & 23 deletions spec/html-proofer/cache_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,38 +112,36 @@ def read_cache(cache_file)
let(:cache_file_name) { '.within_date.log' }

it 'does not write file if timestamp is within date' do
# this is frozen to within 7 days of the log
new_time = Time.local(2015, 10, 20, 12, 0, 0)
Timecop.freeze(new_time)

log = read_cache(cache_file)
current_time = log.values.first['time']

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

# note that the timestamp did not change
log = read_cache(cache_file)
new_time = log.values.first['time']
expect(current_time).to eq(new_time)
# we expect no add...
expect_any_instance_of(HTMLProofer::Cache).to_not receive(:add)
# ...since we are mocking within_timeframe? to be true
within = true
expect_any_instance_of(HTMLProofer::Cache).to receive(:within_timeframe?).and_return(within)

run_proofer(['www.github.com'], :links, cache: { timeframe: '30d', cache_file: cache_file_name }.merge(default_cache_options))

Timecop.return
end
end

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

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

# since the timestamp changed, we expect an add
expect_any_instance_of(HTMLProofer::Cache).to receive(:add)
# mock within_timeframe
expect_any_instance_of(HTMLProofer::Cache).to receive(:write)

# we expect an add...
expect_any_instance_of(HTMLProofer::Cache).to receive(:add).with('www.github.com', nil, 200)
# ...since we are mocking within_timeframe? to be true
within = false
expect_any_instance_of(HTMLProofer::Cache).to receive(:within_timeframe?).and_return(within)

run_proofer(['www.github.com'], :links, cache: { timeframe: '4d', cache_file: cache_file_name }.merge(default_cache_options))

Timecop.return
end
end

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

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

expect_any_instance_of(HTMLProofer::Cache).to receive(:write)

# we expect the same link to be readded...
expect_any_instance_of(HTMLProofer::Cache).to receive(:add)
# ...even though we are within the time frame
within = true
expect_any_instance_of(HTMLProofer::Cache).to receive(:within_timeframe?).and_return(within)

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

Timecop.return
end
end
end
2 changes: 1 addition & 1 deletion spec/html-proofer/fixtures/cache/.recheck_failure.log
Original file line number Diff line number Diff line change
@@ -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 "}}
{"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 comments on commit 821ff0f

Please sign in to comment.