From b4282f6ea714ddfac3d380b032b70d01b2717bbd Mon Sep 17 00:00:00 2001 From: Brian Henderson Date: Tue, 27 Sep 2016 15:56:45 -0700 Subject: [PATCH 1/2] Fix typo. --- test/test_rake_clean.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_rake_clean.rb b/test/test_rake_clean.rb index 04367907a..aac45cae3 100644 --- a/test/test_rake_clean.rb +++ b/test/test_rake_clean.rb @@ -46,7 +46,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 From f2a38bcfd7f0b993a7e2626100e39db2d57f1999 Mon Sep 17 00:00:00 2001 From: Brian Henderson Date: Tue, 27 Sep 2016 16:00:16 -0700 Subject: [PATCH 2/2] Teach cleanup to respect the trace option. --- lib/rake/clean.rb | 1 + test/test_rake_clean.rb | 60 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) 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 aac45cae3..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") @@ -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