diff --git a/__tests__/commands/run.js b/__tests__/commands/run.js new file mode 100644 index 0000000000..b3dadbdc07 --- /dev/null +++ b/__tests__/commands/run.js @@ -0,0 +1,87 @@ +/* @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'); + +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 { + const cwd = path.join(fixturesLoc, name); + const reporter = new reporters.NoopReporter(); + const config = new Config(reporter); + await config.init({cwd}); + await run(config, reporter, flags, args); + return config; +} + +beforeEach(() => { + executeLifecycleScript.mockClear(); +}); + +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); +}); + +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); +}); + +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/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/some-binary/lib/index.js b/__tests__/fixtures/run/run should run binary with space in path/node_modules/some-binary/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/some-binary/package.json b/__tests__/fixtures/run/run should run binary with space in path/node_modules/some-binary/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/some-binary/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/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/some-binary/lib/index.js b/__tests__/fixtures/run/run-should-run-binary-with-args/node_modules/some-binary/lib/index.js new file mode 100644 index 0000000000..71ad48963a --- /dev/null +++ b/__tests__/fixtures/run/run-should-run-binary-with-args/node_modules/some-binary/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/some-binary/package.json b/__tests__/fixtures/run/run-should-run-binary-with-args/node_modules/some-binary/package.json new file mode 100644 index 0000000000..f9e9af7364 --- /dev/null +++ b/__tests__/fixtures/run/run-should-run-binary-with-args/node_modules/some-binary/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 @@ +{ + +} 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/some-binary/lib/index.js b/__tests__/fixtures/run/run-should-run-binary/node_modules/some-binary/lib/index.js new file mode 100644 index 0000000000..e69de29bb2 diff --git a/__tests__/fixtures/run/run-should-run-binary/node_modules/some-binary/package.json b/__tests__/fixtures/run/run-should-run-binary/node_modules/some-binary/package.json new file mode 100644 index 0000000000..f9e9af7364 --- /dev/null +++ b/__tests__/fixtures/run/run-should-run-binary/node_modules/some-binary/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" + } +}