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.



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

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
22
# 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)


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

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



43
44
45
# File 'lib/chefspec/matchers/resource_matcher.rb', line 43

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

def failure_message
  if resource
    if unmatched_parameters.empty?
      if @compile_time
        %Q{expected "#{resource}" to be run at compile time}
      else
        %Q{expected "#{resource}" to be run at converge time}
      end
    else
      message = %Q{expected "#{resource}" 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_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



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/chefspec/matchers/resource_matcher.rb', line 84

def failure_message_when_negated
  if resource
    message = %Q{expected "#{resource}" actions #{resource.performed_actions.inspect} to not exist}
  else
    message = %Q{expected "#{resource}" 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)


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

def matches?(runner)
  @runner = runner

  if resource
    ChefSpec::Coverage.cover!(resource)
    unmatched_parameters.empty? && correct_phase?
  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