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

Ssrc targeting numeric version fixes #2656

Merged
merged 4 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
32 changes: 19 additions & 13 deletions src/remote-config/condition-evaluator-internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,9 @@ function compareNumbers(
return predicateFn(actual < target ? -1 : actual > target ? 1 : 0);
}

// Max number of segments a numeric version can have. This is enforced by the server as well.
const MAX_LENGTH = 5;
Copy link
Contributor

@erikeldridge erikeldridge Jul 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking: five segments deviates from the semver spec, but consistency w the backend is more important, since RC is cross-platform.


// Compares semantic version strings against each other.
// Calls the predicate function with -1, 0, 1 if actual is less than, equal to, or greater than target.
function compareSemanticVersions(
Expand All @@ -303,21 +306,24 @@ function compareSemanticVersions(
const version1 = String(actualValue).split('.').map(Number);
const version2 = targetValue.split('.').map(Number);

if (version1.some(isNaN) || version2.some(isNaN)) {
return false;
}

for (let i = 0;; i++) {
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 (!version1HasSegment) {
return predicateFn(version2HasSegment ? -1 : 0);
} else if (!version2HasSegment) {
return predicateFn(1);
}

if (version1[i] !== version2[i]) {
return predicateFn(version1[i] < version2[i] ? -1 : 1);
}
// 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;

// At this point, if either segment is NaN, we return false directly.
if (isNaN(version1[i]) || isNaN(version2[i])) return false;

// Check if we have a difference in segments. Otherwise continue to next segment.
if (version1[i] < version2[i]) return predicateFn(-1);
if (version1[i] > version2[i]) return predicateFn(1);
}
return false;
}
13 changes: 11 additions & 2 deletions test/unit/remote-config/condition-evaluator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,7 @@ describe('ConditionEvaluator', () => {

interface CustomSignalTestCase {
targets: string[];
actual: string;
actual: string|number;
outcome: boolean;
}

Expand Down Expand Up @@ -1008,7 +1008,6 @@ describe('ConditionEvaluator', () => {
{ targets: ['5'], actual: '4', outcome: true },
{ targets: ['5'], actual: '5', outcome: true },
{ targets: ['5'], actual: '6', outcome: false },
{ targets: ['5'], actual: ' 4 ', outcome: true },
invalidNumericSignalTestCase,
];

Expand Down Expand Up @@ -1050,6 +1049,8 @@ describe('ConditionEvaluator', () => {
const testCases: CustomSignalTestCase[] = [
{ targets: ['5'], actual: '6', outcome: true },
{ targets: ['5'], actual: '5', outcome: true },
{ targets: ['5'], actual: 5.0, outcome: true },
{ targets: ['5.0'], actual: 5.0, outcome: true },
{ targets: ['5'], actual: '4', outcome: false },
invalidNumericSignalTestCase,
];
Expand Down Expand Up @@ -1084,7 +1085,10 @@ describe('ConditionEvaluator', () => {
describe('SEMANTIC_VERSION_EQUAL', () => {
const testCases: CustomSignalTestCase[] = [
{ targets: ['5.12.3'], actual: '5.12.3', outcome: true },
{ targets: ['5'], actual: 5.0, outcome: true },
{ 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 },
invalidNumericSignalTestCase,
];

Expand All @@ -1094,7 +1098,10 @@ describe('ConditionEvaluator', () => {
describe('SEMANTIC_VERSION_NOT_EQUAL', () => {
const testCases: CustomSignalTestCase[] = [
{ targets: ['5.12.3'], actual: '5.12.9', outcome: true },
{ targets: ['5'], actual: 5.0, outcome: false },
{ targets: ['5.0'], actual: 5.0, outcome: false },
{ targets: ['5.12.3'], actual: '5.12.3', outcome: false },
{ targets: ['5.12.3'], actual: '5.12.3.0.0.0.0', outcome: false },
invalidNumericSignalTestCase,
];

Expand All @@ -1118,6 +1125,8 @@ describe('ConditionEvaluator', () => {
const testCases: CustomSignalTestCase[] = [
{ targets: ['5.12.3'], actual: '5.13.9', outcome: true },
{ targets: ['5.12.3'], actual: '5.12.3', outcome: true },
{ 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 },
invalidNumericSignalTestCase
];
Expand Down