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

Always run postversion lifecycle method #7154

Merged
merged 9 commits into from Jul 24, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion CHANGELOG.md
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
102 changes: 102 additions & 0 deletions __tests__/commands/version.js
Expand Up @@ -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<void> => {
const fixture = 'no-args';
await fs.mkdirp(path.join(fixturesLoc, fixture, '.git'));

return runRun([], {newVersion, gitTagVersion: false}, fixture, async (config): ?Promise<void> => {
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<void> => {
const fixture = 'no-args-no-git-tags';
await fs.mkdirp(path.join(fixturesLoc, fixture, '.git'));

return runRun([], {newVersion, gitTagVersion}, fixture, async (config): ?Promise<void> => {
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<void> => {
const fixture = 'no-args';
await fs.mkdirp(path.join(fixturesLoc, fixture, '.git'));

return runRun([], {newVersion, gitTagVersion: false}, fixture, async (config): ?Promise<void> => {
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<void> => {
const fixture = 'no-args-no-git-tags';
await fs.mkdirp(path.join(fixturesLoc, fixture, '.git'));

return runRun([], {newVersion, gitTagVersion}, fixture, async (config): ?Promise<void> => {
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<void> => {
return runRun([], {gitTagVersion, major: true}, 'no-args', async (config): ?Promise<void> => {
const pkg = await fs.readJson(path.join(config.cwd, 'package.json'));
Expand Down
1 change: 1 addition & 0 deletions __tests__/fixtures/version/no-args-no-git-tags/.yarnrc
@@ -0,0 +1 @@
version-git-tag false
9 changes: 9 additions & 0 deletions __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"
}
}
55 changes: 26 additions & 29 deletions src/cli/commands/version.js
Expand Up @@ -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<void> {
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<string> = ['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<string> = ['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');
Expand Down