File: spec_helper.rb

package info (click to toggle)
puppet-lint 4.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 932 kB
  • sloc: ruby: 11,769; sh: 16; makefile: 11
file content (153 lines) | stat: -rw-r--r-- 3,824 bytes parent folder | download | duplicates (2)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# Disable GitHub Actions reporting since it breaks the test suite
ENV.delete('GITHUB_ACTION')

if ENV['COVERAGE'] == 'yes'
  begin
    require 'simplecov'
    require 'simplecov-console'
    SimpleCov.formatters = [
      SimpleCov::Formatter::HTMLFormatter,
      SimpleCov::Formatter::Console,
    ]

    SimpleCov.start do
      add_filter('/spec/')
      add_filter('/vendor/')
      add_group('Checks', 'lib/puppet-lint/plugins')
    end
  rescue LoadError
    raise 'Add the simplecov & simplecov-console gems to Gemfile to enable this task'
  end
end

require 'puppet-lint'
require 'rspec/its'
begin
  require 'rspec/json_expectations'
rescue LoadError, SyntaxError
  puts 'rspec/json_expectations is not available'
end

module RSpec::LintExampleGroup
  class HaveProblem
    def initialize(method, message)
      @expected_problem = {
        kind: method.to_s.delete_prefix('contain_').to_sym,
        message: message
      }
      @description = ["contain a #{@expected_problem[:kind]}"]
    end

    def on_line(line)
      @expected_problem[:line] = line
      @description << "on line #{line}"
      self
    end

    def in_column(column)
      @expected_problem[:column] = column
      @description << "starting in column #{column}"
      self
    end

    def with_reason(reason)
      @expected_problem[:reason] = reason
      @description << "with reason '#{reason}'"
      self
    end

    def matches?(problems)
      @problems = problems

      problems.any? do |problem|
        @expected_problem.all? do |key, value|
          problem.key?(key) && problem[key] == value
        end
      end
    end

    def description
      @description.join(' ')
    end

    def check_attr(attr, prefix)
      return if @expected_problem[attr] == @problems.first[attr]

      expected = @expected_problem[attr].inspect
      actual = @problems.first[attr].inspect
      "#{prefix} #{expected}, but it was #{actual}"
    end

    def failure_message
      case @problems.length
      when 0
        'expected that the check would create a problem but it did not'
      when 1
        messages = ['expected that the problem']

        messages << check_attr(:kind, 'would be of kind')
        messages << check_attr(:message, 'would have the message')
        messages << check_attr(:line, 'would be on line')
        messages << check_attr(:column, 'would start on column')
        messages << check_attr(:reason, 'would have the reason')

        messages.compact.join("\n  ")
      else
        [
          'expected that the check would create',
          PP.pp(@expected_problem, '').strip,
          'but it instead created',
          PP.pp(@problems, ''),
        ].join("\n")
      end
    end

    def failure_message_when_negated
      'expected that the check would not create the problem, but it did'
    end
  end

  def method_missing(method, *args, &block)
    return HaveProblem.new(method, args.first) if method.to_s.start_with?('contain_')

    super
  end

  def respond_to_missing?(method, *)
    method.to_s.start_with?('contain_') || super
  end

  def problems
    subject.problems
  end

  def manifest
    subject.manifest
  end

  def subject
    klass = PuppetLint::Checks.new
    filepath = respond_to?(:path) ? path : ''
    klass.load_data(filepath, code)
    check_name = self.class.top_level_description.to_sym
    check = PuppetLint.configuration.check_object[check_name].new
    klass.problems = check.run

    klass.problems = check.fix_problems if PuppetLint.configuration.fix

    klass
  end
end

RSpec.configure do |config|
  config.mock_framework = :rspec
  config.include(
    RSpec::LintExampleGroup,
    type: :lint,
    file_path: Regexp.new('spec[\\\/](unit[\\\/])?puppet-lint[\\\/]plugins'),
  )

  config.expect_with(:rspec) do |c|
    c.syntax = :expect
  end
end