Skip to content

Commit

Permalink
Merge pull request #75 from viralpraxis/actualize-thread-safety-dir-c…
Browse files Browse the repository at this point in the history
…hdir-cop-specs

Actualize `ThreadSafety::DirChdir` cop specs
  • Loading branch information
viralpraxis authored Jan 12, 2025
2 parents 4176ff7 + 0e28892 commit 0689d13
Showing 1 changed file with 95 additions and 103 deletions.
198 changes: 95 additions & 103 deletions spec/rubocop/cop/thread_safety/dir_chdir_spec.rb
Original file line number Diff line number Diff line change
@@ -1,126 +1,118 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::ThreadSafety::DirChdir, :config do
let(:msg) { 'Avoid using `Dir.chdir` due to its process-wide effect.' }

context 'with `Dir.chdir` method' do
it 'registers an offense' do
expect_offense(<<~RUBY)
Dir.chdir("/var/run")
^^^^^^^^^^^^^^^^^^^^^ #{msg}
RUBY
end

it 'registers an offense when called without arguments' do
expect_offense(<<~RUBY)
Dir.chdir
^^^^^^^^^ #{msg}
RUBY
end

it 'registers an offense with top-level constant' do
expect_offense(<<~RUBY)
::Dir.chdir("/var/run")
^^^^^^^^^^^^^^^^^^^^^^^ #{msg}
RUBY
end

it 'registers an offense with provided block' do
expect_offense(<<~RUBY)
Dir.chdir("/var/run") do
^^^^^^^^^^^^^^^^^^^^^ #{msg}
puts Dir.pwd
end
RUBY
end

it 'registers an offense with provided block with argument' do
expect_offense(<<~RUBY)
Dir.chdir("/var/run") do |dir|
^^^^^^^^^^^^^^^^^^^^^ #{msg}
puts dir
end
RUBY
end

it 'registers an offense with provided block argument' do
expect_offense(<<~RUBY)
def change_dir(&block)
Dir.chdir("/var/run", &block)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg}
end
RUBY
%w[Dir.chdir FileUtils.chdir FileUtils.cd].each do |expression|
context "with `#{expression}` call" do
it 'registers an offense' do
expect_offense(<<~RUBY, expression: expression)
%{expression}("/var/run")
^{expression}^^^^^^^^^^^^ Avoid using `%{expression}` due to its process-wide effect.
RUBY
end

it 'registers an offense without arguments' do
expect_offense(<<~RUBY, expression: expression)
%{expression}
^{expression} Avoid using `%{expression}` due to its process-wide effect.
RUBY
end

it 'registers an offense with fully quialified constant name' do
expect_offense(<<~RUBY, expression: expression)
::%{expression}("/var/run")
^{expression}^^^^^^^^^^^^^^ Avoid using `%{expression}` due to its process-wide effect.
RUBY
end

it 'registers an offense with provided block' do
expect_offense(<<~RUBY, expression: expression)
%{expression}("/var/run") do
^{expression}^^^^^^^^^^^^ Avoid using `%{expression}` due to its process-wide effect.
puts Dir.pwd
end
RUBY
end

it 'registers an offense with provided block with argument' do
expect_offense(<<~RUBY, expression: expression)
%{expression}("/var/run") do |dir|
^{expression}^^^^^^^^^^^^ Avoid using `%{expression}` due to its process-wide effect.
puts dir
end
RUBY
end

it 'registers an offense with provided block argument' do
expect_offense(<<~RUBY, expression: expression)
def change_dir(&block)
%{expression}("/var/run", &block)
^{expression}^^^^^^^^^^^^^^^^^^^^ Avoid using `%{expression}` due to its process-wide effect.
end
RUBY
end
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}
%w[Dir FileUtils].each do |constant_name|
it "does not register an offense for unrelated `#{constant_name}` method" do
expect_no_offenses(<<~RUBY)
#{constant_name}.pwd
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}
%w[chdir cd].each do |method_name|
it "does not register an offense for unrelated `#{method_name}` with unrelated receiver" do
expect_no_offenses(<<~RUBY)
Foo.#{method_name}
RUBY
end
end

context 'with another `Dir` class method' do
it 'does not register an offense' do
expect_no_offenses 'Dir.pwd'
end
end

context 'when receiver is not `Dir`' do
it 'does not register an offense' do
expect_no_offenses 'chdir("/tmp")'
end
end

context 'with `AllowCallWithBlock` config set to `true`' do
let(:cop_config) do
{ 'Enabled' => true, 'AllowCallWithBlock' => true }
end

it 'registers an offense' do
expect_offense(<<~RUBY)
Dir.chdir("/var/run")
^^^^^^^^^^^^^^^^^^^^^ #{msg}
expect_no_offenses(<<~RUBY)
foo.#{method_name}
RUBY
end

it 'does not register an offense with provided block' do
expect_no_offenses(<<~RUBY)
Dir.chdir("/var/run") do
puts Dir.pwd
end
#{method_name}
RUBY
end
end

it 'does not register an offense with provided block with argument' do
expect_no_offenses(<<~RUBY)
Dir.chdir("/var/run") do |dir|
puts dir
end
RUBY
context 'with `AllowCallWithBlock` configuration option set to `true`' do
let(:cop_config) do
{ 'Enabled' => true, 'AllowCallWithBlock' => true }
end

it 'does not register an offense with provided block argument' do
expect_no_offenses(<<~RUBY)
def change_dir(&block)
Dir.chdir("/var/run", &block)
end
RUBY
%w[Dir.chdir FileUtils.chdir FileUtils.cd].each do |expression|
it 'registers an offense for block-less call' do
expect_offense(<<~RUBY, expression: expression)
%{expression}("/var/run")
^{expression}^^^^^^^^^^^^ Avoid using `%{expression}` due to its process-wide effect.
RUBY
end

it 'does not register an offense for block-ful call' do
expect_no_offenses(<<~RUBY)
#{expression}("/var/run") do
p Dir.pwd
end
RUBY
end

it 'does no register an offense with provided block with argument' do
expect_no_offenses(<<~RUBY)
#{expression}("/var/run") do |dir|
p dir
end
RUBY
end

it 'does not register an offense with provided block argument' do
expect_no_offenses(<<~RUBY)
def change_dir(&block)
#{expression}("/var/run", &block)
end
RUBY
end
end
end
end

0 comments on commit 0689d13

Please sign in to comment.