diff --git a/lib/versioning/loose/generic.ts b/lib/versioning/loose/generic.ts index 0691111d2d5fc6c234895dc84cc94c6f41122837..cfcb3b657fa8b5534448a06cc86e4dbd8bb26f89 100644 --- a/lib/versioning/loose/generic.ts +++ b/lib/versioning/loose/generic.ts @@ -19,7 +19,7 @@ export abstract class GenericVersioningApi< T extends GenericVersion = GenericVersion > implements VersioningApi { - private _getSection(version: string, index: number): number { + private _getSection(version: string, index: number): number | null { const parsed = this._parse(version); return parsed && parsed.release.length > index ? parsed.release[index] @@ -84,7 +84,7 @@ export abstract class GenericVersioningApi< isStable(version: string): boolean { const parsed = this._parse(version); - return parsed && !parsed.prerelease; + return !!(parsed && !parsed.prerelease); } isSingleVersion(version: string): boolean { @@ -120,11 +120,13 @@ export abstract class GenericVersioningApi< } getSatisfyingVersion(versions: string[], range: string): string | null { - return versions.find((v) => this.equals(v, range)) || null; + const result = versions.find((v) => this.equals(v, range)); + return result ?? null; } minSatisfyingVersion(versions: string[], range: string): string | null { - return versions.find((v) => this.equals(v, range)) || null; + const result = versions.find((v) => this.equals(v, range)); + return result ?? null; } getNewValue(newValueConfig: NewValueConfig): string { diff --git a/lib/versioning/loose/index.ts b/lib/versioning/loose/index.ts index 39d27d631b77f7253de73e4cecb87c26fa249bc2..4386ab2ad438b77b47882a53eca3f346b6989fd9 100644 --- a/lib/versioning/loose/index.ts +++ b/lib/versioning/loose/index.ts @@ -50,6 +50,10 @@ class LooseVersioningApi extends GenericVersioningApi { return part1 - part2; } } + // istanbul ignore if + if (!(parsed1.suffix && parsed2.suffix)) { + return 1; + } // equals return parsed1.suffix.localeCompare(parsed2.suffix); } diff --git a/lib/versioning/loose/utils.spec.ts b/lib/versioning/loose/utils.spec.ts index bfaa7cc66dd341a695208690833f1a2ac7f3fdc8..5d9bae18dd21c219d349575ffdb3effe4ef123ad 100644 --- a/lib/versioning/loose/utils.spec.ts +++ b/lib/versioning/loose/utils.spec.ts @@ -14,7 +14,7 @@ describe('versioning/loose/utils', () => { 'valueOf', ]; function getAllPropertyNames(obj: any): string[] { - const props = []; + const props: string[] = []; let o = obj; do { @@ -34,8 +34,15 @@ describe('versioning/loose/utils', () => { return _version ? _version.localeCompare(_other) : 0; } - protected _parse(_version: string): GenericVersion { - return _version === 'test' ? null : { release: [1, 0, 0] }; + protected _parse(_version: string): GenericVersion | null { + const matchGroups = _version.match( + /^(?<major>\d)\.(?<minor>\d)\.(?<patch>\d)$/ + )?.groups; + if (!matchGroups) { + return null; + } + const { major, minor, patch } = matchGroups; + return { release: [major, minor, patch].map((n) => parseInt(n, 10)) }; } } @@ -68,37 +75,83 @@ describe('versioning/loose/utils', () => { ]); }); - test.each` - fn | expected - ${'equals'} | ${true} - ${'getMajor'} | ${1} - ${'getMinor'} | ${0} - ${'getNewValue'} | ${undefined} - ${'getPatch'} | ${0} - ${'isCompatible'} | ${true} - ${'isGreaterThan'} | ${false} - ${'isSingleVersion'} | ${true} - ${'isStable'} | ${true} - ${'isValid'} | ${true} - ${'isVersion'} | ${true} - ${'matches'} | ${true} - ${'sortVersions'} | ${0} - `('$fn', ({ fn, expected }) => { - expect(api[fn]()).toBe(expected); + it('equals', () => { + expect(api.equals('1.2.3', '1.2.3')).toBeTrue(); + expect(api.equals('1.2.3', '3.2.1')).toBeFalse(); }); - - it('getMajor is null', () => { - expect(api.getMajor('test')).toBeNull(); + it('getMajor', () => { + expect(api.getMajor('4.5.6')).toBe(4); + expect(api.getMajor('invalid')).toBeNull(); + }); + it('getMinor', () => { + expect(api.getMinor('4.5.6')).toBe(5); + expect(api.getMinor('invalid')).toBeNull(); + }); + it('getPatch', () => { + expect(api.getPatch('4.5.6')).toBe(6); + expect(api.getPatch('invalid')).toBeNull(); + }); + it('getNewValue', () => { + expect( + api.getNewValue({ + currentValue: '1.2.3', + rangeStrategy: 'auto', + currentVersion: '1.2.3', + newVersion: '3.2.1', + }) + ).toBe('3.2.1'); + }); + it('isCompatible', () => { + expect(api.isCompatible('1.2.3', '')).toBe(true); + }); + it('isGreaterThan', () => { + expect(api.isGreaterThan('1.2.3', '3.2.1')).toBe(false); + expect(api.isGreaterThan('3.2.1', '1.2.3')).toBe(true); + }); + it('isSingleVersion', () => { + expect(api.isSingleVersion('1.2.3')).toBe(true); + }); + it('isStable', () => { + expect(api.isStable('1.2.3')).toBe(true); + }); + it('isValid', () => { + expect(api.isValid('1.2.3')).toBe(true); + expect(api.isValid('invalid')).toBe(false); + }); + it('isVersion', () => { + expect(api.isVersion('1.2.3')).toBe(true); + expect(api.isVersion('invalid')).toBe(false); + }); + it('matches', () => { + expect(api.matches('1.2.3', '1.2.3')).toBe(true); + expect(api.matches('1.2.3', '3.2.1')).toBe(false); + }); + it('sortVersions', () => { + expect(api.sortVersions('1.2.3', '1.2.3')).toBe(0); + expect(api.sortVersions('1.2.3', '3.2.1')).toBe(-1); + expect(api.sortVersions('3.2.1', '1.2.3')).toBe(1); }); - it('isLessThanRange', () => { - expect(api.isLessThanRange('', '')).toBeFalsy(); + expect(api.isLessThanRange('1.2.3', '3.2.1')).toBeTrue(); + expect(api.isLessThanRange('3.2.1', '1.2.3')).toBeFalse(); }); it('minSatisfyingVersion', () => { - expect(api.minSatisfyingVersion([''], '')).toBeNull(); + expect(api.minSatisfyingVersion(['1.2.3'], '1.2.3')).toBe('1.2.3'); + expect( + api.minSatisfyingVersion(['1.1.1', '2.2.2', '3.3.3'], '2.2.2') + ).toBe('2.2.2'); + expect( + api.minSatisfyingVersion(['1.1.1', '2.2.2', '3.3.3'], '1.2.3') + ).toBeNull(); }); it('getSatisfyingVersion', () => { - expect(api.getSatisfyingVersion([''], '')).toBeNull(); + expect(api.getSatisfyingVersion(['1.2.3'], '1.2.3')).toBe('1.2.3'); + expect( + api.getSatisfyingVersion(['1.1.1', '2.2.2', '3.3.3'], '2.2.2') + ).toBe('2.2.2'); + expect( + api.getSatisfyingVersion(['1.1.1', '2.2.2', '3.3.3'], '1.2.3') + ).toBeNull(); }); }); }); diff --git a/tsconfig.strict.json b/tsconfig.strict.json index 988938b15dd9af4b3fc73611d9d826117cc09368..24880eb5bf8f8cfe1e9bb65b4da6e1a24b5ae036 100644 --- a/tsconfig.strict.json +++ b/tsconfig.strict.json @@ -45,6 +45,7 @@ "lib/util/**/*.ts", "lib/versioning/gradle/compare.ts", "lib/versioning/hex/**/*.ts", + "lib/versioning/loose/**/*.ts", "lib/versioning/maven/**/*.ts", "lib/versioning/node/**/*.ts", "lib/versioning/npm/**/*.ts",