|
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)
|
|
444 | 523 | context 'when EnforcedStyle is `arguments`' do
|
445 | 524 | let(:cop_config) { { 'EnforcedStyle' => 'arguments' } }
|
446 | 525 |
|
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 |
448 | 566 | 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 |
450 | 587 | end
|
451 | 588 | end
|
452 | 589 |
|
453 | 590 | 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") |
457 | 612 | RUBY
|
458 | 613 | end
|
459 | 614 | end
|
|
466 | 621 | end
|
467 | 622 | end
|
468 | 623 |
|
| 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 | + |
469 | 632 | context 'when using string interpolation without Rails.root' do
|
470 | 633 | it 'does not register an offense' do
|
471 | 634 | expect_no_offenses(<<~'RUBY')
|
|
508 | 671 | end
|
509 | 672 | end
|
510 | 673 |
|
| 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 | + |
511 | 707 | context 'when using Rails.root.join with slash separated path string' do
|
512 | 708 | it 'registers an offense' do
|
513 | 709 | expect_offense(<<~RUBY)
|
|
534 | 730 | end
|
535 | 731 | end
|
536 | 732 |
|
| 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 | + |
537 | 746 | context 'when concat Rails.root and file separator using string interpolation' do
|
538 | 747 | it 'registers an offense' do
|
539 | 748 | expect_offense(<<~'RUBY')
|
|
622 | 831 | "#{Rails.root rescue '.'}/config"
|
623 | 832 | RUBY
|
624 | 833 | 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 |
625 | 846 | end
|
626 | 847 |
|
627 | 848 | context 'with `join` method with implicit receiver' do
|
|
0 commit comments