summaryrefslogtreecommitdiff
path: root/spec/rubocop/cop/gitlab/duplicate_spec_location_spec.rb
blob: 3b3d5b01a306e6c25901642e639195f2f65fe1b2 (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
# frozen_string_literal: true

require 'fast_spec_helper'

require_relative '../../../../rubocop/cop/gitlab/duplicate_spec_location'

RSpec.describe RuboCop::Cop::Gitlab::DuplicateSpecLocation do
  subject(:cop) { described_class.new }

  let(:rails_root) { '../../../../' }

  def full_path(path)
    File.expand_path(File.join(rails_root, path), __dir__)
  end

  context 'Non-EE spec file' do
    it 'registers no offenses' do
      expect_no_offenses(<<~SOURCE, full_path('spec/foo_spec.rb'))
        describe 'Foo' do
        end
      SOURCE
    end
  end

  context 'Non-EE application file' do
    it 'registers no offenses' do
      expect_no_offenses(<<~SOURCE, full_path('app/models/blog_post.rb'))
        class BlogPost
        end
      SOURCE
    end
  end

  context 'EE application file' do
    it 'registers no offenses' do
      expect_no_offenses(<<~SOURCE, full_path('ee/app/models/blog_post.rb'))
        class BlogPost
        end
      SOURCE
    end
  end

  context 'EE spec file for EE only code' do
    let(:spec_file_path) { full_path('ee/spec/controllers/foo_spec.rb') }

    it 'registers no offenses' do
      expect_no_offenses(<<~SOURCE, spec_file_path)
        describe 'Foo' do
        end
      SOURCE
    end

    context 'there is a duplicate file' do
      before do
        allow(File).to receive(:exist?).and_call_original

        allow(File).to receive(:exist?)
          .with(full_path('ee/spec/controllers/ee/foo_spec.rb'))
          .and_return(true)
      end

      it 'marks the describe as offending' do
        expect_offense(<<~SOURCE, spec_file_path)
          describe 'Foo' do
          ^^^^^^^^^^^^^^ Duplicate spec location in `ee/spec/controllers/ee/foo_spec.rb`.
          end
        SOURCE
      end
    end
  end

  context 'EE spec file for EE extension' do
    let(:spec_file_path) { full_path('ee/spec/controllers/ee/foo_spec.rb') }

    it 'registers no offenses' do
      expect_no_offenses(<<~SOURCE, spec_file_path)
        describe 'Foo' do
        end
      SOURCE
    end

    context 'there is a duplicate file' do
      before do
        allow(File).to receive(:exist?).and_call_original

        allow(File).to receive(:exist?)
          .with(full_path('ee/spec/controllers/foo_spec.rb'))
          .and_return(true)
      end

      it 'marks the describe as offending' do
        expect_offense(<<~SOURCE, spec_file_path)
          describe 'Foo' do
          ^^^^^^^^^^^^^^ Duplicate spec location in `ee/spec/controllers/foo_spec.rb`.
          end
        SOURCE
      end
    end
  end
end