Sha256: c62f16b16d7d50e704af8b68b8f7297e1bb577e949ea31cfb28790322432c42d

Contents?: true

Size: 1.71 KB

Versions: 16

Compression:

Stored size: 1.71 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.
          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

      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?('_') }
                                      .map { |k, v| [k.sub(/^_/, '').to_sym, v] }.to_h
        self
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
bolt-2.1.0 lib/bolt_spec/plans/action_stubs/task_stub.rb
bolt-2.0.1 lib/bolt_spec/plans/action_stubs/task_stub.rb
bolt-2.0.0 lib/bolt_spec/plans/action_stubs/task_stub.rb
bolt-1.49.0 lib/bolt_spec/plans/action_stubs/task_stub.rb
bolt-1.48.0 lib/bolt_spec/plans/action_stubs/task_stub.rb
bolt-1.47.0 lib/bolt_spec/plans/action_stubs/task_stub.rb
bolt-1.45.0 lib/bolt_spec/plans/action_stubs/task_stub.rb
bolt-1.44.0 lib/bolt_spec/plans/action_stubs/task_stub.rb
bolt-1.43.0 lib/bolt_spec/plans/action_stubs/task_stub.rb
bolt-1.42.0 lib/bolt_spec/plans/action_stubs/task_stub.rb
bolt-1.41.0 lib/bolt_spec/plans/action_stubs/task_stub.rb
bolt-1.40.0 lib/bolt_spec/plans/action_stubs/task_stub.rb
bolt-1.39.0 lib/bolt_spec/plans/action_stubs/task_stub.rb
bolt-1.38.0 lib/bolt_spec/plans/action_stubs/task_stub.rb
bolt-1.37.0 lib/bolt_spec/plans/action_stubs/task_stub.rb
bolt-1.36.0 lib/bolt_spec/plans/action_stubs/task_stub.rb