Sha256: 8ec83fe2b304513f80ce1696e2ed6c75ceb5e0fe8b9ab945b8b4a9b42a08fda7

Contents?: true

Size: 1.54 KB

Versions: 12

Compression:

Stored size: 1.54 KB

Contents

# frozen_string_literal: true

module BoltSpec
  module Plans
    class TaskStub < ActionStub
      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

      def call(targets, task, arguments, options)
        @calls += 1
        if @return_block
          # Merge arguments and options into params to match puppet function signature.
          check_resultset(@return_block.call(targets: targets, task: task, params: arguments.merge(options)), task)
        else
          Bolt::ResultSet.new(targets.map { |target| @data[target.name] || default_for(target) })
        end
      end

      def parameters
        @invocation[:params]
      end

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

      # Public methods

      # Restricts the stub to only match invocations with certain parameters.
      # All parameters must match exactly.
      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?('_') }
        self
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
bolt-1.32.0 lib/bolt_spec/plans/action_stubs/task_stub.rb
bolt-1.31.1 lib/bolt_spec/plans/action_stubs/task_stub.rb
bolt-1.31.0 lib/bolt_spec/plans/action_stubs/task_stub.rb
bolt-1.30.1 lib/bolt_spec/plans/action_stubs/task_stub.rb
bolt-1.30.0 lib/bolt_spec/plans/action_stubs/task_stub.rb
bolt-1.29.1 lib/bolt_spec/plans/action_stubs/task_stub.rb
bolt-1.29.0 lib/bolt_spec/plans/action_stubs/task_stub.rb
bolt-1.28.0 lib/bolt_spec/plans/action_stubs/task_stub.rb
bolt-1.27.1 lib/bolt_spec/plans/action_stubs/task_stub.rb
bolt-1.27.0 lib/bolt_spec/plans/action_stubs/task_stub.rb
bolt-1.26.0 lib/bolt_spec/plans/action_stubs/task_stub.rb
bolt-1.25.0 lib/bolt_spec/plans/action_stubs/task_stub.rb