From 0c06a23a3f5130299a564aea7909c7c2d8cae804 Mon Sep 17 00:00:00 2001 From: Rhys Arkins Date: Thu, 25 Jun 2020 16:40:19 +0200 Subject: [PATCH] fix(hex): better handling of double equals (#6586) Co-authored-by: proton <25139420+proton-ab@users.noreply.github.com> --- lib/versioning/hex/index.spec.ts | 43 +++++++++++++++++++++++++++++--- lib/versioning/hex/index.ts | 7 ++++-- 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/lib/versioning/hex/index.spec.ts b/lib/versioning/hex/index.spec.ts index 8894bf1dc6545c..59fc6e8f82b828 100644 --- a/lib/versioning/hex/index.spec.ts +++ b/lib/versioning/hex/index.spec.ts @@ -7,6 +7,8 @@ describe('lib/versioning/hex', () => { expect(hexScheme.matches('2.1.0', '~> 2.0.0')).toBe(false); expect(hexScheme.matches('2.0.0', '>= 2.0.0 and < 2.1.0')).toBe(true); expect(hexScheme.matches('2.1.0', '== 2.0.0 or < 2.1.0')).toBe(false); + expect(hexScheme.matches('1.9.4', '== 1.9.4')).toBe(true); + expect(hexScheme.matches('1.9.5', '== 1.9.4')).toBe(false); }); }); it('handles tilde greater than', () => { @@ -33,6 +35,9 @@ describe('lib/versioning/hex', () => { it('handles !=', () => { expect(hexScheme.isValid('!= 1.0.0')).toBeTruthy(); }); + it('handles ==', () => { + expect(hexScheme.isValid('== 1.0.0')).toBeTruthy(); + }); }); describe('hexScheme.isLessThanRange()', () => { it('handles and', () => { @@ -69,6 +74,36 @@ describe('lib/versioning/hex', () => { }); }); describe('hexScheme.getNewValue()', () => { + it('handles exact pin', () => { + expect( + hexScheme.getNewValue({ + currentValue: '== 1.2.3', + rangeStrategy: 'pin', + fromVersion: '1.2.3', + toVersion: '2.0.7', + }) + ).toEqual('== 2.0.7'); + }); + it('handles exact bump', () => { + expect( + hexScheme.getNewValue({ + currentValue: '== 3.6.1', + rangeStrategy: 'bump', + fromVersion: '3.6.1', + toVersion: '3.6.2', + }) + ).toEqual('== 3.6.2'); + }); + it('handles exact replace', () => { + expect( + hexScheme.getNewValue({ + currentValue: '== 3.6.1', + rangeStrategy: 'replace', + fromVersion: '3.6.1', + toVersion: '3.6.2', + }) + ).toEqual('== 3.6.2'); + }); it('handles tilde greater than', () => { expect( hexScheme.getNewValue({ @@ -85,7 +120,7 @@ describe('lib/versioning/hex', () => { fromVersion: '1.2.3', toVersion: '2.0.7', }) - ).toEqual('2.0.7'); + ).toEqual('== 2.0.7'); expect( hexScheme.getNewValue({ currentValue: '~> 1.2', @@ -109,7 +144,7 @@ describe('lib/versioning/hex', () => { fromVersion: '1.2.3', toVersion: '2.0.7', }) - ).toEqual('2.0.7'); + ).toEqual('== 2.0.7'); expect( hexScheme.getNewValue({ currentValue: '~> 1.2.0', @@ -144,7 +179,7 @@ describe('lib/versioning/hex', () => { fromVersion: '1.2.3', toVersion: '2.0.7', }) - ).toEqual('2.0.7'); + ).toEqual('== 2.0.7'); }); it('handles or', () => { expect( @@ -170,6 +205,6 @@ describe('lib/versioning/hex', () => { fromVersion: '1.2.3', toVersion: '2.0.7', }) - ).toEqual('2.0.7'); + ).toEqual('== 2.0.7'); }); }); diff --git a/lib/versioning/hex/index.ts b/lib/versioning/hex/index.ts index 5608a58115a49e..47ab7afa0b180b 100644 --- a/lib/versioning/hex/index.ts +++ b/lib/versioning/hex/index.ts @@ -13,7 +13,8 @@ function hex2npm(input: string): string { .replace(/~>\s*(\d+\.\d+\.\d+)/, '~$1') .replace(/==|and/, '') .replace('or', '||') - .replace(/!=\s*(\d+\.\d+(\.\d+.*)?)/, '>$1 <$1'); + .replace(/!=\s*(\d+\.\d+(\.\d+.*)?)/, '>$1 <$1') + .trim(); } function npm2hex(input: string): string { @@ -76,7 +77,9 @@ const getNewValue = ({ } else { newSemver = newSemver.replace(/~\s*(\d+\.\d+\.\d)/, '~> $1'); } - + if (npm.isVersion(newSemver)) { + newSemver = `== ${newSemver}`; + } return newSemver; };