command.configureOutput.test.js
This file
Path
tests/command.configureOutput.test.js
275 lines · 7.7 KiB · JavaScript
Last changed by
Rename columns to helpWidth (#1396)John Geebefore adoptioneac4787
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276const commander = require('../');
test('when default writeErr() then error on stderr', () => { const writeSpy = jest.spyOn(process.stderr, 'write').mockImplementation(() => { }); const program = new commander.Command(); program.exitOverride();
try { program.parse(['--unknown'], { from: 'user' }); } catch (err) { }
expect(writeSpy).toHaveBeenCalledTimes(1); writeSpy.mockRestore();});
test('when custom writeErr() then error on custom output', () => { const writeSpy = jest.spyOn(process.stderr, 'write').mockImplementation(() => { }); const customWrite = jest.fn(); const program = new commander.Command(); program .exitOverride() .configureOutput({ writeErr: customWrite });
try { program.parse(['--unknown'], { from: 'user' }); } catch (err) { }
expect(writeSpy).toHaveBeenCalledTimes(0); expect(customWrite).toHaveBeenCalledTimes(1); writeSpy.mockRestore();});
test('when default write() then version on stdout', () => { const writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { }); const program = new commander.Command(); program .exitOverride() .version('1.2.3');
expect(() => { program.parse(['--version'], { from: 'user' }); }).toThrow();
expect(writeSpy).toHaveBeenCalledTimes(1); writeSpy.mockRestore();});
test('when custom write() then version on custom output', () => { const writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { }); const customWrite = jest.fn(); const program = new commander.Command(); program .exitOverride() .version('1.2.3') .configureOutput({ writeOut: customWrite });
expect(() => { program.parse(['--version'], { from: 'user' }); }).toThrow();
expect(writeSpy).toHaveBeenCalledTimes(0); expect(customWrite).toHaveBeenCalledTimes(1); writeSpy.mockRestore();});
test('when default write() then help on stdout', () => { const writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { }); const program = new commander.Command(); program.outputHelp();
expect(writeSpy).toHaveBeenCalledTimes(1); writeSpy.mockRestore();});
test('when custom write() then help error on custom output', () => { const writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { }); const customWrite = jest.fn(); const program = new commander.Command(); program.configureOutput({ writeOut: customWrite }); program.outputHelp();
expect(writeSpy).toHaveBeenCalledTimes(0); expect(customWrite).toHaveBeenCalledTimes(1); writeSpy.mockRestore();});
test('when default writeErr then help error on stderr', () => { const writeSpy = jest.spyOn(process.stderr, 'write').mockImplementation(() => { }); const program = new commander.Command(); program.outputHelp({ error: true });
expect(writeSpy).toHaveBeenCalledTimes(1); writeSpy.mockRestore();});
test('when custom writeErr then help error on custom output', () => { const writeSpy = jest.spyOn(process.stderr, 'write').mockImplementation(() => { }); const customWrite = jest.fn(); const program = new commander.Command(); program.configureOutput({ writeErr: customWrite }); program.outputHelp({ error: true });
expect(writeSpy).toHaveBeenCalledTimes(0); expect(customWrite).toHaveBeenCalledTimes(1); writeSpy.mockRestore();});
test('when default getOutHelpWidth then help helpWidth from stdout', () => { const expectedColumns = 123; const holdIsTTY = process.stdout.isTTY; const holdColumns = process.stdout.columns; let helpWidth;
process.stderr.isTTY = true; process.stdout.columns = expectedColumns; process.stdout.isTTY = true; const program = new commander.Command(); program .configureHelp({ formatHelp: (cmd, helper) => { helpWidth = helper.helpWidth; return ''; } }); program.outputHelp();
expect(helpWidth).toBe(expectedColumns); process.stdout.columns = holdColumns; process.stdout.isTTY = holdIsTTY;});
test('when custom getOutHelpWidth then help helpWidth custom', () => { const expectedColumns = 123; let helpWidth;
const program = new commander.Command(); program .configureHelp({ formatHelp: (cmd, helper) => { helpWidth = helper.helpWidth; return ''; } }).configureOutput({ getOutHelpWidth: () => expectedColumns }); program.outputHelp();
expect(helpWidth).toBe(expectedColumns);});
test('when default getErrHelpWidth then help error helpWidth from stderr', () => { const expectedColumns = 123; const holdIsTTY = process.stderr.isTTY; const holdColumns = process.stderr.columns; let helpWidth;
process.stderr.isTTY = true; process.stderr.columns = expectedColumns; const program = new commander.Command(); program .configureHelp({ formatHelp: (cmd, helper) => { helpWidth = helper.helpWidth; return ''; } }); program.outputHelp({ error: true });
expect(helpWidth).toBe(expectedColumns); process.stderr.isTTY = holdIsTTY; process.stderr.columns = holdColumns;});
test('when custom getErrHelpWidth then help error helpWidth custom', () => { const expectedColumns = 123; let helpWidth;
const program = new commander.Command(); program .configureHelp({ formatHelp: (cmd, helper) => { helpWidth = helper.helpWidth; return ''; } }).configureOutput({ getErrHelpWidth: () => expectedColumns }); program.outputHelp({ error: true });
expect(helpWidth).toBe(expectedColumns);});
test('when custom getOutHelpWidth and configureHelp:helpWidth then help helpWidth from configureHelp', () => { const expectedColumns = 123; let helpWidth;
const program = new commander.Command(); program .configureHelp({ formatHelp: (cmd, helper) => { helpWidth = helper.helpWidth; return ''; }, helpWidth: expectedColumns }).configureOutput({ getOutHelpWidth: () => 999 }); program.outputHelp();
expect(helpWidth).toBe(expectedColumns);});
test('when custom getErrHelpWidth and configureHelp:helpWidth then help error helpWidth from configureHelp', () => { const expectedColumns = 123; let helpWidth;
const program = new commander.Command(); program .configureHelp({ formatHelp: (cmd, helper) => { helpWidth = helper.helpWidth; return ''; }, helpWidth: expectedColumns }).configureOutput({ getErrHelpWidth: () => 999 }); program.outputHelp({ error: true });
expect(helpWidth).toBe(expectedColumns);});
test('when set configureOutput then get configureOutput', () => { const outputOptions = { writeOut: jest.fn(), writeErr: jest.fn(), getOutHelpWidth: jest.fn(), getErrHelpWidth: jest.fn(), outputError: jest.fn() }; const program = new commander.Command(); program.configureOutput(outputOptions); expect(program.configureOutput()).toEqual(outputOptions);});
test('when custom outputErr and error then outputErr called', () => { const outputError = jest.fn(); const program = new commander.Command(); program .exitOverride() .configureOutput({ outputError });
expect(() => { program.parse(['--unknownOption'], { from: 'user' }); }).toThrow(); expect(outputError).toHaveBeenCalledWith("error: unknown option '--unknownOption'\n", program._outputConfiguration.writeErr);});
test('when custom outputErr and writeErr and error then outputErr passed writeErr', () => { const writeErr = () => jest.fn(); const outputError = jest.fn(); const program = new commander.Command(); program .exitOverride() .configureOutput({ writeErr, outputError });
expect(() => { program.parse(['--unknownOption'], { from: 'user' }); }).toThrow(); expect(outputError).toHaveBeenCalledWith("error: unknown option '--unknownOption'\n", writeErr);});