Skip to content

Commit f4a03c0

Browse files
committed
feat: make feature toggles case insensitive and space aware
1 parent f0737b9 commit f4a03c0

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

lib/pact_broker/feature_toggle.rb

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
module PactBroker
22
class FeatureToggle
3-
43
def self.enabled?(feature)
5-
ENV['RACK_ENV'] != 'production' || (ENV['PACT_BROKER_FEATURES'] || "").include?(feature.to_s)
4+
not_production? || feature_in_env_var?(feature)
5+
end
6+
7+
def self.not_production?
8+
ENV['RACK_ENV'] != 'production'
69
end
710

11+
def self.feature_in_env_var?(feature)
12+
(features =~ /\b#{feature}\b/i) != nil
13+
end
14+
15+
def self.features
16+
ENV['PACT_BROKER_FEATURES'] || ""
17+
end
818
end
919

1020
def self.feature_enabled?(feature)

spec/lib/pact_broker/feature_toggle_spec.rb

+23-7
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ module PactBroker
1010
subject { FeatureToggle.enabled?(:foo) }
1111

1212
context "when RACK_ENV is not production" do
13-
1413
before do
1514
allow(ENV).to receive(:[]).with('RACK_ENV').and_return('development')
1615
end
@@ -23,13 +22,13 @@ module PactBroker
2322
it { is_expected.to be true }
2423
end
2524

26-
context "when PACT_BROKER_FEATURES does not include the given string" do
27-
before do
28-
allow(ENV).to receive(:[]).with('PACT_BROKER_FEATURES').and_return(nil)
29-
end
25+
context "when PACT_BROKER_FEATURES does not include the given string" do
26+
before do
27+
allow(ENV).to receive(:[]).with('PACT_BROKER_FEATURES').and_return(nil)
28+
end
3029

31-
it { is_expected.to be true }
32-
end
30+
it { is_expected.to be true }
31+
end
3332
end
3433

3534
context "when RACK_ENV is production" do
@@ -45,6 +44,23 @@ module PactBroker
4544
it { is_expected.to be true }
4645
end
4746

47+
context "when PACT_BROKER_FEATURES includes the given string inside another word" do
48+
before do
49+
allow(ENV).to receive(:[]).with('PACT_BROKER_FEATURES').and_return('foowiffle bar')
50+
end
51+
52+
it { is_expected.to be false }
53+
end
54+
55+
context "when PACT_BROKER_FEATURES includes the given string but the case doesn't match" do
56+
before do
57+
allow(ENV).to receive(:[]).with('PACT_BROKER_FEATURES').and_return('FOO bar')
58+
end
59+
60+
it { is_expected.to be true }
61+
end
62+
63+
4864
context "when PACT_BROKER_FEATURES does not include the given string" do
4965
before do
5066
allow(ENV).to receive(:[]).with('PACT_BROKER_FEATURES').and_return(nil)

0 commit comments

Comments
 (0)