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

Respect trace option in cleanup #164

Merged
merged 2 commits into from
Oct 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/rake/clean.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
62 changes: 61 additions & 1 deletion test/test_rake_clean.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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

Expand All @@ -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