From ef4eafae95e7aa84e44ae29b9dc448f9953598c9 Mon Sep 17 00:00:00 2001 From: "Garen J. Torikian" Date: Thu, 3 Dec 2020 11:17:29 +0000 Subject: [PATCH] Correct internal links when pulled from outdated cache --- lib/html-proofer/runner.rb | 3 +++ spec/html-proofer/cache_spec.rb | 19 +++++++++++++++++++ .../cache/.not_within_date_internal.log | 1 + 3 files changed, 23 insertions(+) create mode 100644 spec/html-proofer/fixtures/cache/.not_within_date_internal.log diff --git a/lib/html-proofer/runner.rb b/lib/html-proofer/runner.rb index 94840a34..d37523c7 100644 --- a/lib/html-proofer/runner.rb +++ b/lib/html-proofer/runner.rb @@ -147,6 +147,9 @@ def validate_internal_urls urls_to_check = load_internal_cache urls_to_check.each_pair do |url, internal_urls| + # pulled from cache + internal_urls = @internal_urls[url] unless internal_urls.first.is_a?(LinkCheck::InternalLink) + result = @internal_link_checks.check_internal_link(internal_urls.first.link, internal_urls.first.path, internal_urls.first.line, internal_urls.first.content) code = result ? 200 : 404 @cache.add(url, @internal_urls_to_paths[url].sort, code, '') # TODO: blank msg for now diff --git a/spec/html-proofer/cache_spec.rb b/spec/html-proofer/cache_spec.rb index 74e3c31f..7c5b97a0 100644 --- a/spec/html-proofer/cache_spec.rb +++ b/spec/html-proofer/cache_spec.rb @@ -144,6 +144,25 @@ def read_cache(cache_file) end end + context 'not within date for internal url' do + let(:cache_file_name) { '.not_within_date_internal.log' } + it 'does write file if timestamp is not within date' do + # this is frozen to within 7 days of the log + new_time = Time.local(2015, 10, 27, 12, 0, 0) + Timecop.freeze(new_time) + + expect_any_instance_of(HTMLProofer::Cache).to receive(:write) + root_link = "#{FIXTURES_DIR}/links/root_link/root_link.html" + + # we expect an add since we are mocking outside the timeframe + expect_any_instance_of(HTMLProofer::Cache).to receive(:add).with('/', ['spec/html-proofer/fixtures/links/root_link/root_link.html'], 200, '') + + run_proofer(root_link, :file, disable_external: true, cache: { timeframe: '4d', cache_file: cache_file_name }.merge(default_cache_options)) + + Timecop.return + end + end + context 'new external url added' do let(:cache_file_name) { '.new_external_url.log' } it 'does write file if a new URL is added' do diff --git a/spec/html-proofer/fixtures/cache/.not_within_date_internal.log b/spec/html-proofer/fixtures/cache/.not_within_date_internal.log new file mode 100644 index 00000000..3353cd02 --- /dev/null +++ b/spec/html-proofer/fixtures/cache/.not_within_date_internal.log @@ -0,0 +1 @@ +{"/":{"time":"2015-10-20 12:00:00 -0700","filenames":["spec/html-proofer/fixtures/links/root_link/root_link.html"],"status":200,"message":""}}