2019-07-25 01:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-07-13 03:58:05 -04:00
|
|
|
module StubFeatureFlags
|
2020-08-11 11:10:08 -04:00
|
|
|
def self.included(base)
|
|
|
|
# Extend Feature class with methods that can stub feature flags.
|
|
|
|
Feature.prepend(StubbedFeature)
|
|
|
|
end
|
|
|
|
|
2020-05-29 14:08:26 -04:00
|
|
|
class StubFeatureGate
|
|
|
|
attr_reader :flipper_id
|
|
|
|
|
|
|
|
def initialize(flipper_id)
|
|
|
|
@flipper_id = flipper_id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-11 11:10:08 -04:00
|
|
|
# Ensure feature flags are stubbed and reset.
|
2020-05-29 14:08:26 -04:00
|
|
|
def stub_all_feature_flags
|
2020-08-11 11:10:08 -04:00
|
|
|
Feature.stub = true
|
|
|
|
Feature.reset_flipper
|
|
|
|
end
|
2020-05-29 14:08:26 -04:00
|
|
|
|
2020-08-11 11:10:08 -04:00
|
|
|
def unstub_all_feature_flags
|
|
|
|
Feature.stub = false
|
2020-05-29 14:08:26 -04:00
|
|
|
end
|
|
|
|
|
2018-08-20 14:52:56 -04:00
|
|
|
# Stub Feature flags with `flag_name: true/false`
|
|
|
|
#
|
2019-02-13 01:31:31 -05:00
|
|
|
# @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-13 05:08:37 -04:00
|
|
|
# - `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-07-13 03:58:05 -04:00
|
|
|
def stub_feature_flags(features)
|
2020-05-13 05:08:37 -04:00
|
|
|
features.each do |feature_name, actors|
|
2020-05-29 14:08:26 -04:00
|
|
|
# Remove feature flag overwrite
|
2020-06-02 08:08:33 -04:00
|
|
|
feature = Feature.get(feature_name) # rubocop:disable Gitlab/AvoidFeatureGet
|
2020-05-29 14:08:26 -04:00
|
|
|
feature.remove
|
2020-05-13 05:08:37 -04:00
|
|
|
|
|
|
|
Array(actors).each do |actor|
|
|
|
|
raise ArgumentError, "actor cannot be Hash" if actor.is_a?(Hash)
|
2019-02-13 01:31:31 -05:00
|
|
|
|
2020-05-29 14:08:26 -04:00
|
|
|
# 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-13 05:08:37 -04:00
|
|
|
else
|
2020-05-29 14:08:26 -04:00
|
|
|
raise ArgumentError, "#stub_feature_flags accepts only `nil`, `bool`, an object responding to `#flipper_id` or including `FeatureGate`."
|
2020-05-13 05:08:37 -04:00
|
|
|
end
|
2019-02-13 01:31:31 -05:00
|
|
|
end
|
2017-07-13 03:58:05 -04:00
|
|
|
end
|
|
|
|
end
|
2020-05-29 14:08:26 -04:00
|
|
|
|
|
|
|
def stub_feature_flag_gate(object)
|
|
|
|
return if object.nil?
|
|
|
|
return object if object.is_a?(FeatureGate)
|
|
|
|
|
|
|
|
StubFeatureGate.new(object)
|
|
|
|
end
|
2020-09-21 08:09:34 -04:00
|
|
|
|
|
|
|
def skip_feature_flags_yaml_validation
|
|
|
|
allow(Feature::Definition).to receive(:valid_usage!)
|
|
|
|
end
|
2021-01-07 19:10:44 -05:00
|
|
|
|
|
|
|
def skip_default_enabled_yaml_check
|
|
|
|
allow(Feature::Definition).to receive(:default_enabled?).and_return(false)
|
|
|
|
end
|
2022-05-04 17:08:35 -04:00
|
|
|
|
|
|
|
def stub_feature_flag_definition(name, opts = {})
|
|
|
|
opts = opts.with_defaults(
|
|
|
|
name: name,
|
|
|
|
type: 'development',
|
|
|
|
default_enabled: false
|
|
|
|
)
|
|
|
|
|
|
|
|
Feature::Definition.new("#{opts[:type]}/#{name}.yml", opts).tap do |definition|
|
|
|
|
all_definitions = Feature::Definition.definitions
|
|
|
|
all_definitions[definition.key] = definition
|
|
|
|
allow(Feature::Definition).to receive(:definitions).and_return(all_definitions)
|
|
|
|
end
|
|
|
|
end
|
2017-07-13 03:58:05 -04:00
|
|
|
end
|