From 84e01a97781f903f326e6ca5d7f48935cea74142 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hampus=20T=C3=A5gerud?= Date: Wed, 24 Jul 2019 14:44:04 +0200 Subject: [PATCH] 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 --- CHANGELOG.md | 6 +- __tests__/commands/version.js | 102 ++++++++++++++++++ .../version/no-args-no-git-tags/.yarnrc | 1 + .../version/no-args-no-git-tags/package.json | 9 ++ src/cli/commands/version.js | 55 +++++----- 5 files changed, 143 insertions(+), 30 deletions(-) create mode 100644 __tests__/fixtures/version/no-args-no-git-tags/.yarnrc create mode 100644 __tests__/fixtures/version/no-args-no-git-tags/package.json diff --git a/CHANGELOG.md b/CHANGELOG.md index 806b31d9de..ab65e6ba3c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ Please add one entry in this file for each change in Yarn's behavior. Use the sa ## Master +- Fixes the `postversion` lifecycle method not being called when using `--no-git-tag-version`. + + [#7154](https://github.com/yarnpkg/yarn/pull/7154) - [**Hampus Tågerud**](https://github.com/hampustagerud) + - Ignores potentially large vscode keys in package.json to avoid E2BIG errors. [#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 - Exposes the script environment variables to `yarn create` spawned processes. [#7127](https://github.com/yarnpkg/yarn/pull/7127) - [**Eli Perelman**](https://github.com/eliperelman) - + - Prevents EPIPE errors from being printed. [#7194](https://github.com/yarnpkg/yarn/pull/7194) - [**Abhishek Reddy**](https://github.com/arbscht) diff --git a/__tests__/commands/version.js b/__tests__/commands/version.js index 6210a40a5e..50c1111b2b 100644 --- a/__tests__/commands/version.js +++ b/__tests__/commands/version.js @@ -162,6 +162,108 @@ test('run version and make sure commit hooks are disabled by config', async (): }); }); +test('run version with --no-git-tag-version and make sure git tags are disabled', async (): Promise => { + const fixture = 'no-args'; + await fs.mkdirp(path.join(fixturesLoc, fixture, '.git')); + + return runRun([], {newVersion, gitTagVersion: false}, fixture, async (config): ?Promise => { + const pkg = await fs.readJson(path.join(config.cwd, 'package.json')); + expect(pkg.version).toBe(newVersion); + + expect(spawn.mock.calls.length).toBe(0); + }); +}); + +test('run version and make sure git tags are disabled by config', async (): Promise => { + const fixture = 'no-args-no-git-tags'; + await fs.mkdirp(path.join(fixturesLoc, fixture, '.git')); + + return runRun([], {newVersion, gitTagVersion}, fixture, async (config): ?Promise => { + const pkg = await fs.readJson(path.join(config.cwd, 'package.json')); + expect(pkg.version).toBe(newVersion); + + expect(spawn.mock.calls.length).toBe(0); + }); +}); + +test('run version with --no-git-tag-version, make sure all lifecycle steps runs', async (): Promise => { + const fixture = 'no-args'; + await fs.mkdirp(path.join(fixturesLoc, fixture, '.git')); + + return runRun([], {newVersion, gitTagVersion: false}, fixture, async (config): ?Promise => { + expect(spawn.mock.calls.length).toBe(0); + + const pkg = await fs.readJson(path.join(config.cwd, 'package.json')); + + const preversionLifecycle = { + stage: 'preversion', + config, + cmd: pkg.scripts.preversion, + cwd: config.cwd, + isInteractive: true, + }; + const versionLifecycle = { + stage: 'version', + config, + cmd: pkg.scripts.version, + cwd: config.cwd, + isInteractive: true, + }; + const postversionLifecycle = { + stage: 'postversion', + config, + cmd: pkg.scripts.postversion, + cwd: config.cwd, + isInteractive: true, + }; + + expect(execCommand.mock.calls.length).toBe(3); + + expect(execCommand.mock.calls[0]).toEqual([preversionLifecycle]); + expect(execCommand.mock.calls[1]).toEqual([versionLifecycle]); + expect(execCommand.mock.calls[2]).toEqual([postversionLifecycle]); + }); +}); + +test('run version with git tags disabled in config, make sure all lifecycle steps runs', async (): Promise => { + const fixture = 'no-args-no-git-tags'; + await fs.mkdirp(path.join(fixturesLoc, fixture, '.git')); + + return runRun([], {newVersion, gitTagVersion}, fixture, async (config): ?Promise => { + expect(spawn.mock.calls.length).toBe(0); + + const pkg = await fs.readJson(path.join(config.cwd, 'package.json')); + + const preversionLifecycle = { + stage: 'preversion', + config, + cmd: pkg.scripts.preversion, + cwd: config.cwd, + isInteractive: true, + }; + const versionLifecycle = { + stage: 'version', + config, + cmd: pkg.scripts.version, + cwd: config.cwd, + isInteractive: true, + }; + const postversionLifecycle = { + stage: 'postversion', + config, + cmd: pkg.scripts.postversion, + cwd: config.cwd, + isInteractive: true, + }; + + expect(execCommand.mock.calls.length).toBe(3); + + expect(execCommand.mock.calls[0]).toEqual([preversionLifecycle]); + expect(execCommand.mock.calls[1]).toEqual([versionLifecycle]); + expect(execCommand.mock.calls[2]).toEqual([postversionLifecycle]); + }); +}); + test('run version with --major flag and make sure major version is incremented', (): Promise => { return runRun([], {gitTagVersion, major: true}, 'no-args', async (config): ?Promise => { const pkg = await fs.readJson(path.join(config.cwd, 'package.json')); diff --git a/__tests__/fixtures/version/no-args-no-git-tags/.yarnrc b/__tests__/fixtures/version/no-args-no-git-tags/.yarnrc new file mode 100644 index 0000000000..5cb9a09b64 --- /dev/null +++ b/__tests__/fixtures/version/no-args-no-git-tags/.yarnrc @@ -0,0 +1 @@ +version-git-tag false diff --git a/__tests__/fixtures/version/no-args-no-git-tags/package.json b/__tests__/fixtures/version/no-args-no-git-tags/package.json new file mode 100644 index 0000000000..c8c699e438 --- /dev/null +++ b/__tests__/fixtures/version/no-args-no-git-tags/package.json @@ -0,0 +1,9 @@ +{ + "version": "1.0.0", + "license": "BSD-2-Clause", + "scripts": { + "preversion": "echo preversion", + "version": "echo version", + "postversion": "echo postversion" + } +} diff --git a/src/cli/commands/version.js b/src/cli/commands/version.js index eb77c696de..f7ade77bf7 100644 --- a/src/cli/commands/version.js +++ b/src/cli/commands/version.js @@ -170,44 +170,41 @@ export async function setVersion( await runLifecycle('version'); - // check if committing the new version to git is overriden - if (!flags.gitTagVersion || !config.getOption('version-git-tag')) { - // Don't tag the version in Git - return () => Promise.resolve(); - } - return async function(): Promise { invariant(newVersion, 'expected version'); - // add git commit and tag - let isGit = false; - const parts = config.cwd.split(path.sep); - while (parts.length) { - isGit = await fs.exists(path.join(parts.join(path.sep), '.git')); - if (isGit) { - break; - } else { - parts.pop(); + // check if a new git tag should be created + if (flags.gitTagVersion && config.getOption('version-git-tag')) { + // add git commit and tag + let isGit = false; + const parts = config.cwd.split(path.sep); + while (parts.length) { + isGit = await fs.exists(path.join(parts.join(path.sep), '.git')); + if (isGit) { + break; + } else { + parts.pop(); + } } - } - if (isGit) { - const message = (flags.message || String(config.getOption('version-git-message'))).replace(/%s/g, newVersion); - const sign: boolean = Boolean(config.getOption('version-sign-git-tag')); - const flag = sign ? '-sm' : '-am'; - const prefix: string = String(config.getOption('version-tag-prefix')); - const args: Array = ['commit', '-m', message, ...(isCommitHooksDisabled() ? ['-n'] : [])]; + if (isGit) { + const message = (flags.message || String(config.getOption('version-git-message'))).replace(/%s/g, newVersion); + const sign: boolean = Boolean(config.getOption('version-sign-git-tag')); + const flag = sign ? '-sm' : '-am'; + const prefix: string = String(config.getOption('version-tag-prefix')); + const args: Array = ['commit', '-m', message, ...(isCommitHooksDisabled() ? ['-n'] : [])]; - const gitRoot = (await spawnGit(['rev-parse', '--show-toplevel'], {cwd: config.cwd})).trim(); + const gitRoot = (await spawnGit(['rev-parse', '--show-toplevel'], {cwd: config.cwd})).trim(); - // add manifest - await spawnGit(['add', path.relative(gitRoot, pkgLoc)], {cwd: gitRoot}); + // add manifest + await spawnGit(['add', path.relative(gitRoot, pkgLoc)], {cwd: gitRoot}); - // create git commit - await spawnGit(args, {cwd: gitRoot}); + // create git commit + await spawnGit(args, {cwd: gitRoot}); - // create git tag - await spawnGit(['tag', `${prefix}${newVersion}`, flag, message], {cwd: gitRoot}); + // create git tag + await spawnGit(['tag', `${prefix}${newVersion}`, flag, message], {cwd: gitRoot}); + } } await runLifecycle('postversion');