diff --git a/lib/rake/clean.rb b/lib/rake/clean.rb index 1e5da81cb..e3c3b3a4a 100644 --- a/lib/rake/clean.rb +++ b/lib/rake/clean.rb @@ -29,6 +29,7 @@ def cleanup_files(file_names) def cleanup(file_name, opts={}) begin + opts = {:verbose => Rake.application.options.trace}.merge(opts) rm_r file_name, opts rescue StandardError => ex puts "Failed to remove #{file_name}: #{ex}" unless file_already_gone?(file_name) diff --git a/test/test_rake_clean.rb b/test/test_rake_clean.rb index 04367907a..36ca5305a 100644 --- a/test/test_rake_clean.rb +++ b/test/test_rake_clean.rb @@ -32,8 +32,56 @@ def test_cleanup_ignores_missing_files refute_match(/failed to remove/i, out) end + def test_cleanup_trace + file_name = create_file + + assert_output "", "rm -r #{file_name}\n" do + with_trace true do + Rake::Cleaner.cleanup(file_name) + end + end + end + + def test_cleanup_without_trace + file_name = create_file + + assert_output "", "" do + with_trace false do + Rake::Cleaner.cleanup(file_name) + end + end + end + + def test_cleanup_opt_overrides_trace_silent + file_name = create_file + + assert_output "", "" do + with_trace true do + Rake::Cleaner.cleanup(file_name, verbose: false) + end + end + end + + def test_cleanup_opt_overrides_trace_verbose + file_name = create_file + + assert_output "", "rm -r #{file_name}\n" do + with_trace false do + Rake::Cleaner.cleanup(file_name, verbose: true) + end + end + end + private + def create_file + dir_name = File.join(@tempdir, "deletedir") + file_name = File.join(dir_name, "deleteme") + FileUtils.mkdir(dir_name) + FileUtils.touch(file_name) + file_name + end + def create_undeletable_file dir_name = File.join(@tempdir, "deletedir") file_name = File.join(dir_name, "deleteme") @@ -46,7 +94,7 @@ def create_undeletable_file rescue file_name else - skip "Permission to delete files is different on thie system" + skip "Permission to delete files is different on this system" end end @@ -58,4 +106,16 @@ def remove_undeletable_file Rake::Cleaner.cleanup(file_name, verbose: false) Rake::Cleaner.cleanup(dir_name, verbose: false) end + + def with_trace value + old, Rake.application.options.trace = + Rake.application.options.trace, value + + # FileUtils caches the $stderr object, which breaks capture_io et. al. + # We hack it here where it's convenient to do so. + Rake::Cleaner.instance_variable_set :@fileutils_output, nil + yield + ensure + Rake.application.options.trace = old + end end