Skip to content

Commit a278f52

Browse files
authored
Fix behavior for yarn version pre* to match the docs (#6879)
## What's the problem this PR addresses? <!-- Describe the rationale of your PR. --> <!-- Link all issues that it closes. (Closes/Resolves #xxxx.) --> Resolves #5902 The behavior for `yarn version prepatch/preminor/premajor` and `yarn version prerelease` did not match [the documentation](https://yarnpkg.com/cli/version). Respectively, `preminor` errored, and `prerelease` did not bump the prerelease number if used more than once. ... ## How did you fix it? <!-- A detailed description of your implementation. --> - `prepatch/preminor/premajor` were added to the accepted CLI arguments - In case of `prerelease` on an already pre-release version, the semver bump was adapted to bump the current prerelease version instead of the `stableVersion` ... ## Checklist <!--- Don't worry if you miss something, chores are automatically tested. --> <!--- This checklist exists to help you remember doing the chores when you submit a PR. --> <!--- Put an `x` in all the boxes that apply. --> - [x] I have read the [Contributing Guide](https://yarnpkg.com/advanced/contributing). <!-- See https://yarnpkg.com/advanced/contributing#preparing-your-pr-to-be-released for more details. --> <!-- Check with `yarn version check` and fix with `yarn version check -i` --> - [x] I have set the packages that need to be released for my changes to be effective. <!-- The "Testing chores" workflow validates that your PR follows our guidelines. --> <!-- If it doesn't pass, click on it to see details as to what your PR might be missing. --> - [x] I will check that all automated PR checks pass before the PR gets reviewed.
1 parent b553533 commit a278f52

File tree

4 files changed

+75
-7
lines changed

4 files changed

+75
-7
lines changed

.yarn/versions/59b2c8b4.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
releases:
2+
"@yarnpkg/cli": patch
3+
"@yarnpkg/plugin-version": minor
4+
5+
declined:
6+
- "@yarnpkg/plugin-compat"
7+
- "@yarnpkg/plugin-constraints"
8+
- "@yarnpkg/plugin-dlx"
9+
- "@yarnpkg/plugin-essentials"
10+
- "@yarnpkg/plugin-init"
11+
- "@yarnpkg/plugin-interactive-tools"
12+
- "@yarnpkg/plugin-nm"
13+
- "@yarnpkg/plugin-npm-cli"
14+
- "@yarnpkg/plugin-pack"
15+
- "@yarnpkg/plugin-patch"
16+
- "@yarnpkg/plugin-pnp"
17+
- "@yarnpkg/plugin-pnpm"
18+
- "@yarnpkg/plugin-stage"
19+
- "@yarnpkg/plugin-typescript"
20+
- "@yarnpkg/plugin-workspace-tools"
21+
- "@yarnpkg/builder"
22+
- "@yarnpkg/core"
23+
- "@yarnpkg/doctor"

packages/acceptance-tests/pkg-tests-specs/sources/commands/version.test.ts

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ describe(`Commands`, () => {
55
test(
66
`it shouldn't work if the strategy isn't semver and there is no prior version`,
77
makeTemporaryEnv({}, async ({path, run, source}) => {
8-
await expect(run(`version`, `patch`)).rejects.toThrow();
8+
await expect(run(`version`, `patch`)).rejects.toThrow(`Usage Error: Can't bump the version if there wasn't a version to begin with - use 0.0.0 as initial version then run the command again.`);
99
}),
1010
);
1111

@@ -15,7 +15,7 @@ describe(`Commands`, () => {
1515
version: `1.0.0`,
1616
}, async ({path, run, source}) => {
1717
await run(`version`, `1.1.0`, `--deferred`);
18-
await expect(run(`version`, `1.0.1`)).rejects.toThrow();
18+
await expect(run(`version`, `1.0.1`)).rejects.toThrow(`Usage Error: Can't bump the version to one that would be lower than the current deferred one (1.1.0)`);
1919
}),
2020
);
2121

@@ -25,7 +25,7 @@ describe(`Commands`, () => {
2525
version: `1.0.0`,
2626
}, async ({path, run, source}) => {
2727
await run(`version`, `1.1.0`, `--deferred`);
28-
await expect(run(`version`, `patch`)).rejects.toThrow();
28+
await expect(run(`version`, `patch`)).rejects.toThrow(`Usage Error: Can't bump the version to one that would be lower than the current deferred one (1.1.0)`);
2929
}),
3030
);
3131

@@ -99,7 +99,33 @@ describe(`Commands`, () => {
9999
);
100100

101101
test(
102-
`it shouldn't immediatly increase the version number for a workspace when using --deferred`,
102+
`it should bump then append a prerelease version number to a release version`,
103+
makeTemporaryEnv({
104+
version: `1.2.3`,
105+
}, async ({path, run, source}) => {
106+
await run(`version`, `prerelease`);
107+
108+
await expect(xfs.readJsonPromise(`${path}/package.json` as PortablePath)).resolves.toMatchObject({
109+
version: `1.2.4-0`,
110+
});
111+
}),
112+
);
113+
114+
test(
115+
`it should bump the prerelease version number on a prerelease version`,
116+
makeTemporaryEnv({
117+
version: `11.22.33-9`,
118+
}, async ({path, run, source}) => {
119+
await run(`version`, `prerelease`);
120+
121+
await expect(xfs.readJsonPromise(`${path}/package.json` as PortablePath)).resolves.toMatchObject({
122+
version: `11.22.33-10`,
123+
});
124+
}),
125+
);
126+
127+
test(
128+
`it shouldn't immediately increase the version number for a workspace when using --deferred`,
103129
makeTemporaryEnv({
104130
version: `0.0.0`,
105131
}, async ({path, run, source}) => {
@@ -118,7 +144,7 @@ describe(`Commands`, () => {
118144
);
119145

120146
test(
121-
`it shouldn't immediatly increase the version number for a workspace when using preferDeferredVersions`,
147+
`it shouldn't immediately increase the version number for a workspace when using preferDeferredVersions`,
122148
makeTemporaryEnv({
123149
version: `0.0.0`,
124150
}, {
@@ -139,7 +165,7 @@ describe(`Commands`, () => {
139165
);
140166

141167
test(
142-
`it should immediatly increase the version number for a workspace when using --immediate, even if preferDeferredVersions is set`,
168+
`it should immediately increase the version number for a workspace when using --immediate, even if preferDeferredVersions is set`,
143169
makeTemporaryEnv({
144170
version: `0.0.0`,
145171
}, {

packages/plugin-version/sources/versionUtils.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,16 @@ describe(`versionUtils`, () => {
1414
expect(versionUtils.applyPrerelease(`1.3.0`, {current: `1.2.3-rc.41`, prerelease: `rc.%n`})).toEqual(`1.3.0-rc.1`);
1515
});
1616
});
17+
18+
describe(`applyStrategy`, () => {
19+
it(`should increase the prerelease number with prerelease strategy`, () => {
20+
expect(versionUtils.applyStrategy(`1.2.3-0`, `prerelease`)).toEqual(`1.2.3-1`);
21+
});
22+
23+
it(`should apply prepatch, preminor, premajor strategies`, () => {
24+
expect(versionUtils.applyStrategy(`1.2.3`, `prepatch`)).toEqual(`1.2.4-0`);
25+
expect(versionUtils.applyStrategy(`1.2.3`, `preminor`)).toEqual(`1.3.0-0`);
26+
expect(versionUtils.applyStrategy(`1.2.3`, `premajor`)).toEqual(`2.0.0-0`);
27+
});
28+
});
1729
});

packages/plugin-version/sources/versionUtils.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ export enum Decision {
1515
MAJOR = `major`,
1616
MINOR = `minor`,
1717
PATCH = `patch`,
18+
PREMAJOR = `premajor`,
19+
PREMINOR = `preminor`,
20+
PREPATCH = `prepatch`,
1821
PRERELEASE = `prerelease`,
1922
}
2023

@@ -88,7 +91,11 @@ export async function resolveVersionFiles(project: Project, {prerelease = null}:
8891
const baseVersion = workspace.manifest.raw.stableVersion ?? workspace.manifest.version;
8992

9093
const candidateRelease = candidateReleases.get(workspace);
91-
const suggestedRelease = applyStrategy(baseVersion, validateReleaseDecision(decision));
94+
// In case of prerelease decision with a prerelease version, the trailing number should be bumped
95+
const suggestedRelease = Decision.PRERELEASE === decision ?
96+
applyStrategy(workspace.manifest.version, validateReleaseDecision(decision)) :
97+
applyStrategy(baseVersion, validateReleaseDecision(decision));
98+
9299

93100
if (suggestedRelease === null)
94101
throw new Error(`Assertion failed: Expected ${baseVersion} to support being bumped via strategy ${decision}`);

0 commit comments

Comments
 (0)