Skip to content

Commit

Permalink
test(publish): Avoid npm login-required validation in CI
Browse files Browse the repository at this point in the history
  • Loading branch information
evocateur committed Aug 9, 2018
1 parent 8d80b2c commit 012afcc
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 18 deletions.
15 changes: 12 additions & 3 deletions commands/publish/index.js
Expand Up @@ -238,9 +238,18 @@ class PublishCommand extends Command {
}

prepareRegistryActions() {
return Promise.resolve()
.then(() => verifyNpmRegistry(this.project.rootPath, this.npmConfig))
.then(() => verifyNpmPackageAccess(this.packagesToPublish, this.project.rootPath, this.npmConfig));
let chain = Promise.resolve();

chain = chain.then(() => verifyNpmRegistry(this.project.rootPath, this.npmConfig));

/* istanbul ignore else */
if (process.env.LERNA_INTEGRATION !== "SKIP") {
chain = chain.then(() =>
verifyNpmPackageAccess(this.packagesToPublish, this.project.rootPath, this.npmConfig)
);
}

return chain;
}

updateCanaryVersions() {
Expand Down
48 changes: 33 additions & 15 deletions integration/lerna-publish.test.js
Expand Up @@ -38,16 +38,20 @@ async function commitChangeToPackage(cwd, packageName, commitMsg, data) {
}

describe("lerna publish", () => {
// never actually upload when calling `npm install`
const dryRun = { npm_config_dry_run: true };
const env = {
// never actually upload when calling `npm install`
npm_config_dry_run: true,
// skip npm package validation, none of the stubs are real
LERNA_INTEGRATION: "SKIP",
};

test("exit 0 when no updates", async () => {
const { cwd } = await cloneFixture("normal");
const args = ["publish"];

await gitTag(cwd, "v1.0.0");

const { code, stdout } = await cliRunner(cwd, dryRun)(...args);
const { code, stdout } = await cliRunner(cwd, env)(...args);

expect(code).toBe(0);
expect(stdout).toBe("");
Expand All @@ -58,7 +62,7 @@ describe("lerna publish", () => {
const args = ["publish", "--yes", "--scope", "package-1"];

try {
await cliRunner(cwd, dryRun)(...args);
await cliRunner(cwd, env)(...args);
} catch (err) {
expect(err.code).toBe(1);
expect(err.stderr).toMatch("Unknown argument: scope");
Expand All @@ -67,11 +71,25 @@ describe("lerna publish", () => {
expect.assertions(2);
});

test("exits with error when package access validation fails", async () => {
const { cwd } = await cloneFixture("normal");
const args = ["publish", "prerelease", "--yes"];

try {
await cliRunner(cwd, { LERNA_INTEGRATION: "ALLOW" })(...args);
} catch (err) {
expect(err.code).toBe(1);
expect(err.stderr).toMatch("ENEEDAUTH");
}

expect.assertions(2);
});

test("updates fixed versions", async () => {
const { cwd } = await cloneFixture("normal");
const args = ["publish", "patch", "--yes"];

const { stdout } = await cliRunner(cwd, dryRun)(...args);
const { stdout } = await cliRunner(cwd, env)(...args);
expect(stdout).toMatchSnapshot("stdout");

const [allPackageJsons, commitMessage] = await Promise.all([loadManifests(cwd), lastCommitMessage(cwd)]);
Expand All @@ -87,7 +105,7 @@ describe("lerna publish", () => {
await gitTag(cwd, "v1.0.0");
await commitChangeToPackage(cwd, "package-1", "change", { change: true });

await cliRunner(cwd, dryRun)(...args);
await cliRunner(cwd, env)(...args);

expect(await loadManifests(cwd)).toMatchSnapshot();
});
Expand All @@ -99,15 +117,15 @@ describe("lerna publish", () => {
await gitTag(cwd, "v1.0.0");
await commitChangeToPackage(cwd, "package-1", "change", { change: true });

const { stdout } = await cliRunner(cwd, dryRun)(...args);
const { stdout } = await cliRunner(cwd, env)(...args);
expect(stdout).toMatchSnapshot("stdout");
});

test("updates independent versions", async () => {
const { cwd } = await cloneFixture("independent");
const args = ["publish", "major", "--yes"];

const { stdout } = await cliRunner(cwd, dryRun)(...args);
const { stdout } = await cliRunner(cwd, env)(...args);
expect(stdout).toMatchSnapshot("stdout");

const [allPackageJsons, commitMessage] = await Promise.all([loadManifests(cwd), lastCommitMessage(cwd)]);
Expand All @@ -131,7 +149,7 @@ describe("lerna publish", () => {
{ baz: true }
);

const { stdout } = await cliRunner(cwd, dryRun)(...args);
const { stdout } = await cliRunner(cwd, env)(...args);
expect(stdout).toMatchSnapshot();

const changelogFilePaths = await globby(["**/CHANGELOG.md"], {
Expand All @@ -147,13 +165,13 @@ describe("lerna publish", () => {
})
);

it("replaces file: specifier with local version before npm publish but after git commit", async () => {
test("replaces file: specifier with local version before npm publish but after git commit", async () => {
const { cwd } = await cloneFixture("relative-file-specs");

await gitTag(cwd, "v1.0.0");
await commitChangeToPackage(cwd, "package-1", "feat(package-1): changed", { changed: true });

await cliRunner(cwd, dryRun)("publish", "major", "--yes");
await cliRunner(cwd, env)("publish", "major", "--yes");

expect(await showCommit(cwd)).toMatchSnapshot();
});
Expand All @@ -162,15 +180,15 @@ describe("lerna publish", () => {
const { cwd } = await cloneFixture("lifecycle");
const args = ["publish", "minor", "--yes"];

const { stdout } = await cliRunner(cwd, dryRun)(...args);
const { stdout } = await cliRunner(cwd, env)(...args);
expect(normalizeTestRoot(cwd)(stdout)).toMatchSnapshot();
});

test("silences lifecycle scripts with --loglevel=silent", async () => {
const { cwd } = await cloneFixture("lifecycle");
const args = ["publish", "minor", "--yes", "--loglevel", "silent"];

const { stdout } = await cliRunner(cwd, dryRun)(...args);
const { stdout } = await cliRunner(cwd, env)(...args);
expect(normalizeTestRoot(cwd)(stdout)).toMatchSnapshot();
});

Expand All @@ -186,10 +204,10 @@ describe("lerna publish", () => {
await execa("git", ["push", "origin", "master"], { cwd: cloneDir });

// throws during interactive publish (local)
await expect(cliRunner(cwd, dryRun)("publish", "--no-ci")).rejects.toThrowError(/EBEHIND/);
await expect(cliRunner(cwd, env)("publish", "--no-ci")).rejects.toThrowError(/EBEHIND/);

// warns during non-interactive publish (CI)
const { stderr } = await cliRunner(cwd, dryRun)("publish", "--ci");
const { stderr } = await cliRunner(cwd, env)("publish", "--ci");
expect(stderr).toMatch("EBEHIND");
});
});
5 changes: 5 additions & 0 deletions utils/npm-publish/npm-publish.js
Expand Up @@ -38,5 +38,10 @@ function npmPack(pkg) {
const opts = getExecOpts(pkg);
const args = ["pack"];

if (process.env.LERNA_INTEGRATION) {
// override process.env.npm_config_dry_run from integration tests
args.push("--no-dry-run");
}

return ChildProcessUtilities.exec("npm", args, opts);
}

0 comments on commit 012afcc

Please sign in to comment.