From d62f8232a9f24b8c21f1bb0839fe1c73ae3ed1bf Mon Sep 17 00:00:00 2001 From: Igor Redchuk Date: Fri, 16 Dec 2016 00:44:58 +0100 Subject: [PATCH] Fix adding package with name like one of the options * Fixes #2112 --- .../index/run-add-lockfile/package.json | 5 + .../run-add-option-in-front/package.json | 5 + .../index/run-add-option/package.json | 5 + .../run-add-progress-globally/global/.gitkeep | 0 .../run-add-progress-globally/package.json | 5 + __tests__/fixtures/index/run-add/package.json | 5 + .../index/run-custom-script/package.json | 8 ++ __tests__/fixtures/index/run-help/.gitkeep | 0 __tests__/index.js | 108 ++++++++++++++++++ src/cli/index.js | 15 +-- 10 files changed, 149 insertions(+), 7 deletions(-) create mode 100644 __tests__/fixtures/index/run-add-lockfile/package.json create mode 100644 __tests__/fixtures/index/run-add-option-in-front/package.json create mode 100644 __tests__/fixtures/index/run-add-option/package.json create mode 100644 __tests__/fixtures/index/run-add-progress-globally/global/.gitkeep create mode 100644 __tests__/fixtures/index/run-add-progress-globally/package.json create mode 100644 __tests__/fixtures/index/run-add/package.json create mode 100644 __tests__/fixtures/index/run-custom-script/package.json create mode 100644 __tests__/fixtures/index/run-help/.gitkeep create mode 100644 __tests__/index.js diff --git a/__tests__/fixtures/index/run-add-lockfile/package.json b/__tests__/fixtures/index/run-add-lockfile/package.json new file mode 100644 index 0000000000..7114728d11 --- /dev/null +++ b/__tests__/fixtures/index/run-add-lockfile/package.json @@ -0,0 +1,5 @@ +{ + "name": "test_add_lockfile", + "version": "1.0.0", + "license": "UNLICENSED" +} diff --git a/__tests__/fixtures/index/run-add-option-in-front/package.json b/__tests__/fixtures/index/run-add-option-in-front/package.json new file mode 100644 index 0000000000..2094013895 --- /dev/null +++ b/__tests__/fixtures/index/run-add-option-in-front/package.json @@ -0,0 +1,5 @@ +{ + "name": "test_add_with_option_in_front", + "version": "1.0.0", + "license": "UNLICENSED" +} diff --git a/__tests__/fixtures/index/run-add-option/package.json b/__tests__/fixtures/index/run-add-option/package.json new file mode 100644 index 0000000000..80d6b9c829 --- /dev/null +++ b/__tests__/fixtures/index/run-add-option/package.json @@ -0,0 +1,5 @@ +{ + "name": "test_add_with_option", + "version": "1.0.0", + "license": "UNLICENSED" +} diff --git a/__tests__/fixtures/index/run-add-progress-globally/global/.gitkeep b/__tests__/fixtures/index/run-add-progress-globally/global/.gitkeep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/__tests__/fixtures/index/run-add-progress-globally/package.json b/__tests__/fixtures/index/run-add-progress-globally/package.json new file mode 100644 index 0000000000..69bf2e2e69 --- /dev/null +++ b/__tests__/fixtures/index/run-add-progress-globally/package.json @@ -0,0 +1,5 @@ +{ + "name": "test_add_progress_globally", + "version": "1.0.0", + "license": "UNLICENSED" +} diff --git a/__tests__/fixtures/index/run-add/package.json b/__tests__/fixtures/index/run-add/package.json new file mode 100644 index 0000000000..501b0f2ddf --- /dev/null +++ b/__tests__/fixtures/index/run-add/package.json @@ -0,0 +1,5 @@ +{ + "name": "test_add", + "version": "1.0.0", + "license": "UNLICENSED" +} diff --git a/__tests__/fixtures/index/run-custom-script/package.json b/__tests__/fixtures/index/run-custom-script/package.json new file mode 100644 index 0000000000..e1129f4040 --- /dev/null +++ b/__tests__/fixtures/index/run-custom-script/package.json @@ -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" + } +} diff --git a/__tests__/fixtures/index/run-help/.gitkeep b/__tests__/fixtures/index/run-help/.gitkeep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/__tests__/index.js b/__tests__/index.js new file mode 100644 index 0000000000..e2e96907d8 --- /dev/null +++ b/__tests__/index.js @@ -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, name: string, makeTempDir: ?boolean): +Promise> { + 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]'); +}); diff --git a/src/cli/index.js b/src/cli/index.js index 71e569139f..b7f3fa038f 100644 --- a/src/cli/index.js +++ b/src/cli/index.js @@ -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) {