Class: ChefSpec::Matchers::ResourceMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/chefspec/matchers/resource_matcher.rb

Instance Method Summary collapse

Constructor Details

#initialize(resource_name, expected_action, expected_identity) ⇒ ResourceMatcher

Returns a new instance of ResourceMatcher.



6
7
8
9
10
# File 'lib/chefspec/matchers/resource_matcher.rb', line 6

def initialize(resource_name, expected_action, expected_identity)
  @resource_name     = resource_name
  @expected_action   = expected_action
  @expected_identity = expected_identity
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

Allow users to specify fancy #with matchers.



32
33
34
35
36
37
38
39
# File 'lib/chefspec/matchers/resource_matcher.rb', line 32

def method_missing(m, *args, &block)
  if m.to_s =~ /^with_(.+)$/
    with($1.to_sym => args.first)
    self
  else
    super
  end
end

Instance Method Details

#at_compile_timeObject

Raises:

  • (ArgumentError)


17
18
19
20
21
# File 'lib/chefspec/matchers/resource_matcher.rb', line 17

def at_compile_time
  raise ArgumentError, 'Cannot specify both .at_converge_time and .at_compile_time!' if @converge_time
  @compile_time = true
  self
end

#at_converge_timeObject

Raises:

  • (ArgumentError)


23
24
25
26
27
# File 'lib/chefspec/matchers/resource_matcher.rb', line 23

def at_converge_time
  raise ArgumentError, 'Cannot specify both .at_compile_time and .at_converge_time!' if @compile_time
  @converge_time = true
  self
end

#descriptionObject



41
42
43
# File 'lib/chefspec/matchers/resource_matcher.rb', line 41

def description
  %Q{#{@expected_action} #{@resource_name} "#{@expected_identity}"}
end

#failure_messageObject



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
# File 'lib/chefspec/matchers/resource_matcher.rb', line 56

def failure_message
  if resource
    if resource.performed_action?(@expected_action)
      if unmatched_parameters.empty?
        if @compile_time
          %Q{expected "#{resource.to_s}" to be run at compile time}
        else
          %Q{expected "#{resource.to_s}" to be run at converge time}
        end
      else
        message = %Q{expected "#{resource.to_s}" to have parameters:} \
        "\n\n" \
        "  " + unmatched_parameters.collect { |parameter, h|
          msg = "#{parameter} #{h[:expected].inspect}, was #{h[:actual].inspect}"
          diff = ::RSpec::Matchers::ExpectedsForMultipleDiffs.from(h[:expected]) \
                 .message_with_diff(message, ::RSpec::Expectations::differ, h[:actual])
          msg += diff if diff
          msg
        }.join("\n  ")
      end
    else
      %Q{expected "#{resource.to_s}" actions #{resource.performed_actions.inspect}} \
      " to include :#{@expected_action}"
    end
  else
    %Q{expected "#{@resource_name}[#{@expected_identity}]"} \
    " with action :#{@expected_action} to be in Chef run." \
    " Other #{@resource_name} resources:" \
    "\n\n" \
    "  " + similar_resources.map(&:to_s).join("\n  ") + "\n "
  end
end

#failure_message_when_negatedObject



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/chefspec/matchers/resource_matcher.rb', line 89

def failure_message_when_negated
  if resource
    message = %Q{expected "#{resource.to_s}" actions #{resource.performed_actions.inspect} to not exist}
  else
    message = %Q{expected "#{resource.to_s}" to not exist}
  end

  message << " at compile time"  if @compile_time
  message << " at converge time" if @converge_time
  message
end

#matches?(runner) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
48
49
50
51
52
53
54
# File 'lib/chefspec/matchers/resource_matcher.rb', line 45

def matches?(runner)
  @runner = runner

  if resource
    ChefSpec::Coverage.cover!(resource)
    resource.performed_action?(@expected_action) && unmatched_parameters.empty? && correct_phase?
  else
    false
  end
end

#with(parameters = {}) ⇒ Object



12
13
14
15
# File 'lib/chefspec/matchers/resource_matcher.rb', line 12

def with(parameters = {})
  params.merge!(parameters)
  self
end