|
4 | 4 | context 'when EnforcedStyle is `slashes`' do
|
5 | 5 | let(:cop_config) { { 'EnforcedStyle' => 'slashes' } }
|
6 | 6 |
|
7 |
| - context 'when using Rails.root.join with some path strings' do |
| 7 | + context 'when using Rails.root.parent' do |
8 | 8 | it 'registers an offense' do
|
9 | 9 | 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')`. |
12 | 12 | RUBY
|
13 | 13 |
|
14 | 14 | expect_correction(<<~RUBY)
|
15 |
| - Rails.root.join("app/models/user.rb") |
| 15 | + Rails.root.parent.join("app/models") |
16 | 16 | RUBY
|
17 | 17 | end
|
18 | 18 | end
|
19 | 19 |
|
20 |
| - context 'when using File.join with Rails.root and path starting with `/`' do |
| 20 | + context 'when using Rails.root.dirname' do |
21 | 21 | it 'registers an offense' do
|
22 | 22 | 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')`. |
25 | 25 | RUBY
|
26 | 26 |
|
27 | 27 | expect_correction(<<~RUBY)
|
28 |
| - Rails.root.join("app/models/user.rb").to_s |
| 28 | + Rails.root.dirname.join("config/initializers") |
29 | 29 | RUBY
|
30 | 30 | end
|
31 | 31 | end
|
32 | 32 |
|
33 |
| - context 'when using ::Rails.root.join with some path strings' do |
| 33 | + context 'when using Rails.root.basename' do |
34 | 34 | it 'registers an offense' do
|
35 | 35 | 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')`. |
38 | 38 | RUBY
|
39 | 39 |
|
40 | 40 | 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") |
42 | 67 | RUBY
|
43 | 68 | end
|
44 | 69 | end
|
|
56 | 81 | end
|
57 | 82 | end
|
58 | 83 |
|
| 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 | + |
59 | 110 | context 'when using Rails.root.join in string interpolation with nothing after it' do
|
60 | 111 | it 'does not register an offense' do
|
61 | 112 | expect_no_offenses(<<~'RUBY')
|
|
136 | 187 |
|
137 | 188 | context 'when using Rails.root.join with slash separated path string' do
|
138 | 189 | 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 |
140 | 193 | end
|
141 | 194 | end
|
142 | 195 |
|
|
237 | 290 | end
|
238 | 291 | end
|
239 | 292 |
|
| 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 | + |
240 | 319 | context 'with `join` method with implicit receiver' do
|
241 | 320 | it 'does not register an offense' do
|
242 | 321 | expect_no_offenses(<<~RUBY)
|
|
566 | 645 | context 'when EnforcedStyle is `arguments`' do
|
567 | 646 | let(:cop_config) { { 'EnforcedStyle' => 'arguments' } }
|
568 | 647 |
|
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 |
570 | 688 | 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 |
572 | 709 | end
|
573 | 710 | end
|
574 | 711 |
|
575 | 712 | 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") |
579 | 734 | RUBY
|
580 | 735 | end
|
581 | 736 | end
|
|
588 | 743 | end
|
589 | 744 | end
|
590 | 745 |
|
| 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 | + |
591 | 754 | context 'when using string interpolation without Rails.root' do
|
592 | 755 | it 'does not register an offense' do
|
593 | 756 | expect_no_offenses(<<~'RUBY')
|
|
630 | 793 | end
|
631 | 794 | end
|
632 | 795 |
|
| 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 | + |
633 | 829 | context 'when using Rails.root.join with slash separated path string' do
|
634 | 830 | it 'registers an offense' do
|
635 | 831 | expect_offense(<<~RUBY)
|
|
656 | 852 | end
|
657 | 853 | end
|
658 | 854 |
|
| 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 | + |
659 | 868 | context 'when concat Rails.root and file separator using string interpolation' do
|
660 | 869 | it 'registers an offense' do
|
661 | 870 | expect_offense(<<~'RUBY')
|
|
744 | 953 | "#{Rails.root rescue '.'}/config"
|
745 | 954 | RUBY
|
746 | 955 | 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 |
747 | 968 | end
|
748 | 969 |
|
749 | 970 | context 'with `join` method with implicit receiver' do
|
|
0 commit comments