Skip to content

Commit

Permalink
Make dry-run smart about potential file task executions. Fixes ruby#92
Browse files Browse the repository at this point in the history
  • Loading branch information
grzuy committed Feb 7, 2018
1 parent efe3f02 commit 4edfee4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/rake/file_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ def needed?
! File.exist?(name) || out_of_date?(timestamp) || @application.options.build_all
end

def potentially_needed?
needed? || prerequisite_tasks.any?(&:potentially_needed?)
end

# Time stamp for file task.
def timestamp
if File.exist?(name)
Expand Down
11 changes: 10 additions & 1 deletion lib/rake/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,23 @@ def invoke_with_call_chain(task_args, invocation_chain) # :nodoc:
return if @already_invoked
@already_invoked = true
invoke_prerequisites(task_args, new_chain)
execute(task_args) if needed?

if needed?
execute(task_args)
elsif application.options.dryrun && potentially_needed?
application.trace "** May execute (dry run) #{name}"
end
end
rescue Exception => ex
add_chain_to(ex, new_chain)
raise ex
end
protected :invoke_with_call_chain

def potentially_needed?
needed?
end

def add_chain_to(exception, new_chain) # :nodoc:
exception.extend(InvocationExceptionMixin) unless
exception.respond_to?(:chain)
Expand Down
23 changes: 23 additions & 0 deletions test/test_rake_functional.rb
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,29 @@ def test_dry_run
refute_match %r{OTHER}, @out
end

def test_dry_run_smart_about_potential_file_executions
FileUtils.touch("fileA")
FileUtils.touch("fileB")
FileUtils.touch("fileC")
FileUtils.touch("fileD")

rakefile <<-RAKEFILE
file "fileA" do; end
file "fileB" => ['fileA'] do; end
file "fileC" => ['fileB'] do; end
file "fileD" => ['fileC'] do; end
RAKEFILE

sleep 1
FileUtils.touch("fileA")

rake "-n", "fileD"

assert_match(/Execute \(dry run\) fileB/, @err)
assert_match(/May execute \(dry run\) fileC/, @err)
assert_match(/May execute \(dry run\) fileD/, @err)
end

# Test for the trace/dry_run bug found by Brian Chandler
def test_dry_run_bug
rakefile_dryrun
Expand Down

0 comments on commit 4edfee4

Please sign in to comment.