Skip to content

Commit

Permalink
fix: accept different ways of writing a range
Browse files Browse the repository at this point in the history
Fixes #18
  • Loading branch information
voxpelli committed Mar 11, 2024
1 parent 018ed1f commit 211018c
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 3 deletions.
9 changes: 6 additions & 3 deletions lib/check-version-range.js
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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`
Expand Down Expand Up @@ -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}` };
}
52 changes: 52 additions & 0 deletions test/check-version-range.spec.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
6 changes: 6 additions & 0 deletions test/fixtures/engine-ranges/node_modules/foo/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions test/fixtures/engine-ranges/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"engines": { "node": "^12 || ^14 || ^16" },
"dependencies": { "foo": "^1.0.0" }
}
12 changes: 12 additions & 0 deletions test/installed-check.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [],
});
});
});
});

0 comments on commit 211018c

Please sign in to comment.