File: spec_helper.rb

package info (click to toggle)
puppet-strings 4.1.3-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,412 kB
  • sloc: ruby: 7,324; makefile: 12
file content (87 lines) | stat: -rw-r--r-- 2,509 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
# frozen_string_literal: true

if ENV['COVERAGE'] == 'yes'
  require 'simplecov'
  require 'simplecov-console'

  SimpleCov.formatters = [
    SimpleCov::Formatter::HTMLFormatter,
    SimpleCov::Formatter::Console
  ]

  SimpleCov.start do
    track_files 'lib/**/*.rb'
    add_filter 'lib/puppet-strings/version.rb'
    add_filter '/spec'
  end
end

require 'mocha'
require 'mdl'
require 'rspec'
require 'json_spec'
require 'puppet/version'
require 'puppet-strings'
require 'puppet-strings/markdown'
require 'puppet-strings/markdown/base'
require 'puppet-strings/yard'

# Explicitly set up YARD once
PuppetStrings::Yard.setup!

# Enable testing of Puppet functions if running against 4.1+
TEST_PUPPET_FUNCTIONS = Puppet::Util::Package.versioncmp(Puppet.version, '4.1.0') >= 0

# Enable testing of Puppet language functions declared with return type if running against 4.8+
TEST_FUNCTION_RETURN_TYPE = Puppet::Util::Package.versioncmp(Puppet.version, '4.8.0') >= 0

# Enable testing of Plans if Puppet version is greater than 5.0.0
TEST_PUPPET_PLANS = Puppet::Util::Package.versioncmp(Puppet.version, '5.0.0') >= 0

# Enable testing of Data Types if Puppet version is greater than 4.1.0
TEST_PUPPET_DATATYPES = Puppet::Util::Package.versioncmp(Puppet.version, '4.1.0') >= 0

RSpec.configure do |config|
  config.mock_with :mocha

  config.before do
    # Always clear the YARD registry before each example
    YARD::Registry.clear
  end
end

def lint_markdown(content)
  # Load default mdl ruleset
  ruleset = MarkdownLint::RuleSet.new.tap(&:load_default)

  # Apply custom style to ruleset rules
  MarkdownLint::Style.load(File.join(__dir__, 'markdownlint_style.rb'), ruleset.rules)

  # Create a document
  doc = MarkdownLint::Doc.new(content, false)

  # Run the rules
  violations = []
  ruleset.rules.each do |id, rule|
    error_lines = rule.check.call(doc)
    next if error_lines.nil? || error_lines.empty?

    # record the error
    error_lines.each do |line|
      line += doc.offset # Correct line numbers for any yaml front matter
      violations << "#{line}: #{id} #{rule.description}: #{doc.lines[line - 1]}"
    end
  end
  violations
end

RSpec::Matchers.define :have_no_markdown_lint_errors do
  match do |actual|
    @violations = lint_markdown(actual)
    @violations.empty?
  end

  failure_message do |actual|
    "expected that #{actual.length > 80 ? "#{actual.slice(0, 80).inspect}..." : actual.inspect} would have no markdown lint errors but got #{@violations.join("\n")}"
  end
end