Class: BoltSpec::Plans::TaskStub

Inherits:
ActionStub show all
Defined in:
lib/bolt_spec/plans/action_stubs/task_stub.rb

Instance Attribute Summary

Attributes inherited from ActionStub

#invocation

Instance Method Summary collapse

Methods inherited from ActionStub

#always_return, #assert_called, #be_called_times, #check_resultset, #default_for, #error_with, #expect_call, #initialize, #not_be_called, #return, #return_for_targets, #with_targets

Constructor Details

This class inherits a constructor from BoltSpec::Plans::ActionStub

Instance Method Details

#call(targets, task, arguments, options) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/bolt_spec/plans/action_stubs/task_stub.rb', line 22

def call(targets, task, arguments, options)
  @calls += 1
  if @return_block
    # Merge arguments and options into params to match puppet function signature.
    params = options.map { |k, v| ["_#{k}", v] }.to_h
    params = params.merge(arguments)

    check_resultset(@return_block.call(targets: targets, task: task, params: params), task)
  else
    Bolt::ResultSet.new(targets.map { |target| @data[target.name] || default_for(target) })
  end
end

#matches(targets, _task, arguments, options) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/bolt_spec/plans/action_stubs/task_stub.rb', line 6

def matches(targets, _task, arguments, options)
  if @invocation[:targets] && Set.new(@invocation[:targets]) != Set.new(targets.map(&:name))
    return false
  end

  if @invocation[:arguments] && arguments != @invocation[:arguments]
    return false
  end

  if @invocation[:options] && options != @invocation[:options]
    return false
  end

  true
end

#parametersObject



35
36
37
# File 'lib/bolt_spec/plans/action_stubs/task_stub.rb', line 35

def parameters
  @invocation[:params]
end

#result_for(target, data) ⇒ Object

Allow any data.



40
41
42
# File 'lib/bolt_spec/plans/action_stubs/task_stub.rb', line 40

def result_for(target, data)
  Bolt::Result.new(target, value: Bolt::Util.walk_keys(data, &:to_s))
end

#with_params(params) ⇒ Object

Restricts the stub to only match invocations with certain parameters. All parameters must match exactly.



48
49
50
51
52
53
54
# File 'lib/bolt_spec/plans/action_stubs/task_stub.rb', line 48

def with_params(params)
  @invocation[:params] = params
  @invocation[:arguments] = params.reject { |k, _v| k.start_with?('_') }
  @invocation[:options] = params.select { |k, _v| k.start_with?('_') }
                                .map { |k, v| [k.sub(/^_/, '').to_sym, v] }.to_h
  self
end