diff --git a/lib/rake/task.rb b/lib/rake/task.rb index 18c09eefd..23faf96e5 100644 --- a/lib/rake/task.rb +++ b/lib/rake/task.rb @@ -247,14 +247,7 @@ def execute(args=nil) end application.trace "** Execute #{name}" if application.options.trace application.enhance_with_matching_rule(name) if @actions.empty? - @actions.each do |act| - case act.arity - when 1 - act.call(self) - else - act.call(self, args) - end - end + @actions.each { |act| act.call(self, args) } end # Is this task needed? diff --git a/test/test_rake_task_with_arguments.rb b/test/test_rake_task_with_arguments.rb index ba28d11f9..9c396d3b2 100644 --- a/test/test_rake_task_with_arguments.rb +++ b/test/test_rake_task_with_arguments.rb @@ -80,6 +80,36 @@ def test_actions_of_various_arity_are_ok_with_args assert_equal [:a, :b, :c, :d], notes end + + def test_actions_adore_keywords + # A brutish trick to avoid parsing. Remove it once support for 1.9 and 2.0 is dropped + # https://ci.appveyor.com/project/ruby/rake/build/1.0.301 + skip 'Keywords aren\'t a feature in this version' if RUBY_VERSION =~ /^1|^2\.0/ + eval <<-RUBY, binding, __FILE__, __LINE__+1 + notes = [] + t = task :t, [:reqr, :ovrd, :dflt] # required, overridden-optional, default-optional + verify = lambda do |name, expecteds, actuals| + notes << name + assert_equal expecteds.length, actuals.length + expecteds.zip(actuals) { |e, a| assert_equal e, a, "(TEST \#{name})" } + end + + t.enhance { |dflt: 'd', **| verify.call :a, ['d'], [dflt] } + t.enhance { |ovrd: '-', **| verify.call :b, ['o'], [ovrd] } + t.enhance { |reqr: , **| verify.call :c, ['r'], [reqr] } + + t.enhance { |t2, dflt: 'd', **| verify.call :d, [t,'d'], [t2,dflt] } + t.enhance { |t2, ovrd: 'd', **| verify.call :e, [t,'o'], [t2,ovrd] } + t.enhance { |t2, reqr: , **| verify.call :f, [t,'r'], [t2,reqr] } + + t.enhance { |t2, dflt: 'd', reqr:, **| verify.call :g, [t,'d','r'], [t2,dflt,reqr] } + t.enhance { |t2, ovrd: '-', reqr:, **| verify.call :h, [t,'o','r'], [t2,ovrd,reqr] } + + t.invoke('r', 'o') + assert_equal [*:a..:h], notes + RUBY + end + def test_arguments_are_passed_to_block t = task(:t, :a, :b) { |tt, args| assert_equal({ a: 1, b: 2 }, args.to_hash)