Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: equality comparison for semantic version when number of segments = max allowed segments #2794

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/remote-config/condition-evaluator-internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,14 +302,15 @@ function compareSemanticVersions(
const version1 = String(actualValue).split('.').map(Number);
const version2 = targetValue.split('.').map(Number);

if (version1.length > MAX_LENGTH || version2.length > MAX_LENGTH) {
return false;
}

for (let i = 0; i < MAX_LENGTH; i++) {
// Check to see if segments are present. Note that these may be present and be NaN.
const version1HasSegment = version1[i] !== undefined;
const version2HasSegment = version2[i] !== undefined;

// If both are undefined, we've consumed everything and they're equal.
if (!version1HasSegment && !version2HasSegment) return predicateFn(0)

// Insert zeros if undefined for easier comparison.
if (!version1HasSegment) version1[i] = 0;
if (!version2HasSegment) version2[i] = 0;
Expand All @@ -321,5 +322,6 @@ function compareSemanticVersions(
if (version1[i] < version2[i]) return predicateFn(-1);
if (version1[i] > version2[i]) return predicateFn(1);
}
return false;
// If this point is reached, the semantic versions are equal.
return predicateFn(0);
}
4 changes: 4 additions & 0 deletions test/unit/remote-config/condition-evaluator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1114,6 +1114,7 @@ describe('ConditionEvaluator', () => {
{ targets: ['5.12.3'], actual: '5.11.9', outcome: true },
{ targets: ['5.12.3'], actual: '5.12.3', outcome: true },
{ targets: ['5.12.3'], actual: '5.12.9', outcome: false },
{ targets: ['5.6.7.8.9'], actual: '5.6.7.8.9', outcome: true },
invalidNumericSignalTestCase,
];

Expand All @@ -1127,6 +1128,8 @@ describe('ConditionEvaluator', () => {
{ targets: ['5.0'], actual: 5.0, outcome: true },
{ targets: ['5.12.3'], actual: '5.12.9', outcome: false },
{ targets: ['5.12.3'], actual: '5.12.3.0.0.0.0', outcome: false },
{ targets: ['5.6.7.8.9'], actual: '5.6.7.8.9', outcome: true },
Namyalg marked this conversation as resolved.
Show resolved Hide resolved
{ targets: ['5.6.7.8.9.0'], actual: '5.6.7.8.9.0', outcome: false },
invalidNumericSignalTestCase,
];

Expand Down Expand Up @@ -1166,6 +1169,7 @@ describe('ConditionEvaluator', () => {
{ targets: ['5'], actual: 5.0, outcome: true },
{ targets: ['5.0'], actual: 5.0, outcome: true },
{ targets: ['5.12.3'], actual: '5.11.9', outcome: false },
{ targets: ['5.6.7.8.9'], actual: '5.6.7.8.9', outcome: true },
invalidNumericSignalTestCase
];

Expand Down
Loading