forked from covermymeds/rubocop-thread_safety
-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #75 from viralpraxis/actualize-thread-safety-dir-c…
…hdir-cop-specs Actualize `ThreadSafety::DirChdir` cop specs
- Loading branch information
Showing
1 changed file
with
95 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |