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(test-version-utils): Fix version calculation to account for legacy breaking minor releases #23603

Merged
merged 8 commits into from
Jan 24, 2025
Merged
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
15 changes: 11 additions & 4 deletions packages/test/test-version-utils/src/test/versionUtils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,21 @@ describe("versionUtils", () => {
const adjustPublicMajor = false;
createTest("1.0.0", -1, adjustPublicMajor, "^0.59.0");
createTest("1.0.0", -2, adjustPublicMajor, "^0.58.0");
createTest("2.0.0", -1, adjustPublicMajor, "^2.0.0-rc.4.0.0");
createTest("2.3.5", -1, adjustPublicMajor, "^2.0.0-rc.4.0.0");
createTest("2.0.0", -1, adjustPublicMajor, "^2.0.0-rc.5.0.0");
createTest("2.3.5", -1, adjustPublicMajor, "~2.0.0-rc.5.0.0");
createTest("2.10.0", -1, adjustPublicMajor, "~2.5.0");
createTest("2.10.0", -2, adjustPublicMajor, "^2.0.0-rc.5.0.0");
createTest("2.13.2", -1, adjustPublicMajor, "~2.5.0");
createTest("2.20.0", -1, adjustPublicMajor, "~2.13.0");
createTest("2.20.0", -2, adjustPublicMajor, "~2.5.0");
createTest("2.20.0", -3, adjustPublicMajor, "^2.0.0-rc.5.0.0");
});

describe("bumping public releases (adjustPublicMajor = true)", () => {
const adjustPublicMajor = true;
createTest("2.0.0", -1, adjustPublicMajor, "^1.0.0");
createTest("2.3.5", -1, adjustPublicMajor, "^1.0.0");
createTest("2.13.5", -1, adjustPublicMajor, "^1.0.0");
});

describe("bumping internal releases to public releases (adjustPublicMajor = false)", () => {
Expand Down Expand Up @@ -127,8 +134,8 @@ describe("versionUtils", () => {
createTest("2.0.0-rc.1.3.4", -2, adjustPublicMajor, "^2.0.0-internal.7.0.0");

// These tests should be enabled once 2.0.0-rc.1.0.0 is released (currently throws trying to fetch the unreleased packages)
// createTest("2.0.0-rc.2.0.0", -1, adjustPublicMajor, "^2.0.0-rc.1.0.0");
// createTest("2.0.0-rc.2.0.0", -2, adjustPublicMajor, "^2.0.0-internal.8.0.0");
createTest("2.0.0-rc.2.0.0", -1, adjustPublicMajor, "^2.0.0-rc.1.0.0");
createTest("2.0.0-rc.2.0.0", -2, adjustPublicMajor, "^2.0.0-internal.8.0.0");
});

it("error cases for malformed versions", () => {
Expand Down
36 changes: 31 additions & 5 deletions packages/test/test-version-utils/src/versionUtils.ts
scottn12 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -438,13 +438,39 @@ function calculateRequestedRange(
}

// If the base version is a public version and `adjustPublicMajor` is false, then we need to ensure that we
// calculate N-1 as he previous major release, regardless if it is public or internal.
// Currently, this case only applies to calculating N-X for 2.0.0.
// calculate N-1 as the previous major release, regardless if it is public or internal.
// Currently, this case only applies to calculating N-X for 2.x.y.
// TODO: This is a temporary solution and we need to entirely rewrite this function to handle the changes the version schemas. See ADO:8198.
if (adjustPublicMajor === false && version.major > 1) {
// In this case, we can pretend that 2.0 is RC6 and calculate the range as if it were an internal version.
const internalSchemeRange = internalSchema("2.0.0", "6.0.0", "rc", requested);
return internalSchemeRange;
if (version.minor < 10) {
// If 2.0 <= N < 2.10, then we can pretend that N is RC6 (*which doesn't exist*) and calculate the range as if it were an internal version.
const internalSchemeRange = internalSchema("2.0.0", "6.0.0", "rc", requested);
return internalSchemeRange;
} else {
// For each requested version to go back, we go back 10 minor versions. If requested is -2, then we need to go back 20 minor versions.
const legacyMinorsToSkip = Math.abs(requested * 10);
if (legacyMinorsToSkip > version.minor) {
// If the number of minors we need to go back is greater than the minor version, then that means we will be going back to RC releases.
// Here we calculate how many more releases we need to go back **after** we take into account going from the current minor version to 2.0.
// For example, if N is 2.20, then the range we need to return for N-1 starts at 2.10, for N-2 it starts at 2.0, N-3 is RC5, N-4 is RC4, etc.
// So if N is 2.20 and requested is 4, then we still need to go back 2 more releases from 2.0 (treated as RC6).
const remainingRequested =
(legacyMinorsToSkip - Math.floor(version.minor / 10) * 10) / 10;
const internalSchemeRange = internalSchema(
"2.0.0",
"6.0.0",
"rc",
remainingRequested * -1, // make sure the value is negative since we made it positive above
);
return internalSchemeRange;
}
// Here we know that the requested version will be >=2.0, so we can avoid all the RC releases.
// If N >= 2.10, then the range we need to return for N-1 starts at legacy breaking minor before the one N belongs to.
const lowerMinorRange = Math.floor((version.minor - legacyMinorsToSkip) / 10) * 10;
const upperMinorRange = lowerMinorRange + 10;
// Here we do a range that, when resolved, will result in the latest minor version that satisfies the request.
return `>=${version.major}.${lowerMinorRange}.0-0 <${version.major}.${upperMinorRange}.0-0`;
}
} else {
// calculate requested major version number
const requestedMajorVersion = version.major + requested;
Expand Down
Loading