File: spec_helper.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 (96 lines) | stat: -rw-r--r-- 2,444 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
# frozen_string_literal: true

require "timecop"
require "html_proofer"
require "open3"

FIXTURES_DIR = "spec/html-proofer/fixtures"

RSpec.configure do |config|
  # Use color in STDOUT
  config.color = true

  # Use color not only in STDOUT but also in pagers and files
  config.tty = true

  # Use the specified formatter
  config.formatter = :documentation # :progress, :html, :textmate

  # Run in a random order
  config.order = :random
end

def capture_stderr(*)
  original_stderr = $stderr
  original_stdout = $stdout
  $stderr = fake_err = StringIO.new
  $stdout = StringIO.new unless ENV["DEBUG"]
  begin
    yield
  rescue SystemExit # rubocop:disable Lint/SuppressedException
  ensure
    $stderr = original_stderr
    $stdout = original_stdout unless ENV["DEBUG"]
  end
  fake_err.string
end

def make_proofer(item, type, opts)
  opts[:log_level] ||= :error
  case type
  when :file
    HTMLProofer.check_file(item, opts)
  when :directory
    HTMLProofer.check_directory(item, opts)
  when :directories
    HTMLProofer.check_directories(item, opts)
  when :links
    HTMLProofer.check_links(item, opts)
  end
end

def run_proofer(item, type, opts = {})
  skip 'VCR not available'
  proofer = make_proofer(item, type, opts)
  cassette_name = make_cassette_name(item, opts)
  VCR.use_cassette(cassette_name, record: :new_episodes) do
    capture_stderr { proofer.run }
    proofer
  end
end

def capture_proofer_http(item, type, opts = {})
  skip 'VCR not available'
  proofer = make_proofer(item, type, opts)
  cassette_name = make_cassette_name(item, opts)
  VCR.use_cassette(cassette_name, record: :new_episodes) do
    capture_stderr { proofer.run }
    VCR.current_cassette.serializable_hash["http_interactions"].last
  end
end

def make_bin(args)
  ap("#{RbConfig.ruby} exe/htmlproofer #{args}") if debug?
  stdout, stderr = Open3.capture3("#{RbConfig.ruby} exe/htmlproofer #{args}")
  # Filter out Ruby experimental warnings (e.g., IO::Buffer in Ruby 4.0)
  stderr = stderr.lines.reject { |line| line.include?("warning:") && line.include?("experimental") }.join
  "#{stdout}\n#{stderr}".encode("UTF-8", invalid: :replace, undef: :replace)
end

def make_cassette_name(file, opts)
  filename = if file.is_a?(Array)
    file.join("_")
  else
    file.split("/")[-2..].join("/")
  end
  (filename += opts.inspect) unless opts.empty?
  filename
end

def debug?
  ENV.fetch("DEBUG", false)
end

def ci?
  ENV.fetch("CI", false)
end