Skip to content

Commit 08e2a8e

Browse files
committed
Add more tests for Rails/FilePath
1 parent f7b0e42 commit 08e2a8e

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)
@@ -566,16 +645,92 @@
566645
context 'when EnforcedStyle is `arguments`' do
567646
let(:cop_config) { { 'EnforcedStyle' => 'arguments' } }
568647

569-
context 'when using Rails.root.join with some path strings' do
648+
context 'when using Rails.root.parent' do
649+
it 'registers an offense' do
650+
expect_offense(<<~RUBY)
651+
Rails.root.parent.join("app/models")
652+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path', 'to')`.
653+
RUBY
654+
655+
expect_correction(<<~RUBY)
656+
Rails.root.parent.join("app", "models")
657+
RUBY
658+
end
659+
end
660+
661+
context 'when using Rails.root.dirname' do
662+
it 'registers an offense' do
663+
expect_offense(<<~RUBY)
664+
Rails.root.dirname.join("config/initializers")
665+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path', 'to')`.
666+
RUBY
667+
668+
expect_correction(<<~RUBY)
669+
Rails.root.dirname.join("config", "initializers")
670+
RUBY
671+
end
672+
end
673+
674+
context 'when using Rails.root.basename' do
675+
it 'registers an offense' do
676+
expect_offense(<<~RUBY)
677+
Rails.root.basename.join("config/initializers")
678+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path', 'to')`.
679+
RUBY
680+
681+
expect_correction(<<~RUBY)
682+
Rails.root.basename.join("config", "initializers")
683+
RUBY
684+
end
685+
end
686+
687+
context 'when using Rails.application.config.root' do
570688
it 'does not register an offense' do
571-
expect_no_offenses("Rails.root.join('app', 'models', 'user.rb')")
689+
expect_no_offenses(<<~RUBY)
690+
File.join(Rails.application.config.root, "app", "models")
691+
RUBY
692+
693+
expect_no_offenses(<<~RUBY)
694+
File.join(Rails.application.config.root, "app/models")
695+
RUBY
696+
end
697+
end
698+
699+
context 'when using Rails.root.join with some path strings' do
700+
it 'registers an offense' do
701+
expect_offense(<<~RUBY)
702+
Rails.root.join('app/models/user.rb')
703+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path', 'to')`.
704+
RUBY
705+
706+
expect_correction(<<~RUBY)
707+
Rails.root.join('app', "models", "user.rb")
708+
RUBY
572709
end
573710
end
574711

575712
context 'when using Rails.root.join in string interpolation of argument' do
576-
it 'does not register an offense' do
577-
expect_no_offenses(<<~'RUBY')
578-
'system "rm -rf #{Rails.root.join(\'a\', \'b.png\')}"'
713+
it 'registers an offense' do
714+
expect_offense(<<~'RUBY')
715+
system "rm -rf #{Rails.root.join("a/b.png")}"
716+
^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path', 'to')`.
717+
RUBY
718+
719+
expect_correction(<<~'RUBY')
720+
system "rm -rf #{Rails.root.join("a", "b.png")}"
721+
RUBY
722+
end
723+
end
724+
725+
context 'when using ::Rails.root.join with some path strings' do
726+
it 'registers an offense' do
727+
expect_offense(<<~RUBY)
728+
::Rails.root.join("app/models/user.rb")
729+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path', 'to')`.
730+
RUBY
731+
732+
expect_correction(<<~RUBY)
733+
::Rails.root.join("app", "models", "user.rb")
579734
RUBY
580735
end
581736
end
@@ -588,6 +743,14 @@
588743
end
589744
end
590745

746+
context 'when string interpolated `Rails.root` is followed by a message starting with `.`' do
747+
it 'does not register an offense' do
748+
expect_no_offenses(<<~'RUBY')
749+
"#{Rails.root}. a message"
750+
RUBY
751+
end
752+
end
753+
591754
context 'when using string interpolation without Rails.root' do
592755
it 'does not register an offense' do
593756
expect_no_offenses(<<~'RUBY')
@@ -630,6 +793,39 @@
630793
end
631794
end
632795

796+
context 'when using ::File.join with Rails.root' do
797+
it 'registers an offense' do
798+
expect_offense(<<~RUBY)
799+
::File.join(Rails.root, 'app', 'models')
800+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path', 'to').to_s`.
801+
RUBY
802+
803+
expect_correction(<<~RUBY)
804+
Rails.root.join("app", "models").to_s
805+
RUBY
806+
end
807+
end
808+
809+
context 'when using File.join with an array' do
810+
it 'registers an offense' do
811+
expect_offense(<<~RUBY)
812+
File.join([Rails.root, 'foo'])
813+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path', 'to').to_s`.
814+
RUBY
815+
816+
expect_no_corrections
817+
end
818+
819+
it 'registers an offense for nested arrays' do
820+
expect_offense(<<~RUBY)
821+
File.join([Rails.root, 'foo', ['bar']])
822+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path', 'to').to_s`.
823+
RUBY
824+
825+
expect_no_corrections
826+
end
827+
end
828+
633829
context 'when using Rails.root.join with slash separated path string' do
634830
it 'registers an offense' do
635831
expect_offense(<<~RUBY)
@@ -656,6 +852,19 @@
656852
end
657853
end
658854

855+
context 'when using Rails.root called by double quoted string that ends with string interpolation' do
856+
it 'registers an offense' do
857+
expect_offense(<<~'RUBY')
858+
"#{Rails.root}/a/#{b}"
859+
^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path', 'to')`.
860+
RUBY
861+
862+
expect_correction(<<~'RUBY')
863+
"#{Rails.root.join("a/#{b}")}"
864+
RUBY
865+
end
866+
end
867+
659868
context 'when concat Rails.root and file separator using string interpolation' do
660869
it 'registers an offense' do
661870
expect_offense(<<~'RUBY')
@@ -744,6 +953,18 @@
744953
"#{Rails.root rescue '.'}/config"
745954
RUBY
746955
end
956+
957+
it 'does not register an offense for if condition' do
958+
expect_no_offenses(<<~'RUBY')
959+
"#{Rails.root if flag}/app/models"
960+
RUBY
961+
end
962+
963+
it 'does not register an offense for a ternary operator' do
964+
expect_no_offenses(<<~'RUBY')
965+
"#{some_condition ? Rails.root : '/tmp'}/app/models"
966+
RUBY
967+
end
747968
end
748969

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

0 commit comments

Comments
 (0)