From a2ff4428e2bad21acb769a6ecff383213216fcd1 Mon Sep 17 00:00:00 2001 From: Issy Long Date: Sun, 14 Jul 2024 12:09:59 -0400 Subject: [PATCH] rubocop/no_fileutils_rmrf: Scope to just formulae and casks --- Library/.rubocop.yml | 5 ++ .../Homebrew/rubocops/no_fileutils_rmrf.rb | 15 +++--- .../test/rubocops/no_fileutils_rmrf_spec.rb | 49 ++++++------------- 3 files changed, 27 insertions(+), 42 deletions(-) diff --git a/Library/.rubocop.yml b/Library/.rubocop.yml index aaddb602a6ddc8..829dd2dcdc50b6 100644 --- a/Library/.rubocop.yml +++ b/Library/.rubocop.yml @@ -60,6 +60,11 @@ Homebrew/CompactBlank: # `blank?` is not necessarily available here: - "Homebrew/extend/enumerable.rb" +Homebrew/NoFileutilsRmrf: + Include: + - "/**/{Formula,Casks}/**/*.rb" + - "**/{Formula,Casks}/**/*.rb" + # only used internally Homebrew/MoveToExtendOS: Enabled: false diff --git a/Library/Homebrew/rubocops/no_fileutils_rmrf.rb b/Library/Homebrew/rubocops/no_fileutils_rmrf.rb index a99c9c5452e296..ed234c96992b9c 100644 --- a/Library/Homebrew/rubocops/no_fileutils_rmrf.rb +++ b/Library/Homebrew/rubocops/no_fileutils_rmrf.rb @@ -9,15 +9,14 @@ module Homebrew class NoFileutilsRmrf < Base extend AutoCorrector - MSG = "Use `FileUtils.rm` or `FileUtils.rm_r` instead of `FileUtils.rm_rf`, `FileUtils.rm_f`, " \ - "or `{FileUtils,Pathname}.rmtree`." + MSG = "Use `rm` or `rm_r` instead of `rm_rf`, `rm_f`, or `rmtree`." - def_node_matcher :fileutils_rm_r_f?, <<~PATTERN - (send (const {nil? cbase} :FileUtils) {:rm_rf :rm_f :rmtree} ...) + def_node_matcher :self_rm_r_f_tree?, <<~PATTERN + (send (self) {:rm_rf :rm_f :rmtree} ...) PATTERN - def_node_matcher :pathname_rmtree?, <<~PATTERN - (send (const {nil? cbase} :Pathname) :rmtree ...) + def_node_matcher :plain_rm_r_f_tree?, <<~PATTERN + (send nil? {:rm_rf :rm_f :rmtree} ...) PATTERN def on_send(node) @@ -30,12 +29,12 @@ def on_send(node) "rm" end - corrector.replace(node.loc.expression, "FileUtils.#{new_method}(#{node.arguments.first.source})") + corrector.replace(node.loc.expression, "#{new_method}(#{node.arguments.first.source})") end end def neither_rm_rf_nor_rmtree?(node) - !fileutils_rm_r_f?(node) && !pathname_rmtree?(node) + !self_rm_r_f_tree?(node) && !plain_rm_r_f_tree?(node) end end end diff --git a/Library/Homebrew/test/rubocops/no_fileutils_rmrf_spec.rb b/Library/Homebrew/test/rubocops/no_fileutils_rmrf_spec.rb index 4581fb2dbba70d..749808ccbd8ab9 100644 --- a/Library/Homebrew/test/rubocops/no_fileutils_rmrf_spec.rb +++ b/Library/Homebrew/test/rubocops/no_fileutils_rmrf_spec.rb @@ -5,78 +5,59 @@ RSpec.describe RuboCop::Cop::Homebrew::NoFileutilsRmrf do subject(:cop) { described_class.new } - describe "FileUtils.rm_rf" do + describe "rm_rf" do it "registers an offense" do expect_offense(<<~RUBY) - FileUtils.rm_rf("path/to/directory") - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Homebrew/NoFileutilsRmrf: #{RuboCop::Cop::Homebrew::NoFileutilsRmrf::MSG} + rm_rf("path/to/directory") + ^^^^^^^^^^^^^^^^^^^^^^^^^^ Homebrew/NoFileutilsRmrf: #{RuboCop::Cop::Homebrew::NoFileutilsRmrf::MSG} RUBY end it "autocorrects" do corrected = autocorrect_source(<<~RUBY) - FileUtils.rm_rf("path/to/directory") + rm_rf("path/to/directory") RUBY expect(corrected).to eq(<<~RUBY) - FileUtils.rm_r("path/to/directory") + rm_r("path/to/directory") RUBY end end - describe "FileUtils.rm_f" do + describe "rm_f" do it "registers an offense" do expect_offense(<<~RUBY) - FileUtils.rm_f("path/to/directory") - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Homebrew/NoFileutilsRmrf: #{RuboCop::Cop::Homebrew::NoFileutilsRmrf::MSG} + rm_f("path/to/directory") + ^^^^^^^^^^^^^^^^^^^^^^^^^ Homebrew/NoFileutilsRmrf: #{RuboCop::Cop::Homebrew::NoFileutilsRmrf::MSG} RUBY end it "autocorrects" do corrected = autocorrect_source(<<~RUBY) - FileUtils.rm_f("path/to/directory") + rm_f("path/to/directory") RUBY expect(corrected).to eq(<<~RUBY) - FileUtils.rm("path/to/directory") + rm("path/to/directory") RUBY end end - describe "FileUtils.rmtree" do + describe "rmtree" do it "registers an offense" do expect_offense(<<~RUBY) - FileUtils.rmtree("path/to/directory") - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Homebrew/NoFileutilsRmrf: #{RuboCop::Cop::Homebrew::NoFileutilsRmrf::MSG} + rmtree("path/to/directory") + ^^^^^^^^^^^^^^^^^^^^^^^^^^^ Homebrew/NoFileutilsRmrf: #{RuboCop::Cop::Homebrew::NoFileutilsRmrf::MSG} RUBY end it "autocorrects" do corrected = autocorrect_source(<<~RUBY) - FileUtils.rmtree("path/to/directory") + rmtree("path/to/directory") RUBY expect(corrected).to eq(<<~RUBY) - FileUtils.rm_r("path/to/directory") - RUBY - end - end - - describe "Pathname.rmtree" do - it "registers an offense" do - expect_offense(<<~RUBY) - Pathname.rmtree("path/to/directory") - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Homebrew/NoFileutilsRmrf: #{RuboCop::Cop::Homebrew::NoFileutilsRmrf::MSG} - RUBY - end - - it "autocorrects" do - corrected = autocorrect_source(<<~RUBY) - Pathname.rmtree("path/to/directory") - RUBY - - expect(corrected).to eq(<<~RUBY) - FileUtils.rm_r("path/to/directory") + rm_r("path/to/directory") RUBY end end