From 211018c72be0e6b982cfa98a6167bb6c58768b94 Mon Sep 17 00:00:00 2001 From: Pelle Wessman Date: Mon, 11 Mar 2024 17:41:35 +0100 Subject: [PATCH] fix: accept different ways of writing a range Fixes #18 --- lib/check-version-range.js | 9 ++-- test/check-version-range.spec.js | 52 +++++++++++++++++++ .../node_modules/foo/package.json | 6 +++ test/fixtures/engine-ranges/package.json | 4 ++ test/installed-check.spec.js | 12 +++++ 5 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 test/check-version-range.spec.js create mode 100644 test/fixtures/engine-ranges/node_modules/foo/package.json create mode 100644 test/fixtures/engine-ranges/package.json diff --git a/lib/check-version-range.js b/lib/check-version-range.js index b7e14a3..b84923a 100644 --- a/lib/check-version-range.js +++ b/lib/check-version-range.js @@ -1,5 +1,6 @@ import { semverIntersect } from '@voxpelli/semver-set'; import { omit } from '@voxpelli/typed-utils'; +import { validRange } from 'semver'; import { getStringValueByPath } from './utils.js'; @@ -63,11 +64,13 @@ export function checkVersionRange (mainPackage, key, installedDependencies, opti } } + const valid = intersection !== false && validRange(intersection) === validRange(referenceRange); + return { - valid: intersection === referenceRange + valid: valid ? true : ((rawReferenceRange || strict) ? false : undefined), - suggested: intersection || undefined, + suggested: valid ? undefined : (intersection || undefined), packageNotes, note: rawReferenceRange === false ? `Invalid "${key}" in main package` @@ -118,7 +121,7 @@ export function checkDependencyRange (referenceRange, key, dependencyPackage, is return { valid: false, note: `Incompatible "${key}" requirement: ${range}` }; } - return intersection === referenceRange + return validRange(intersection) === validRange(referenceRange) ? undefined : { valid: false, suggested: intersection, note: `Narrower "${key}" is needed: ${intersection}` }; } diff --git a/test/check-version-range.spec.js b/test/check-version-range.spec.js new file mode 100644 index 0000000..685fde3 --- /dev/null +++ b/test/check-version-range.spec.js @@ -0,0 +1,52 @@ +import chai from 'chai'; +import { ErrorWithCause } from 'pony-cause'; + +import { checkVersionRange, checkDependencyRange } from '../lib/check-version-range.js'; + +const should = chai.should(); + +process.on('unhandledRejection', cause => { + throw new ErrorWithCause('Unhandled rejection', { cause }); +}); + +describe('checkVersionRange', () => { + describe('checkVersionRange()', () => { + it('should handle engine ranges', () => { + checkVersionRange( + { + engines: { node: '^12 || ^14 || ^16' }, + dependencies: { foo: '^1.0.0' }, + }, + 'engines.node', + { + foo: { engines: { node: '^12 || ^14 || ^16' } }, + } + ).should.deep.equal({ + valid: true, + note: undefined, + packageNotes: [], + suggested: undefined, + }); + }); + }); + + describe('checkDependencyRange()', () => { + it('should handle engine ranges', () => { + const installedDependencies = new Map(); + + installedDependencies.set('foo', { + engines: { node: '^12 || ^14 || ^16' }, + }); + + const result = checkDependencyRange( + '^12 || ^14 || ^16', + 'engines.node', + { + engines: { node: '^12 || ^14 || ^16' }, + } + ); + + should.not.exist(result); + }); + }); +}); diff --git a/test/fixtures/engine-ranges/node_modules/foo/package.json b/test/fixtures/engine-ranges/node_modules/foo/package.json new file mode 100644 index 0000000..82b2151 --- /dev/null +++ b/test/fixtures/engine-ranges/node_modules/foo/package.json @@ -0,0 +1,6 @@ +{ + "private": true, + "name": "foo", + "version": "1.0.0", + "engines": { "node": "^12 || ^14 || ^16" } +} diff --git a/test/fixtures/engine-ranges/package.json b/test/fixtures/engine-ranges/package.json new file mode 100644 index 0000000..fc97055 --- /dev/null +++ b/test/fixtures/engine-ranges/package.json @@ -0,0 +1,4 @@ +{ + "engines": { "node": "^12 || ^14 || ^16" }, + "dependencies": { "foo": "^1.0.0" } +} diff --git a/test/installed-check.spec.js b/test/installed-check.spec.js index 5952928..1fcad2e 100644 --- a/test/installed-check.spec.js +++ b/test/installed-check.spec.js @@ -135,5 +135,17 @@ describe('installedCheck()', () => { warnings: [], }); }); + + it('should handle engine ranges', async () => { + await installedCheck({ + path: join(import.meta.url, 'fixtures/engine-ranges'), + engineCheck: true, + versionCheck: false, + }) + .should.eventually.deep.equal({ + 'errors': [], + warnings: [], + }); + }); }); });