Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix adding package with name like one of the options #2268

Merged
merged 1 commit into from
Dec 18, 2016
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
5 changes: 5 additions & 0 deletions __tests__/fixtures/index/run-add-lockfile/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "test_add_lockfile",
"version": "1.0.0",
"license": "UNLICENSED"
}
5 changes: 5 additions & 0 deletions __tests__/fixtures/index/run-add-option-in-front/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "test_add_with_option_in_front",
"version": "1.0.0",
"license": "UNLICENSED"
}
5 changes: 5 additions & 0 deletions __tests__/fixtures/index/run-add-option/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "test_add_with_option",
"version": "1.0.0",
"license": "UNLICENSED"
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "test_add_progress_globally",
"version": "1.0.0",
"license": "UNLICENSED"
}
5 changes: 5 additions & 0 deletions __tests__/fixtures/index/run-add/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "test_add",
"version": "1.0.0",
"license": "UNLICENSED"
}
8 changes: 8 additions & 0 deletions __tests__/fixtures/index/run-custom-script/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "test_run_custom_script",
"version": "1.0.0",
"license": "UNLICENSED",
"scripts": {
"custom-script": "echo \"A message from custom script\" && exit 0"
}
}
Empty file.
108 changes: 108 additions & 0 deletions __tests__/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/* @flow */

import NoopReporter from '../src/reporters/base-reporter.js';
import makeTemp from './_temp';
import * as fs from '../src/util/fs.js';

const path = require('path');
const exec = require('child_process').exec;

const fixturesLoc = path.join(__dirname, './fixtures/index');
const yarnBin = path.join(__dirname, '../bin/yarn.js');

jasmine.DEFAULT_TIMEOUT_INTERVAL = 60000;

async function execCommand(cmd: string, args: Array<string>, name: string, makeTempDir: ?boolean):
Promise<Array<?string>> {
const srcDir = path.join(fixturesLoc, name);
let workingDir = srcDir;
if (makeTempDir) {
workingDir = await makeTemp(name);
await fs.copy(srcDir, workingDir, new NoopReporter());
}

return new Promise((resolve, reject) => {
exec(`node ${yarnBin} ${cmd} ${args.join(' ')}`, {cwd:workingDir, env:process.env}, (err, stdout) => {
if (err) {
reject(err);
} else {
const stdoutLines = stdout.toString()
.split('\n')
.map((line: ?string) => line && line.trim())
.filter((line: ?string) => line);

resolve(stdoutLines);
}
});
});
}

test.concurrent('should add package', async () => {
const stdout = await execCommand('add', ['left-pad'], 'run-add', true);
const lastLines = stdout.slice(stdout.length - 4);
expect(lastLines[0]).toEqual('success Saved lockfile.');
expect(lastLines[1]).toEqual('success Saved 1 new dependency.');
expect(lastLines[2]).toMatch(/left-pad/);
expect(lastLines[3]).toMatch(/^Done/);
});

test.concurrent('should add package with no-lockfile option', async () => {
const stdout = await execCommand('add', ['repeating', '--no-lockfile'], 'run-add-option', true);
const lastLines = stdout.slice(stdout.length - 4);
expect(lastLines[0]).not.toMatch(/Saved lockfile/);
expect(lastLines[1]).toEqual('success Saved 1 new dependency.');
expect(lastLines[2]).toMatch(/repeating/);
expect(lastLines[3]).toMatch(/^Done/);
});

test.concurrent('should add package with no-lockfile option in front', async () => {
const stdout = await execCommand('add', ['--no-lockfile', 'split-lines'], 'run-add-option-in-front', true);
const lastLines = stdout.slice(stdout.length - 4);
expect(lastLines[0]).not.toMatch(/Saved lockfile/);
expect(lastLines[1]).toEqual('success Saved 1 new dependency.');
expect(lastLines[2]).toMatch(/split-lines/);
expect(lastLines[3]).toMatch(/^Done/);
});

test.concurrent('should add lockfile package', async () => {
const stdout = await execCommand('add', ['lockfile'], 'run-add-lockfile', true);
const lastLines = stdout.slice(stdout.length - 4);
expect(lastLines[0]).toEqual('success Saved lockfile.');
expect(lastLines[1]).toEqual('success Saved 1 new dependency.');
expect(lastLines[2]).toMatch(/lockfile/);
expect(lastLines[3]).toMatch(/^Done/);
});

test.concurrent('should add progress package globally', async () => {
const stdout = await execCommand('global',
['add', 'progress', '--global-folder', './global'],
'run-add-progress-globally',
true);

const lastLine = stdout[stdout.length - 1];
expect(lastLine).toMatch(/^Done/);
});

test.concurrent('should run custom script', async () => {
const stdout = await execCommand('run', ['custom-script'], 'run-custom-script');
const lastLines = stdout.slice(stdout.length - 2);
expect(lastLines[0]).toMatch(/A message from custom script/);
expect(lastLines[1]).toMatch(/^Done/);
});

test.concurrent('should run custom script without run command', async () => {
const stdout = await execCommand('custom-script', [], 'run-custom-script');
const lastLines = stdout.slice(stdout.length - 2);
expect(lastLines[0]).toMatch(/A message from custom script/);
expect(lastLines[1]).toMatch(/^Done/);
});

test.concurrent('should run help command', async () => {
const stdout = await execCommand('help', [], 'run-help');
expect(stdout[0]).toEqual('Usage: yarn [command] [flags]');
});

test.concurrent('should run add command with help option', async () => {
const stdout = await execCommand('add', ['--help'], 'run-help');
expect(stdout[0]).toEqual('Usage: yarn add [packages ...] [flags]');
});
15 changes: 8 additions & 7 deletions src/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,17 +170,18 @@ if (commandName === 'help' || args.indexOf('--help') >= 0 || args.indexOf('-h')
process.exit(1);
}

//
if (!command) {
args.unshift(commandName);
command = commands.run;
}
invariant(command, 'missing command');

// parse flags
args.unshift(commandName);
commander.parse(startArgs.concat(args));
commander.args = commander.args.concat(endArgs);

if (command) {
commander.args.shift();
} else {
command = commands.run;
}
invariant(command, 'missing command');

//
let Reporter = ConsoleReporter;
if (commander.json) {
Expand Down