Skip to content

Commit

Permalink
feat: make feature toggles case insensitive and space aware
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Jul 23, 2018
1 parent f0737b9 commit f4a03c0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
14 changes: 12 additions & 2 deletions lib/pact_broker/feature_toggle.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
module PactBroker
class FeatureToggle

def self.enabled?(feature)
ENV['RACK_ENV'] != 'production' || (ENV['PACT_BROKER_FEATURES'] || "").include?(feature.to_s)
not_production? || feature_in_env_var?(feature)
end

def self.not_production?
ENV['RACK_ENV'] != 'production'
end

def self.feature_in_env_var?(feature)
(features =~ /\b#{feature}\b/i) != nil
end

def self.features
ENV['PACT_BROKER_FEATURES'] || ""
end
end

def self.feature_enabled?(feature)
Expand Down
30 changes: 23 additions & 7 deletions spec/lib/pact_broker/feature_toggle_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ module PactBroker
subject { FeatureToggle.enabled?(:foo) }

context "when RACK_ENV is not production" do

before do
allow(ENV).to receive(:[]).with('RACK_ENV').and_return('development')
end
Expand All @@ -23,13 +22,13 @@ module PactBroker
it { is_expected.to be true }
end

context "when PACT_BROKER_FEATURES does not include the given string" do
before do
allow(ENV).to receive(:[]).with('PACT_BROKER_FEATURES').and_return(nil)
end
context "when PACT_BROKER_FEATURES does not include the given string" do
before do
allow(ENV).to receive(:[]).with('PACT_BROKER_FEATURES').and_return(nil)
end

it { is_expected.to be true }
end
it { is_expected.to be true }
end
end

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

context "when PACT_BROKER_FEATURES includes the given string inside another word" do
before do
allow(ENV).to receive(:[]).with('PACT_BROKER_FEATURES').and_return('foowiffle bar')
end

it { is_expected.to be false }
end

context "when PACT_BROKER_FEATURES includes the given string but the case doesn't match" do
before do
allow(ENV).to receive(:[]).with('PACT_BROKER_FEATURES').and_return('FOO bar')
end

it { is_expected.to be true }
end


context "when PACT_BROKER_FEATURES does not include the given string" do
before do
allow(ENV).to receive(:[]).with('PACT_BROKER_FEATURES').and_return(nil)
Expand Down

0 comments on commit f4a03c0

Please sign in to comment.