|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +RSpec.describe(RuboCop::Cop::Rails::ActiveSupportOnLoad, :config) do |
| 4 | + it 'adds offense when trying to extend a framework class with include' do |
| 5 | + expect_offense(<<~RUBY) |
| 6 | + ActiveRecord::Base.include(MyClass) |
| 7 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `ActiveSupport.on_load(:active_record) { include MyClass }` instead of `ActiveRecord::Base.include(MyClass)`. |
| 8 | + RUBY |
| 9 | + end |
| 10 | + |
| 11 | + it 'adds offense when trying to extend a framework class with prepend' do |
| 12 | + expect_offense(<<~RUBY) |
| 13 | + ActiveRecord::Base.prepend(MyClass) |
| 14 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `ActiveSupport.on_load(:active_record) { prepend MyClass }` instead of `ActiveRecord::Base.prepend(MyClass)`. |
| 15 | + RUBY |
| 16 | + end |
| 17 | + |
| 18 | + it 'adds offense when trying to extend a framework class with extend' do |
| 19 | + expect_offense(<<~RUBY) |
| 20 | + ActiveRecord::Base.extend(MyClass) |
| 21 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `ActiveSupport.on_load(:active_record) { extend MyClass }` instead of `ActiveRecord::Base.extend(MyClass)`. |
| 22 | + RUBY |
| 23 | + end |
| 24 | + |
| 25 | + it 'adds offense when trying to extend a framework class with absolute name' do |
| 26 | + expect_offense(<<~RUBY) |
| 27 | + ::ActiveRecord::Base.extend(MyClass) |
| 28 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `ActiveSupport.on_load(:active_record) { extend MyClass }` instead of `::ActiveRecord::Base.extend(MyClass)`. |
| 29 | + RUBY |
| 30 | + end |
| 31 | + |
| 32 | + it 'adds offense when trying to extend a framework class with a variable' do |
| 33 | + expect_offense(<<~RUBY) |
| 34 | + ActiveRecord::Base.extend(my_class) |
| 35 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `ActiveSupport.on_load(:active_record) { extend my_class }` instead of `ActiveRecord::Base.extend(my_class)`. |
| 36 | + RUBY |
| 37 | + end |
| 38 | + |
| 39 | + it 'does not add offense when extending a variable' do |
| 40 | + expect_no_offenses(<<~RUBY) |
| 41 | + foo.extend(MyClass) |
| 42 | + RUBY |
| 43 | + end |
| 44 | + |
| 45 | + it 'does not add offense when extending the framework using on_load and include' do |
| 46 | + expect_no_offenses(<<~RUBY) |
| 47 | + ActiveSupport.on_load(:active_record) { include MyClass } |
| 48 | + RUBY |
| 49 | + end |
| 50 | + |
| 51 | + it 'does not add offense when extending the framework using on_load and include in a multi-line block' do |
| 52 | + expect_no_offenses(<<~RUBY) |
| 53 | + ActiveSupport.on_load(:active_record) do |
| 54 | + include MyClass |
| 55 | + end |
| 56 | + RUBY |
| 57 | + end |
| 58 | + |
| 59 | + it 'does not add offense when not including a class' do |
| 60 | + expect_no_offenses(<<~RUBY) |
| 61 | + ActiveRecord::Base.include_root_in_json = false |
| 62 | + RUBY |
| 63 | + end |
| 64 | + |
| 65 | + it 'does not add offense when using include?' do |
| 66 | + expect_no_offenses(<<~RUBY) |
| 67 | + name.include?('bob') |
| 68 | + RUBY |
| 69 | + end |
| 70 | + |
| 71 | + context 'autocorrect' do |
| 72 | + it 'autocorrects extension on supported classes' do |
| 73 | + source = <<~RUBY |
| 74 | + ActiveRecord::Base.prepend(MyClass) |
| 75 | + RUBY |
| 76 | + |
| 77 | + corrected_source = <<~RUBY |
| 78 | + ActiveSupport.on_load(:active_record) { prepend MyClass } |
| 79 | + RUBY |
| 80 | + |
| 81 | + corrected = autocorrect_source(source) |
| 82 | + |
| 83 | + expect(corrected).to(eq(corrected_source)) |
| 84 | + end |
| 85 | + |
| 86 | + it 'does not autocorrect extension on unsupported classes' do |
| 87 | + source = <<~RUBY |
| 88 | + MyClass1.prepend(MyClass) |
| 89 | + RUBY |
| 90 | + |
| 91 | + corrected = autocorrect_source(source) |
| 92 | + |
| 93 | + expect(corrected).to(eq(source)) |
| 94 | + |
| 95 | + source = <<~RUBY |
| 96 | + MyClass1.include(MyClass) |
| 97 | + RUBY |
| 98 | + |
| 99 | + corrected = autocorrect_source(source) |
| 100 | + |
| 101 | + expect(corrected).to(eq(source)) |
| 102 | + end |
| 103 | + |
| 104 | + it 'does not autocorrect when there is no extension on the supported classes' do |
| 105 | + source = <<~RUBY |
| 106 | + ActiveRecord::Base.include_root_in_json = false |
| 107 | + RUBY |
| 108 | + |
| 109 | + corrected = autocorrect_source(source) |
| 110 | + |
| 111 | + expect(corrected).to(eq(source)) |
| 112 | + end |
| 113 | + end |
| 114 | +end |
0 commit comments