summaryrefslogtreecommitdiff
path: root/spec/rubocop/cop/inject_enterprise_edition_module_spec.rb
blob: 27df42c0aeed7c88d415ef553c5f46c2abdce521 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# frozen_string_literal: true

require 'spec_helper'
require 'rubocop'
require 'rubocop/rspec/support'
require_relative '../../../rubocop/cop/inject_enterprise_edition_module'

describe RuboCop::Cop::InjectEnterpriseEditionModule do
  include CopHelper

  subject(:cop) { described_class.new }

  it 'flags the use of `prepend_if_ee EE` in the middle of a file' do
    expect_offense(<<~SOURCE)
    class Foo
      prepend_if_ee 'EE::Foo'
      ^^^^^^^^^^^^^^^^^^^^^^^ Injecting EE modules must be done on the last line of this file, outside of any class or module definitions
    end
    SOURCE
  end

  it 'does not flag the use of `prepend_if_ee EEFoo` in the middle of a file' do
    expect_no_offenses(<<~SOURCE)
    class Foo
      prepend_if_ee 'EEFoo'
    end
    SOURCE
  end

  it 'flags the use of `prepend_if_ee EE::Foo::Bar` in the middle of a file' do
    expect_offense(<<~SOURCE)
    class Foo
      prepend_if_ee 'EE::Foo::Bar'
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Injecting EE modules must be done on the last line of this file, outside of any class or module definitions
    end
    SOURCE
  end

  it 'flags the use of `prepend_if_ee(EE::Foo::Bar)` in the middle of a file' do
    expect_offense(<<~SOURCE)
    class Foo
      prepend_if_ee('EE::Foo::Bar')
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Injecting EE modules must be done on the last line of this file, outside of any class or module definitions
    end
    SOURCE
  end

  it 'flags the use of `prepend_if_ee EE::Foo::Bar::Baz` in the middle of a file' do
    expect_offense(<<~SOURCE)
    class Foo
      prepend_if_ee 'EE::Foo::Bar::Baz'
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Injecting EE modules must be done on the last line of this file, outside of any class or module definitions
    end
    SOURCE
  end

  it 'flags the use of `prepend_if_ee ::EE` in the middle of a file' do
    expect_offense(<<~SOURCE)
    class Foo
      prepend_if_ee '::EE::Foo'
      ^^^^^^^^^^^^^^^^^^^^^^^^^ Injecting EE modules must be done on the last line of this file, outside of any class or module definitions
    end
    SOURCE
  end

  it 'flags the use of `include_if_ee EE` in the middle of a file' do
    expect_offense(<<~SOURCE)
    class Foo
      include_if_ee 'EE::Foo'
      ^^^^^^^^^^^^^^^^^^^^^^^ Injecting EE modules must be done on the last line of this file, outside of any class or module definitions
    end
    SOURCE
  end

  it 'flags the use of `include_if_ee ::EE` in the middle of a file' do
    expect_offense(<<~SOURCE)
    class Foo
      include_if_ee '::EE::Foo'
      ^^^^^^^^^^^^^^^^^^^^^^^^^ Injecting EE modules must be done on the last line of this file, outside of any class or module definitions
    end
    SOURCE
  end

  it 'flags the use of `extend_if_ee EE` in the middle of a file' do
    expect_offense(<<~SOURCE)
    class Foo
      extend_if_ee 'EE::Foo'
      ^^^^^^^^^^^^^^^^^^^^^^ Injecting EE modules must be done on the last line of this file, outside of any class or module definitions
    end
    SOURCE
  end

  it 'flags the use of `extend_if_ee ::EE` in the middle of a file' do
    expect_offense(<<~SOURCE)
    class Foo
      extend_if_ee '::EE::Foo'
      ^^^^^^^^^^^^^^^^^^^^^^^^ Injecting EE modules must be done on the last line of this file, outside of any class or module definitions
    end
    SOURCE
  end

  it 'does not flag prepending of regular modules' do
    expect_no_offenses(<<~SOURCE)
    class Foo
      prepend_if_ee 'Foo'
    end
    SOURCE
  end

  it 'does not flag including of regular modules' do
    expect_no_offenses(<<~SOURCE)
    class Foo
      include_if_ee 'Foo'
    end
    SOURCE
  end

  it 'does not flag extending using regular modules' do
    expect_no_offenses(<<~SOURCE)
    class Foo
      extend_if_ee 'Foo'
    end
    SOURCE
  end

  it 'does not flag the use of `prepend_if_ee EE` on the last line' do
    expect_no_offenses(<<~SOURCE)
    class Foo
    end

    Foo.prepend_if_ee('EE::Foo')
    SOURCE
  end

  it 'does not flag the use of `include_if_ee EE` on the last line' do
    expect_no_offenses(<<~SOURCE)
    class Foo
    end

    Foo.include_if_ee('EE::Foo')
    SOURCE
  end

  it 'does not flag the use of `extend_if_ee EE` on the last line' do
    expect_no_offenses(<<~SOURCE)
    class Foo
    end

    Foo.extend_if_ee('EE::Foo')
    SOURCE
  end

  it 'autocorrects offenses by just disabling the Cop' do
    source = <<~SOURCE
    class Foo
      prepend_if_ee 'EE::Foo'
      include_if_ee 'Bar'
    end
    SOURCE

    expect(autocorrect_source(source)).to eq(<<~SOURCE)
    class Foo
      prepend_if_ee 'EE::Foo' # rubocop: disable Cop/InjectEnterpriseEditionModule
      include_if_ee 'Bar'
    end
    SOURCE
  end

  it 'disallows the use of prepend to inject an EE module' do
    expect_offense(<<~SOURCE)
    class Foo
    end

    Foo.prepend(EE::Foo)
    ^^^^^^^^^^^^^^^^^^^^ EE modules must be injected using `include_if_ee`, `extend_if_ee`, or `prepend_if_ee`
    SOURCE
  end

  it 'disallows the use of extend to inject an EE module' do
    expect_offense(<<~SOURCE)
    class Foo
    end

    Foo.extend(EE::Foo)
    ^^^^^^^^^^^^^^^^^^^ EE modules must be injected using `include_if_ee`, `extend_if_ee`, or `prepend_if_ee`
    SOURCE
  end

  it 'disallows the use of include to inject an EE module' do
    expect_offense(<<~SOURCE)
    class Foo
    end

    Foo.include(EE::Foo)
    ^^^^^^^^^^^^^^^^^^^^ EE modules must be injected using `include_if_ee`, `extend_if_ee`, or `prepend_if_ee`
    SOURCE
  end

  it 'disallows the use of prepend_if_ee without a String' do
    expect_offense(<<~SOURCE)
    class Foo
    end

    Foo.prepend_if_ee(EE::Foo)
                      ^^^^^^^ EE modules to inject must be specified as a String
    SOURCE
  end

  it 'disallows the use of include_if_ee without a String' do
    expect_offense(<<~SOURCE)
    class Foo
    end

    Foo.include_if_ee(EE::Foo)
                      ^^^^^^^ EE modules to inject must be specified as a String
    SOURCE
  end

  it 'disallows the use of extend_if_ee without a String' do
    expect_offense(<<~SOURCE)
    class Foo
    end

    Foo.extend_if_ee(EE::Foo)
                     ^^^^^^^ EE modules to inject must be specified as a String
    SOURCE
  end
end