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

feature_setup_spec.rb « helpers « specs « spec « qa - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 07203e31aca2f2cd45a91a841e9ec23ccced6ff6 (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
# frozen_string_literal: true

describe QA::Specs::Helpers::FeatureSetup do
  include QA::Support::Helpers::StubEnv

  let(:rspec_config) { instance_double(RSpec::Core::Configuration) }
  let(:options) { {} }
  let(:feature_enabled) { true }

  let(:feature_flags_env) { "" }
  let(:feature_flags) { feature_flags_env.split(",").to_h { |ff| ff.split("=") } }

  before do
    stub_env('QA_FEATURE_FLAGS', feature_flags_env)

    allow(RSpec).to receive(:configure).and_yield(rspec_config)
    allow(rspec_config).to receive(:before).with(:suite).and_yield
    allow(rspec_config).to receive(:after).with(:suite).and_yield

    allow(QA::Support::GlobalOptions).to receive(:get).and_return(options)
    allow(QA::Runtime::Feature).to receive(:disable)
    allow(QA::Runtime::Feature).to receive(:enable)
    allow(QA::Runtime::Feature).to receive(:set).with(feature_flags)
    allow(QA::Runtime::Feature).to receive(:enabled?).and_return(feature_enabled)
    allow(QA::Runtime::Logger).to receive(:logger).and_return(instance_double(ActiveSupport::Logger, error: nil))

    described_class.configure!
  end

  context "without any features configured" do
    it "doesn't perform any operations" do
      expect(QA::Runtime::Feature).not_to have_received(:set)
      expect(QA::Runtime::Feature).not_to have_received(:enable)
      expect(QA::Runtime::Feature).not_to have_received(:disable)
    end
  end

  context "with enabling a feature" do
    let(:options) { { enable_feature: 'a-feature' } }

    context "when feature is not enabled" do
      let(:feature_enabled) { false }

      it "enables and restores feature" do
        expect(QA::Runtime::Feature).to have_received(:enable).with(options[:enable_feature])
        expect(QA::Runtime::Feature).to have_received(:disable).with(options[:enable_feature])
      end
    end

    context "when feature is already enabled" do
      it "skips feature" do
        expect(QA::Runtime::Feature).not_to have_received(:disable)
        expect(QA::Runtime::Feature).not_to have_received(:enable)
      end
    end
  end

  context "with disabling a feature" do
    let(:options) { { disable_feature: 'a-feature' } }

    context "when feature is enabled" do
      it "disables and restore feature" do
        expect(QA::Runtime::Feature).to have_received(:disable).with(options[:disable_feature])
        expect(QA::Runtime::Feature).to have_received(:enable).with(options[:disable_feature])
      end
    end

    context "when feature is already disabled" do
      let(:feature_enabled) { false }

      it "skips feature" do
        expect(QA::Runtime::Feature).not_to have_received(:disable)
        expect(QA::Runtime::Feature).not_to have_received(:enable)
      end
    end
  end

  context "with feature flags" do
    context "with valid ff string" do
      let(:feature_flags_env) { "some_flag=enabled,some_other_flag=disabled" }

      it "sets feature flags" do
        expect(QA::Runtime::Feature).to have_received(:set).with(feature_flags)
      end
    end

    context "with not valid ff string" do
      let(:feature_flags_env) { "some_flag=enabled,some_other_flag=invalid_state" }
      let(:feature_flags) { { "some_flag" => "enabled" } }

      it "skips invalid pair" do
        expect(QA::Runtime::Feature).to have_received(:set).with(feature_flags)
      end
    end
  end
end