From 821ff0f223ff6c413c017cb9d07c0d914cc6d16c Mon Sep 17 00:00:00 2001 From: riccardoporreca Date: Fri, 7 Feb 2020 01:22:23 +0100 Subject: [PATCH] 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. --- spec/html-proofer/cache_spec.rb | 43 +++++++++---------- .../fixtures/cache/.recheck_failure.log | 2 +- 2 files changed, 21 insertions(+), 24 deletions(-) diff --git a/spec/html-proofer/cache_spec.rb b/spec/html-proofer/cache_spec.rb index 857c51fb..40b120ed 100644 --- a/spec/html-proofer/cache_spec.rb +++ b/spec/html-proofer/cache_spec.rb @@ -112,22 +112,17 @@ 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 @@ -135,15 +130,18 @@ def read_cache(cache_file) 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 @@ -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 diff --git a/spec/html-proofer/fixtures/cache/.recheck_failure.log b/spec/html-proofer/fixtures/cache/.recheck_failure.log index 4aa0509e..06b73003 100644 --- a/spec/html-proofer/fixtures/cache/.recheck_failure.log +++ b/spec/html-proofer/fixtures/cache/.recheck_failure.log @@ -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 "}} \ No newline at end of file +{"http://www.foofoofoo.biz":{"time":"2015-10-20 12:00:00 -0700","filenames":null,"status":0,"message":"External link www.foofoofoo.biz failed: 0 "}}