Skip to content

Commit b2f71be

Browse files
committed
Add more tests for Rails/FilePath
1 parent f028c15 commit b2f71be

File tree

1 file changed

+239
-18
lines changed

1 file changed

+239
-18
lines changed

spec/rubocop/cop/rails/file_path_spec.rb

Lines changed: 239 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,41 +4,66 @@
44
context 'when EnforcedStyle is `slashes`' do
55
let(:cop_config) { { 'EnforcedStyle' => 'slashes' } }
66

7-
context 'when using Rails.root.join with some path strings' do
7+
context 'when using Rails.root.parent' do
88
it 'registers an offense' do
99
expect_offense(<<~RUBY)
10-
Rails.root.join('app', 'models', 'user.rb')
11-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path/to')`.
10+
Rails.root.parent.join("app", "models")
11+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path/to')`.
1212
RUBY
1313

1414
expect_correction(<<~RUBY)
15-
Rails.root.join("app/models/user.rb")
15+
Rails.root.parent.join("app/models")
1616
RUBY
1717
end
1818
end
1919

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

2727
expect_correction(<<~RUBY)
28-
Rails.root.join("app/models/user.rb").to_s
28+
Rails.root.dirname.join("config/initializers")
2929
RUBY
3030
end
3131
end
3232

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

4040
expect_correction(<<~RUBY)
41-
::Rails.root.join("app/models/user.rb")
41+
Rails.root.basename.join("config/initializers")
42+
RUBY
43+
end
44+
end
45+
46+
context 'when using Rails.application.config.root' do
47+
it 'does not register an offense' do
48+
expect_no_offenses(<<~RUBY)
49+
File.join(Rails.application.config.root, "app", "models")
50+
RUBY
51+
52+
expect_no_offenses(<<~RUBY)
53+
File.join(Rails.application.config.root, "app/models")
54+
RUBY
55+
end
56+
end
57+
58+
context 'when using Rails.root.join with some path strings' do
59+
it 'registers an offense' do
60+
expect_offense(<<~RUBY)
61+
Rails.root.join('app', 'models', 'user.rb')
62+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path/to')`.
63+
RUBY
64+
65+
expect_correction(<<~RUBY)
66+
Rails.root.join("app/models/user.rb")
4267
RUBY
4368
end
4469
end
@@ -56,6 +81,32 @@
5681
end
5782
end
5883

84+
context 'when using File.join with Rails.root and path starting with `/`' do
85+
it 'registers an offense' do
86+
expect_offense(<<~RUBY)
87+
File.join(Rails.root, '/app/models', '/user.rb')
88+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path/to').to_s`.
89+
RUBY
90+
91+
expect_correction(<<~RUBY)
92+
Rails.root.join("app/models/user.rb").to_s
93+
RUBY
94+
end
95+
end
96+
97+
context 'when using ::Rails.root.join with some path strings' do
98+
it 'registers an offense' do
99+
expect_offense(<<~RUBY)
100+
::Rails.root.join('app', 'models', 'user.rb')
101+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path/to')`.
102+
RUBY
103+
104+
expect_correction(<<~RUBY)
105+
::Rails.root.join("app/models/user.rb")
106+
RUBY
107+
end
108+
end
109+
59110
context 'when using Rails.root.join in string interpolation with nothing after it' do
60111
it 'does not register an offense' do
61112
expect_no_offenses(<<~'RUBY')
@@ -136,7 +187,9 @@
136187

137188
context 'when using Rails.root.join with slash separated path string' do
138189
it 'does not register an offense' do
139-
expect_no_offenses("Rails.root.join('app/models/goober')")
190+
expect_no_offenses(<<~RUBY)
191+
Rails.root.join('app/models/goober')
192+
RUBY
140193
end
141194
end
142195

@@ -237,6 +290,32 @@
237290
end
238291
end
239292

293+
context 'when interpolation with `Rails.root` contains other operations' do
294+
it 'does not register an offense for boolean method' do
295+
expect_no_offenses(<<~'RUBY')
296+
"#{Rails.root || '.'}/config"
297+
RUBY
298+
end
299+
300+
it 'does not register an offense for `rescue`' do
301+
expect_no_offenses(<<~'RUBY')
302+
"#{Rails.root rescue '.'}/config"
303+
RUBY
304+
end
305+
306+
it 'does not register an offense for if condition' do
307+
expect_no_offenses(<<~'RUBY')
308+
"#{Rails.root if flag}/app/models"
309+
RUBY
310+
end
311+
312+
it 'does not register an offense for a ternary operator' do
313+
expect_no_offenses(<<~'RUBY')
314+
"#{some_condition ? Rails.root : '/tmp'}/app/models"
315+
RUBY
316+
end
317+
end
318+
240319
context 'with `join` method with implicit receiver' do
241320
it 'does not register an offense' do
242321
expect_no_offenses(<<~RUBY)
@@ -444,16 +523,92 @@
444523
context 'when EnforcedStyle is `arguments`' do
445524
let(:cop_config) { { 'EnforcedStyle' => 'arguments' } }
446525

447-
context 'when using Rails.root.join with some path strings' do
526+
context 'when using Rails.root.parent' do
527+
it 'registers an offense' do
528+
expect_offense(<<~RUBY)
529+
Rails.root.parent.join("app/models")
530+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path', 'to')`.
531+
RUBY
532+
533+
expect_correction(<<~RUBY)
534+
Rails.root.parent.join("app", "models")
535+
RUBY
536+
end
537+
end
538+
539+
context 'when using Rails.root.dirname' do
540+
it 'registers an offense' do
541+
expect_offense(<<~RUBY)
542+
Rails.root.dirname.join("config/initializers")
543+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path', 'to')`.
544+
RUBY
545+
546+
expect_correction(<<~RUBY)
547+
Rails.root.dirname.join("config", "initializers")
548+
RUBY
549+
end
550+
end
551+
552+
context 'when using Rails.root.basename' do
553+
it 'registers an offense' do
554+
expect_offense(<<~RUBY)
555+
Rails.root.basename.join("config/initializers")
556+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path', 'to')`.
557+
RUBY
558+
559+
expect_correction(<<~RUBY)
560+
Rails.root.basename.join("config", "initializers")
561+
RUBY
562+
end
563+
end
564+
565+
context 'when using Rails.application.config.root' do
448566
it 'does not register an offense' do
449-
expect_no_offenses("Rails.root.join('app', 'models', 'user.rb')")
567+
expect_no_offenses(<<~RUBY)
568+
File.join(Rails.application.config.root, "app", "models")
569+
RUBY
570+
571+
expect_no_offenses(<<~RUBY)
572+
File.join(Rails.application.config.root, "app/models")
573+
RUBY
574+
end
575+
end
576+
577+
context 'when using Rails.root.join with some path strings' do
578+
it 'registers an offense' do
579+
expect_offense(<<~RUBY)
580+
Rails.root.join('app/models/user.rb')
581+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path', 'to')`.
582+
RUBY
583+
584+
expect_correction(<<~RUBY)
585+
Rails.root.join('app', "models", "user.rb")
586+
RUBY
450587
end
451588
end
452589

453590
context 'when using Rails.root.join in string interpolation of argument' do
454-
it 'does not register an offense' do
455-
expect_no_offenses(<<~'RUBY')
456-
'system "rm -rf #{Rails.root.join(\'a\', \'b.png\')}"'
591+
it 'registers an offense' do
592+
expect_offense(<<~'RUBY')
593+
system "rm -rf #{Rails.root.join("a/b.png")}"
594+
^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path', 'to')`.
595+
RUBY
596+
597+
expect_correction(<<~'RUBY')
598+
system "rm -rf #{Rails.root.join("a", "b.png")}"
599+
RUBY
600+
end
601+
end
602+
603+
context 'when using ::Rails.root.join with some path strings' do
604+
it 'registers an offense' do
605+
expect_offense(<<~RUBY)
606+
::Rails.root.join("app/models/user.rb")
607+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path', 'to')`.
608+
RUBY
609+
610+
expect_correction(<<~RUBY)
611+
::Rails.root.join("app", "models", "user.rb")
457612
RUBY
458613
end
459614
end
@@ -466,6 +621,14 @@
466621
end
467622
end
468623

624+
context 'when string interpolated `Rails.root` is followed by a message starting with `.`' do
625+
it 'does not register an offense' do
626+
expect_no_offenses(<<~'RUBY')
627+
"#{Rails.root}. a message"
628+
RUBY
629+
end
630+
end
631+
469632
context 'when using string interpolation without Rails.root' do
470633
it 'does not register an offense' do
471634
expect_no_offenses(<<~'RUBY')
@@ -508,6 +671,39 @@
508671
end
509672
end
510673

674+
context 'when using ::File.join with Rails.root' do
675+
it 'registers an offense' do
676+
expect_offense(<<~RUBY)
677+
::File.join(Rails.root, 'app', 'models')
678+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path', 'to').to_s`.
679+
RUBY
680+
681+
expect_correction(<<~RUBY)
682+
Rails.root.join("app", "models").to_s
683+
RUBY
684+
end
685+
end
686+
687+
context 'when using File.join with an array' do
688+
it 'registers an offense' do
689+
expect_offense(<<~RUBY)
690+
File.join([Rails.root, 'foo'])
691+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path', 'to').to_s`.
692+
RUBY
693+
694+
expect_no_corrections
695+
end
696+
697+
it 'registers an offense for nested arrays' do
698+
expect_offense(<<~RUBY)
699+
File.join([Rails.root, 'foo', ['bar']])
700+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path', 'to').to_s`.
701+
RUBY
702+
703+
expect_no_corrections
704+
end
705+
end
706+
511707
context 'when using Rails.root.join with slash separated path string' do
512708
it 'registers an offense' do
513709
expect_offense(<<~RUBY)
@@ -534,6 +730,19 @@
534730
end
535731
end
536732

733+
context 'when using Rails.root called by double quoted string that ends with string interpolation' do
734+
it 'registers an offense' do
735+
expect_offense(<<~'RUBY')
736+
"#{Rails.root}/a/#{b}"
737+
^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path', 'to')`.
738+
RUBY
739+
740+
expect_correction(<<~'RUBY')
741+
"#{Rails.root.join("a/#{b}")}"
742+
RUBY
743+
end
744+
end
745+
537746
context 'when concat Rails.root and file separator using string interpolation' do
538747
it 'registers an offense' do
539748
expect_offense(<<~'RUBY')
@@ -622,6 +831,18 @@
622831
"#{Rails.root rescue '.'}/config"
623832
RUBY
624833
end
834+
835+
it 'does not register an offense for if condition' do
836+
expect_no_offenses(<<~'RUBY')
837+
"#{Rails.root if flag}/app/models"
838+
RUBY
839+
end
840+
841+
it 'does not register an offense for a ternary operator' do
842+
expect_no_offenses(<<~'RUBY')
843+
"#{some_condition ? Rails.root : '/tmp'}/app/models"
844+
RUBY
845+
end
625846
end
626847

627848
context 'with `join` method with implicit receiver' do

0 commit comments

Comments
 (0)