Skip to content

Commit

Permalink
Merge pull request #623 from gjtorikian/internal-drama
Browse files Browse the repository at this point in the history
Handle internal & external cache merging
  • Loading branch information
gjtorikian committed Feb 21, 2021
2 parents 98554c8 + ed39330 commit 925a2b0
Show file tree
Hide file tree
Showing 12 changed files with 246 additions and 10 deletions.
7 changes: 6 additions & 1 deletion lib/html-proofer/cache.rb
Expand Up @@ -109,8 +109,13 @@ def detect_url_changes(found)
additions
end

# TODO: Garbage performance--both the external and internal
# caches need access to this file. Write a proper versioned
# schema in the future
def write
File.write(cache_file, @cache_log.to_json)
file = {}
file = JSON.parse(File.read(cache_file)) if File.exist?(cache_file)
File.write(cache_file, file.merge(@cache_log).to_json)
end

def load?
Expand Down
7 changes: 2 additions & 5 deletions lib/html-proofer/check/links.rb
Expand Up @@ -59,11 +59,8 @@ def run
add_to_external_urls(@link.href || @link.src)
next
elsif @link.internal?
if @link.exists? || @link.hash
add_to_internal_urls(@link.href, InternalLink.new(@link, @path, line, content))
else
add_issue("internally linking to #{@link.href}, which does not exist", line: line, content: content)
end
add_to_internal_urls(@link.href, InternalLink.new(@link, @path, line, content))
add_issue("internally linking to #{@link.href}, which does not exist", line: line, content: content) if !@link.exists? && !@link.hash
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/html-proofer/element.rb
Expand Up @@ -24,7 +24,7 @@ def initialize(obj, check, logger)
raise e
end

@aria_hidden = defined?(@aria_hidden) && @aria_hidden == 'true' ? true : false
@aria_hidden = defined?(@aria_hidden) && @aria_hidden == 'true'

@data_proofer_ignore = defined?(@data_proofer_ignore)

Expand Down
6 changes: 4 additions & 2 deletions lib/html-proofer/runner.rb
Expand Up @@ -63,7 +63,9 @@ def check_list_of_links
swap(url, @options[:url_swap])
end
end
@external_urls = Hash[*@src.map { |s| [s, nil] }.flatten]
@external_urls = @src.each_with_object({}) do |url, hash|
hash[url] = nil
end
validate_external_urls
end

Expand Down Expand Up @@ -123,7 +125,7 @@ def check_parsed(html, path)
end

external_urls = check.external_urls
external_urls = Hash[check.external_urls.map { |url, file| [swap(url, @options[:url_swap]), file] }] if @options[:url_swap]
external_urls = check.external_urls.map { |url, file| [swap(url, @options[:url_swap]), file] }.to_h if @options[:url_swap]
result[:external_urls].merge!(external_urls)
result[:failures].concat(check.issues)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/html-proofer/url_validator.rb
Expand Up @@ -85,7 +85,7 @@ def extract_domain_path(uri)
# for HEAD. If we've decided to check for hashes, we must do a GET--HEAD is
# not available as an option.
def external_link_checker(external_urls)
external_urls = Hash[external_urls.sort]
external_urls = external_urls.sort.to_h

count = external_urls.length
check_text = pluralize(count, 'external link', 'external links')
Expand Down
54 changes: 54 additions & 0 deletions spec/html-proofer/cache_spec.rb
Expand Up @@ -247,4 +247,58 @@ def read_cache(cache_file)
Timecop.return
end
end

it 'does recheck failures, regardless of external-only cache' do
cache_file_name = '.external.log'
cache_location = File.join(storage_dir, cache_file_name)

file = "#{FIXTURES_DIR}/cache/external_example.html"

new_time = Time.local(2021, 0o1, 27, 12, 0, 0)
Timecop.freeze(new_time)

File.delete(cache_location) if File.exist?(cache_location)

run_proofer(file, :file, external_only: true, links_only: true, cache: { timeframe: '1d', cache_file: cache_file_name }.merge(default_cache_options))

cache = JSON.parse(File.read(cache_location))

expect(cache.keys.count).to eq(1)
expect(cache.keys[0]).to eq('https://github.com/gjtorikian/html-proofer')

run_proofer(file, :file, links_only: true, cache: { timeframe: '1d', cache_file: cache_file_name }.merge(default_cache_options))

cache = JSON.parse(File.read(cache_location))
expect(cache.keys.count).to eq(1)
expect(cache.keys[0]).to eq('https://github.com/gjtorikian/html-proofer')

Timecop.return
end

it 'does recheck failures, regardless of external and internal cache' do
cache_file_name = '.internal_and_external.log'
cache_location = File.join(storage_dir, cache_file_name)

file = "#{FIXTURES_DIR}/cache/internal_and_external_example.html"

new_time = Time.local(2021, 0o1, 27, 12, 0, 0)
Timecop.freeze(new_time)

File.delete(cache_location) if File.exist?(cache_location)

run_proofer(file, :file, external_only: true, links_only: true, cache: { timeframe: '1d', cache_file: cache_file_name }.merge(default_cache_options))

cache = JSON.parse(File.read(cache_location))

expect(cache.keys.count).to eq(1)
expect(cache.keys[0]).to eq('https://github.com/gjtorikian/html-proofer')

run_proofer(file, :file, links_only: true, cache: { timeframe: '1d', cache_file: cache_file_name }.merge(default_cache_options))

cache = JSON.parse(File.read(cache_location))
expect(cache.keys.count).to eq(2)
expect(cache.keys[0]).to eq('https://github.com/gjtorikian/html-proofer')

Timecop.return
end
end
1 change: 1 addition & 0 deletions spec/html-proofer/fixtures/cache/.external.log
@@ -0,0 +1 @@
{"https://github.com/gjtorikian/html-proofer":{"time":"2021-01-27 12:00:00 +0000","filenames":["spec/html-proofer/fixtures/cache/external_example.html"],"status":200,"message":""}}
@@ -0,0 +1 @@
{"https://github.com/gjtorikian/html-proofer":{"time":"2021-01-27 12:00:00 +0000","filenames":["spec/html-proofer/fixtures/cache/internal_and_external_example.html"],"status":200,"message":""},"/somewhere.html":{"time":"2021-01-27 12:00:00 +0000","filenames":["spec/html-proofer/fixtures/cache/internal_and_external_example.html"],"status":200,"message":""}}
11 changes: 11 additions & 0 deletions spec/html-proofer/fixtures/cache/external_example.html
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
</head>
<body>
<a href="https://github.com/gjtorikian/html-proofer"
>The amazing HTMLProofer</a
>
</body>
</html>
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
</head>
<body>
<a href="https://github.com/gjtorikian/html-proofer"
>The amazing HTMLProofer</a
>

<a href="/somewhere.html">Somewhere</a>
</body>
</html>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 925a2b0

Please sign in to comment.