diff --git a/lib/rubocop/cop/thread_safety/dir_chdir.rb b/lib/rubocop/cop/thread_safety/dir_chdir.rb index 534a494..6a13b12 100644 --- a/lib/rubocop/cop/thread_safety/dir_chdir.rb +++ b/lib/rubocop/cop/thread_safety/dir_chdir.rb @@ -8,17 +8,28 @@ module ThreadSafety # @example # # bad # Dir.chdir("/var/run") + # + # # bad + # FileUtils.chdir("/var/run") class DirChdir < Base - MSG = 'Avoid using `Dir.chdir` due to its process-wide effect.' - RESTRICT_ON_SEND = %i[chdir].freeze + MESSAGE = 'Avoid using `%s.%s` due to its process-wide effect.' + RESTRICT_ON_SEND = %i[chdir cd].freeze - # @!method dir_chdir?(node) - def_node_matcher :dir_chdir?, <<~MATCHER - (send (const {nil? cbase} :Dir) :chdir ...) + # @!method chdir?(node) + def_node_matcher :chdir?, <<~MATCHER + { + (send (const {nil? cbase} {:Dir :FileUtils}) :chdir ...) + (send (const {nil? cbase} :FileUtils) :cd ...) + } MATCHER def on_send(node) - dir_chdir?(node) { add_offense(node) } + chdir?(node) do + add_offense( + node, + message: format(MESSAGE, module: node.receiver.short_name, method: node.method_name) + ) + end end end end diff --git a/spec/rubocop/cop/thread_safety/dir_chdir_spec.rb b/spec/rubocop/cop/thread_safety/dir_chdir_spec.rb index 0dbe7db..c916931 100644 --- a/spec/rubocop/cop/thread_safety/dir_chdir_spec.rb +++ b/spec/rubocop/cop/thread_safety/dir_chdir_spec.rb @@ -26,6 +26,28 @@ end end + context 'with `FileUtils.chdir` method' do + let(:msg) { 'Avoid using `FileUtils.chdir` due to its process-wide effect.' } + + it 'registers an offense' do + expect_offense(<<~RUBY) + FileUtils.chdir("/var/run") + ^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg} + RUBY + end + end + + context 'with `FileUtils.cd` method' do + let(:msg) { 'Avoid using `FileUtils.cd` due to its process-wide effect.' } + + it 'registers an offense' do + expect_offense(<<~RUBY) + FileUtils.cd("/var/run") + ^^^^^^^^^^^^^^^^^^^^^^^^ #{msg} + RUBY + end + end + context 'with another `Dir` class method' do it 'does not register an offense' do expect_no_offenses 'Dir.pwd'