Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions __tests__/commands/run.js
Original file line number Diff line number Diff line change
@@ -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<string>,
name: string,
): Promise<Config> {
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<void> => {
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<void> => {
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<void> => {
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<void> => {
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);
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see what this is doing! If you do this, and add specs for the same thing but with spaces in the fixture directory name, that should have you covering the issue #633 intended to fix completely! 😃

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, the rest should be just copy&paste. I just want to be sure that I am on the right path. Currently only "add", "install" and "self-update" commands are tested so there isn't much to use as an example.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Come to think of it, it could be reasonable to mock out child_process (used all the way over here) for these purposes, but I guess it depends if you want to actually integration test with the shell.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{

}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{

}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions __tests__/fixtures/run/run-should-run-binary/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{

}
5 changes: 5 additions & 0 deletions __tests__/fixtures/run/run-should-run-script/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"scripts": {
"some-script": "echo success"
}
}