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

feature_flags_spec.rb « yaml_processor « ci « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0bd9563d1913e7c0f8c6fab999819d0543c0b46e (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
# frozen_string_literal: true

require 'fast_spec_helper'

RSpec.describe Gitlab::Ci::YamlProcessor::FeatureFlags do
  let(:feature_flag) { :my_feature_flag }

  context 'when the actor is set' do
    let(:actor) { double }
    let(:another_actor) { double }

    it 'checks the feature flag using the given actor' do
      described_class.with_actor(actor) do
        expect(Feature).to receive(:enabled?).with(feature_flag, actor)

        described_class.enabled?(feature_flag)
      end
    end

    it 'returns the value of the block' do
      result = described_class.with_actor(actor) do
        :test
      end

      expect(result).to eq(:test)
    end

    it 'restores the existing actor if any' do
      described_class.with_actor(actor) do
        described_class.with_actor(another_actor) do
          expect(Feature).to receive(:enabled?).with(feature_flag, another_actor)

          described_class.enabled?(feature_flag)
        end

        expect(Feature).to receive(:enabled?).with(feature_flag, actor)
        described_class.enabled?(feature_flag)
      end
    end

    it 'restores the actor to nil after the block' do
      described_class.with_actor(actor) do
        expect(Thread.current[described_class::ACTOR_KEY]).to eq(actor)
      end

      expect(Thread.current[described_class::ACTOR_KEY]).to be nil
    end
  end

  context 'when feature flag is checked outside the "with_actor" block' do
    it 'raises an error on dev/test environment' do
      expect { described_class.enabled?(feature_flag) }.to raise_error(described_class::NoActorError)
    end

    context 'when on production' do
      before do
        allow(Gitlab::ErrorTracking).to receive(:should_raise_for_dev?).and_return(false)
      end

      it 'checks the feature flag without actor' do
        expect(Feature).to receive(:enabled?).with(feature_flag, nil)
        expect(Gitlab::ErrorTracking)
          .to receive(:track_and_raise_for_dev_exception)
          .and_call_original

        described_class.enabled?(feature_flag)
      end
    end
  end

  context 'when actor is explicitly nil' do
    it 'checks the feature flag without actor' do
      described_class.with_actor(nil) do
        expect(Feature).to receive(:enabled?).with(feature_flag, nil)

        described_class.enabled?(feature_flag)
      end
    end
  end
end