Skip to content

Commit 84e01a9

Browse files
hampustagerudarcanis
authored andcommitted
Always run postversion lifecycle method (#7154)
* Always run postversion lifecycle method Runs postversion even if no git commit is made. * Update CHANGELOG.md * Improve logic, add tests, update CHANGELOG.md * Update CHANGELOG.md * Update CHANGELOG.md
1 parent a3b1294 commit 84e01a9

File tree

5 files changed

+143
-30
lines changed

5 files changed

+143
-30
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ Please add one entry in this file for each change in Yarn's behavior. Use the sa
44

55
## Master
66

7+
- Fixes the `postversion` lifecycle method not being called when using `--no-git-tag-version`.
8+
9+
[#7154](https://github.com/yarnpkg/yarn/pull/7154) - [**Hampus Tågerud**](https://github.com/hampustagerud)
10+
711
- Ignores potentially large vscode keys in package.json to avoid E2BIG errors.
812

913
[#7419](https://github.com/yarnpkg/yarn/pull/7419) - [**Eric Amodio**](https://twitter.com/eamodio)
@@ -55,7 +59,7 @@ Please add one entry in this file for each change in Yarn's behavior. Use the sa
5559
- Exposes the script environment variables to `yarn create` spawned processes.
5660

5761
[#7127](https://github.com/yarnpkg/yarn/pull/7127) - [**Eli Perelman**](https://github.com/eliperelman)
58-
62+
5963
- Prevents EPIPE errors from being printed.
6064

6165
[#7194](https://github.com/yarnpkg/yarn/pull/7194) - [**Abhishek Reddy**](https://github.com/arbscht)

__tests__/commands/version.js

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,108 @@ test('run version and make sure commit hooks are disabled by config', async ():
162162
});
163163
});
164164

165+
test('run version with --no-git-tag-version and make sure git tags are disabled', async (): Promise<void> => {
166+
const fixture = 'no-args';
167+
await fs.mkdirp(path.join(fixturesLoc, fixture, '.git'));
168+
169+
return runRun([], {newVersion, gitTagVersion: false}, fixture, async (config): ?Promise<void> => {
170+
const pkg = await fs.readJson(path.join(config.cwd, 'package.json'));
171+
expect(pkg.version).toBe(newVersion);
172+
173+
expect(spawn.mock.calls.length).toBe(0);
174+
});
175+
});
176+
177+
test('run version and make sure git tags are disabled by config', async (): Promise<void> => {
178+
const fixture = 'no-args-no-git-tags';
179+
await fs.mkdirp(path.join(fixturesLoc, fixture, '.git'));
180+
181+
return runRun([], {newVersion, gitTagVersion}, fixture, async (config): ?Promise<void> => {
182+
const pkg = await fs.readJson(path.join(config.cwd, 'package.json'));
183+
expect(pkg.version).toBe(newVersion);
184+
185+
expect(spawn.mock.calls.length).toBe(0);
186+
});
187+
});
188+
189+
test('run version with --no-git-tag-version, make sure all lifecycle steps runs', async (): Promise<void> => {
190+
const fixture = 'no-args';
191+
await fs.mkdirp(path.join(fixturesLoc, fixture, '.git'));
192+
193+
return runRun([], {newVersion, gitTagVersion: false}, fixture, async (config): ?Promise<void> => {
194+
expect(spawn.mock.calls.length).toBe(0);
195+
196+
const pkg = await fs.readJson(path.join(config.cwd, 'package.json'));
197+
198+
const preversionLifecycle = {
199+
stage: 'preversion',
200+
config,
201+
cmd: pkg.scripts.preversion,
202+
cwd: config.cwd,
203+
isInteractive: true,
204+
};
205+
const versionLifecycle = {
206+
stage: 'version',
207+
config,
208+
cmd: pkg.scripts.version,
209+
cwd: config.cwd,
210+
isInteractive: true,
211+
};
212+
const postversionLifecycle = {
213+
stage: 'postversion',
214+
config,
215+
cmd: pkg.scripts.postversion,
216+
cwd: config.cwd,
217+
isInteractive: true,
218+
};
219+
220+
expect(execCommand.mock.calls.length).toBe(3);
221+
222+
expect(execCommand.mock.calls[0]).toEqual([preversionLifecycle]);
223+
expect(execCommand.mock.calls[1]).toEqual([versionLifecycle]);
224+
expect(execCommand.mock.calls[2]).toEqual([postversionLifecycle]);
225+
});
226+
});
227+
228+
test('run version with git tags disabled in config, make sure all lifecycle steps runs', async (): Promise<void> => {
229+
const fixture = 'no-args-no-git-tags';
230+
await fs.mkdirp(path.join(fixturesLoc, fixture, '.git'));
231+
232+
return runRun([], {newVersion, gitTagVersion}, fixture, async (config): ?Promise<void> => {
233+
expect(spawn.mock.calls.length).toBe(0);
234+
235+
const pkg = await fs.readJson(path.join(config.cwd, 'package.json'));
236+
237+
const preversionLifecycle = {
238+
stage: 'preversion',
239+
config,
240+
cmd: pkg.scripts.preversion,
241+
cwd: config.cwd,
242+
isInteractive: true,
243+
};
244+
const versionLifecycle = {
245+
stage: 'version',
246+
config,
247+
cmd: pkg.scripts.version,
248+
cwd: config.cwd,
249+
isInteractive: true,
250+
};
251+
const postversionLifecycle = {
252+
stage: 'postversion',
253+
config,
254+
cmd: pkg.scripts.postversion,
255+
cwd: config.cwd,
256+
isInteractive: true,
257+
};
258+
259+
expect(execCommand.mock.calls.length).toBe(3);
260+
261+
expect(execCommand.mock.calls[0]).toEqual([preversionLifecycle]);
262+
expect(execCommand.mock.calls[1]).toEqual([versionLifecycle]);
263+
expect(execCommand.mock.calls[2]).toEqual([postversionLifecycle]);
264+
});
265+
});
266+
165267
test('run version with --major flag and make sure major version is incremented', (): Promise<void> => {
166268
return runRun([], {gitTagVersion, major: true}, 'no-args', async (config): ?Promise<void> => {
167269
const pkg = await fs.readJson(path.join(config.cwd, 'package.json'));
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
version-git-tag false
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"version": "1.0.0",
3+
"license": "BSD-2-Clause",
4+
"scripts": {
5+
"preversion": "echo preversion",
6+
"version": "echo version",
7+
"postversion": "echo postversion"
8+
}
9+
}

src/cli/commands/version.js

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -170,44 +170,41 @@ export async function setVersion(
170170

171171
await runLifecycle('version');
172172

173-
// check if committing the new version to git is overriden
174-
if (!flags.gitTagVersion || !config.getOption('version-git-tag')) {
175-
// Don't tag the version in Git
176-
return () => Promise.resolve();
177-
}
178-
179173
return async function(): Promise<void> {
180174
invariant(newVersion, 'expected version');
181175

182-
// add git commit and tag
183-
let isGit = false;
184-
const parts = config.cwd.split(path.sep);
185-
while (parts.length) {
186-
isGit = await fs.exists(path.join(parts.join(path.sep), '.git'));
187-
if (isGit) {
188-
break;
189-
} else {
190-
parts.pop();
176+
// check if a new git tag should be created
177+
if (flags.gitTagVersion && config.getOption('version-git-tag')) {
178+
// add git commit and tag
179+
let isGit = false;
180+
const parts = config.cwd.split(path.sep);
181+
while (parts.length) {
182+
isGit = await fs.exists(path.join(parts.join(path.sep), '.git'));
183+
if (isGit) {
184+
break;
185+
} else {
186+
parts.pop();
187+
}
191188
}
192-
}
193189

194-
if (isGit) {
195-
const message = (flags.message || String(config.getOption('version-git-message'))).replace(/%s/g, newVersion);
196-
const sign: boolean = Boolean(config.getOption('version-sign-git-tag'));
197-
const flag = sign ? '-sm' : '-am';
198-
const prefix: string = String(config.getOption('version-tag-prefix'));
199-
const args: Array<string> = ['commit', '-m', message, ...(isCommitHooksDisabled() ? ['-n'] : [])];
190+
if (isGit) {
191+
const message = (flags.message || String(config.getOption('version-git-message'))).replace(/%s/g, newVersion);
192+
const sign: boolean = Boolean(config.getOption('version-sign-git-tag'));
193+
const flag = sign ? '-sm' : '-am';
194+
const prefix: string = String(config.getOption('version-tag-prefix'));
195+
const args: Array<string> = ['commit', '-m', message, ...(isCommitHooksDisabled() ? ['-n'] : [])];
200196

201-
const gitRoot = (await spawnGit(['rev-parse', '--show-toplevel'], {cwd: config.cwd})).trim();
197+
const gitRoot = (await spawnGit(['rev-parse', '--show-toplevel'], {cwd: config.cwd})).trim();
202198

203-
// add manifest
204-
await spawnGit(['add', path.relative(gitRoot, pkgLoc)], {cwd: gitRoot});
199+
// add manifest
200+
await spawnGit(['add', path.relative(gitRoot, pkgLoc)], {cwd: gitRoot});
205201

206-
// create git commit
207-
await spawnGit(args, {cwd: gitRoot});
202+
// create git commit
203+
await spawnGit(args, {cwd: gitRoot});
208204

209-
// create git tag
210-
await spawnGit(['tag', `${prefix}${newVersion}`, flag, message], {cwd: gitRoot});
205+
// create git tag
206+
await spawnGit(['tag', `${prefix}${newVersion}`, flag, message], {cwd: gitRoot});
207+
}
211208
}
212209

213210
await runLifecycle('postversion');

0 commit comments

Comments
 (0)