Skip to content

Commit

Permalink
fix: version test for equal pre-release versions
Browse files Browse the repository at this point in the history
  • Loading branch information
vaind committed Feb 27, 2024
1 parent 873eb62 commit 2613b71
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
19 changes: 19 additions & 0 deletions src/utils/__tests__/version.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,25 @@ describe('versionGreaterOrEqualThan', () => {
expect(() => versionGreaterOrEqualThan(v1, v2)).toThrowError();
expect(() => versionGreaterOrEqualThan(v2, v1)).toThrowError();
});

test('can compare pre parts that are the same', () => {
let v1 = parseVersion('1.2.3-dev.0')!;

Check warning on line 200 in src/utils/__tests__/version.test.ts

View workflow job for this annotation

GitHub Actions / Lint fixes

[@typescript-eslint/no-non-null-assertion] Forbidden non-null assertion.
let v2 = parseVersion('1.2.3-dev.0')!;

Check warning on line 201 in src/utils/__tests__/version.test.ts

View workflow job for this annotation

GitHub Actions / Lint fixes

[@typescript-eslint/no-non-null-assertion] Forbidden non-null assertion.
expect(versionGreaterOrEqualThan(v1, v2)).toBe(true);
expect(versionGreaterOrEqualThan(v2, v1)).toBe(true);

v1 = parseVersion('1.2.3-dev.0+A')!;

Check warning on line 205 in src/utils/__tests__/version.test.ts

View workflow job for this annotation

GitHub Actions / Lint fixes

[@typescript-eslint/no-non-null-assertion] Forbidden non-null assertion.
v2 = parseVersion('1.2.3-dev.0+A')!;

Check warning on line 206 in src/utils/__tests__/version.test.ts

View workflow job for this annotation

GitHub Actions / Lint fixes

[@typescript-eslint/no-non-null-assertion] Forbidden non-null assertion.
expect(versionGreaterOrEqualThan(v1, v2)).toBe(true);
expect(versionGreaterOrEqualThan(v2, v1)).toBe(true);
});

test('can compare pre parts that are the same but have different builds', () => {
const v1 = parseVersion('1.2.3-dev.0+buildA')!;

Check warning on line 212 in src/utils/__tests__/version.test.ts

View workflow job for this annotation

GitHub Actions / Lint fixes

[@typescript-eslint/no-non-null-assertion] Forbidden non-null assertion.
const v2 = parseVersion('1.2.3-dev.0+buildB')!;

Check warning on line 213 in src/utils/__tests__/version.test.ts

View workflow job for this annotation

GitHub Actions / Lint fixes

[@typescript-eslint/no-non-null-assertion] Forbidden non-null assertion.
expect(versionGreaterOrEqualThan(v1, v2)).toBe(false);
expect(versionGreaterOrEqualThan(v2, v1)).toBe(false);
});
});

describe('getPackage', () => {
Expand Down
7 changes: 4 additions & 3 deletions src/utils/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ export function versionGreaterOrEqualThan(v1: SemVer, v2: SemVer): boolean {
return true;
} else if (v1.pre && !v2.pre) {
return false;
} else if (v1.pre && v2.pre && v1.pre === v2.pre) {
return v1.build === v2.build;
} else if (v1.pre && v2.pre && v1.pre !== v2.pre && /^\d+$/.test(v1.pre) && /^\d+$/.test(v2.pre)) {
return v1.pre > v2.pre;
} else if (v1.build || v2.build || v1.pre || v2.pre) {
Expand Down Expand Up @@ -157,7 +159,6 @@ export function getPackageVersion(): string {
* Returns the stringified version of the passed SemVer object.
*/
export function semVerToString(s: SemVer) {
return `${s.major}.${s.minor}.${s.patch}${s.pre ? `-${s.pre}` : ''}${
s.build ? `+${s.build}` : ''
}`;
return `${s.major}.${s.minor}.${s.patch}${s.pre ? `-${s.pre}` : ''}${s.build ? `+${s.build}` : ''
}`;
}

0 comments on commit 2613b71

Please sign in to comment.