Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: version test for equal pre-release versions #521

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/utils/__tests__/version.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@
});

test('can compare pre parts', () => {
const v1 = parseVersion('1.2.3-1')!;

Check warning on line 186 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-2')!;

Check warning on line 187 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(true);
});
Expand All @@ -195,6 +195,25 @@
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}` : ''
}`;
}
Loading