summaryrefslogtreecommitdiff
path: root/spec/rubocop/code_reuse_helpers_spec.rb
blob: 3220cff16813ff4a6167f8e0986dcb62ce4bf8b8 (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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
# frozen_string_literal: true

require 'fast_spec_helper'
require 'parser/current'
require_relative '../../rubocop/code_reuse_helpers'

RSpec.describe RuboCop::CodeReuseHelpers do
  def build_and_parse_source(source, path = 'foo.rb')
    buffer = Parser::Source::Buffer.new(path)
    buffer.source = source

    builder = RuboCop::AST::Builder.new
    parser = Parser::CurrentRuby.new(builder)

    parser.parse(buffer)
  end

  let(:cop) do
    Class.new do
      include RuboCop::CodeReuseHelpers
    end.new
  end

  let(:ee_file_path) { File.expand_path('../../ee/app/models/license.rb', __dir__) }

  describe '#send_to_constant?' do
    it 'returns true when sending to a constant' do
      node = build_and_parse_source('Foo.bar')

      expect(cop.send_to_constant?(node)).to eq(true)
    end

    it 'returns false when sending to something other than a constant' do
      node = build_and_parse_source('10')

      expect(cop.send_to_constant?(node)).to eq(false)
    end
  end

  describe '#send_receiver_name_ends_with?' do
    it 'returns true when the receiver ends with a suffix' do
      node = build_and_parse_source('FooFinder.new')

      expect(cop.send_receiver_name_ends_with?(node, 'Finder')).to eq(true)
    end

    it 'returns false when the receiver is the same as a suffix' do
      node = build_and_parse_source('Finder.new')

      expect(cop.send_receiver_name_ends_with?(node, 'Finder')).to eq(false)
    end
  end

  describe '#file_path_for_node' do
    it 'returns the file path of a node' do
      node = build_and_parse_source('10')
      path = cop.file_path_for_node(node)

      expect(path).to eq('foo.rb')
    end
  end

  describe '#name_of_constant' do
    it 'returns the name of a constant' do
      node = build_and_parse_source('Foo')

      expect(cop.name_of_constant(node)).to eq(:Foo)
    end
  end

  describe '#in_finder?' do
    it 'returns true for a node in the finders directory' do
      node = build_and_parse_source('10', rails_root_join('app', 'finders', 'foo.rb'))

      expect(cop.in_finder?(node)).to eq(true)
    end

    it 'returns false for a node outside the finders directory' do
      node = build_and_parse_source('10', rails_root_join('app', 'foo', 'foo.rb'))

      expect(cop.in_finder?(node)).to eq(false)
    end
  end

  describe '#in_model?' do
    it 'returns true for a node in the models directory' do
      node = build_and_parse_source('10', rails_root_join('app', 'models', 'foo.rb'))

      expect(cop.in_model?(node)).to eq(true)
    end

    it 'returns false for a node outside the models directory' do
      node = build_and_parse_source('10', rails_root_join('app', 'foo', 'foo.rb'))

      expect(cop.in_model?(node)).to eq(false)
    end
  end

  describe '#in_service_class?' do
    it 'returns true for a node in the services directory' do
      node = build_and_parse_source('10', rails_root_join('app', 'services', 'foo.rb'))

      expect(cop.in_service_class?(node)).to eq(true)
    end

    it 'returns false for a node outside the services directory' do
      node = build_and_parse_source('10', rails_root_join('app', 'foo', 'foo.rb'))

      expect(cop.in_service_class?(node)).to eq(false)
    end
  end

  describe '#in_presenter?' do
    it 'returns true for a node in the presenters directory' do
      node = build_and_parse_source('10', rails_root_join('app', 'presenters', 'foo.rb'))

      expect(cop.in_presenter?(node)).to eq(true)
    end

    it 'returns false for a node outside the presenters directory' do
      node = build_and_parse_source('10', rails_root_join('app', 'foo', 'foo.rb'))

      expect(cop.in_presenter?(node)).to eq(false)
    end
  end

  describe '#in_serializer?' do
    it 'returns true for a node in the serializers directory' do
      node = build_and_parse_source('10', rails_root_join('app', 'serializers', 'foo.rb'))

      expect(cop.in_serializer?(node)).to eq(true)
    end

    it 'returns false for a node outside the serializers directory' do
      node = build_and_parse_source('10', rails_root_join('app', 'foo', 'foo.rb'))

      expect(cop.in_serializer?(node)).to eq(false)
    end
  end

  describe '#in_worker?' do
    it 'returns true for a node in the workers directory' do
      node = build_and_parse_source('10', rails_root_join('app', 'workers', 'foo.rb'))

      expect(cop.in_worker?(node)).to eq(true)
    end

    it 'returns false for a node outside the workers directory' do
      node = build_and_parse_source('10', rails_root_join('app', 'foo', 'foo.rb'))

      expect(cop.in_worker?(node)).to eq(false)
    end
  end

  describe '#in_graphql_types?' do
    %w[
      app/graphql/types
      ee/app/graphql/ee/types
      ee/app/graphql/types
    ].each do |path|
      it "returns true for a node in #{path}" do
        node = build_and_parse_source('10', rails_root_join(path, 'foo.rb'))

        expect(cop.in_graphql_types?(node)).to eq(true)
      end
    end

    %w[
      app/graphql/resolvers
      app/foo
    ].each do |path|
      it "returns true for a node in #{path}" do
        node = build_and_parse_source('10', rails_root_join(path, 'foo.rb'))

        expect(cop.in_graphql_types?(node)).to eq(false)
      end
    end
  end

  describe '#in_api?' do
    it 'returns true for a node in the API directory' do
      node = build_and_parse_source('10', rails_root_join('lib', 'api', 'foo.rb'))

      expect(cop.in_api?(node)).to eq(true)
    end

    it 'returns false for a node outside the API directory' do
      node = build_and_parse_source('10', rails_root_join('lib', 'foo', 'foo.rb'))

      expect(cop.in_api?(node)).to eq(false)
    end
  end

  describe '#in_spec?' do
    it 'returns true for a node in the spec directory' do
      node = build_and_parse_source('10', rails_root_join('spec', 'foo.rb'))

      expect(cop.in_spec?(node)).to eq(true)
    end

    it 'returns true for a node in the ee/spec directory' do
      node = build_and_parse_source('10', rails_root_join('ee', 'spec', 'foo.rb'))

      expect(cop.in_spec?(node)).to eq(true)
    end

    it 'returns false for a node outside the spec directory' do
      node = build_and_parse_source('10', rails_root_join('lib', 'foo.rb'))

      expect(cop.in_spec?(node)).to eq(false)
    end
  end

  describe '#in_app_directory?' do
    it 'returns true for a directory in the CE app/ directory' do
      node = build_and_parse_source('10', rails_root_join('app', 'models', 'foo.rb'))

      expect(cop.in_app_directory?(node, 'models')).to eq(true)
    end

    it 'returns true for a directory in the EE app/ directory' do
      node =
        build_and_parse_source('10', rails_root_join('ee', 'app', 'models', 'foo.rb'))

      expect(cop.in_app_directory?(node, 'models')).to eq(true)
    end

    it 'returns false for a directory in the lib/ directory' do
      node =
        build_and_parse_source('10', rails_root_join('lib', 'models', 'foo.rb'))

      expect(cop.in_app_directory?(node, 'models')).to eq(false)
    end
  end

  describe '#in_lib_directory?' do
    it 'returns true for a directory in the CE lib/ directory' do
      node = build_and_parse_source('10', rails_root_join('lib', 'models', 'foo.rb'))

      expect(cop.in_lib_directory?(node, 'models')).to eq(true)
    end

    it 'returns true for a directory in the EE lib/ directory' do
      node =
        build_and_parse_source('10', rails_root_join('ee', 'lib', 'models', 'foo.rb'))

      expect(cop.in_lib_directory?(node, 'models')).to eq(true)
    end

    it 'returns false for a directory in the app/ directory' do
      node =
        build_and_parse_source('10', rails_root_join('app', 'models', 'foo.rb'))

      expect(cop.in_lib_directory?(node, 'models')).to eq(false)
    end
  end

  describe '#name_of_receiver' do
    it 'returns the name of a send receiver' do
      node = build_and_parse_source('Foo.bar')

      expect(cop.name_of_receiver(node)).to eq('Foo')
    end
  end

  describe '#each_class_method' do
    it 'yields every class method to the supplied block' do
      node = build_and_parse_source(<<~RUBY)
        class Foo
          class << self
            def first
            end
          end

          def self.second
          end
        end
      RUBY

      nodes = cop.each_class_method(node).to_a

      expect(nodes.length).to eq(2)

      expect(nodes[0].children[0]).to eq(:first)
      expect(nodes[1].children[1]).to eq(:second)
    end
  end

  describe '#each_send_node' do
    it 'yields every send node to the supplied block' do
      node = build_and_parse_source("foo\nbar")
      nodes = cop.each_send_node(node).to_a

      expect(nodes.length).to eq(2)
      expect(nodes[0].children[1]).to eq(:foo)
      expect(nodes[1].children[1]).to eq(:bar)
    end
  end

  describe '#disallow_send_to' do
    it 'disallows sending a message to a constant' do
      def_node = build_and_parse_source(<<~RUBY)
        def foo
          FooFinder.new
        end
      RUBY

      send_node = def_node.each_child_node(:send).first

      expect(cop)
        .to receive(:add_offense)
        .with(send_node, location: :expression, message: 'oops')

      cop.disallow_send_to(def_node, 'Finder', 'oops')
    end
  end

  describe '#ee?' do
    before do
      stub_env('FOSS_ONLY', nil)
      allow(File).to receive(:exist?).with(ee_file_path) { true }
    end

    it 'returns true when ee/app/models/license.rb exists' do
      expect(cop.ee?).to eq(true)
    end
  end

  describe '#jh?' do
    context 'when jh directory exists and EE_ONLY is not set' do
      before do
        stub_env('EE_ONLY', nil)

        allow(Dir).to receive(:exist?).with(File.expand_path('../../jh', __dir__)) { true }
      end

      context 'when ee/app/models/license.rb exists' do
        before do
          allow(File).to receive(:exist?).with(ee_file_path) { true }
        end

        context 'when FOSS_ONLY is not set' do
          before do
            stub_env('FOSS_ONLY', nil)
          end

          it 'returns true' do
            expect(cop.jh?).to eq(true)
          end
        end

        context 'when FOSS_ONLY is set to 1' do
          before do
            stub_env('FOSS_ONLY', '1')
          end

          it 'returns false' do
            expect(cop.jh?).to eq(false)
          end
        end
      end

      context 'when ee/app/models/license.rb not exist' do
        before do
          allow(File).to receive(:exist?).with(ee_file_path) { false }
        end

        context 'when FOSS_ONLY is not set' do
          before do
            stub_env('FOSS_ONLY', nil)
          end

          it 'returns true' do
            expect(cop.jh?).to eq(false)
          end
        end

        context 'when FOSS_ONLY is set to 1' do
          before do
            stub_env('FOSS_ONLY', '1')
          end

          it 'returns false' do
            expect(cop.jh?).to eq(false)
          end
        end
      end
    end
  end
end