|
| 1 | +/** |
| 2 | + * Tests for angular-cli-ghpages fork-specific fixes and features |
| 3 | + * |
| 4 | + * This file contains tests for the 2 intentional improvements we made |
| 5 | + * to commander v3.0.2 for angular-cli-ghpages: |
| 6 | + * |
| 7 | + * FIX 1/2: Tightened negate detection regex |
| 8 | + * FIX 2/2: Support for short-only and custom version flags |
| 9 | + * |
| 10 | + * These tests do NOT exist in the original commander v3.0.2. |
| 11 | + */ |
| 12 | + |
| 13 | +const commander = require('../'); |
| 14 | + |
| 15 | +describe('Fork Fix 1: Negate detection with tightened regex', () => { |
| 16 | + // This is implicitly tested by test.options.bool.no.spec.ts |
| 17 | + // No additional tests needed here |
| 18 | +}); |
| 19 | + |
| 20 | +describe('Fork Fix 2: Version short-only and custom flags', () => { |
| 21 | + describe('version() with short-only flag', () => { |
| 22 | + let oldExit: any; |
| 23 | + let oldWrite: any; |
| 24 | + let captured: {out: string, code: null | number}; |
| 25 | + |
| 26 | + beforeEach(() => { |
| 27 | + captured = {out: '', code: null}; |
| 28 | + oldExit = process.exit; |
| 29 | + oldWrite = process.stdout.write; |
| 30 | + process.exit = ((c: any) => { |
| 31 | + captured.code = c as number; |
| 32 | + }) as any; |
| 33 | + process.stdout.write = ((s: any) => { |
| 34 | + captured.out += s; |
| 35 | + return true; |
| 36 | + }) as any; |
| 37 | + }); |
| 38 | + |
| 39 | + afterEach(() => { |
| 40 | + process.exit = oldExit; |
| 41 | + process.stdout.write = oldWrite; |
| 42 | + }); |
| 43 | + |
| 44 | + it('prints version and exits with -v short-only flag', () => { |
| 45 | + const program = new commander.Command(); |
| 46 | + program.version('1.2.3', '-v'); |
| 47 | + program.parse(['node', 'x', '-v']); |
| 48 | + expect(captured.out).toBe('1.2.3\n'); |
| 49 | + expect(captured.code).toBe(0); |
| 50 | + }); |
| 51 | + |
| 52 | + it('prints version with long-only custom flag', () => { |
| 53 | + captured = {out: '', code: null}; |
| 54 | + const program = new commander.Command(); |
| 55 | + program.version('2.0.0', '--ver'); |
| 56 | + program.parse(['node', 'x', '--ver']); |
| 57 | + expect(captured.out).toBe('2.0.0\n'); |
| 58 | + expect(captured.code).toBe(0); |
| 59 | + }); |
| 60 | + |
| 61 | + it('prints version with combined custom flags', () => { |
| 62 | + captured = {out: '', code: null}; |
| 63 | + const program = new commander.Command(); |
| 64 | + program.version('3.0.0', '-v, --ver'); |
| 65 | + program.parse(['node', 'x', '--ver']); |
| 66 | + expect(captured.out).toBe('3.0.0\n'); |
| 67 | + expect(captured.code).toBe(0); |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + describe('helpOption() with custom flags', () => { |
| 72 | + let oldExit: any; |
| 73 | + let oldWrite: any; |
| 74 | + let exitCode: null | number; |
| 75 | + |
| 76 | + beforeEach(() => { |
| 77 | + exitCode = null; |
| 78 | + oldExit = process.exit; |
| 79 | + oldWrite = process.stdout.write; |
| 80 | + process.exit = ((c: any) => { |
| 81 | + exitCode = c as number; |
| 82 | + throw new Error(`exit(${c})`); |
| 83 | + }) as any; |
| 84 | + process.stdout.write = (() => true) as any; |
| 85 | + }); |
| 86 | + |
| 87 | + afterEach(() => { |
| 88 | + process.exit = oldExit; |
| 89 | + process.stdout.write = oldWrite; |
| 90 | + }); |
| 91 | + |
| 92 | + it('fires help listener with default flags', () => { |
| 93 | + const program = new commander.Command(); |
| 94 | + const spy = jest.fn(); |
| 95 | + program.on('--help', spy); |
| 96 | + expect(() => program.parse(['node', 'x', '--help'])).toThrow('exit(0)'); |
| 97 | + expect(spy).toHaveBeenCalled(); |
| 98 | + expect(exitCode).toBe(0); |
| 99 | + }); |
| 100 | + |
| 101 | + it('fires help listener after helpOption override', () => { |
| 102 | + const program = new commander.Command().helpOption('-?, --helpme'); |
| 103 | + const spy = jest.fn(); |
| 104 | + program.on('--helpme', spy); |
| 105 | + expect(() => program.parse(['node', 'x', '--helpme'])).toThrow('exit(0)'); |
| 106 | + expect(spy).toHaveBeenCalled(); |
| 107 | + expect(exitCode).toBe(0); |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + describe('opts() includes version with custom flags', () => { |
| 112 | + let oldExit: any; |
| 113 | + let oldWrite: any; |
| 114 | + |
| 115 | + beforeEach(() => { |
| 116 | + oldExit = process.exit; |
| 117 | + oldWrite = process.stdout.write; |
| 118 | + process.exit = (() => {}) as any; |
| 119 | + process.stdout.write = (() => true) as any; |
| 120 | + }); |
| 121 | + |
| 122 | + afterEach(() => { |
| 123 | + process.exit = oldExit; |
| 124 | + process.stdout.write = oldWrite; |
| 125 | + }); |
| 126 | + |
| 127 | + it('includes version in opts() with default flags', () => { |
| 128 | + const program = new commander.Command(); |
| 129 | + program.version('1.2.3'); |
| 130 | + program.parse(['node', 'x']); |
| 131 | + |
| 132 | + const opts = program.opts(); |
| 133 | + expect(opts.version).toBe('1.2.3'); |
| 134 | + }); |
| 135 | + |
| 136 | + it('includes version in opts() with custom long flag', () => { |
| 137 | + const program = new commander.Command(); |
| 138 | + program.version('2.0.0', '-V, --ver'); |
| 139 | + program.parse(['node', 'x']); |
| 140 | + |
| 141 | + const opts = program.opts(); |
| 142 | + expect(opts.ver).toBe('2.0.0'); |
| 143 | + }); |
| 144 | + |
| 145 | + it('includes version in opts() with short-only flag', () => { |
| 146 | + const program = new commander.Command(); |
| 147 | + program.version('3.1.4', '-v'); |
| 148 | + program.parse(['node', 'x']); |
| 149 | + |
| 150 | + const opts = program.opts(); |
| 151 | + // Note: Short-only flags like '-v' become capital 'V' in opts due to camelCase conversion |
| 152 | + expect(opts.V).toBe('3.1.4'); |
| 153 | + }); |
| 154 | + |
| 155 | + it('includes version in opts() with long-only flag', () => { |
| 156 | + const program = new commander.Command(); |
| 157 | + program.version('1.0.0', '--version-info'); |
| 158 | + program.parse(['node', 'x']); |
| 159 | + |
| 160 | + const opts = program.opts(); |
| 161 | + expect(opts.versionInfo).toBe('1.0.0'); |
| 162 | + }); |
| 163 | + |
| 164 | + it('includes version alongside other options', () => { |
| 165 | + const program = new commander.Command(); |
| 166 | + program |
| 167 | + .version('1.2.3') |
| 168 | + .option('-d, --dir <dir>', 'directory', 'dist') |
| 169 | + .option('-v, --verbose', 'verbose'); |
| 170 | + program.parse(['node', 'x', '--verbose']); |
| 171 | + |
| 172 | + const opts = program.opts(); |
| 173 | + expect(opts.version).toBe('1.2.3'); |
| 174 | + expect(opts.dir).toBe('dist'); |
| 175 | + expect(opts.verbose).toBe(true); |
| 176 | + }); |
| 177 | + }); |
| 178 | + |
| 179 | + describe('unknown options with attached values', () => { |
| 180 | + let errSpy: jest.SpyInstance; |
| 181 | + |
| 182 | + beforeEach(() => { |
| 183 | + errSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); |
| 184 | + jest.spyOn(process, 'exit').mockImplementation((() => {}) as any); |
| 185 | + }); |
| 186 | + |
| 187 | + afterEach(() => { |
| 188 | + errSpy.mockRestore(); |
| 189 | + (process.exit as any).mockRestore(); |
| 190 | + }); |
| 191 | + |
| 192 | + describe('errors by default', () => { |
| 193 | + test.each([ |
| 194 | + ['--mystery=42'], |
| 195 | + ['--mystery='], |
| 196 | + ['--mystery', '-5'], |
| 197 | + ])('errors on %p', (...args) => { |
| 198 | + const p = new commander.Command().option('-k, --known <v>'); |
| 199 | + p.parse(['node', 'x', ...args]); |
| 200 | + expect(errSpy).toHaveBeenCalled(); |
| 201 | + }); |
| 202 | + }); |
| 203 | + |
| 204 | + describe('allowed with allowUnknownOption', () => { |
| 205 | + test.each([ |
| 206 | + ['--mystery=42'], |
| 207 | + ['--mystery='], |
| 208 | + ['--mystery', '-5'], |
| 209 | + ])('allowed with %p', (...args) => { |
| 210 | + errSpy.mockClear(); |
| 211 | + const p = new commander.Command().allowUnknownOption(); |
| 212 | + p.parse(['node', 'x', ...args]); |
| 213 | + expect(errSpy).not.toHaveBeenCalled(); |
| 214 | + }); |
| 215 | + }); |
| 216 | + |
| 217 | + it('handles mixed known and unknown options', () => { |
| 218 | + errSpy.mockClear(); |
| 219 | + const p = new commander.Command() |
| 220 | + .option('-k, --known <val>') |
| 221 | + .allowUnknownOption(); |
| 222 | + p.parse(['node', 'x', '-k', 'knownValue', '--unknown=foo']); |
| 223 | + expect(errSpy).not.toHaveBeenCalled(); |
| 224 | + expect(p.known).toBe('knownValue'); |
| 225 | + }); |
| 226 | + |
| 227 | + it('handles equals with spaces in value', () => { |
| 228 | + errSpy.mockClear(); |
| 229 | + const p = new commander.Command().allowUnknownOption(); |
| 230 | + p.parse(['node', 'x', '--mystery=some value']); |
| 231 | + expect(errSpy).not.toHaveBeenCalled(); |
| 232 | + }); |
| 233 | + }); |
| 234 | +}); |
0 commit comments