From b64de7d984476eb2311a67c8696aa8d47d34cdfa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A5borg?= Date: Tue, 1 Oct 2019 17:12:06 +0200 Subject: [PATCH 1/8] test: add test linking relative to root folder --- .../fixtures/links/working_root_link_internal.html | 10 ++++++++++ spec/html-proofer/links_spec.rb | 6 ++++++ 2 files changed, 16 insertions(+) create mode 100644 spec/html-proofer/fixtures/links/working_root_link_internal.html diff --git a/spec/html-proofer/fixtures/links/working_root_link_internal.html b/spec/html-proofer/fixtures/links/working_root_link_internal.html new file mode 100644 index 00000000..b6700a25 --- /dev/null +++ b/spec/html-proofer/fixtures/links/working_root_link_internal.html @@ -0,0 +1,10 @@ + + + +

+ Blah blah blah. + Not a real link! +

+ + + diff --git a/spec/html-proofer/links_spec.rb b/spec/html-proofer/links_spec.rb index 3214dc78..f323927f 100644 --- a/spec/html-proofer/links_spec.rb +++ b/spec/html-proofer/links_spec.rb @@ -79,6 +79,12 @@ expect(proofer.failed_tests.first).to match(%r{internally linking to .\/notreal.html, which does not exist}) end + it 'succeeds for working internal root links to other folder' do + broken_root_link_internal_filepath = "#{FIXTURES_DIR}/links/working_root_link_internal.html" + proofer = run_proofer(broken_root_link_internal_filepath, :file, {:root_dir => "/app/spec/html-proofer/fixtures"}) + expect(proofer.failed_tests).to eq [] + end + it 'fails for link with no href' do missing_link_href_filepath = "#{FIXTURES_DIR}/links/missing_link_href.html" proofer = run_proofer(missing_link_href_filepath, :file) From b72026ac951de47a763c87603392c940cd8c2b38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A5borg?= Date: Tue, 1 Oct 2019 17:27:10 +0200 Subject: [PATCH 2/8] feat: enable specifying a root folder Enables user to run .check_file while having a non-flat file hierarchy. --- bin/htmlproofer | 1 + lib/html-proofer/element.rb | 7 ++++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/bin/htmlproofer b/bin/htmlproofer index 51f3e95e..f5220e94 100755 --- a/bin/htmlproofer +++ b/bin/htmlproofer @@ -47,6 +47,7 @@ Mercenary.program(:htmlproofer) do |p| p.option 'typhoeus_config', '--typhoeus-config CONFIG', String, 'JSON-formatted string of Typhoeus config. Will override the html-proofer defaults.' p.option 'url_ignore', '--url-ignore link1,[link2,...]', Array, 'A comma-separated list of Strings or RegExps containing URLs that are safe to ignore. It affects all HTML attributes. Note that non-HTTP(S) URIs are always ignored' p.option 'url_swap', '--url-swap re:string,[re:string,...]', Array, 'A comma-separated list containing key-value pairs of `RegExp => String`. It transforms URLs that match `RegExp` into `String` via `gsub`. The escape sequences `\\:` should be used to produce literal `:`s.' + p.option 'root_dir', '--root-folder PATH', String, 'The absolute path to the directory serving your html-files. To be used when running html-proofer on a file, rather than a dir. (i.e HTMLProofer.check_file(...))' p.action do |args, opts| args = ['.'] if args.empty? diff --git a/lib/html-proofer/element.rb b/lib/html-proofer/element.rb index cf93a6b1..d0f88b8d 100644 --- a/lib/html-proofer/element.rb +++ b/lib/html-proofer/element.rb @@ -164,7 +164,12 @@ def file_path end if path =~ %r{^/} # path relative to root - base = File.directory?(@check.src) ? @check.src : File.dirname(@check.src) + if File.directory?(@check.src) + base = @check.src + else + root_dir = @check.options[:root_dir] + base = root_dir ? root_dir : File.dirname(@check.src) + end elsif File.exist?(File.expand_path(path, @check.src)) || File.exist?(File.expand_path(path_dot_ext, @check.src)) # relative links, path is a file base = File.dirname @check.path elsif File.exist?(File.join(File.dirname(@check.path), path)) || File.exist?(File.join(File.dirname(@check.path), path_dot_ext)) # relative links in nested dir, path is a file From cfcabadb2ad7635323c474b766a28a562fe64945 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A5borg?= Date: Tue, 1 Oct 2019 17:36:35 +0200 Subject: [PATCH 3/8] chore: change fixture to be more truthful --- .../html-proofer/fixtures/links/working_root_link_internal.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/html-proofer/fixtures/links/working_root_link_internal.html b/spec/html-proofer/fixtures/links/working_root_link_internal.html index b6700a25..9ca8490c 100644 --- a/spec/html-proofer/fixtures/links/working_root_link_internal.html +++ b/spec/html-proofer/fixtures/links/working_root_link_internal.html @@ -3,7 +3,7 @@

Blah blah blah. - Not a real link! + A real link!

From 1d1f73de8e9dd0c9be2ab913b7fa3a2d5e4014f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A5borg?= Date: Wed, 2 Oct 2019 14:37:04 +0200 Subject: [PATCH 4/8] chore: update test name to retrigger travis --- spec/html-proofer/links_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/html-proofer/links_spec.rb b/spec/html-proofer/links_spec.rb index f323927f..30826718 100644 --- a/spec/html-proofer/links_spec.rb +++ b/spec/html-proofer/links_spec.rb @@ -79,7 +79,7 @@ expect(proofer.failed_tests.first).to match(%r{internally linking to .\/notreal.html, which does not exist}) end - it 'succeeds for working internal root links to other folder' do + it 'succeeds for working internal-root-links pointing to other folder' do broken_root_link_internal_filepath = "#{FIXTURES_DIR}/links/working_root_link_internal.html" proofer = run_proofer(broken_root_link_internal_filepath, :file, {:root_dir => "/app/spec/html-proofer/fixtures"}) expect(proofer.failed_tests).to eq [] From acc08e7c93c929082cd5dc0d5f81d7c4c91c34f0 Mon Sep 17 00:00:00 2001 From: Mikael Date: Mon, 7 Oct 2019 12:43:10 +0200 Subject: [PATCH 5/8] fix: remove double quotes and brackets --- spec/html-proofer/links_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/html-proofer/links_spec.rb b/spec/html-proofer/links_spec.rb index 30826718..c385106c 100644 --- a/spec/html-proofer/links_spec.rb +++ b/spec/html-proofer/links_spec.rb @@ -81,7 +81,7 @@ it 'succeeds for working internal-root-links pointing to other folder' do broken_root_link_internal_filepath = "#{FIXTURES_DIR}/links/working_root_link_internal.html" - proofer = run_proofer(broken_root_link_internal_filepath, :file, {:root_dir => "/app/spec/html-proofer/fixtures"}) + proofer = run_proofer(broken_root_link_internal_filepath, :file, root_dir: '/app/spec/html-proofer/fixtures') expect(proofer.failed_tests).to eq [] end From 92a11737027d8609892261bdbe95922d5f1e22a6 Mon Sep 17 00:00:00 2001 From: Mikael Date: Mon, 7 Oct 2019 12:44:03 +0200 Subject: [PATCH 6/8] fix: remove /app/ --- spec/html-proofer/links_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/html-proofer/links_spec.rb b/spec/html-proofer/links_spec.rb index c385106c..8b04eb2c 100644 --- a/spec/html-proofer/links_spec.rb +++ b/spec/html-proofer/links_spec.rb @@ -81,7 +81,7 @@ it 'succeeds for working internal-root-links pointing to other folder' do broken_root_link_internal_filepath = "#{FIXTURES_DIR}/links/working_root_link_internal.html" - proofer = run_proofer(broken_root_link_internal_filepath, :file, root_dir: '/app/spec/html-proofer/fixtures') + proofer = run_proofer(broken_root_link_internal_filepath, :file, root_dir: '/spec/html-proofer/fixtures') expect(proofer.failed_tests).to eq [] end From f447fc04591475c02b5b1a8bcf8f6e999a637fb1 Mon Sep 17 00:00:00 2001 From: Mikael Date: Mon, 7 Oct 2019 12:51:44 +0200 Subject: [PATCH 7/8] fix: remove leading slash --- spec/html-proofer/links_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/html-proofer/links_spec.rb b/spec/html-proofer/links_spec.rb index 8b04eb2c..bf87b0c4 100644 --- a/spec/html-proofer/links_spec.rb +++ b/spec/html-proofer/links_spec.rb @@ -81,7 +81,7 @@ it 'succeeds for working internal-root-links pointing to other folder' do broken_root_link_internal_filepath = "#{FIXTURES_DIR}/links/working_root_link_internal.html" - proofer = run_proofer(broken_root_link_internal_filepath, :file, root_dir: '/spec/html-proofer/fixtures') + proofer = run_proofer(broken_root_link_internal_filepath, :file, root_dir: 'spec/html-proofer/fixtures') expect(proofer.failed_tests).to eq [] end From e739950f01d94d8557cf71adef8238ef3135a10c Mon Sep 17 00:00:00 2001 From: "Garen J. Torikian" Date: Sun, 17 Nov 2019 19:07:54 +0000 Subject: [PATCH 8/8] Update docs --- README.md | 1 + bin/htmlproofer | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 6fd6fa20..00e20797 100644 --- a/README.md +++ b/README.md @@ -297,6 +297,7 @@ The `HTMLProofer` constructor takes an optional hash of additional options: | `internal_domains`| An array of Strings containing domains that will be treated as internal urls. | `[]` | | `log_level` | Sets the logging level, as determined by [Yell](https://github.com/rudionrails/yell). One of `:debug`, `:info`, `:warn`, `:error`, or `:fatal`. | `:info` | `only_4xx` | Only reports errors for links that fall within the 4xx status code range. | `false` | +| `root_dir` | The absolute path to the directory serving your html-files. Used when running html-proofer on a file, rather than a directory. | "" | | `typhoeus_config` | A JSON-formatted string. Parsed using `JSON.parse` and mapped on top of the default configuration values so that they can be overridden. | `{}` | | `url_ignore` | An array of Strings or RegExps containing URLs that are safe to ignore. It affects all HTML attributes. Note that non-HTTP(S) URIs are always ignored. | `[]` | | `url_swap` | A hash containing key-value pairs of `RegExp => String`. It transforms URLs that match `RegExp` into `String` via `gsub`. | `{}` | diff --git a/bin/htmlproofer b/bin/htmlproofer index f5220e94..eed31fbc 100755 --- a/bin/htmlproofer +++ b/bin/htmlproofer @@ -47,7 +47,7 @@ Mercenary.program(:htmlproofer) do |p| p.option 'typhoeus_config', '--typhoeus-config CONFIG', String, 'JSON-formatted string of Typhoeus config. Will override the html-proofer defaults.' p.option 'url_ignore', '--url-ignore link1,[link2,...]', Array, 'A comma-separated list of Strings or RegExps containing URLs that are safe to ignore. It affects all HTML attributes. Note that non-HTTP(S) URIs are always ignored' p.option 'url_swap', '--url-swap re:string,[re:string,...]', Array, 'A comma-separated list containing key-value pairs of `RegExp => String`. It transforms URLs that match `RegExp` into `String` via `gsub`. The escape sequences `\\:` should be used to produce literal `:`s.' - p.option 'root_dir', '--root-folder PATH', String, 'The absolute path to the directory serving your html-files. To be used when running html-proofer on a file, rather than a dir. (i.e HTMLProofer.check_file(...))' + p.option 'root_dir', '--root-folder PATH', String, 'The absolute path to the directory serving your html-files. Used when running html-proofer on a file, rather than a directory.' p.action do |args, opts| args = ['.'] if args.empty?