Skip to content

Commit

Permalink
Add more tests for Rails/FilePath
Browse files Browse the repository at this point in the history
  • Loading branch information
ydakuka committed Jan 26, 2025
1 parent f028c15 commit b2f71be
Showing 1 changed file with 239 additions and 18 deletions.
257 changes: 239 additions & 18 deletions spec/rubocop/cop/rails/file_path_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,66 @@
context 'when EnforcedStyle is `slashes`' do
let(:cop_config) { { 'EnforcedStyle' => 'slashes' } }

context 'when using Rails.root.join with some path strings' do
context 'when using Rails.root.parent' do
it 'registers an offense' do
expect_offense(<<~RUBY)
Rails.root.join('app', 'models', 'user.rb')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path/to')`.
Rails.root.parent.join("app", "models")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path/to')`.
RUBY

expect_correction(<<~RUBY)
Rails.root.join("app/models/user.rb")
Rails.root.parent.join("app/models")
RUBY
end
end

context 'when using File.join with Rails.root and path starting with `/`' do
context 'when using Rails.root.dirname' do
it 'registers an offense' do
expect_offense(<<~RUBY)
File.join(Rails.root, '/app/models', '/user.rb')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path/to').to_s`.
Rails.root.dirname.join("config", "initializers")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path/to')`.
RUBY

expect_correction(<<~RUBY)
Rails.root.join("app/models/user.rb").to_s
Rails.root.dirname.join("config/initializers")
RUBY
end
end

context 'when using ::Rails.root.join with some path strings' do
context 'when using Rails.root.basename' do
it 'registers an offense' do
expect_offense(<<~RUBY)
::Rails.root.join('app', 'models', 'user.rb')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path/to')`.
Rails.root.basename.join("config", "initializers")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path/to')`.
RUBY

expect_correction(<<~RUBY)
::Rails.root.join("app/models/user.rb")
Rails.root.basename.join("config/initializers")
RUBY
end
end

context 'when using Rails.application.config.root' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
File.join(Rails.application.config.root, "app", "models")
RUBY

expect_no_offenses(<<~RUBY)
File.join(Rails.application.config.root, "app/models")
RUBY
end
end

context 'when using Rails.root.join with some path strings' do
it 'registers an offense' do
expect_offense(<<~RUBY)
Rails.root.join('app', 'models', 'user.rb')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path/to')`.
RUBY

expect_correction(<<~RUBY)
Rails.root.join("app/models/user.rb")
RUBY
end
end
Expand All @@ -56,6 +81,32 @@
end
end

context 'when using File.join with Rails.root and path starting with `/`' do
it 'registers an offense' do
expect_offense(<<~RUBY)
File.join(Rails.root, '/app/models', '/user.rb')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path/to').to_s`.
RUBY

expect_correction(<<~RUBY)
Rails.root.join("app/models/user.rb").to_s
RUBY
end
end

context 'when using ::Rails.root.join with some path strings' do
it 'registers an offense' do
expect_offense(<<~RUBY)
::Rails.root.join('app', 'models', 'user.rb')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path/to')`.
RUBY

expect_correction(<<~RUBY)
::Rails.root.join("app/models/user.rb")
RUBY
end
end

context 'when using Rails.root.join in string interpolation with nothing after it' do
it 'does not register an offense' do
expect_no_offenses(<<~'RUBY')
Expand Down Expand Up @@ -136,7 +187,9 @@

context 'when using Rails.root.join with slash separated path string' do
it 'does not register an offense' do
expect_no_offenses("Rails.root.join('app/models/goober')")
expect_no_offenses(<<~RUBY)
Rails.root.join('app/models/goober')
RUBY
end
end

Expand Down Expand Up @@ -237,6 +290,32 @@
end
end

context 'when interpolation with `Rails.root` contains other operations' do
it 'does not register an offense for boolean method' do
expect_no_offenses(<<~'RUBY')
"#{Rails.root || '.'}/config"
RUBY
end

it 'does not register an offense for `rescue`' do
expect_no_offenses(<<~'RUBY')
"#{Rails.root rescue '.'}/config"
RUBY
end

it 'does not register an offense for if condition' do
expect_no_offenses(<<~'RUBY')
"#{Rails.root if flag}/app/models"
RUBY
end

it 'does not register an offense for a ternary operator' do
expect_no_offenses(<<~'RUBY')
"#{some_condition ? Rails.root : '/tmp'}/app/models"
RUBY
end
end

context 'with `join` method with implicit receiver' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
Expand Down Expand Up @@ -444,16 +523,92 @@
context 'when EnforcedStyle is `arguments`' do
let(:cop_config) { { 'EnforcedStyle' => 'arguments' } }

context 'when using Rails.root.join with some path strings' do
context 'when using Rails.root.parent' do
it 'registers an offense' do
expect_offense(<<~RUBY)
Rails.root.parent.join("app/models")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path', 'to')`.
RUBY

expect_correction(<<~RUBY)
Rails.root.parent.join("app", "models")
RUBY
end
end

context 'when using Rails.root.dirname' do
it 'registers an offense' do
expect_offense(<<~RUBY)
Rails.root.dirname.join("config/initializers")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path', 'to')`.
RUBY

expect_correction(<<~RUBY)
Rails.root.dirname.join("config", "initializers")
RUBY
end
end

context 'when using Rails.root.basename' do
it 'registers an offense' do
expect_offense(<<~RUBY)
Rails.root.basename.join("config/initializers")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path', 'to')`.
RUBY

expect_correction(<<~RUBY)
Rails.root.basename.join("config", "initializers")
RUBY
end
end

context 'when using Rails.application.config.root' do
it 'does not register an offense' do
expect_no_offenses("Rails.root.join('app', 'models', 'user.rb')")
expect_no_offenses(<<~RUBY)
File.join(Rails.application.config.root, "app", "models")
RUBY

expect_no_offenses(<<~RUBY)
File.join(Rails.application.config.root, "app/models")
RUBY
end
end

context 'when using Rails.root.join with some path strings' do
it 'registers an offense' do
expect_offense(<<~RUBY)
Rails.root.join('app/models/user.rb')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path', 'to')`.
RUBY

expect_correction(<<~RUBY)
Rails.root.join('app', "models", "user.rb")
RUBY
end
end

context 'when using Rails.root.join in string interpolation of argument' do
it 'does not register an offense' do
expect_no_offenses(<<~'RUBY')
'system "rm -rf #{Rails.root.join(\'a\', \'b.png\')}"'
it 'registers an offense' do
expect_offense(<<~'RUBY')
system "rm -rf #{Rails.root.join("a/b.png")}"
^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path', 'to')`.
RUBY

expect_correction(<<~'RUBY')
system "rm -rf #{Rails.root.join("a", "b.png")}"
RUBY
end
end

context 'when using ::Rails.root.join with some path strings' do
it 'registers an offense' do
expect_offense(<<~RUBY)
::Rails.root.join("app/models/user.rb")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path', 'to')`.
RUBY

expect_correction(<<~RUBY)
::Rails.root.join("app", "models", "user.rb")
RUBY
end
end
Expand All @@ -466,6 +621,14 @@
end
end

context 'when string interpolated `Rails.root` is followed by a message starting with `.`' do
it 'does not register an offense' do
expect_no_offenses(<<~'RUBY')
"#{Rails.root}. a message"
RUBY
end
end

context 'when using string interpolation without Rails.root' do
it 'does not register an offense' do
expect_no_offenses(<<~'RUBY')
Expand Down Expand Up @@ -508,6 +671,39 @@
end
end

context 'when using ::File.join with Rails.root' do
it 'registers an offense' do
expect_offense(<<~RUBY)
::File.join(Rails.root, 'app', 'models')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path', 'to').to_s`.
RUBY

expect_correction(<<~RUBY)
Rails.root.join("app", "models").to_s
RUBY
end
end

context 'when using File.join with an array' do
it 'registers an offense' do
expect_offense(<<~RUBY)
File.join([Rails.root, 'foo'])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path', 'to').to_s`.
RUBY

expect_no_corrections
end

it 'registers an offense for nested arrays' do
expect_offense(<<~RUBY)
File.join([Rails.root, 'foo', ['bar']])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path', 'to').to_s`.
RUBY

expect_no_corrections
end
end

context 'when using Rails.root.join with slash separated path string' do
it 'registers an offense' do
expect_offense(<<~RUBY)
Expand All @@ -534,6 +730,19 @@
end
end

context 'when using Rails.root called by double quoted string that ends with string interpolation' do
it 'registers an offense' do
expect_offense(<<~'RUBY')
"#{Rails.root}/a/#{b}"
^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path', 'to')`.
RUBY

expect_correction(<<~'RUBY')
"#{Rails.root.join("a/#{b}")}"
RUBY
end
end

context 'when concat Rails.root and file separator using string interpolation' do
it 'registers an offense' do
expect_offense(<<~'RUBY')
Expand Down Expand Up @@ -622,6 +831,18 @@
"#{Rails.root rescue '.'}/config"
RUBY
end

it 'does not register an offense for if condition' do
expect_no_offenses(<<~'RUBY')
"#{Rails.root if flag}/app/models"
RUBY
end

it 'does not register an offense for a ternary operator' do
expect_no_offenses(<<~'RUBY')
"#{some_condition ? Rails.root : '/tmp'}/app/models"
RUBY
end
end

context 'with `join` method with implicit receiver' do
Expand Down

0 comments on commit b2f71be

Please sign in to comment.