From fd31e4ab5dddb79c397508c7f024d4fedc01a66e Mon Sep 17 00:00:00 2001 From: sighphyre Date: Wed, 7 Dec 2022 14:45:37 +0200 Subject: [PATCH] fix an issue where constraints would bubble an exception when context was nil --- lib/unleash/constraint.rb | 1 + spec/unleash/constraint_spec.rb | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/lib/unleash/constraint.rb b/lib/unleash/constraint.rb index f5c9524e..51607c12 100644 --- a/lib/unleash/constraint.rb +++ b/lib/unleash/constraint.rb @@ -43,6 +43,7 @@ def initialize(context_name, operator, value = [], inverted: false, case_insensi def matches_context?(context) Unleash.logger.debug "Unleash::Constraint matches_context? value: #{self.value} context.get_by_name(#{self.context_name})" + return false if context.nil? match = matches_constraint?(context) self.inverted ? !match : match diff --git a/spec/unleash/constraint_spec.rb b/spec/unleash/constraint_spec.rb index e63c5553..4c3f6531 100644 --- a/spec/unleash/constraint_spec.rb +++ b/spec/unleash/constraint_spec.rb @@ -451,4 +451,9 @@ end end end + + it 'does resolves to false rather than crashing when passed a nil context' do + constraint = Unleash::Constraint.new('anything', 'NUM_GTE', '6.282') + expect(constraint.matches_context?(nil)).to be false + end end