Skip to content

Commit

Permalink
Casks use FileUtils.rm_rf & Pathname.rmtree still
Browse files Browse the repository at this point in the history
  • Loading branch information
issyl0 committed Jul 14, 2024
1 parent d161912 commit 0f48cb3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
14 changes: 12 additions & 2 deletions Library/Homebrew/rubocops/no_fileutils_rmrf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ class NoFileutilsRmrf < Base

MSG = "Use `rm` or `rm_r` instead of `rm_rf`, `rm_f`, or `rmtree`."

def_node_matcher :fileutils_rm_r_f_tree?, <<~PATTERN
(send (const {nil? cbase} :FileUtils) {:rm_rf :rm_f :rmtree} ...)
PATTERN

def_node_matcher :pathname_rm_r_f_tree?, <<~PATTERN
(send (const {nil? cbase} :Pathname) :rmtree ...)
PATTERN

def_node_matcher :self_rm_r_f_tree?, <<~PATTERN
(send (self) {:rm_rf :rm_f :rmtree} ...)
PATTERN
Expand All @@ -23,18 +31,20 @@ def on_send(node)
return if neither_rm_rf_nor_rmtree?(node)

add_offense(node) do |corrector|
class_name = "FileUtils." if fileutils_rm_r_f_tree?(node) || pathname_rm_r_f_tree?(node)
new_method = if node.method?(:rm_rf) || node.method?(:rmtree)
"rm_r"
else
"rm"
end

corrector.replace(node.loc.expression, "#{new_method}(#{node.arguments.first.source})")
corrector.replace(node.loc.expression, "#{class_name}#{new_method}(#{node.arguments.first.source})")
end
end

def neither_rm_rf_nor_rmtree?(node)
!self_rm_r_f_tree?(node) && !plain_rm_r_f_tree?(node)
!self_rm_r_f_tree?(node) && !plain_rm_r_f_tree?(node) &&
!fileutils_rm_r_f_tree?(node) && !pathname_rm_r_f_tree?(node)
end
end
end
Expand Down
12 changes: 12 additions & 0 deletions Library/Homebrew/test/rubocops/no_fileutils_rmrf_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,20 @@
expect_offense(<<~RUBY)
rm_rf("path/to/directory")
^^^^^^^^^^^^^^^^^^^^^^^^^^ Homebrew/NoFileutilsRmrf: #{RuboCop::Cop::Homebrew::NoFileutilsRmrf::MSG}
FileUtils.rm_rf("path/to/directory")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Homebrew/NoFileutilsRmrf: #{RuboCop::Cop::Homebrew::NoFileutilsRmrf::MSG}
RUBY
end

it "autocorrects" do
corrected = autocorrect_source(<<~RUBY)
rm_rf("path/to/directory")
FileUtils.rm_rf("path/to/other/directory")
RUBY

expect(corrected).to eq(<<~RUBY)
rm_r("path/to/directory")
FileUtils.rm_r("path/to/other/directory")
RUBY
end
end
Expand All @@ -29,16 +33,20 @@
expect_offense(<<~RUBY)
rm_f("path/to/directory")
^^^^^^^^^^^^^^^^^^^^^^^^^ Homebrew/NoFileutilsRmrf: #{RuboCop::Cop::Homebrew::NoFileutilsRmrf::MSG}
FileUtils.rm_f("path/to/other/directory")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Homebrew/NoFileutilsRmrf: #{RuboCop::Cop::Homebrew::NoFileutilsRmrf::MSG}
RUBY
end

it "autocorrects" do
corrected = autocorrect_source(<<~RUBY)
rm_f("path/to/directory")
FileUtils.rm_f("path/to/other/directory")
RUBY

expect(corrected).to eq(<<~RUBY)
rm("path/to/directory")
FileUtils.rm("path/to/other/directory")
RUBY
end
end
Expand All @@ -48,16 +56,20 @@
expect_offense(<<~RUBY)
rmtree("path/to/directory")
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Homebrew/NoFileutilsRmrf: #{RuboCop::Cop::Homebrew::NoFileutilsRmrf::MSG}
Pathname.rmtree("path/to/other/directory")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Homebrew/NoFileutilsRmrf: #{RuboCop::Cop::Homebrew::NoFileutilsRmrf::MSG}
RUBY
end

it "autocorrects" do
corrected = autocorrect_source(<<~RUBY)
rmtree("path/to/directory")
Pathname.rmtree("path/to/other/directory")
RUBY

expect(corrected).to eq(<<~RUBY)
rm_r("path/to/directory")
FileUtils.rm_r("path/to/other/directory")
RUBY
end
end
Expand Down

0 comments on commit 0f48cb3

Please sign in to comment.