Skip to content

Commit

Permalink
fix(npm-publish): Ensure process exits non-zero when libnpm/publish
Browse files Browse the repository at this point in the history
… fails
  • Loading branch information
evocateur committed Jan 22, 2019
1 parent a2362b8 commit 9e9ce08
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
24 changes: 24 additions & 0 deletions integration/lerna-publish-error.test.js
@@ -0,0 +1,24 @@
"use strict";

const path = require("path");

const cliRunner = require("@lerna-test/cli-runner");
const cloneFixture = require("@lerna-test/clone-fixture")(
path.resolve(__dirname, "../commands/publish/__tests__")
);

test("lerna publish sets correct exit code when libnpm/publish fails", async () => {
const { cwd } = await cloneFixture("normal");

try {
await cliRunner(cwd)("publish", "patch", "--yes", "--no-verify-access", "--loglevel", "error");
} catch (err) {
expect(err.stderr).toMatchInlineSnapshot(`
lerna ERR! E401 You must be logged in to publish packages.
`);
expect(err.code).toBe(1);
}

expect.hasAssertions();
});
44 changes: 44 additions & 0 deletions utils/npm-publish/__tests__/npm-publish.test.js
Expand Up @@ -131,4 +131,48 @@ describe("npm-publish", () => {
expect(runLifecycle).toHaveBeenCalledWith(pkg, "publish", aFiggyPudding);
expect(runLifecycle).toHaveBeenLastCalledWith(pkg, "postpublish", aFiggyPudding);
});

it("catches libnpm errors", async () => {
publish.mockImplementationOnce(() => {
const err = new Error("whoopsy");
err.code = "E401";
err.body = {
error: "doodle",
};
return Promise.reject(err);
});

const log = {
verbose: jest.fn(),
silly: jest.fn(),
error: jest.fn(),
};
const opts = new Map().set("log", log);

try {
await npmPublish(pkg, tarFilePath, opts);
} catch (err) {
expect(err.message).toBe("whoopsy");
expect(err.name).toBe("ValidationError");
expect(log.error).toHaveBeenLastCalledWith("E401", "doodle");
expect(process.exitCode).toBe(1);
}

publish.mockImplementationOnce(() => {
const err = new Error("lolwut");
err.code = "E404";
err.errno = 9001;
return Promise.reject(err);
});

try {
await npmPublish(pkg, tarFilePath, opts);
} catch (err) {
expect(err.message).toBe("lolwut");
expect(log.error).toHaveBeenLastCalledWith("E404", "lolwut");
expect(process.exitCode).toBe(9001);
}

expect.hasAssertions();
});
});
14 changes: 13 additions & 1 deletion utils/npm-publish/npm-publish.js
Expand Up @@ -50,7 +50,19 @@ function npmPublish(pkg, tarFilePath, _opts) {
manifest.publishConfig.tag = opts.tag;
}

return publish(manifest, tarData, opts);
return publish(manifest, tarData, opts).catch(err => {
opts.log.silly("", err);
opts.log.error(err.code, (err.body && err.body.error) || err.message);

// avoid dumping logs, this isn't a lerna problem
err.name = "ValidationError";

// ensure process exits non-zero
process.exitCode = "errno" in err ? err.errno : 1;

// re-throw to break chain upstream
throw err;
});
});
}

Expand Down

0 comments on commit 9e9ce08

Please sign in to comment.