Skip to content

Commit

Permalink
Merge pull request #696 from riccardoporreca/feature/695-fix-internal…
Browse files Browse the repository at this point in the history
…-cache-metadata-check

Fix internal cache metadata check
  • Loading branch information
gjtorikian committed Jan 9, 2022
2 parents 8107f0c + e32958d commit 58bc1d3
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 25 deletions.
42 changes: 20 additions & 22 deletions lib/html_proofer/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,40 +97,38 @@ def check_files
# Walks over each implemented check and runs them on the files, in parallel.
def process_files
if @options[:parallel][:enable]
Parallel.map(files, @options[:parallel]) { |path| load_file(path) }
Parallel.map(files, @options[:parallel]) { |file| load_file(file[:path], file[:source]) }
else
files.map { |path| load_file(path) }
files.map do |file|
load_file(file[:path], file[:source])
end
end
end

def load_file(path)
def load_file(path, source)
@html = create_nokogiri(path)
check_parsed(path)
check_parsed(path, source)
end

# Collects any external URLs found in a directory of files. Also collectes
# every failed test from process_files.
def check_parsed(path)
def check_parsed(path, source)
result = { internal_urls: {}, external_urls: {}, failures: [] }

@source = [@source] if @type == :file

@source.each do |current_source|
checks.each do |klass|
@current_source = current_source
@current_path = path
checks.each do |klass|
@current_source = source
@current_path = path

check = Object.const_get(klass).new(self, @html)
@logger.log :debug, "Running #{check.short_name} in #{path}"
check = Object.const_get(klass).new(self, @html)
@logger.log :debug, "Running #{check.short_name} in #{path}"

@current_check = check
@current_check = check

check.run
check.run

result[:external_urls].merge!(check.external_urls)
result[:internal_urls].merge!(check.internal_urls)
result[:failures].concat(check.failures)
end
result[:external_urls].merge!(check.external_urls) { |_key, old, current| old.concat(current) }
result[:internal_urls].merge!(check.internal_urls) { |_key, old, current| old.concat(current) }
result[:failures].concat(check.failures)
end
result
end
Expand All @@ -150,10 +148,10 @@ def files
@files ||= if @type == :directory
@source.map do |src|
pattern = File.join(src, '**', "*{#{@options[:extensions].join(',')}}")
Dir.glob(pattern).select { |f| File.file?(f) && !ignore_file?(f) }
Dir.glob(pattern).select { |f| File.file?(f) && !ignore_file?(f) }.map { |f| { source: src, path: f } }
end.flatten
elsif @type == :file && @options[:extensions].include?(File.extname(@source))
[@source].reject { |f| ignore_file?(f) }
elsif @type == :file && @options[:extensions].include?(File.extname(@source))
[@source].reject { |f| ignore_file?(f) }.map { |f| { source: f, path: f } }
else
[]
end
Expand Down
12 changes: 12 additions & 0 deletions spec/html-proofer/fixtures/links/multiple_links.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
</head>
<body>
<a href="https://missing.png">The amazing gjtorikian</a>
<img alt="missing same as link" src="https://missing.png" />
<img alt="missing only as img" src="https://another.png" />
<a href="/pdfs.html">Existing internal link</a>
</body>
</html>

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

17 changes: 15 additions & 2 deletions spec/html-proofer/links_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,12 @@
expect(proofer.failed_checks.first.description).to match(/internally linking to #anadaasdadsadschor; the file exists, but the hash 'anadaasdadsadschor' does not/)
end

it 'finds the same broken link multiple times' do
multiple_problems = File.join(FIXTURES_DIR, 'links', 'multiple_links.html')
proofer = run_proofer(multiple_problems, :file)
expect(proofer.failed_checks.length).to eq(3)
end

it 'ignores valid mailto links' do
ignorable_links = File.join(FIXTURES_DIR, 'links', 'mailto_link.html')
proofer = run_proofer(ignorable_links, :file)
Expand Down Expand Up @@ -671,11 +677,18 @@
end

it 'works for a direct link through directory' do
file = File.join(FIXTURES_DIR, 'links', 'internals')
proofer = run_proofer(file, :directory)
dir = File.join(FIXTURES_DIR, 'links', 'internals')
proofer = run_proofer(dir, :directory)
expect(proofer.failed_checks).to eq []
end

it 'knows how to find internal link with additional sources' do
empty_dir = File.join(FIXTURES_DIR, 'links', 'same_name_as_dir')
valid_dir = File.join(FIXTURES_DIR, 'links', 'internals')
proofer = run_proofer([valid_dir, empty_dir], :directories)
expect(proofer.failed_checks.length).to eq(0)
end

it 'reports linked internal through directory' do
file = File.join(FIXTURES_DIR, 'links', 'hashes')
proofer = run_proofer(file, :directory)
Expand Down
2 changes: 1 addition & 1 deletion spec/html-proofer/proofer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
it 'works for directory that ends with .html' do
folder = File.join(FIXTURES_DIR, 'links', '_site/folder.html')
proofer = HTMLProofer.check_directory(folder)
expect(proofer.files).to eq(["#{folder}/index.html"])
expect(proofer.files).to eq([{ source: folder, path: "#{folder}/index.html" }])
end
end

Expand Down

0 comments on commit 58bc1d3

Please sign in to comment.