command.default.test.js
This file
Path
tests/command.default.test.js
94 lines · 3.2 KiB · JavaScript
Last changed by
Merge branch 'master' into release/7.xJohn Geebefore adoption5fd6e88
History of this fileTake it
The walk stopped after 200 commits: lines older than 669190d point at it.
Merge branch 'master' into developJohn Geebefore adoption669190d
lines 1–5why, one step back1const childProcess = require('child_process');2const commander = require('../');3const path = require('path');4const util = require('util');5Merge branch 'master' into release/7.xJohn Geebefore adoption5fd6e88
line 6why, one step back6const execFileAsync = util.promisify(childProcess.execFile);Merge branch 'master' into developJohn Geebefore adoption669190d
lines 7–12why, one step back78describe('default executable command', () => {9 // Calling node explicitly so pm works without file suffix cross-platform.10 const pm = path.join(__dirname, './fixtures/pm');1112 test('when default subcommand and no command then call default', async() => {Merge branch 'master' into release/7.xJohn Geebefore adoption5fd6e88
line 13why, one step back13 const { stdout } = await execFileAsync('node', [pm]);Merge branch 'master' into developJohn Geebefore adoption669190d
lines 14–17why, one step back14 expect(stdout).toBe('default\n');15 });1617 test('when default subcommand and unrecognised argument then call default with argument', async() => {Merge branch 'master' into release/7.xJohn Geebefore adoption5fd6e88
line 18why, one step back18 const { stdout } = await execFileAsync('node', [pm, 'an-argument']);Merge branch 'master' into developJohn Geebefore adoption669190d
lines 19–22why, one step back19 expect(stdout).toBe("default\n[ 'an-argument' ]\n");20 });2122 test('when default subcommand and unrecognised option then call default with option', async() => {Merge branch 'master' into release/7.xJohn Geebefore adoption5fd6e88
line 23why, one step back23 const { stdout } = await execFileAsync('node', [pm, '--an-option']);Merge branch 'master' into developJohn Geebefore adoption669190d
lines 24–36why, one step back24 expect(stdout).toBe("default\n[ '--an-option' ]\n");25 });26});2728describe('default action command', () => {29 function makeProgram() {30 const program = new commander.Command();31 const actionMock = jest.fn();32 program33 .command('other');34 program35 .command('default', { isDefault: true })36 .allowUnknownOption()Change to safe storage of options by default, and change action parameters (#1409)John Geebefore adoptionf76bc71
line 37why, one step back37 .allowExcessArguments()Merge branch 'master' into developJohn Geebefore adoption669190d
lines 38–65why, one step back38 .action(actionMock);39 return { program, actionMock };40 }4142 test('when default subcommand and no command then call default', () => {43 const { program, actionMock } = makeProgram();44 program.parse('node test.js'.split(' '));45 expect(actionMock).toHaveBeenCalled();46 });4748 test('when default subcommand and unrecognised argument then call default', () => {49 const { program, actionMock } = makeProgram();50 program.parse('node test.js an-argument'.split(' '));51 expect(actionMock).toHaveBeenCalled();52 });5354 test('when default subcommand and unrecognised option then call default', () => {55 const { program, actionMock } = makeProgram();56 program.parse('node test.js --an-option'.split(' '));57 expect(actionMock).toHaveBeenCalled();58 });59});6061describe('default added command', () => {62 function makeProgram() {63 const actionMock = jest.fn();64 const defaultCmd = new commander.Command('default')65 .allowUnknownOption()Change to safe storage of options by default, and change action parameters (#1409)John Geebefore adoptionf76bc71
line 66why, one step back66 .allowExcessArguments()Merge branch 'master' into developJohn Geebefore adoption669190d
lines 67–94why, one step back67 .action(actionMock);6869 const program = new commander.Command();70 program71 .command('other');72 program73 .addCommand(defaultCmd, { isDefault: true });74 return { program, actionMock };75 }7677 test('when default subcommand and no command then call default', () => {78 const { program, actionMock } = makeProgram();79 program.parse('node test.js'.split(' '));80 expect(actionMock).toHaveBeenCalled();81 });8283 test('when default subcommand and unrecognised argument then call default', () => {84 const { program, actionMock } = makeProgram();85 program.parse('node test.js an-argument'.split(' '));86 expect(actionMock).toHaveBeenCalled();87 });8889 test('when default subcommand and unrecognised option then call default', () => {90 const { program, actionMock } = makeProgram();91 program.parse('node test.js --an-option'.split(' '));92 expect(actionMock).toHaveBeenCalled();93 });94});