Skip to content

Commit

Permalink
fix(collect-updates): Avoid improper bumps from prompt selections
Browse files Browse the repository at this point in the history
Fixes #1357

Huge thanks to all the contributors on that issue, especially @Adam13531
for their exceptionally detailed reproduction and @randy-askin for their
clear (and patient!) identification of the ternary bug.
  • Loading branch information
evocateur committed Jan 9, 2019
1 parent 5ca8a2b commit 06a1cff
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 4 deletions.
73 changes: 73 additions & 0 deletions commands/version/__tests__/version-bump-prerelease.test.js
Expand Up @@ -13,13 +13,21 @@ jest.mock("../lib/remote-branch-exists");
const fs = require("fs-extra");
const path = require("path");

// mocked modules
const prompt = require("@lerna/prompt");

// helpers
const initFixture = require("@lerna-test/init-fixture")(path.resolve(__dirname, "../../publish/__tests__"));
const showCommit = require("@lerna-test/show-commit");
const gitInit = require("@lerna-test/git-init");
const gitAdd = require("@lerna-test/git-add");
const gitTag = require("@lerna-test/git-tag");
const gitCommit = require("@lerna-test/git-commit");
const getCommitMessage = require("@lerna-test/get-commit-message");
const Tacks = require("tacks");
const tempy = require("tempy");

const { File, Dir } = Tacks;

// test command
const lernaVersion = require("@lerna-test/command-runner")(require("../command"));
Expand Down Expand Up @@ -107,3 +115,68 @@ test("version prerelease with immediate graduation", async () => {
const secondDiff = await showCommit(testDir);
expect(secondDiff).toMatchSnapshot();
});

test("independent version prerelease does not bump on every unrelated change", async () => {
const cwd = tempy.directory();
const fixture = new Tacks(
Dir({
"lerna.json": File({
version: "independent",
}),
"package.json": File({
name: "unrelated-bumps",
}),
packages: Dir({
"pkg-a": Dir({
"package.json": File({
name: "pkg-a",
version: "1.0.0",
}),
}),
"pkg-b": Dir({
"package.json": File({
name: "pkg-b",
version: "1.0.0-bumps.1",
}),
}),
}),
})
);

fixture.create(cwd);

await gitInit(cwd, ".");
await gitAdd(cwd, "-A");
await gitCommit(cwd, "init");

// simulate choices for pkg-a then pkg-b
prompt.mockChoices("patch", "PRERELEASE");
prompt.input.mockImplementationOnce((msg, cfg) =>
// a convoluted way of entering "bumps" at the prompt
Promise.resolve(cfg.filter("bumps"))
);

await lernaVersion(cwd)();

const first = await getCommitMessage(cwd);
expect(first).toMatchInlineSnapshot(`
Publish
- pkg-a@1.0.1
- pkg-b@1.0.0-bumps.2
`);

await fs.outputFile(path.join(cwd, "packages/pkg-a/hello.js"), "world");
await gitAdd(cwd, ".");
await gitCommit(cwd, "feat: hello world");

// all of this just to say...
await lernaVersion(cwd)();

const second = await getCommitMessage(cwd);
expect(second).toMatchInlineSnapshot(`
Publish
- pkg-a@1.0.2
`);
});
9 changes: 5 additions & 4 deletions utils/collect-updates/collect-updates.js
Expand Up @@ -56,10 +56,11 @@ function collectUpdates(filteredPackages, packageGraph, execOpts, commandOptions
candidates = new Set();

const hasDiff = makeDiffPredicate(committish, execOpts, commandOptions.ignoreChanges);
const needsBump = (commandOptions.bump || "").startsWith("pre")
? () => false
: /* skip packages that have not been previously prereleased */
node => node.prereleaseId;
const needsBump =
!commandOptions.bump || commandOptions.bump.startsWith("pre")
? () => false
: /* skip packages that have not been previously prereleased */
node => node.prereleaseId;

packages.forEach((node, name) => {
if (forced.has(name) || needsBump(node) || hasDiff(node)) {
Expand Down

0 comments on commit 06a1cff

Please sign in to comment.