|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +RSpec.describe RuboCop::Cop::Rails::EnvLocal, :config do |
| 4 | + shared_examples 'non-local candidates' do |
| 5 | + it 'registers no offenses for non-local `Rails.env._? || Rails.env._?`' do |
| 6 | + expect_no_offenses(<<~RUBY) |
| 7 | + Rails.env.development? || Rails.env.production? |
| 8 | + Rails.env.test? || Rails.env.production? |
| 9 | + Rails.env.production? || Rails.env.other? |
| 10 | + RUBY |
| 11 | + end |
| 12 | + |
| 13 | + it 'registers no offenses for single `Rails.env._?`' do |
| 14 | + expect_no_offenses(<<~RUBY) |
| 15 | + Rails.env.development? |
| 16 | + Rails.env.test? |
| 17 | + Rails.env.production? |
| 18 | + Rails.env.other? |
| 19 | + RUBY |
| 20 | + end |
| 21 | + end |
| 22 | + |
| 23 | + context 'In Rails >= 7.1', :rails71 do |
| 24 | + it 'registers an offense for `Rails.env.development? || Rails.env.test?`' do |
| 25 | + expect_offense(<<~RUBY) |
| 26 | + Rails.env.development? || Rails.env.test? |
| 27 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `Rails.env.local?` instead. |
| 28 | + Rails.env.test? || Rails.env.development? |
| 29 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `Rails.env.local?` instead. |
| 30 | + RUBY |
| 31 | + |
| 32 | + expect_correction(<<~RUBY) |
| 33 | + Rails.env.local? |
| 34 | + Rails.env.local? |
| 35 | + RUBY |
| 36 | + end |
| 37 | + |
| 38 | + it 'registers no offenses for `Rails.env.local?`' do |
| 39 | + expect_no_offenses(<<~RUBY) |
| 40 | + Rails.env.local? |
| 41 | + RUBY |
| 42 | + end |
| 43 | + |
| 44 | + include_examples 'non-local candidates' |
| 45 | + end |
| 46 | + |
| 47 | + context 'In Rails < 7.1', :rails70 do |
| 48 | + it 'registers no offenses for `Rails.env.development? || Rails.env.test?`' do |
| 49 | + expect_no_offenses(<<~RUBY) |
| 50 | + Rails.env.development? || Rails.env.test? |
| 51 | + Rails.env.test? || Rails.env.development? |
| 52 | + RUBY |
| 53 | + end |
| 54 | + |
| 55 | + it 'registers no offenses for `Rails.env.local?`' do |
| 56 | + expect_no_offenses(<<~RUBY) |
| 57 | + Rails.env.local? |
| 58 | + RUBY |
| 59 | + end |
| 60 | + |
| 61 | + include_examples 'non-local candidates' |
| 62 | + end |
| 63 | +end |
0 commit comments