From 4edfee431ef3fdc3a95b2dc9c8ff5322c9b480a3 Mon Sep 17 00:00:00 2001 From: Gonzalo Date: Wed, 7 Feb 2018 14:18:14 -0300 Subject: [PATCH] Make dry-run smart about potential file task executions. Fixes #92 --- lib/rake/file_task.rb | 4 ++++ lib/rake/task.rb | 11 ++++++++++- test/test_rake_functional.rb | 23 +++++++++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/lib/rake/file_task.rb b/lib/rake/file_task.rb index 3130b5cfc..48c52c470 100644 --- a/lib/rake/file_task.rb +++ b/lib/rake/file_task.rb @@ -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) diff --git a/lib/rake/task.rb b/lib/rake/task.rb index 256571112..4b4061c72 100644 --- a/lib/rake/task.rb +++ b/lib/rake/task.rb @@ -192,7 +192,12 @@ 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) @@ -200,6 +205,10 @@ def invoke_with_call_chain(task_args, invocation_chain) # :nodoc: 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) diff --git a/test/test_rake_functional.rb b/test/test_rake_functional.rb index 33d4dc0b2..fb3886ac8 100644 --- a/test/test_rake_functional.rb +++ b/test/test_rake_functional.rb @@ -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