Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keyword arguments for a command aren't passed correctly to commands that use callbacks #136

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/dry/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ def perform_registry(arguments)

command, args = parse(result.command, result.arguments, result.names)

result.before_callbacks.run(command, args)
result.before_callbacks.run(command, **args)
command.call(**args)
result.after_callbacks.run(command, args)
result.after_callbacks.run(command, **args)
end

# Parse arguments for a command.
Expand Down
4 changes: 2 additions & 2 deletions lib/dry/cli/command_registry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,9 @@ def append(&callback)

# @since 0.4.0
# @api private
def run(context, *args)
def run(context, **args)
chain.each do |callback|
context.instance_exec(*args, &callback)
context.instance_exec(**args, &callback)
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions spec/integration/third_party_gems_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
expected = <<~OUTPUT
before command callback Foo::Webpack::CLI::CallbacksCommand {:url=>"https://hanamirb.test", :dir=>"."}
before callback (class), 2 arg(s): {:url=>"https://hanamirb.test", :dir=>"."}
before callback (class), kwarg(s): url: https://hanamirb.test, dir: .
before callback (object), 2 arg(s): {:url=>"https://hanamirb.test", :dir=>"."}
dir: ., url: "https://hanamirb.test"
after command callback Foo::Webpack::CLI::CallbacksCommand {:url=>"https://hanamirb.test", :dir=>"."}
after callback (class), 2 arg(s): {:url=>"https://hanamirb.test", :dir=>"."}
after callback (class), kwarg(s): url: https://hanamirb.test, dir: .
after callback (object), 2 arg(s): {:url=>"https://hanamirb.test", :dir=>"."}
OUTPUT
expect(output).to eq(expected)
Expand Down
14 changes: 14 additions & 0 deletions spec/support/fixtures/foo
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,12 @@ module Callbacks
end
end

class BeforeKwargsClass
def call(url:, dir:, **)
puts "before callback (class), kwarg(s): url: #{url}, dir: #{dir}"
end
end

class AfterClass
def call(args)
puts "after callback (class), #{count(args)} arg(s): #{args.inspect}"
Expand All @@ -553,6 +559,12 @@ module Callbacks
end
end

class AfterKwargsClass
def call(url:, dir:, **)
puts "after callback (class), kwarg(s): url: #{url}, dir: #{dir}"
end
end

class Before
def call(args)
puts "before callback (object), #{count(args)} arg(s): #{args.inspect}"
Expand Down Expand Up @@ -580,7 +592,9 @@ end
# rubocop:enable Layout/LineLength

Foo::CLI::Commands.before("callbacks", Callbacks::BeforeClass)
Foo::CLI::Commands.before("callbacks", Callbacks::BeforeKwargsClass)
Foo::CLI::Commands.after("callbacks", Callbacks::AfterClass)
Foo::CLI::Commands.after("callbacks", Callbacks::AfterKwargsClass)
Foo::CLI::Commands.before("callbacks", Callbacks::Before.new)
Foo::CLI::Commands.after("callbacks", Callbacks::After.new)

Expand Down