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


3
4
5
6
7
# File 'lib/chefspec/matchers/resource_matcher.rb', line 3

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.


29
30
31
32
33
34
35
36
# File 'lib/chefspec/matchers/resource_matcher.rb', line 29

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)

14
15
16
17
18
# File 'lib/chefspec/matchers/resource_matcher.rb', line 14

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)

20
21
22
23
24
# File 'lib/chefspec/matchers/resource_matcher.rb', line 20

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


38
39
40
# File 'lib/chefspec/matchers/resource_matcher.rb', line 38

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

#failure_messageObject


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

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
        %Q{expected "#{resource.to_s}" to have parameters:} \
        "\n\n" \
        "  " + unmatched_parameters.collect { |parameter, h|
          "#{parameter} #{h[:expected].inspect}, was #{h[:actual].inspect}"
        }.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


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

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)

42
43
44
45
46
47
48
49
50
51
# File 'lib/chefspec/matchers/resource_matcher.rb', line 42

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


9
10
11
12
# File 'lib/chefspec/matchers/resource_matcher.rb', line 9

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