From 58de52e54f92ee408b176c7e5fe5cfb7827dcc62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alican=20=C3=87ubuk=C3=A7uo=C4=9Flu?= Date: Wed, 12 Oct 2016 10:17:57 +0300 Subject: [PATCH 1/3] Add basic tests for command "run" --- __tests__/commands/run.js | 31 +++++++++++++++++++ .../node_modules/.bin/echo | 3 ++ .../node_modules/echo/lib/index.js | 0 .../node_modules/echo/package.json | 3 ++ .../run/run-should-run-binary/package.json | 3 ++ .../run/run-should-run-script/package.json | 5 +++ 6 files changed, 45 insertions(+) create mode 100644 __tests__/commands/run.js create mode 100755 __tests__/fixtures/run/run-should-run-binary/node_modules/.bin/echo create mode 100644 __tests__/fixtures/run/run-should-run-binary/node_modules/echo/lib/index.js create mode 100644 __tests__/fixtures/run/run-should-run-binary/node_modules/echo/package.json create mode 100644 __tests__/fixtures/run/run-should-run-binary/package.json create mode 100644 __tests__/fixtures/run/run-should-run-script/package.json diff --git a/__tests__/commands/run.js b/__tests__/commands/run.js new file mode 100644 index 0000000000..b89a68776a --- /dev/null +++ b/__tests__/commands/run.js @@ -0,0 +1,31 @@ +/* @flow */ + +import {run} from '../../src/cli/commands/run.js'; +import * as reporters from '../../src/reporters/index.js'; +import Config from '../../src/config.js'; + +jasmine.DEFAULT_TIMEOUT_INTERVAL = 60000; + +const path = require('path'); + +const fixturesLoc = path.join(__dirname, '..', 'fixtures', 'run'); + +async function runRun( + flags: Object, + args: Array, + name: string, +): Promise { + const cwd = path.join(fixturesLoc, name); + const reporter = new reporters.NoopReporter(); + const config = new Config(reporter); + await config.init({cwd}); + return run(config, reporter, flags, args); +} + +test.concurrent('run should run script', (): Promise => { + return runRun({}, ['some-script'], 'run-should-run-script'); +}); + +test.concurrent('run should run binary', (): Promise => { + return runRun({}, ['echo'], 'run-should-run-binary'); +}); diff --git a/__tests__/fixtures/run/run-should-run-binary/node_modules/.bin/echo b/__tests__/fixtures/run/run-should-run-binary/node_modules/.bin/echo new file mode 100755 index 0000000000..bc228dca41 --- /dev/null +++ b/__tests__/fixtures/run/run-should-run-binary/node_modules/.bin/echo @@ -0,0 +1,3 @@ +#!/usr/bin/env node + +require("../echo"); diff --git a/__tests__/fixtures/run/run-should-run-binary/node_modules/echo/lib/index.js b/__tests__/fixtures/run/run-should-run-binary/node_modules/echo/lib/index.js new file mode 100644 index 0000000000..e69de29bb2 diff --git a/__tests__/fixtures/run/run-should-run-binary/node_modules/echo/package.json b/__tests__/fixtures/run/run-should-run-binary/node_modules/echo/package.json new file mode 100644 index 0000000000..f9e9af7364 --- /dev/null +++ b/__tests__/fixtures/run/run-should-run-binary/node_modules/echo/package.json @@ -0,0 +1,3 @@ +{ + "main": "lib/index" +} diff --git a/__tests__/fixtures/run/run-should-run-binary/package.json b/__tests__/fixtures/run/run-should-run-binary/package.json new file mode 100644 index 0000000000..1797133380 --- /dev/null +++ b/__tests__/fixtures/run/run-should-run-binary/package.json @@ -0,0 +1,3 @@ +{ + +} diff --git a/__tests__/fixtures/run/run-should-run-script/package.json b/__tests__/fixtures/run/run-should-run-script/package.json new file mode 100644 index 0000000000..283b688a75 --- /dev/null +++ b/__tests__/fixtures/run/run-should-run-script/package.json @@ -0,0 +1,5 @@ +{ + "scripts": { + "some-script": "echo success" + } +} From 72234170d6f87ef1238a59b0427e018b6faf2889 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alican=20=C3=87ubuk=C3=A7uo=C4=9Flu?= Date: Wed, 12 Oct 2016 14:24:34 +0300 Subject: [PATCH 2/3] Add run test with args and space in path --- __tests__/commands/run.js | 8 ++++++++ .../node_modules/.bin/echo | 3 +++ .../node_modules/echo/lib/index.js | 0 .../node_modules/echo/package.json | 3 +++ .../run should run binary with space in path/package.json | 3 +++ .../node_modules/.bin/echo | 3 +++ .../node_modules/echo/lib/index.js | 7 +++++++ .../node_modules/echo/package.json | 3 +++ .../run/run-should-run-binary-with-args/package.json | 3 +++ 9 files changed, 33 insertions(+) create mode 100755 __tests__/fixtures/run/run should run binary with space in path/node_modules/.bin/echo create mode 100644 __tests__/fixtures/run/run should run binary with space in path/node_modules/echo/lib/index.js create mode 100644 __tests__/fixtures/run/run should run binary with space in path/node_modules/echo/package.json create mode 100644 __tests__/fixtures/run/run should run binary with space in path/package.json create mode 100755 __tests__/fixtures/run/run-should-run-binary-with-args/node_modules/.bin/echo create mode 100644 __tests__/fixtures/run/run-should-run-binary-with-args/node_modules/echo/lib/index.js create mode 100644 __tests__/fixtures/run/run-should-run-binary-with-args/node_modules/echo/package.json create mode 100644 __tests__/fixtures/run/run-should-run-binary-with-args/package.json diff --git a/__tests__/commands/run.js b/__tests__/commands/run.js index b89a68776a..436fa8a5fa 100644 --- a/__tests__/commands/run.js +++ b/__tests__/commands/run.js @@ -29,3 +29,11 @@ test.concurrent('run should run script', (): Promise => { test.concurrent('run should run binary', (): Promise => { return runRun({}, ['echo'], 'run-should-run-binary'); }); + +test.concurrent('run should run binary with args', (): Promise => { + return runRun({}, ['echo', '--test-arg'], 'run-should-run-binary-with-args'); +}); + +test.concurrent('run should run binary with space in path', (): Promise => { + return runRun({}, ['echo'], 'run should run binary with space in path'); +}); diff --git a/__tests__/fixtures/run/run should run binary with space in path/node_modules/.bin/echo b/__tests__/fixtures/run/run should run binary with space in path/node_modules/.bin/echo new file mode 100755 index 0000000000..bc228dca41 --- /dev/null +++ b/__tests__/fixtures/run/run should run binary with space in path/node_modules/.bin/echo @@ -0,0 +1,3 @@ +#!/usr/bin/env node + +require("../echo"); diff --git a/__tests__/fixtures/run/run should run binary with space in path/node_modules/echo/lib/index.js b/__tests__/fixtures/run/run should run binary with space in path/node_modules/echo/lib/index.js new file mode 100644 index 0000000000..e69de29bb2 diff --git a/__tests__/fixtures/run/run should run binary with space in path/node_modules/echo/package.json b/__tests__/fixtures/run/run should run binary with space in path/node_modules/echo/package.json new file mode 100644 index 0000000000..f9e9af7364 --- /dev/null +++ b/__tests__/fixtures/run/run should run binary with space in path/node_modules/echo/package.json @@ -0,0 +1,3 @@ +{ + "main": "lib/index" +} diff --git a/__tests__/fixtures/run/run should run binary with space in path/package.json b/__tests__/fixtures/run/run should run binary with space in path/package.json new file mode 100644 index 0000000000..1797133380 --- /dev/null +++ b/__tests__/fixtures/run/run should run binary with space in path/package.json @@ -0,0 +1,3 @@ +{ + +} diff --git a/__tests__/fixtures/run/run-should-run-binary-with-args/node_modules/.bin/echo b/__tests__/fixtures/run/run-should-run-binary-with-args/node_modules/.bin/echo new file mode 100755 index 0000000000..bc228dca41 --- /dev/null +++ b/__tests__/fixtures/run/run-should-run-binary-with-args/node_modules/.bin/echo @@ -0,0 +1,3 @@ +#!/usr/bin/env node + +require("../echo"); diff --git a/__tests__/fixtures/run/run-should-run-binary-with-args/node_modules/echo/lib/index.js b/__tests__/fixtures/run/run-should-run-binary-with-args/node_modules/echo/lib/index.js new file mode 100644 index 0000000000..71ad48963a --- /dev/null +++ b/__tests__/fixtures/run/run-should-run-binary-with-args/node_modules/echo/lib/index.js @@ -0,0 +1,7 @@ +/* @flow */ + +const arg = '--test-arg'; + +if (process.argv.length !== 3 || process.argv[2] !== arg) { + throw new Error(`Expected a single arg "${arg}".`); +} diff --git a/__tests__/fixtures/run/run-should-run-binary-with-args/node_modules/echo/package.json b/__tests__/fixtures/run/run-should-run-binary-with-args/node_modules/echo/package.json new file mode 100644 index 0000000000..f9e9af7364 --- /dev/null +++ b/__tests__/fixtures/run/run-should-run-binary-with-args/node_modules/echo/package.json @@ -0,0 +1,3 @@ +{ + "main": "lib/index" +} diff --git a/__tests__/fixtures/run/run-should-run-binary-with-args/package.json b/__tests__/fixtures/run/run-should-run-binary-with-args/package.json new file mode 100644 index 0000000000..1797133380 --- /dev/null +++ b/__tests__/fixtures/run/run-should-run-binary-with-args/package.json @@ -0,0 +1,3 @@ +{ + +} From 54cafbe7f13bb256fc20e424a601f6ca3eb5d4fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alican=20=C3=87ubuk=C3=A7uo=C4=9Flu?= Date: Sat, 22 Oct 2016 21:24:57 +0300 Subject: [PATCH 3/3] Expect executeLifecycleScript to be called properly --- __tests__/commands/run.js | 68 ++++++++++++++++--- .../node_modules/.bin/echo | 3 - .../node_modules/.bin/some-binary | 3 + .../{echo => some-binary}/lib/index.js | 0 .../{echo => some-binary}/package.json | 0 .../node_modules/.bin/echo | 3 - .../node_modules/.bin/some-binary | 3 + .../{echo => some-binary}/lib/index.js | 0 .../{echo => some-binary}/package.json | 0 .../node_modules/.bin/echo | 3 - .../node_modules/.bin/some-binary | 3 + .../{echo => some-binary}/lib/index.js | 0 .../{echo => some-binary}/package.json | 0 13 files changed, 67 insertions(+), 19 deletions(-) delete mode 100755 __tests__/fixtures/run/run should run binary with space in path/node_modules/.bin/echo create mode 100755 __tests__/fixtures/run/run should run binary with space in path/node_modules/.bin/some-binary rename __tests__/fixtures/run/run should run binary with space in path/node_modules/{echo => some-binary}/lib/index.js (100%) rename __tests__/fixtures/run/run should run binary with space in path/node_modules/{echo => some-binary}/package.json (100%) delete mode 100755 __tests__/fixtures/run/run-should-run-binary-with-args/node_modules/.bin/echo create mode 100755 __tests__/fixtures/run/run-should-run-binary-with-args/node_modules/.bin/some-binary rename __tests__/fixtures/run/run-should-run-binary-with-args/node_modules/{echo => some-binary}/lib/index.js (100%) rename __tests__/fixtures/run/run-should-run-binary-with-args/node_modules/{echo => some-binary}/package.json (100%) delete mode 100755 __tests__/fixtures/run/run-should-run-binary/node_modules/.bin/echo create mode 100755 __tests__/fixtures/run/run-should-run-binary/node_modules/.bin/some-binary rename __tests__/fixtures/run/run-should-run-binary/node_modules/{echo => some-binary}/lib/index.js (100%) rename __tests__/fixtures/run/run-should-run-binary/node_modules/{echo => some-binary}/package.json (100%) diff --git a/__tests__/commands/run.js b/__tests__/commands/run.js index 436fa8a5fa..b3dadbdc07 100644 --- a/__tests__/commands/run.js +++ b/__tests__/commands/run.js @@ -10,30 +10,78 @@ const path = require('path'); const fixturesLoc = path.join(__dirname, '..', 'fixtures', 'run'); +jest.mock('../../src/util/execute-lifecycle-script'); +const executeLifecycleScript = (require('../../src/util/execute-lifecycle-script').default: $FlowFixMe); + async function runRun( flags: Object, args: Array, name: string, -): Promise { +): Promise { const cwd = path.join(fixturesLoc, name); const reporter = new reporters.NoopReporter(); const config = new Config(reporter); await config.init({cwd}); - return run(config, reporter, flags, args); + await run(config, reporter, flags, args); + return config; } -test.concurrent('run should run script', (): Promise => { - return runRun({}, ['some-script'], 'run-should-run-script'); +beforeEach(() => { + executeLifecycleScript.mockClear(); }); -test.concurrent('run should run binary', (): Promise => { - return runRun({}, ['echo'], 'run-should-run-binary'); +it('run should run script', async (): Promise => { + const config = await runRun({}, ['some-script'], 'run-should-run-script'); + + const expectedCall = [ + 'some-script', + config, + config.cwd, + `echo success `, + ]; + + expect(executeLifecycleScript.mock.calls.length).toEqual(1); + expect(executeLifecycleScript).toBeCalledWith(...expectedCall); }); -test.concurrent('run should run binary with args', (): Promise => { - return runRun({}, ['echo', '--test-arg'], 'run-should-run-binary-with-args'); +it('run should run binary', async (): Promise => { + const config = await runRun({}, ['some-binary'], 'run-should-run-binary'); + + const expectedCall = [ + 'some-binary', + config, + config.cwd, + `"${path.join(config.cwd, 'node_modules/.bin/some-binary')}" `, + ]; + + expect(executeLifecycleScript.mock.calls.length).toEqual(1); + expect(executeLifecycleScript).toBeCalledWith(...expectedCall); }); -test.concurrent('run should run binary with space in path', (): Promise => { - return runRun({}, ['echo'], 'run should run binary with space in path'); +it('run should run binary with args', async (): Promise => { + const config = await runRun({}, ['some-binary', '--test-arg'], 'run-should-run-binary-with-args'); + + const expectedCall = [ + 'some-binary', + config, + config.cwd, + `"${path.join(config.cwd, 'node_modules/.bin/some-binary')}" --test-arg`, + ]; + + expect(executeLifecycleScript.mock.calls.length).toEqual(1); + expect(executeLifecycleScript).toBeCalledWith(...expectedCall); +}); + +it('run should run binary with space in path', async (): Promise => { + const config = await runRun({}, ['some-binary'], 'run should run binary with space in path'); + + const expectedCall = [ + 'some-binary', + config, + config.cwd, + `"${path.join(config.cwd, 'node_modules/.bin/some-binary')}" `, + ]; + + expect(executeLifecycleScript.mock.calls.length).toEqual(1); + expect(executeLifecycleScript).toBeCalledWith(...expectedCall); }); diff --git a/__tests__/fixtures/run/run should run binary with space in path/node_modules/.bin/echo b/__tests__/fixtures/run/run should run binary with space in path/node_modules/.bin/echo deleted file mode 100755 index bc228dca41..0000000000 --- a/__tests__/fixtures/run/run should run binary with space in path/node_modules/.bin/echo +++ /dev/null @@ -1,3 +0,0 @@ -#!/usr/bin/env node - -require("../echo"); diff --git a/__tests__/fixtures/run/run should run binary with space in path/node_modules/.bin/some-binary b/__tests__/fixtures/run/run should run binary with space in path/node_modules/.bin/some-binary new file mode 100755 index 0000000000..83a3c57143 --- /dev/null +++ b/__tests__/fixtures/run/run should run binary with space in path/node_modules/.bin/some-binary @@ -0,0 +1,3 @@ +#!/usr/bin/env node + +require("../some-binary"); diff --git a/__tests__/fixtures/run/run should run binary with space in path/node_modules/echo/lib/index.js b/__tests__/fixtures/run/run should run binary with space in path/node_modules/some-binary/lib/index.js similarity index 100% rename from __tests__/fixtures/run/run should run binary with space in path/node_modules/echo/lib/index.js rename to __tests__/fixtures/run/run should run binary with space in path/node_modules/some-binary/lib/index.js diff --git a/__tests__/fixtures/run/run should run binary with space in path/node_modules/echo/package.json b/__tests__/fixtures/run/run should run binary with space in path/node_modules/some-binary/package.json similarity index 100% rename from __tests__/fixtures/run/run should run binary with space in path/node_modules/echo/package.json rename to __tests__/fixtures/run/run should run binary with space in path/node_modules/some-binary/package.json diff --git a/__tests__/fixtures/run/run-should-run-binary-with-args/node_modules/.bin/echo b/__tests__/fixtures/run/run-should-run-binary-with-args/node_modules/.bin/echo deleted file mode 100755 index bc228dca41..0000000000 --- a/__tests__/fixtures/run/run-should-run-binary-with-args/node_modules/.bin/echo +++ /dev/null @@ -1,3 +0,0 @@ -#!/usr/bin/env node - -require("../echo"); diff --git a/__tests__/fixtures/run/run-should-run-binary-with-args/node_modules/.bin/some-binary b/__tests__/fixtures/run/run-should-run-binary-with-args/node_modules/.bin/some-binary new file mode 100755 index 0000000000..83a3c57143 --- /dev/null +++ b/__tests__/fixtures/run/run-should-run-binary-with-args/node_modules/.bin/some-binary @@ -0,0 +1,3 @@ +#!/usr/bin/env node + +require("../some-binary"); diff --git a/__tests__/fixtures/run/run-should-run-binary-with-args/node_modules/echo/lib/index.js b/__tests__/fixtures/run/run-should-run-binary-with-args/node_modules/some-binary/lib/index.js similarity index 100% rename from __tests__/fixtures/run/run-should-run-binary-with-args/node_modules/echo/lib/index.js rename to __tests__/fixtures/run/run-should-run-binary-with-args/node_modules/some-binary/lib/index.js diff --git a/__tests__/fixtures/run/run-should-run-binary-with-args/node_modules/echo/package.json b/__tests__/fixtures/run/run-should-run-binary-with-args/node_modules/some-binary/package.json similarity index 100% rename from __tests__/fixtures/run/run-should-run-binary-with-args/node_modules/echo/package.json rename to __tests__/fixtures/run/run-should-run-binary-with-args/node_modules/some-binary/package.json diff --git a/__tests__/fixtures/run/run-should-run-binary/node_modules/.bin/echo b/__tests__/fixtures/run/run-should-run-binary/node_modules/.bin/echo deleted file mode 100755 index bc228dca41..0000000000 --- a/__tests__/fixtures/run/run-should-run-binary/node_modules/.bin/echo +++ /dev/null @@ -1,3 +0,0 @@ -#!/usr/bin/env node - -require("../echo"); diff --git a/__tests__/fixtures/run/run-should-run-binary/node_modules/.bin/some-binary b/__tests__/fixtures/run/run-should-run-binary/node_modules/.bin/some-binary new file mode 100755 index 0000000000..83a3c57143 --- /dev/null +++ b/__tests__/fixtures/run/run-should-run-binary/node_modules/.bin/some-binary @@ -0,0 +1,3 @@ +#!/usr/bin/env node + +require("../some-binary"); diff --git a/__tests__/fixtures/run/run-should-run-binary/node_modules/echo/lib/index.js b/__tests__/fixtures/run/run-should-run-binary/node_modules/some-binary/lib/index.js similarity index 100% rename from __tests__/fixtures/run/run-should-run-binary/node_modules/echo/lib/index.js rename to __tests__/fixtures/run/run-should-run-binary/node_modules/some-binary/lib/index.js diff --git a/__tests__/fixtures/run/run-should-run-binary/node_modules/echo/package.json b/__tests__/fixtures/run/run-should-run-binary/node_modules/some-binary/package.json similarity index 100% rename from __tests__/fixtures/run/run-should-run-binary/node_modules/echo/package.json rename to __tests__/fixtures/run/run-should-run-binary/node_modules/some-binary/package.json