Class: RSpec::Puppet::ManifestMatchers::CreateGeneric

Inherits:
Object
  • Object
show all
Includes:
Errors
Defined in:
lib/rspec-puppet/matchers/create_generic.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ CreateGeneric

Returns a new instance of CreateGeneric.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rspec-puppet/matchers/create_generic.rb', line 11

def initialize(*args, &block)
  @exp_resource_type = args.shift.to_s.gsub(/^(create|contain)_/, '')
  @args = args
  @block = block
  @referenced_type = referenced_type(@exp_resource_type)
  @title = args[0]

  @errors = []
  @expected_params = []
  @expected_undef_params = []
  @notifies = []
  @subscribes = []
  @requires = []
  @befores = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/rspec-puppet/matchers/create_generic.rb', line 65

def method_missing(method, *args, &block)
  case method.to_s
  when /^with_/
    param = method.to_s.gsub(/^with_/, '')
    @expected_params << [param, args[0]]
    self
  when /^only_with_/
    param = method.to_s.gsub(/^only_with_/, '')
    @expected_params_count = (@expected_params_count || 0) + 1
    @expected_params << [param, args[0]]
    self
  when /^without_/
    param = method.to_s.gsub(/^without_/, '')
    @expected_undef_params << [param, args[0]]
    self
  else
    super
  end
end

Instance Method Details

#actualObject



192
193
194
# File 'lib/rspec-puppet/matchers/create_generic.rb', line 192

def actual
  @errors.filter_map { |e| e.actual if e.respond_to?(:actual) }.join("\n\n")
end

#descriptionObject



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/rspec-puppet/matchers/create_generic.rb', line 135

def description
  values = []
  value_str_prefix = 'with'

  values << "exactly #{@expected_params_count} parameters" if @expected_params_count

  values.concat(generate_param_list(@expected_params, :should)) if @expected_params.any?

  values.concat(generate_param_list(@expected_undef_params, :not)) if @expected_undef_params.any?

  if @notifies.any?
    value_str_prefix = 'that notifies'
    values = @notifies
  end

  if @subscribes.any?
    value_str_prefix = 'that subscribes to'
    values = @subscribes
  end

  if @requires.any?
    value_str_prefix = 'that requires'
    values = @requires
  end

  if @befores.any?
    value_str_prefix = 'that comes before'
    values = @befores
  end

  unless values.empty?
    value_str = if values.length == 1
                  " #{value_str_prefix} #{values.first}"
                else
                  " #{value_str_prefix} #{values[0..-2].join(', ')} and #{values[-1]}"
                end
  end

  "contain #{@referenced_type}[#{@title}]#{value_str}"
end

#diffable?Boolean

Returns:

  • (Boolean)


176
177
178
# File 'lib/rspec-puppet/matchers/create_generic.rb', line 176

def diffable?
  true
end

#expectedObject



188
189
190
# File 'lib/rspec-puppet/matchers/create_generic.rb', line 188

def expected
  @errors.filter_map { |e| e.expected if e.respond_to?(:expected) }.join("\n\n")
end

#failure_messageObject



127
128
129
# File 'lib/rspec-puppet/matchers/create_generic.rb', line 127

def failure_message
  "expected that the catalogue would contain #{@referenced_type}[#{@title}]#{errors}"
end

#failure_message_when_negatedObject



131
132
133
# File 'lib/rspec-puppet/matchers/create_generic.rb', line 131

def failure_message_when_negated
  "expected that the catalogue would not contain #{@referenced_type}[#{@title}]#{errors}"
end

#matches?(catalogue) ⇒ Boolean

Returns:

  • (Boolean)


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
# File 'lib/rspec-puppet/matchers/create_generic.rb', line 85

def matches?(catalogue)
  ret = true
  @catalogue = catalogue.is_a?(Puppet::Resource::Catalog) ? catalogue : catalogue.call
  resource = @catalogue.resource(@referenced_type, @title)

  if resource.nil?
    false
  else
    RSpec::Puppet::Coverage.cover!(resource)
    rsrc_hsh = resource.to_hash
    if resource.respond_to?(:sensitive_parameters)
      resource.sensitive_parameters.each do |s_param|
        rsrc_hsh[s_param] = ::Puppet::Pops::Types::PSensitiveType::Sensitive.new(rsrc_hsh[s_param])
      end
    end

    namevar = if resource.builtin_type?
                resource.resource_type.key_attributes.first.to_s
              else
                'name'
              end

    if @expected_params.none? { |param| param.first.to_s == namevar } && rsrc_hsh.key?(namevar.to_sym)
      rsrc_hsh.delete(namevar.to_sym)
    end

    if @expected_params_count && rsrc_hsh.size != @expected_params_count
      ret = false
      (@errors ||= []) << "exactly #{@expected_params_count} parameters but the catalogue contains #{rsrc_hsh.size}"
    end

    check_params(rsrc_hsh, @expected_params, :should) if @expected_params.any?
    check_params(rsrc_hsh, @expected_undef_params, :not) if @expected_undef_params.any?
    check_befores(@catalogue, resource) if @befores.any?
    check_requires(@catalogue, resource) if @requires.any?
    check_notifies(@catalogue, resource) if @notifies.any?
    check_subscribes(@catalogue, resource) if @subscribes.any?

    @errors.empty?
  end
end

#only_with(*args, &block) ⇒ Object



33
34
35
36
37
# File 'lib/rspec-puppet/matchers/create_generic.rb', line 33

def only_with(*args, &block)
  params = args.shift
  @expected_params_count = (@expected_params_count || 0) + params.compact.size
  with(params, &block)
end

#supports_block_expectationsObject



180
181
182
# File 'lib/rspec-puppet/matchers/create_generic.rb', line 180

def supports_block_expectations
  true
end

#supports_value_expectationsObject



184
185
186
# File 'lib/rspec-puppet/matchers/create_generic.rb', line 184

def supports_value_expectations
  true
end

#that_comes_before(resource) ⇒ Object



60
61
62
63
# File 'lib/rspec-puppet/matchers/create_generic.rb', line 60

def that_comes_before(resource)
  @befores.concat(Array(resource))
  self
end

#that_notifies(resource) ⇒ Object



45
46
47
48
# File 'lib/rspec-puppet/matchers/create_generic.rb', line 45

def that_notifies(resource)
  @notifies.concat(Array(resource))
  self
end

#that_requires(resource) ⇒ Object



55
56
57
58
# File 'lib/rspec-puppet/matchers/create_generic.rb', line 55

def that_requires(resource)
  @requires.concat(Array(resource))
  self
end

#that_subscribes_to(resource) ⇒ Object



50
51
52
53
# File 'lib/rspec-puppet/matchers/create_generic.rb', line 50

def that_subscribes_to(resource)
  @subscribes.concat(Array(resource))
  self
end

#with(*args) ⇒ Object



27
28
29
30
31
# File 'lib/rspec-puppet/matchers/create_generic.rb', line 27

def with(*args)
  params = args.shift
  @expected_params |= params.to_a
  self
end

#without(*args) ⇒ Object



39
40
41
42
43
# File 'lib/rspec-puppet/matchers/create_generic.rb', line 39

def without(*args)
  params = args.shift
  @expected_undef_params |= Array(params)
  self
end