From 63423a17295c6345bf336e6baa158e0bfbb86e9c Mon Sep 17 00:00:00 2001
From: Sergei Zharinov <zharinov@users.noreply.github.com>
Date: Tue, 28 Sep 2021 17:14:51 +0300
Subject: [PATCH] test(pep440): Refactor versioning scheme (#11930)

Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
---
 lib/versioning/pep440/index.spec.ts | 359 +++++++++++++---------------
 1 file changed, 168 insertions(+), 191 deletions(-)

diff --git a/lib/versioning/pep440/index.spec.ts b/lib/versioning/pep440/index.spec.ts
index 211c287b54..61e0278820 100644
--- a/lib/versioning/pep440/index.spec.ts
+++ b/lib/versioning/pep440/index.spec.ts
@@ -1,67 +1,53 @@
 import pep440 from '.';
 
 describe('versioning/pep440/index', () => {
-  describe('pep440.isValid(input)', () => {
-    it('should support a version without equals', () => {
-      expect(pep440.isValid('0.750')).toBeTruthy();
-      expect(pep440.isValid('1.2.3')).toBeTruthy();
-      expect(pep440.isValid('1.9')).toBeTruthy();
-    });
-    it('should support irregular versions', () => {
-      expect(pep440.isValid('17.04.0')).toBeTruthy();
-    });
-    it('should support simple pep440', () => {
-      expect(pep440.isValid('==1.2.3')).toBeTruthy();
-    });
-    it('should support pep440 with RC', () => {
-      expect(pep440.isValid('==1.2.3rc0')).toBeTruthy();
-    });
-    it('should support ranges', () => {
-      expect(pep440.isValid('~=1.2.3')).toBeTruthy();
-      expect(pep440.isValid('==1.2.*')).toBeTruthy();
-      expect(pep440.isValid('>1.2.3')).toBeTruthy();
-    });
-    it('should reject github repositories', () => {
-      expect(pep440.isValid('renovatebot/renovate')).toBeFalsy();
-      expect(pep440.isValid('renovatebot/renovate#master')).toBeFalsy();
-      expect(
-        pep440.isValid('https://github.com/renovatebot/renovate.git')
-      ).toBeFalsy();
-    });
+  test.each`
+    input                                            | expected
+    ${'0.750'}                                       | ${true}
+    ${'1.2.3'}                                       | ${true}
+    ${'1.9'}                                         | ${true}
+    ${'17.04.0'}                                     | ${true}
+    ${'==1.2.3'}                                     | ${true}
+    ${'==1.2.3rc0'}                                  | ${true}
+    ${'~=1.2.3'}                                     | ${true}
+    ${'==1.2.*'}                                     | ${true}
+    ${'>1.2.3'}                                      | ${true}
+    ${'renovatebot/renovate'}                        | ${false}
+    ${'renovatebot/renovate#master'}                 | ${false}
+    ${'https://github.com/renovatebot/renovate.git'} | ${false}
+  `('isValid("$input") === $expected', ({ input, expected }) => {
+    const res = !!pep440.isValid(input);
+    expect(res).toBe(expected);
   });
 
-  describe('pep440.isStable(version)', () => {
-    it('returns correct value', () => {
-      expect(pep440.isStable('1.2.3')).toBeTruthy();
-      expect(pep440.isStable('1.2.3rc0')).toBeFalsy();
-    });
-    it('returns false when version invalid', () => {
-      expect(pep440.isStable('not_version')).toBeFalsy();
-    });
+  test.each`
+    input            | expected
+    ${'1.2.3'}       | ${true}
+    ${'1.2.3rc0'}    | ${false}
+    ${'not_version'} | ${false}
+  `('isStable("$input") === $expected', ({ input, expected }) => {
+    expect(pep440.isStable(input)).toBe(expected);
   });
 
-  describe('pep440.equals(version1, version2)', () => {
-    it('returns correct true', () => {
-      expect(pep440.equals('1.0', '1.0.0')).toBeTruthy();
-    });
-    it('returns false when version invalid', () => {
-      expect(pep440.equals('1.0.0', '1.0..foo')).toBeFalsy();
-    });
+  test.each`
+    a          | b             | expected
+    ${'1.0'}   | ${'1.0.0'}    | ${true}
+    ${'1.0.0'} | ${'1.0..foo'} | ${null}
+  `('equals($a, $b) === $expected', ({ a, b, expected }) => {
+    expect(pep440.equals(a, b)).toBe(expected);
   });
 
-  describe('pep440.isSingleVersion()', () => {
-    it('returns true if naked version', () => {
-      expect(pep440.isSingleVersion('1.2.3')).toBeTruthy();
-      expect(pep440.isSingleVersion('1.2.3rc0')).toBeTruthy();
-    });
-    it('returns true if double equals', () => {
-      expect(pep440.isSingleVersion('==1.2.3')).toBeTruthy();
-      expect(pep440.isSingleVersion('==1.2')).toBeTruthy();
-      expect(pep440.isSingleVersion('== 1.2.3')).toBeTruthy();
-    });
-    it('returns false when not version', () => {
-      expect(pep440.isSingleVersion('==1.*')).toBeFalsy();
-    });
+  test.each`
+    version       | isSingle
+    ${'1.2.3'}    | ${true}
+    ${'1.2.3rc0'} | ${true}
+    ${'==1.2.3'}  | ${true}
+    ${'==1.2'}    | ${true}
+    ${'== 1.2.3'} | ${true}
+    ${'==1.*'}    | ${false}
+  `('isSingleVersion("$version") === $isSingle', ({ version, isSingle }) => {
+    const res = !!pep440.isSingleVersion(version);
+    expect(res).toBe(isSingle);
   });
 
   const versions = [
@@ -75,143 +61,134 @@ describe('versioning/pep440/index', () => {
     '2.0.3',
   ];
 
-  describe('pep440.getSatisfyingVersion(versions, range)', () => {
-    it('returns correct value', () => {
-      expect(pep440.getSatisfyingVersion(versions, '~=1.2.1')).toBe('1.2.3');
-    });
-    it('returns null when none found', () => {
-      expect(pep440.getSatisfyingVersion(versions, '~=2.1')).toBeNull();
-    });
-  });
-
-  describe('pep440.minSatisfyingVersion(versions, range)', () => {
-    it('returns correct value', () => {
-      expect(pep440.minSatisfyingVersion(versions, '~=1.2.1')).toBe('1.2.1');
-    });
-    it('returns null when none found', () => {
-      expect(pep440.minSatisfyingVersion(versions, '~=2.1')).toBeNull();
-    });
-  });
-
-  describe('pep440.getNewValue()', () => {
-    const { getNewValue } = pep440;
-
-    // cases: [currentValue, expectedBump]
-    [
-      // plain version
-      ['1.0.0', '1.2.3'],
-
-      // simple cases
-      ['==1.0.3', '==1.2.3'],
-      ['>=1.2.0', '>=1.2.3'],
-      ['~=1.2.0', '~=1.2.3'],
-      ['~=1.0.3', '~=1.2.3'],
-
-      // glob
-      ['==1.2.*', '==1.2.*'],
-      ['==1.0.*', '==1.2.*'],
-
-      // future versions guard
-      ['<1.2.2.3', '<1.2.4.0'],
-      ['<1.2.3', '<1.2.4'],
-      ['<1.2', '<1.3'],
-      ['<1', '<2'],
-      ['<2.0.0', '<2.0.0'],
-
-      // minimum version guard
-      ['>0.9.8', '>0.9.8'],
-      // rollback
-      ['>2.0.0', '>=1.2.3'],
-      ['>=2.0.0', '>=1.2.3'],
-
-      // complex ranges
-      ['~=1.1.0, !=1.1.1', '~=1.2.3, !=1.1.1'],
-      ['~=1.1.0,!=1.1.1', '~=1.2.3,!=1.1.1'],
-
-      // invalid & not supported
-      [' ', ' '],
-      ['invalid', null],
-      ['===1.0.3', null],
-      // impossible
-      ['!=1.2.3', null],
-    ].forEach(([currentValue, expectedBump]) => {
-      const bumped = getNewValue({
-        currentValue,
-        rangeStrategy: 'bump',
-        currentVersion: '1.0.0',
-        newVersion: '1.2.3',
-      });
-      it(`bumps '${currentValue}' to '${expectedBump}'`, () => {
-        expect(bumped).toBe(expectedBump);
-      });
-
-      const replaced = getNewValue({
+  test.each`
+    range        | expected
+    ${'~=1.2.1'} | ${'1.2.3'}
+    ${'~=2.1'}   | ${null}
+  `(
+    'getSatisfyingVersion($versions, "$range") === $expected',
+    ({ range, expected }) => {
+      expect(pep440.getSatisfyingVersion(versions, range)).toBe(expected);
+    }
+  );
+
+  test.each`
+    range        | expected
+    ${'~=1.2.1'} | ${'1.2.1'}
+    ${'~=2.1'}   | ${null}
+  `(
+    'minSatisfyingVersion($versions, "$range") === $expected',
+    ({ range, expected }) => {
+      expect(pep440.minSatisfyingVersion(versions, range)).toBe(expected);
+    }
+  );
+
+  test.each`
+    currentValue          | rangeStrategy | currentVersion | newVersion | expected
+    ${'1.0.0'}            | ${'bump'}     | ${'1.0.0'}     | ${'1.2.3'} | ${'1.2.3'}
+    ${'1.0.0'}            | ${'replace'}  | ${'1.0.0'}     | ${'1.2.3'} | ${'1.2.3'}
+    ${'1.0.0'}            | ${'pin'}      | ${'1.0.0'}     | ${'1.2.3'} | ${'==1.2.3'}
+    ${'==1.0.3'}          | ${'bump'}     | ${'1.0.0'}     | ${'1.2.3'} | ${'==1.2.3'}
+    ${'==1.0.3'}          | ${'replace'}  | ${'1.0.0'}     | ${'1.2.3'} | ${'==1.2.3'}
+    ${'==1.0.3'}          | ${'pin'}      | ${'1.0.0'}     | ${'1.2.3'} | ${'==1.2.3'}
+    ${'>=1.2.0'}          | ${'bump'}     | ${'1.0.0'}     | ${'1.2.3'} | ${'>=1.2.3'}
+    ${'>=1.2.0'}          | ${'replace'}  | ${'1.0.0'}     | ${'1.2.3'} | ${'>=1.2.0'}
+    ${'>=1.2.0'}          | ${'pin'}      | ${'1.0.0'}     | ${'1.2.3'} | ${'==1.2.3'}
+    ${'~=1.2.0'}          | ${'bump'}     | ${'1.0.0'}     | ${'1.2.3'} | ${'~=1.2.3'}
+    ${'~=1.2.0'}          | ${'replace'}  | ${'1.0.0'}     | ${'1.2.3'} | ${'~=1.2.0'}
+    ${'~=1.2.0'}          | ${'pin'}      | ${'1.0.0'}     | ${'1.2.3'} | ${'==1.2.3'}
+    ${'~=1.0.3'}          | ${'bump'}     | ${'1.0.0'}     | ${'1.2.3'} | ${'~=1.2.3'}
+    ${'~=1.0.3'}          | ${'replace'}  | ${'1.0.0'}     | ${'1.2.3'} | ${'~=1.2.3'}
+    ${'~=1.0.3'}          | ${'pin'}      | ${'1.0.0'}     | ${'1.2.3'} | ${'==1.2.3'}
+    ${'==1.2.*'}          | ${'bump'}     | ${'1.0.0'}     | ${'1.2.3'} | ${'==1.2.*'}
+    ${'==1.2.*'}          | ${'replace'}  | ${'1.0.0'}     | ${'1.2.3'} | ${'==1.2.*'}
+    ${'==1.2.*'}          | ${'pin'}      | ${'1.0.0'}     | ${'1.2.3'} | ${'==1.2.3'}
+    ${'==1.0.*'}          | ${'bump'}     | ${'1.0.0'}     | ${'1.2.3'} | ${'==1.2.*'}
+    ${'==1.0.*'}          | ${'replace'}  | ${'1.0.0'}     | ${'1.2.3'} | ${'==1.2.*'}
+    ${'==1.0.*'}          | ${'pin'}      | ${'1.0.0'}     | ${'1.2.3'} | ${'==1.2.3'}
+    ${'<1.2.2.3'}         | ${'bump'}     | ${'1.0.0'}     | ${'1.2.3'} | ${'<1.2.4.0'}
+    ${'<1.2.2.3'}         | ${'replace'}  | ${'1.0.0'}     | ${'1.2.3'} | ${'<1.2.4.0'}
+    ${'<1.2.2.3'}         | ${'pin'}      | ${'1.0.0'}     | ${'1.2.3'} | ${'==1.2.3'}
+    ${'<1.2.3'}           | ${'bump'}     | ${'1.0.0'}     | ${'1.2.3'} | ${'<1.2.4'}
+    ${'<1.2.3'}           | ${'replace'}  | ${'1.0.0'}     | ${'1.2.3'} | ${'<1.2.4'}
+    ${'<1.2.3'}           | ${'pin'}      | ${'1.0.0'}     | ${'1.2.3'} | ${'==1.2.3'}
+    ${'<1.2'}             | ${'bump'}     | ${'1.0.0'}     | ${'1.2.3'} | ${'<1.3'}
+    ${'<1.2'}             | ${'replace'}  | ${'1.0.0'}     | ${'1.2.3'} | ${'<1.3'}
+    ${'<1.2'}             | ${'pin'}      | ${'1.0.0'}     | ${'1.2.3'} | ${'==1.2.3'}
+    ${'<1'}               | ${'bump'}     | ${'1.0.0'}     | ${'1.2.3'} | ${'<2'}
+    ${'<1'}               | ${'replace'}  | ${'1.0.0'}     | ${'1.2.3'} | ${'<2'}
+    ${'<1'}               | ${'pin'}      | ${'1.0.0'}     | ${'1.2.3'} | ${'==1.2.3'}
+    ${'<2.0.0'}           | ${'bump'}     | ${'1.0.0'}     | ${'1.2.3'} | ${'<2.0.0'}
+    ${'<2.0.0'}           | ${'replace'}  | ${'1.0.0'}     | ${'1.2.3'} | ${'<2.0.0'}
+    ${'<2.0.0'}           | ${'pin'}      | ${'1.0.0'}     | ${'1.2.3'} | ${'==1.2.3'}
+    ${'>0.9.8'}           | ${'bump'}     | ${'1.0.0'}     | ${'1.2.3'} | ${'>0.9.8'}
+    ${'>0.9.8'}           | ${'replace'}  | ${'1.0.0'}     | ${'1.2.3'} | ${'>0.9.8'}
+    ${'>0.9.8'}           | ${'pin'}      | ${'1.0.0'}     | ${'1.2.3'} | ${'==1.2.3'}
+    ${'>2.0.0'}           | ${'bump'}     | ${'1.0.0'}     | ${'1.2.3'} | ${'>=1.2.3'}
+    ${'>2.0.0'}           | ${'replace'}  | ${'1.0.0'}     | ${'1.2.3'} | ${'>=1.2.3'}
+    ${'>2.0.0'}           | ${'pin'}      | ${'1.0.0'}     | ${'1.2.3'} | ${'==1.2.3'}
+    ${'>=2.0.0'}          | ${'bump'}     | ${'1.0.0'}     | ${'1.2.3'} | ${'>=1.2.3'}
+    ${'>=2.0.0'}          | ${'replace'}  | ${'1.0.0'}     | ${'1.2.3'} | ${'>=1.2.3'}
+    ${'>=2.0.0'}          | ${'pin'}      | ${'1.0.0'}     | ${'1.2.3'} | ${'==1.2.3'}
+    ${'~=1.1.0, !=1.1.1'} | ${'bump'}     | ${'1.0.0'}     | ${'1.2.3'} | ${'~=1.2.3, !=1.1.1'}
+    ${'~=1.1.0, !=1.1.1'} | ${'replace'}  | ${'1.0.0'}     | ${'1.2.3'} | ${'~=1.2.3, !=1.1.1'}
+    ${'~=1.1.0, !=1.1.1'} | ${'pin'}      | ${'1.0.0'}     | ${'1.2.3'} | ${'==1.2.3'}
+    ${'~=1.1.0,!=1.1.1'}  | ${'bump'}     | ${'1.0.0'}     | ${'1.2.3'} | ${'~=1.2.3,!=1.1.1'}
+    ${'~=1.1.0,!=1.1.1'}  | ${'replace'}  | ${'1.0.0'}     | ${'1.2.3'} | ${'~=1.2.3,!=1.1.1'}
+    ${'~=1.1.0,!=1.1.1'}  | ${'pin'}      | ${'1.0.0'}     | ${'1.2.3'} | ${'==1.2.3'}
+    ${' '}                | ${'bump'}     | ${'1.0.0'}     | ${'1.2.3'} | ${' '}
+    ${' '}                | ${'replace'}  | ${'1.0.0'}     | ${'1.2.3'} | ${' '}
+    ${' '}                | ${'pin'}      | ${'1.0.0'}     | ${'1.2.3'} | ${'==1.2.3'}
+    ${'invalid'}          | ${'bump'}     | ${'1.0.0'}     | ${'1.2.3'} | ${null}
+    ${'invalid'}          | ${'replace'}  | ${'1.0.0'}     | ${'1.2.3'} | ${null}
+    ${'invalid'}          | ${'pin'}      | ${'1.0.0'}     | ${'1.2.3'} | ${'==1.2.3'}
+    ${'===1.0.3'}         | ${'bump'}     | ${'1.0.0'}     | ${'1.2.3'} | ${null}
+    ${'===1.0.3'}         | ${'replace'}  | ${'1.0.0'}     | ${'1.2.3'} | ${null}
+    ${'===1.0.3'}         | ${'pin'}      | ${'1.0.0'}     | ${'1.2.3'} | ${'==1.2.3'}
+    ${'!=1.2.3'}          | ${'bump'}     | ${'1.0.0'}     | ${'1.2.3'} | ${null}
+    ${'!=1.2.3'}          | ${'replace'}  | ${'1.0.0'}     | ${'1.2.3'} | ${null}
+    ${'!=1.2.3'}          | ${'pin'}      | ${'1.0.0'}     | ${'1.2.3'} | ${'==1.2.3'}
+  `(
+    'getNewValue("$currentValue", "$rangeStrategy", "$currentVersion", "$newVersion") === "$expected"',
+    ({ currentValue, rangeStrategy, currentVersion, newVersion, expected }) => {
+      const res = pep440.getNewValue({
         currentValue,
-        rangeStrategy: 'replace',
-        currentVersion: '1.0.0',
-        newVersion: '1.2.3',
-      });
-      const needReplace = pep440.matches('1.2.3', currentValue);
-      const expectedReplace = needReplace ? currentValue : bumped;
-      it(`replaces '${currentValue}' to '${expectedReplace}'`, () => {
-        expect(replaced).toBe(expectedReplace);
+        rangeStrategy,
+        currentVersion,
+        newVersion,
       });
-
-      const pinned = getNewValue({
-        currentValue,
-        rangeStrategy: 'pin',
-        currentVersion: '1.0.0',
-        newVersion: '1.2.3',
-      });
-      const expectedPin = '==1.2.3';
-      it(`pins '${currentValue}' to '${expectedPin}'`, () => {
-        expect(pinned).toBe(expectedPin);
-      });
-    });
-
-    it('guards against unsupported rangeStrategy', () => {
-      const invalid = getNewValue({
-        currentValue: '==1.0.0',
-        rangeStrategy: 'update-lockfile',
-        currentVersion: '1.0.0',
-        newVersion: '1.2.3',
-      });
-      expect(invalid).toEqual('==1.2.3');
-    });
-  });
-
-  describe('pep.isLessThanRange()', () => {
-    test.each([
-      ['>= 1.0.0, < 2.0.0', '0.9.9.9', true],
-      ['>= 1.0.0, < 2.0.0', '1.0.0a0', true],
-
-      ['> 1.0.0, < 2.0.0', '1.0.0.0', true],
-      ['> 1.0.0, < 2.0.0', '2.0.1.0', false],
-      ['> 1.0.0, < 2.0.0', '2.0.0.0', false],
-      ['> 1.0.0, < 2.0.0', '2.0.0a0', false],
-
-      ['== 1.2.3', '1.2.2.9', true],
-      ['== 1.2.3', '1.2.3a0', true],
-      ['== 1.2.3', '1.2.3.0', false],
-      ['== 1.2.3', '1.2.3.1', false],
-      ['== 1.2.3', '1.2.4a0', false],
-
-      ['!= 1.2.3', '1.2.2.9', false],
-      ['!= 1.2.3', '1.2.3.0', false],
-      ['!= 1.2.3', '1.2.3.1', false],
-
-      ['< 1.0.0', '0.0.1', false],
-      ['< 1.0.0', '1.0.0', false],
-      ['< 1.0.0', '2.0.0', false],
-
-      ['<= 1.0.0', '0.0.1', false],
-      ['<= 1.0.0', '1.0.0', false],
-      ['<= 1.0.0', '2.0.0', false],
-
-      ['< 1.0.0, > 2.0.0', '0.0.1', true], // fixme (maybe)
-      ['< 1.0.0, > 2.0.0', '3.0.0', false],
-    ])(`%s\t%s\t%s`, (range, version, expected) => {
+      expect(res).toEqual(expected);
+    }
+  );
+
+  test.each`
+    version      | range                  | expected
+    ${'0.9.9.9'} | ${'>= 1.0.0, < 2.0.0'} | ${true}
+    ${'1.0.0a0'} | ${'>= 1.0.0, < 2.0.0'} | ${true}
+    ${'1.0.0.0'} | ${'> 1.0.0, < 2.0.0'}  | ${true}
+    ${'2.0.1.0'} | ${'> 1.0.0, < 2.0.0'}  | ${false}
+    ${'2.0.0.0'} | ${'> 1.0.0, < 2.0.0'}  | ${false}
+    ${'2.0.0a0'} | ${'> 1.0.0, < 2.0.0'}  | ${false}
+    ${'1.2.2.9'} | ${'== 1.2.3'}          | ${true}
+    ${'1.2.3a0'} | ${'== 1.2.3'}          | ${true}
+    ${'1.2.3.0'} | ${'== 1.2.3'}          | ${false}
+    ${'1.2.3.1'} | ${'== 1.2.3'}          | ${false}
+    ${'1.2.4a0'} | ${'== 1.2.3'}          | ${false}
+    ${'1.2.2.9'} | ${'!= 1.2.3'}          | ${false}
+    ${'1.2.3.0'} | ${'!= 1.2.3'}          | ${false}
+    ${'1.2.3.1'} | ${'!= 1.2.3'}          | ${false}
+    ${'0.0.1'}   | ${'< 1.0.0'}           | ${false}
+    ${'1.0.0'}   | ${'< 1.0.0'}           | ${false}
+    ${'2.0.0'}   | ${'< 1.0.0'}           | ${false}
+    ${'0.0.1'}   | ${'<= 1.0.0'}          | ${false}
+    ${'1.0.0'}   | ${'<= 1.0.0'}          | ${false}
+    ${'2.0.0'}   | ${'<= 1.0.0'}          | ${false}
+    ${'0.0.1'}   | ${'< 1.0.0, > 2.0.0'}  | ${true}
+    ${'3.0.0'}   | ${'< 1.0.0, > 2.0.0'}  | ${false}
+  `(
+    'isLessThanRange("$version", "$range") === "$expected"',
+    ({ version, range, expected }) => {
       expect(pep440.isLessThanRange(version, range)).toBe(expected);
-    });
-  });
+    }
+  );
 });
-- 
GitLab