debian-mirror-gitlab/spec/support/helpers/stub_feature_flags.rb

70 lines
2 KiB
Ruby
Raw Normal View History

2019-10-12 21:52:04 +05:30
# frozen_string_literal: true
2017-09-10 17:25:29 +05:30
module StubFeatureFlags
2020-10-24 23:57:45 +05:30
def self.included(base)
# Extend Feature class with methods that can stub feature flags.
Feature.prepend(StubbedFeature)
end
2020-06-23 00:09:42 +05:30
class StubFeatureGate
attr_reader :flipper_id
def initialize(flipper_id)
@flipper_id = flipper_id
end
end
2020-10-24 23:57:45 +05:30
# Ensure feature flags are stubbed and reset.
2020-06-23 00:09:42 +05:30
def stub_all_feature_flags
2020-10-24 23:57:45 +05:30
Feature.stub = true
Feature.reset_flipper
end
2020-06-23 00:09:42 +05:30
2020-10-24 23:57:45 +05:30
def unstub_all_feature_flags
Feature.stub = false
2020-06-23 00:09:42 +05:30
end
2018-11-20 20:47:30 +05:30
# Stub Feature flags with `flag_name: true/false`
#
2019-07-07 11:18:12 +05:30
# @param [Hash] features where key is feature name and value is boolean whether enabled or not.
# Alternatively, you can specify Hash to enable the flag on a specific thing.
#
# Examples
# - `stub_feature_flags(ci_live_trace: false)` ... Disable `ci_live_trace`
# feature flag globally.
2020-05-24 23:13:21 +05:30
# - `stub_feature_flags(ci_live_trace: project)` ...
# - `stub_feature_flags(ci_live_trace: [project1, project2])` ...
# Enable `ci_live_trace` feature flag only on the specified projects.
2017-09-10 17:25:29 +05:30
def stub_feature_flags(features)
2020-05-24 23:13:21 +05:30
features.each do |feature_name, actors|
2020-06-23 00:09:42 +05:30
# Remove feature flag overwrite
feature = Feature.get(feature_name) # rubocop:disable Gitlab/AvoidFeatureGet
feature.remove
2020-05-24 23:13:21 +05:30
Array(actors).each do |actor|
raise ArgumentError, "actor cannot be Hash" if actor.is_a?(Hash)
2019-07-07 11:18:12 +05:30
2020-06-23 00:09:42 +05:30
# Control a state of feature flag
if actor == true || actor.nil? || actor.respond_to?(:flipper_id)
feature.enable(actor)
elsif actor == false
feature.disable
2020-05-24 23:13:21 +05:30
else
2020-06-23 00:09:42 +05:30
raise ArgumentError, "#stub_feature_flags accepts only `nil`, `bool`, an object responding to `#flipper_id` or including `FeatureGate`."
2020-05-24 23:13:21 +05:30
end
2019-07-07 11:18:12 +05:30
end
2017-09-10 17:25:29 +05:30
end
end
2020-06-23 00:09:42 +05:30
def stub_feature_flag_gate(object)
return if object.nil?
return object if object.is_a?(FeatureGate)
StubFeatureGate.new(object)
end
2021-01-03 14:25:43 +05:30
def skip_feature_flags_yaml_validation
allow(Feature::Definition).to receive(:valid_usage!)
end
2017-09-10 17:25:29 +05:30
end