From 869511e5e6d7915edb289d7ee3cad8981b778896 Mon Sep 17 00:00:00 2001 From: sighphyre Date: Fri, 19 Jan 2024 12:33:49 +0200 Subject: [PATCH] fix: make semver logging actually log context value that it failed to parse rather than allowing the logger to potentially crash --- .../Constraints/SemverConstraintOperator.cs | 2 +- .../SemverConstraintOperator_Tests.cs | 22 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/Unleash/Strategies/Constraints/SemverConstraintOperator.cs b/src/Unleash/Strategies/Constraints/SemverConstraintOperator.cs index a35d7d93..e028cec2 100644 --- a/src/Unleash/Strategies/Constraints/SemverConstraintOperator.cs +++ b/src/Unleash/Strategies/Constraints/SemverConstraintOperator.cs @@ -18,7 +18,7 @@ public bool Evaluate(Constraint constraint, UnleashContext context) SemanticVersion contextSemver; if (!SemanticVersion.TryParse(contextValue, out contextSemver)) { - Logger.Info(() => "Couldn't parse version {0} from context"); + Logger.Info(() => $"Couldn't parse version {contextValue} from context"); return false; } diff --git a/tests/Unleash.Tests/Strategy/Constraints/SemverConstraintOperator_Tests.cs b/tests/Unleash.Tests/Strategy/Constraints/SemverConstraintOperator_Tests.cs index de463ee0..52edc4d7 100644 --- a/tests/Unleash.Tests/Strategy/Constraints/SemverConstraintOperator_Tests.cs +++ b/tests/Unleash.Tests/Strategy/Constraints/SemverConstraintOperator_Tests.cs @@ -251,5 +251,27 @@ public void Release_Is_Greater_Than_RC_Inverted() // Assert result.Should().BeFalse(); } + + [Test] + public void SemverConstraint_Handles_Invalid_Semver_Without_Raising_Exception() + { + // Arrange + var target = new SemverConstraintOperator(); + var constraint = new Constraint( + "operator_semver_test", + Operator.SEMVER_GT, + false, + true, + "definitely.not.a.valid.semver" + ); + var context = new UnleashContext(); + context.Properties.Add("operator_semver_test", "also.definitely.not.a.semver"); + + // Act + Assert + Assert.DoesNotThrow(() => + { + target.Evaluate(constraint, context); + }); + } } }