From 5d5bf53c49067b3216e65eb0e93075e9c02a5d5a Mon Sep 17 00:00:00 2001 From: Moti Zilberman Date: Sun, 20 May 2018 09:08:01 +0100 Subject: [PATCH 1/3] feat(cli): hide stack trace on subprocess errors --- src/index.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index bd0b814..f66cbf3 100755 --- a/src/index.js +++ b/src/index.js @@ -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); } @@ -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); From 25694e243bb43688bc110fc1754a942a0961fafb Mon Sep 17 00:00:00 2001 From: Moti Zilberman Date: Sun, 20 May 2018 10:07:34 +0100 Subject: [PATCH 2/3] test(TestEnvironment): add mockBinError method --- src/__mocks__/TestEnvironment.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/__mocks__/TestEnvironment.js b/src/__mocks__/TestEnvironment.js index c092fab..48260c1 100644 --- a/src/__mocks__/TestEnvironment.js +++ b/src/__mocks__/TestEnvironment.js @@ -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"); From ee81abf12c65ff8015df03b9dc0bba46675114d6 Mon Sep 17 00:00:00 2001 From: Moti Zilberman Date: Sun, 20 May 2018 10:08:20 +0100 Subject: [PATCH 3/3] test: add test for failing command --- package.json | 1 + src/__tests__/__snapshots__/index.test.js.snap | 2 ++ src/__tests__/index.test.js | 7 +++++++ yarn.lock | 4 ++++ 4 files changed, 14 insertions(+) diff --git a/package.json b/package.json index 390a788..9123bc3 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/__tests__/__snapshots__/index.test.js.snap b/src/__tests__/__snapshots__/index.test.js.snap index 7b3e5f2..eb54dd7 100644 --- a/src/__tests__/__snapshots__/index.test.js.snap +++ b/src/__tests__/__snapshots__/index.test.js.snap @@ -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-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 --", diff --git a/src/__tests__/index.test.js b/src/__tests__/index.test.js index 7ec19f0..393ca3f 100644 --- a/src/__tests__/index.test.js +++ b/src/__tests__/index.test.js @@ -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(); @@ -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", "--"]); diff --git a/yarn.lock b/yarn.lock index 7048d91..fb2d0e0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"