Skip to content

Commit

Permalink
(BOLT-1585) Ruby 3 compatability for separation of positional vs name…
Browse files Browse the repository at this point in the history
…d args

Changes to the way keyword and positional arguments are handled in ruby 3 are extensively documented in this post: https://www.ruby-lang.org/en/news/2019/12/12/separation-of-positional-and-keyword-arguments-in-ruby-3-0/ This commit updates bolt code to explicitly make the distinction between keyword and positional parameters. This change should retain compatability between both ruby 2 and 3.
  • Loading branch information
donoghuc committed Jun 2, 2022
1 parent e04a399 commit acda807
Show file tree
Hide file tree
Showing 8 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion lib/bolt/result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def self._pcore_init_from_hash

def _pcore_init_from_hash(init_hash)
opts = init_hash.reject { |k, _v| k == 'target' }
initialize(init_hash['target'], opts.transform_keys(&:to_sym))
initialize(init_hash['target'], **opts.transform_keys(&:to_sym))
end

def _pcore_init_hash
Expand Down
4 changes: 2 additions & 2 deletions lib/bolt_spec/plans/action_stubs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def default_for(target)
when Bolt::Error
Bolt::Result.from_exception(target, @data[:default])
when Hash
result_for(target, Bolt::Util.walk_keys(@data[:default], &:to_sym))
result_for(target, **Bolt::Util.walk_keys(@data[:default], &:to_sym))
else
raise 'Default result must be a Hash'
end
Expand Down Expand Up @@ -156,7 +156,7 @@ def return_for_targets(data)
# set the inventory from the BoltSpec::Plans, otherwise if we try to convert
# this target to a string, it will fail to string conversion because the
# inventory is nil
hsh[target] = result_for(Bolt::Target.new(target, @inventory), Bolt::Util.walk_keys(result, &:to_sym))
hsh[target] = result_for(Bolt::Target.new(target, @inventory), **Bolt::Util.walk_keys(result, &:to_sym))
end
raise "Cannot set return values and return block." if @return_block
@data_set = true
Expand Down
2 changes: 1 addition & 1 deletion lib/bolt_spec/plans/action_stubs/download_stub.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def parameters
@invocation[:options]
end

def result_for(_target, _data)
def result_for(_target, **_data)
raise 'Download result cannot be changed'
end

Expand Down
2 changes: 1 addition & 1 deletion lib/bolt_spec/plans/action_stubs/plan_stub.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def parameters
end

# Allow any data.
def result_for(_target, data)
def result_for(_target, **data)
Bolt::PlanResult.new(Bolt::Util.walk_keys(data, &:to_s), 'success')
end

Expand Down
2 changes: 1 addition & 1 deletion lib/bolt_spec/plans/action_stubs/task_stub.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def parameters
end

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

Expand Down
2 changes: 1 addition & 1 deletion lib/bolt_spec/plans/action_stubs/upload_stub.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def parameters
@invocation[:options]
end

def result_for(_target, _data)
def result_for(_target, **_data)
raise 'Upload result cannot be changed'
end

Expand Down
2 changes: 1 addition & 1 deletion lib/bolt_spec/plans/mock_executor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ def stub_apply
@allow_apply = true
end

def wait_until_available(targets, _options)
def wait_until_available(targets, **_options)
Bolt::ResultSet.new(targets.map { |target| Bolt::Result.new(target) })
end

Expand Down
8 changes: 4 additions & 4 deletions spec/bolt_spec/run_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
end

it 'should accept _catch_errors' do
result = run_task('sample::echo', 'non_existent_node', '_catch_errors' => true)
result = run_task('sample::echo', 'non_existent_node', { '_catch_errors' => true })

expect(result[0]['status']).to eq('failure')
expect(result[0]['value']['_error']['kind']).to eq('puppetlabs.tasks/connect-error')
Expand Down Expand Up @@ -85,20 +85,20 @@

describe 'run_plan' do
it 'should run a plan' do
result = run_plan('sample::single_task', 'nodes' => 'ssh')
result = run_plan('sample::single_task', { 'nodes' => 'ssh' })
expect(result['status']).to eq('success')
data = result['value'][0]
expect(data['status']).to eq('success')
end

it 'should return a failure' do
result = run_plan('error::run_fail', 'targets' => 'ssh')
result = run_plan('error::run_fail', { 'targets' => 'ssh' })
expect(result['status']).to eq('failure')
expect(result['value']['kind']).to eq('bolt/run-failure')
end

it 'runs a plan that downloads a file' do
result = run_plan('sample::download_file', 'nodes' => 'ssh')
result = run_plan('sample::download_file', { 'nodes' => 'ssh' })
expect(result['status']).to eq('success')
data = result['value'][0]
expect(data['value']['path']).to match(%r{^/tmp/})
Expand Down

0 comments on commit acda807

Please sign in to comment.