Welcome to mirror list, hosted at ThFree Co, Russian Federation.

invalid_feature_category_spec.rb « rspec « cop « rubocop « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e5287f7105eddadbbb3d223e7c254ea98fba4b93 (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
# frozen_string_literal: true

require 'rubocop_spec_helper'
require 'rspec-parameterized'

require_relative '../../../../rubocop/cop/rspec/invalid_feature_category'

RSpec.describe RuboCop::Cop::RSpec::InvalidFeatureCategory, feature_category: :tooling do
  shared_examples 'feature category validation' do |valid_category|
    it 'flags invalid feature category in top level example group' do
      expect_offense(<<~RUBY, invalid: invalid_category)
        RSpec.describe 'foo', feature_category: :%{invalid}, foo: :bar do
                                                ^^{invalid} Please use a valid feature category. See https://docs.gitlab.com/ee/development/feature_categorization/#rspec-examples.
        end
      RUBY
    end

    it 'flags invalid feature category in nested context' do
      expect_offense(<<~RUBY, valid: valid_category, invalid: invalid_category)
        RSpec.describe 'foo', feature_category: :"%{valid}" do
          context 'bar', foo: :bar, feature_category: :%{invalid} do
                                                      ^^{invalid} Please use a valid feature category. See https://docs.gitlab.com/ee/development/feature_categorization/#rspec-examples.
          end
        end
      RUBY
    end

    it 'flags invalid feature category in examples' do
      expect_offense(<<~RUBY, valid: valid_category, invalid: invalid_category)
        RSpec.describe 'foo', feature_category: :"%{valid}" do
          it 'bar', feature_category: :%{invalid} do
                                      ^^{invalid} Please use a valid feature category. See https://docs.gitlab.com/ee/development/feature_categorization/#rspec-examples.
          end
        end
      RUBY
    end

    it 'does not flag if feature category is valid' do
      expect_no_offenses(<<~RUBY)
        RSpec.describe 'foo', feature_category: :"#{valid_category}" do
          context 'bar', feature_category: :"#{valid_category}" do
            it 'baz', feature_category: :"#{valid_category}" do
            end
          end
        end
      RUBY
    end

    it 'suggests an alternative' do
      mistyped = make_typo(valid_category)

      expect_offense(<<~RUBY, invalid: mistyped, valid: valid_category)
        RSpec.describe 'foo', feature_category: :"%{invalid}" do
                                                ^^^^{invalid} Please use a valid feature category. Did you mean `:%{valid}`? See [...]
        end
      RUBY
    end

    def make_typo(string)
      "#{string}#{string[-1]}"
    end
  end

  let(:invalid_category) { :invalid_category }

  context 'with categories defined in config/feature_categories.yml' do
    where(:valid_category) do
      YAML.load_file(rails_root_join('config/feature_categories.yml'))
    end

    with_them do
      it_behaves_like 'feature category validation', params[:valid_category]
    end
  end

  context 'with custom categories' do
    it_behaves_like 'feature category validation', 'tooling'
    it_behaves_like 'feature category validation', 'shared'
  end

  it 'flags invalid feature category for non-symbols' do
    expect_offense(<<~RUBY, invalid: invalid_category)
      RSpec.describe 'foo', feature_category: "%{invalid}" do
                                              ^^^{invalid} Please use a symbol as value.
      end

      RSpec.describe 'foo', feature_category: 42 do
                                              ^^ Please use a symbol as value.
      end
    RUBY
  end

  it 'does not flag use of invalid categories in non-example code' do
    # See https://gitlab.com/gitlab-org/gitlab/-/issues/381882#note_1265865125
    expect_no_offenses(<<~RUBY)
      RSpec.describe 'A spec' do
        let(:api_handler) do
          Class.new(described_class) do
            namespace '/test' do
              get 'hello', feature_category: :foo, urgency: :#{invalid_category} do
              end
            end
          end
        end

        it 'tests something' do
          Gitlab::ApplicationContext.with_context(feature_category: :#{invalid_category}) do
            payload = generator.generate(exception, extra)
          end
        end
      end
    RUBY
  end

  describe '#external_dependency_checksum' do
    it 'returns a SHA256 digest used by RuboCop to invalid cache' do
      expect(cop.external_dependency_checksum).to match(/^\h{64}$/)
    end
  end
end