File: proofer_spec.rb

package info (click to toggle)
ruby-html-proofer 5.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 22,524 kB
  • sloc: ruby: 4,389; sh: 8; makefile: 4; javascript: 1; php: 1
file content (97 lines) | stat: -rw-r--r-- 4,003 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# frozen_string_literal: true

require "spec_helper"

describe "Proofer" do
  let(:described_class) { HTMLProofer }

  describe "#failed_checks" do
    it "is an array of Failures" do
      broken_link_internal_filepath = File.join(FIXTURES_DIR, "links", "broken_link_internal.html")
      proofer = run_proofer(broken_link_internal_filepath, :file)
      expect(proofer.failed_checks.length).to(eq(2))
      expect(proofer.failed_checks[0].class).to(eq(HTMLProofer::Failure))
      expect(proofer.failed_checks.first.path).to(eq(broken_link_internal_filepath))
      expect(proofer.failed_checks.first.description).to(eq("internally linking to ./notreal.html, which does not exist"))
      expect(proofer.failed_checks.first.line).to(eq(3))
    end
  end

  describe "#files" do
    it "works for directory that ends with .html" do
      folder = File.join(FIXTURES_DIR, "links", "_site/folder.html")
      proofer = described_class.check_directory(folder)
      expect(proofer.files).to(eq([{ source: folder, path: "#{folder}/index.html" }]))
    end
  end

  describe "#options" do
    it "strips out undesired Typhoeus options" do
      folder = File.join(FIXTURES_DIR, "links", "_site/folder.html")
      proofer = described_class.check_file(folder, verbose: true)
      expect(proofer.options[:verbose]).to(be(true))
      expect(proofer.options[:typhoeus][:verbose]).to(be_nil)
    end

    it "only has one UA with file" do
      github_hash = File.join(FIXTURES_DIR, "links", "github_hash.html")
      http = capture_proofer_http(
        github_hash,
        :file,
        typhoeus: { verbose: true, headers: { "User-Agent" => "Mozilla/5.0 (compatible; My New User-Agent)" } },
      )
      expect(http["request"]["headers"]["User-Agent"]).to(eq(["Mozilla/5.0 (compatible; My New User-Agent)"]))
    end
  end

  describe "file ignores" do
    it "knows how to ignore a file by string" do
      options = { ignore_files: [File.join(FIXTURES_DIR, "links", "broken_hash_internal.html")] }
      broken_hash_internal_filepath = File.join(FIXTURES_DIR, "links", "broken_hash_internal.html")
      proofer = run_proofer(broken_hash_internal_filepath, :file, options)
      expect(proofer.failed_checks).to(eq([]))
    end

    it "knows how to ignore a file by regexp" do
      options = { ignore_files: [/broken_hash/] }
      broken_hash_internal_filepath = File.join(FIXTURES_DIR, "links", "broken_hash_internal.html")
      proofer = run_proofer(broken_hash_internal_filepath, :file, options)
      expect(proofer.failed_checks).to(eq([]))
    end

    it "knows how to ignore multiple files by regexp" do
      options = { ignore_files: [%r{.*/javadoc/.*}, %r{.*/catalog/.*}] }
      broken_folders = File.join(FIXTURES_DIR, "links", "folder/multiples")
      proofer = run_proofer([broken_folders], :directories, options)
      expect(proofer.failed_checks).to(eq([]))
    end

    it "knows how to ignore a directory by regexp" do
      options = { ignore_files: [/\S\.html/] }
      links_dir = File.join(FIXTURES_DIR, "links")
      proofer = run_proofer([links_dir], :directories, options)
      expect(proofer.failed_checks).to(eq([]))
    end
  end

  describe "ignored checks" do
    it "knows how to ignore checks" do
      options = { checks_to_ignore: ["ImageRunner"] }
      proofer = make_proofer(File.join(FIXTURES_DIR, "links", "broken_link_external.html"), :file, options)
      expect(proofer.checks).not_to(include("ImageRunner"))
    end

    it "does not care about phoney ignored checks" do
      options = { checks_to_ignore: ["This is nothing."] }
      proofer = make_proofer(File.join(FIXTURES_DIR, "links", "broken_link_external.html"), :file, options)
      expect(proofer.checks.length).to(eq(3))
    end
  end

  describe "external links" do
    it "ignores status codes when asked" do
      proofer = run_proofer(["www.github.com/github/notreallyhere"], :links, ignore_status_codes: [404])
      expect(proofer.failed_checks.length).to(eq(0))
    end
  end
end