|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const {describe, it, expect, beforeEach, afterEach} = require('@jest/globals'); |
| 4 | + |
| 5 | +function createCommander() { |
| 6 | + jest.resetModules(); |
| 7 | + const commander = require('../utils/command'); |
| 8 | + const exits = []; |
| 9 | + commander.exitOverride(err => { |
| 10 | + exits.push(err); |
| 11 | + }); |
| 12 | + return {commander, exits}; |
| 13 | +} |
| 14 | + |
| 15 | +describe('MiniCommander', () => { |
| 16 | + let stdoutSpy; |
| 17 | + let consoleErrorSpy; |
| 18 | + |
| 19 | + beforeEach(() => { |
| 20 | + stdoutSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => true); |
| 21 | + consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); |
| 22 | + }); |
| 23 | + |
| 24 | + afterEach(() => { |
| 25 | + stdoutSpy.mockRestore(); |
| 26 | + consoleErrorSpy.mockRestore(); |
| 27 | + }); |
| 28 | + |
| 29 | + it('displays help and version', () => { |
| 30 | + const {commander, exits} = createCommander(); |
| 31 | + commander.usage('[file]'); |
| 32 | + commander.description('test description'); |
| 33 | + commander.version('1.2.3'); |
| 34 | + |
| 35 | + commander.parse(['node', '/tmp/cli.js', '--help']); |
| 36 | + expect(exits[0].code).toBe('commander.helpDisplayed'); |
| 37 | + expect(exits[0].exitCode).toBe(0); |
| 38 | + expect(stdoutSpy).toHaveBeenCalled(); |
| 39 | + |
| 40 | + commander.parse(['node', '/tmp/cli.js', '--version']); |
| 41 | + expect(exits[1].code).toBe('commander.version'); |
| 42 | + expect(exits[1].exitCode).toBe(0); |
| 43 | + }); |
| 44 | + |
| 45 | + it('handles unknown and missing arguments', () => { |
| 46 | + const {commander, exits} = createCommander(); |
| 47 | + commander.option('-f, --file <path>', 'file path'); |
| 48 | + |
| 49 | + commander.parse(['node', '/tmp/cli.js', '--unknown']); |
| 50 | + expect(exits[0].code).toBe('commander.unknownOption'); |
| 51 | + expect(exits[0].message).toContain('--unknown'); |
| 52 | + |
| 53 | + commander.parse(['node', '/tmp/cli.js', '--file']); |
| 54 | + expect(exits[1].code).toBe('commander.missingArgument'); |
| 55 | + expect(exits[1].message).toContain('--file'); |
| 56 | + }); |
| 57 | + |
| 58 | + it('parses long options, no-* options and positional args', () => { |
| 59 | + const {commander} = createCommander(); |
| 60 | + let capturedArg; |
| 61 | + let capturedOptions; |
| 62 | + |
| 63 | + commander |
| 64 | + .option('--name <value>', 'name') |
| 65 | + .option('--tag [value]', 'tag') |
| 66 | + .option('--no-sort', 'disable sort') |
| 67 | + .action((arg, options) => { |
| 68 | + capturedArg = arg; |
| 69 | + capturedOptions = options; |
| 70 | + }); |
| 71 | + |
| 72 | + commander.parse(['node', '/tmp/cli.js', 'input.yaml', '--name=alice', '--tag', 'beta', '--no-sort']); |
| 73 | + |
| 74 | + expect(capturedArg).toBe('input.yaml'); |
| 75 | + expect(capturedOptions).toMatchObject({ |
| 76 | + name: 'alice', |
| 77 | + tag: 'beta', |
| 78 | + sort: false |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + it('parses short options, including grouped flags and attached values', () => { |
| 83 | + const {commander} = createCommander(); |
| 84 | + let capturedOptions; |
| 85 | + |
| 86 | + commander |
| 87 | + .option('-v, --verbose', 'verbose') |
| 88 | + .option('-n, --number <num>', 'number', 7) |
| 89 | + .action((arg, options) => { |
| 90 | + capturedOptions = options; |
| 91 | + }); |
| 92 | + |
| 93 | + commander.parse(['node', '/tmp/cli.js', 'input.yaml', '-vn5']); |
| 94 | + expect(capturedOptions).toMatchObject({verbose: true, number: 5}); |
| 95 | + |
| 96 | + commander.parse(['node', '/tmp/cli.js', 'input.yaml', '-n', 'not-a-number']); |
| 97 | + expect(capturedOptions.number).toBe(7); |
| 98 | + }); |
| 99 | + |
| 100 | + it('uses parser functions with previous values and keeps default ordering behavior', () => { |
| 101 | + const {commander} = createCommander(); |
| 102 | + let capturedOptions; |
| 103 | + |
| 104 | + commander |
| 105 | + .option( |
| 106 | + '--include [value]', |
| 107 | + 'include values', |
| 108 | + (value, previous) => { |
| 109 | + const list = Array.isArray(previous) ? previous : []; |
| 110 | + return value === undefined ? list : list.concat(value); |
| 111 | + }, |
| 112 | + [] |
| 113 | + ) |
| 114 | + .action((arg, options) => { |
| 115 | + capturedOptions = options; |
| 116 | + }); |
| 117 | + |
| 118 | + commander.parse(['node', '/tmp/cli.js', 'input.yaml', '--include', 'a', '--include', 'b']); |
| 119 | + |
| 120 | + expect(capturedOptions.include).toStrictEqual(['a', 'b']); |
| 121 | + expect(Object.keys(capturedOptions)).toContain('include'); |
| 122 | + }); |
| 123 | + |
| 124 | + it('handles async action rejection by exiting with error code', async () => { |
| 125 | + const {commander, exits} = createCommander(); |
| 126 | + |
| 127 | + commander.action(async () => { |
| 128 | + throw new Error('boom'); |
| 129 | + }); |
| 130 | + |
| 131 | + commander.parse(['node', '/tmp/cli.js', 'input.yaml']); |
| 132 | + |
| 133 | + await new Promise(resolve => setImmediate(resolve)); |
| 134 | + expect(exits[0].code).toBe('commander.asyncActionRejected'); |
| 135 | + expect(exits[0].exitCode).toBe(1); |
| 136 | + }); |
| 137 | + |
| 138 | + it('handles unknown short options', () => { |
| 139 | + const {commander, exits} = createCommander(); |
| 140 | + commander.parse(['node', '/tmp/cli.js', '-z']); |
| 141 | + expect(exits[0].code).toBe('commander.unknownOption'); |
| 142 | + expect(exits[0].message).toContain("'-z'"); |
| 143 | + }); |
| 144 | +}); |
0 commit comments