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

Fix the issues #173 and #192 with the 'Rake::Task#results' #420

Open
wants to merge 2 commits into
base: master
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/rake.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ module Rake; end
require "rake/linked_list"
require "rake/cpu_counter"
require "rake/scope"
require "rake/task_action"
require "rake/task_argument_error"
require "rake/rule_recursion_overflow_error"
require "rake/rake_module"
Expand Down
15 changes: 9 additions & 6 deletions lib/rake/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def initialize(task_name, app)
# Enhance a task with prerequisites or actions. Returns self.
def enhance(deps=nil, &block)
@prerequisites |= deps if deps
@actions << block if block_given?
@actions << TaskAction.new(&block) if block_given?
self
end

Expand Down Expand Up @@ -182,6 +182,11 @@ def clear_args
self
end

# List of results for all invocations of a rake task.
def results
@actions.flat_map { |act| act.results }
end

# Invoke the task if it is needed. Prerequisites are invoked first.
def invoke(*args)
task_args = TaskArguments.new(arg_names, args)
Expand Down Expand Up @@ -275,11 +280,9 @@ def execute(args=nil)
end
application.trace "** Execute #{name}" if application.options.trace
application.enhance_with_matching_rule(name) if @actions.empty?
if opts = Hash.try_convert(args) and !opts.empty?
@actions.each { |act| act.call(self, args, **opts) }
else
@actions.each { |act| act.call(self, args) }
end
opts = Hash.try_convert(args)

@actions.each { |act| act.call(self, args, opts) }
end

# Is this task needed?
Expand Down
32 changes: 32 additions & 0 deletions lib/rake/task_action.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true
module Rake

##
# A TaskAction represents each action of a task.
#
class TaskAction
attr_reader :results

def initialize(&block)
@block = block
@results = []
end

def call(argc, argv, opts)
results << call_block(argc, argv, opts)
rescue => error
results << error
raise error
end

private

def call_block(argc, argv, opts)
if opts && !opts.empty?
@block.call(argc, argv, **opts)
else
@block.call(argc, argv)
end
end
end
end
1 change: 1 addition & 0 deletions rake.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ Rake has the following features:
"lib/rake/rule_recursion_overflow_error.rb",
"lib/rake/scope.rb",
"lib/rake/task.rb",
"lib/rake/task_action.rb",
"lib/rake/task_argument_error.rb",
"lib/rake/task_arguments.rb",
"lib/rake/task_manager.rb",
Expand Down
69 changes: 69 additions & 0 deletions test/test_rake_task_results.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# frozen_string_literal: true
require File.expand_path("../helper", __FILE__)

class TestRakeTaskArgumentParsing < Rake::TestCase # :nodoc:
def setup
super

@app = Rake::Application.new
end

def test_results_with_a_simple_task_and_a_single_invocation
task = Rake::Task.new("simple", @app)

task.enhance(nil) { "result" }
task.invoke

assert_equal ["result"], task.results
end

def test_results_with_a_simple_task_and_multiple_invocations
task = Rake::Task.new("simple", @app)

task.enhance(nil) { "result" }
task.invoke
task.reenable
task.invoke

assert_equal ["result", "result"], task.results
end

def test_results_with_a_composite_task_and_a_single_invocation
task = Rake::Task.new("composite", @app)

task.enhance(nil) { "result1" }
task.enhance(nil) { "result2" }
task.invoke

assert_equal ["result1", "result2"], task.results
end

def test_results_with_a_composite_task_and_multiple_invocations
task = Rake::Task.new("composite", @app)

task.enhance(nil) { "result1" }
task.enhance(nil) { "result2" }

task.invoke
task.reenable
task.invoke

assert_equal ["result1", "result1", "result2", "result2"], task.results
end

def test_results_with_an_empty_task
task = Rake::Task.new("empty", @app)
task.invoke

assert task.results.empty?
end

def test_results_with_a_composite_task_and_no_invocations
task = Rake::Task.new("composite", @app)

task.enhance(nil) { "result1" }
task.enhance(nil) { "result2" }

assert task.results.empty?
end
end