command.parse.test.js
This file
Path
tests/command.parse.test.js
183 lines · 6.4 KiB · JavaScript
Last changed by
Add auto-detection of args when node evaluating script code on command-line (#2164)John Geebefore adoptionb9ca390
History of this fileTake it
The walk stopped after 200 commits: lines older than 411ca95 point at it.
Clarify that more than just READMEJohn Geebefore adoption411ca95
lines 1–6why, one step back1const commander = require('../');23// Testing some Electron conventions but not directly using Electron to avoid overheads.4// https://github.com/electron/electron/issues/4690#issuecomment-2174352225// https://www.electronjs.org/docs/api/process#processdefaultapp-readonly6Add auto-detection of args when node evaluating script code on command-line (#2164)John Geebefore adoptionb9ca390
lines 7–9why, one step back7// (If mutating process.argv and process.execArgv causes problems, could add utility8// functions to get them and then mock the functions for tests.)9Clarify that more than just READMEJohn Geebefore adoption411ca95
lines 10–72why, one step back10describe('.parse() args from', () => {11 test('when no args then use process.argv and app/script/args', () => {12 const program = new commander.Command();13 const hold = process.argv;14 process.argv = 'node script.js user'.split(' ');15 program.parse();16 process.argv = hold;17 expect(program.args).toEqual(['user']);18 });1920 test('when no args and electron properties and not default app then use process.argv and app/args', () => {21 const program = new commander.Command();22 const holdArgv = process.argv;23 process.versions.electron = '1.2.3';24 process.argv = 'node user'.split(' ');25 program.parse();26 delete process.versions.electron;27 process.argv = holdArgv;28 expect(program.args).toEqual(['user']);29 });3031 test('when args then app/script/args', () => {32 const program = new commander.Command();33 program.parse('node script.js user'.split(' '));34 expect(program.args).toEqual(['user']);35 });3637 test('when args from "node" then app/script/args', () => {38 const program = new commander.Command();39 program.parse('node script.js user'.split(' '), { from: 'node' });40 expect(program.args).toEqual(['user']);41 });4243 test('when args from "electron" and not default app then app/args', () => {44 const program = new commander.Command();45 const hold = process.defaultApp;46 process.defaultApp = undefined;47 program.parse('customApp user'.split(' '), { from: 'electron' });48 process.defaultApp = hold;49 expect(program.args).toEqual(['user']);50 });5152 test('when args from "electron" and default app then app/script/args', () => {53 const program = new commander.Command();54 const hold = process.defaultApp;55 process.defaultApp = true;56 program.parse('electron script user'.split(' '), { from: 'electron' });57 process.defaultApp = hold;58 expect(program.args).toEqual(['user']);59 });6061 test('when args from "user" then args', () => {62 const program = new commander.Command();63 program.parse('user'.split(' '), { from: 'user' });64 expect(program.args).toEqual(['user']);65 });6667 test('when args from "silly" then throw', () => {68 const program = new commander.Command();69 expect(() => {70 program.parse(['node', 'script.js'], { from: 'silly' });71 }).toThrow();72 });Add auto-detection of args when node evaluating script code on command-line (#2164)John Geebefore adoptionb9ca390
lines 73–88why, one step back7374 test.each(['-e', '--eval', '-p', '--print'])(75 'when node execArgv includes %s then app/args',76 (flag) => {77 const program = new commander.Command();78 const holdExecArgv = process.execArgv;79 const holdArgv = process.argv;80 process.argv = ['node', 'user-arg'];81 process.execArgv = [flag, 'console.log("hello, world")'];82 program.parse();83 process.argv = holdArgv;84 process.execArgv = holdExecArgv;85 expect(program.args).toEqual(['user-arg']);86 process.execArgv = holdExecArgv;87 },88 );Clarify that more than just READMEJohn Geebefore adoption411ca95
lines 89–93why, one step back89});9091describe('return type', () => {92 test('when call .parse then returns program', () => {93 const program = new commander.Command();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 94why, one step back94 program.action(() => {});Clarify that more than just READMEJohn Geebefore adoption411ca95
lines 95–99why, one step back9596 const result = program.parse(['node', 'test']);97 expect(result).toBe(program);98 });99Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 100why, one step back100 test('when await .parseAsync then returns program', async () => {Clarify that more than just READMEJohn Geebefore adoption411ca95
line 101why, one step back101 const program = new commander.Command();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 102why, one step back102 program.action(() => {});Clarify that more than just READMEJohn Geebefore adoption411ca95
lines 103–115why, one step back103104 const result = await program.parseAsync(['node', 'test']);105 expect(result).toBe(program);106 });107});108109// Easy mistake to make when writing unit tests110test('when parse strings instead of array then throw', () => {111 const program = new commander.Command();112 expect(() => {113 program.parse('node', 'test');114 }).toThrow();115});Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c
lines 116–151why, one step back116117describe('parse parameter is treated as readonly, per TypeScript declaration', () => {118 test('when parse called then parameter does not change', () => {119 const program = new commander.Command();120 program.option('--debug');121 const original = ['node', '--debug', 'arg'];122 const param = original.slice();123 program.parse(param);124 expect(param).toEqual(original);125 });126127 test('when parse called and parsed args later changed then parameter does not change', () => {128 const program = new commander.Command();129 program.option('--debug');130 const original = ['node', '--debug', 'arg'];131 const param = original.slice();132 program.parse(param);133 program.args.length = 0;134 program.rawArgs.length = 0;135 expect(param).toEqual(original);136 });137138 test('when parse called and param later changed then parsed args do not change', () => {139 const program = new commander.Command();140 program.option('--debug');141 const param = ['node', '--debug', 'arg'];142 program.parse(param);143 const oldArgs = program.args.slice();144 const oldRawArgs = program.rawArgs.slice();145 param.length = 0;146 expect(program.args).toEqual(oldArgs);147 expect(program.rawArgs).toEqual(oldRawArgs);148 });149});150151describe('parseAsync parameter is treated as readonly, per TypeScript declaration', () => {Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 152why, one step back152 test('when parse called then parameter does not change', async () => {Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c
lines 153–160why, one step back153 const program = new commander.Command();154 program.option('--debug');155 const original = ['node', '--debug', 'arg'];156 const param = original.slice();157 await program.parseAsync(param);158 expect(param).toEqual(original);159 });160Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 161why, one step back161 test('when parseAsync called and parsed args later changed then parameter does not change', async () => {Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c
lines 162–171why, one step back162 const program = new commander.Command();163 program.option('--debug');164 const original = ['node', '--debug', 'arg'];165 const param = original.slice();166 await program.parseAsync(param);167 program.args.length = 0;168 program.rawArgs.length = 0;169 expect(param).toEqual(original);170 });171Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 172why, one step back172 test('when parseAsync called and param later changed then parsed args do not change', async () => {Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c
lines 173–183why, one step back173 const program = new commander.Command();174 program.option('--debug');175 const param = ['node', '--debug', 'arg'];176 await program.parseAsync(param);177 const oldArgs = program.args.slice();178 const oldRawArgs = program.rawArgs.slice();179 param.length = 0;180 expect(program.args).toEqual(oldArgs);181 expect(program.rawArgs).toEqual(oldRawArgs);182 });183});