Skip to content

Commit

Permalink
Merge pull request #30 from motiz88/fix-11
Browse files Browse the repository at this point in the history
Hide stack trace on subprocess errors
  • Loading branch information
motiz88 authored May 20, 2018
2 parents 8d7dd59 + ee81abf commit 505747a
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"babel-preset-stage-0": "^6.24.1",
"codecov": "^2.2.0",
"if-env": "^1.0.0",
"invert-promise": "^1.0.1",
"jest": "^20.0.0",
"mock-bin": "^2.0.0",
"mock-git": "^2.0.0",
Expand Down
4 changes: 4 additions & 0 deletions src/__mocks__/TestEnvironment.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ export default class TestEnvironment {
this._unmock.push(await mockBin(name, "node", this._makeSpyJs(name)));
}

async mockBinError(name) {
this._unmock.push(await mockBin(name, "false"));
}

_makeSpyJs(...prefix) {
return `
require("fs").appendFileSync(${JSON.stringify(this._spyLogFile)}, JSON.stringify(${JSON.stringify(prefix)}.concat(process.argv.slice(2))) + "\\n", "utf8");
Expand Down
2 changes: 2 additions & 0 deletions src/__tests__/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

exports[`git-exec-and-restage no command, no args and no files staged 1`] = `[Error: Usage: git-exec-and-restage <command> [...command-args --] [...files]]`;
exports[`git-exec-and-restage throws when command fails 1`] = `"Error running command: Exited with status 1"`;
exports[`git-exec-and-restage with command and arguments, no files 1`] = `
Array [
"git diff-index --cached --name-only --diff-filter=ACDMRTUXB HEAD --",
Expand Down
7 changes: 7 additions & 0 deletions src/__tests__/index.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import gitExecAndRestage from "../";
import TestEnvironment from "TestEnvironment";
import path from "path";
import flip from "invert-promise";

describe("git-exec-and-restage", () => {
const env = new TestEnvironment();
Expand All @@ -14,6 +15,12 @@ describe("git-exec-and-restage", () => {
await gitExecAndRestage(["prettier"]);
expect(await env.log).toMatchSnapshot();
});
it("throws when command fails", async () => {
await env.mockBinError("prettier");
const error = await flip(gitExecAndRestage(["prettier"]));
expect(error.__isExpectedError).toBe(true);
expect(error.message).toMatchSnapshot();
});
it("with command and arguments, no files", async () => {
await env.mockBin("prettier");
await gitExecAndRestage(["prettier", "--write", "--"]);
Expand Down
10 changes: 8 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ async function main(argv = []) {
);
}
const fullyStaged = await getFullyStaged(files);
await spawn(command, commandArgs.concat(files), { stdio: "inherit" });
try {
await spawn(command, commandArgs.concat(files), { stdio: "inherit" });
} catch (e) {
e.__isExpectedError = true;
e.message = "Error running command: " + e.message;
throw e;
}
if (fullyStaged.length) {
await stageFiles(fullyStaged);
}
Expand All @@ -42,7 +48,7 @@ export default main;
if (require.main === module) {
require("./polyfills");
main(process.argv.slice(2)).catch(e => {
if (e instanceof CliError) {
if (e instanceof CliError || e.__isExpectedError) {
process.stderr.write(e.message + "\n");
} else {
process.stderr.write(e.stack || e);
Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1559,6 +1559,10 @@ invert-kv@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/invert-kv/-/invert-kv-1.0.0.tgz#104a8e4aaca6d3d8cd157a8ef8bfab2d7a3ffdb6"

invert-promise@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/invert-promise/-/invert-promise-1.0.1.tgz#22d8b5f3cef87633743f86a96b3885dcdfd91fa2"

is-arrayish@^0.2.1:
version "0.2.1"
resolved "https://registry.yarnpkg.com/is-arrayish/-/is-arrayish-0.2.1.tgz#77c99840527aa8ecb1a8ba697b80645a7a926a9d"
Expand Down

0 comments on commit 505747a

Please sign in to comment.