Skip to content

Commit

Permalink
Ensure we use the latest common version
Browse files Browse the repository at this point in the history
  • Loading branch information
stmoreau committed Jul 11, 2023
1 parent fc8aac0 commit 2d08829
Show file tree
Hide file tree
Showing 5 changed files with 1,234 additions and 7,982 deletions.
25 changes: 25 additions & 0 deletions core/get-higher-version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const getHigherVersion = (version1, version2) => {
const v1 = version1 || '';
const v2 = version2 || '';

const v1Components = v1.split('.');
const v2Components = v2.split('.');

for (let i = 0; i < Math.max(v1Components.length, v2Components.length); i += 1) {
const v1Component = Number(v1Components[i]) || 0;
const v2Component = Number(v2Components[i]) || 0;

if (v1Component > v2Component) {
return version1;
}
if (v1Component < v2Component) {
return version2;
}
}

return version1;
};

module.exports = {
getHigherVersion,
};
25 changes: 25 additions & 0 deletions core/get-higher-version.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const { getHigherVersion } = require('./get-higher-version');

describe('getHigherVersion', () => {
test('should return the higher version number when version1 is greater', () => {
expect(getHigherVersion('1.0', '1.0.1')).toBe('1.0.1');
expect(getHigherVersion('2.0', '1.0')).toBe('2.0');
expect(getHigherVersion('2.0.1', '2.0.0.1')).toBe('2.0.1');
expect(getHigherVersion('2.0.1', '2.0.0')).toBe('2.0.1');
expect(getHigherVersion('2.0.1', null)).toBe('2.0.1');
});

test('should return the higher version number when version2 is greater', () => {
expect(getHigherVersion('1.0.1', '1.0')).toBe('1.0.1');
expect(getHigherVersion('1.0', '2.0')).toBe('2.0');
expect(getHigherVersion('2.0.0.1', '2.0.1')).toBe('2.0.1');
expect(getHigherVersion('2.0.0', '2.0.1')).toBe('2.0.1');
expect(getHigherVersion(null, '2.0.1')).toBe('2.0.1');
});

test('should return the same version number when version1 and version2 are equal', () => {
expect(getHigherVersion('1.0', '1.0')).toBe('1.0');
expect(getHigherVersion('2.0.0.1', '2.0.0.1')).toBe('2.0.0.1');
expect(getHigherVersion('2.0.1', '2.0.1')).toBe('2.0.1');
});
});
11 changes: 3 additions & 8 deletions core/html-matrix-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const { writeDocument } = require('./html-matrix-renderer');
const { validateStructure } = require('./yaml-structure');
const { isString } = require('./transform');
const { extractSpecPoints } = require('./specification');
const { getHigherVersion } = require('./get-higher-version');

const loadSource = (filePath) => fs.readFileSync(filePath).toString();
const yamlParserOptions = { mapAsMap: true };
Expand Down Expand Up @@ -52,7 +53,7 @@ class ManifestObjects {
}
});

// Ensure that all objects contain a top-level key with a value indicating the
// Ensure that all objects contain a top-level key with a value indicating the latest
// version of the canonical feature list to which this manifest aligns.
let commonVersion;
objects.forEach((object, suffix) => {
Expand All @@ -67,13 +68,7 @@ class ManifestObjects {
if (version.trim().length < 1) {
throw new Error('common-version may not be empty.');
}
if (commonVersion) {
if (commonVersion !== version) {
throw new Error(`common-version '${version}' must match '${commonVersion}' from previously processed manifests.`);
}
} else {
commonVersion = version;
}
commonVersion = getHigherVersion(commonVersion, version);
} catch (error) {
throw new Error(
`Failed common version locate for manifest with ${suffix}.`,
Expand Down
Loading

0 comments on commit 2d08829

Please sign in to comment.