Skip to content

Support options with only a short flag #1256

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 26, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add tests for lone short and long flags
  • Loading branch information
shadowspawn committed May 24, 2020
commit af45c33ee1f5ee415b191b2252f8c05f4d0d919e
80 changes: 62 additions & 18 deletions tests/command.helpOption.test.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,66 @@
const commander = require('../');

test('when helpOption has custom flags then custom flag invokes help', () => {
// Optional. Suppress normal output to keep test output clean.
const writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { });
const program = new commander.Command();
program
.exitOverride()
.helpOption('--custom-help', 'custom help output');
expect(() => {
program.parse(['node', 'test', '--custom-help']);
}).toThrow('(outputHelp)');
writeSpy.mockClear();
});
describe('helpOption', () => {
let writeSpy;

beforeAll(() => {
// Optional. Suppress normal output to keep test output clean.
writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { });
});

afterEach(() => {
writeSpy.mockClear();
});

afterAll(() => {
writeSpy.mockRestore();
});

test('when helpOption has custom flags then custom short flag invokes help', () => {
const program = new commander.Command();
program
.exitOverride()
.helpOption('-c,--custom-help', 'custom help output');
expect(() => {
program.parse(['-c'], { from: 'user' });
}).toThrow('(outputHelp)');
});

test('when helpOption has custom flags then custom long flag invokes help', () => {
const program = new commander.Command();
program
.exitOverride()
.helpOption('-c,--custom-help', 'custom help output');
expect(() => {
program.parse(['--custom-help'], { from: 'user' });
}).toThrow('(outputHelp)');
});

test('when helpOption has just custom short flag then custom short flag invokes help', () => {
const program = new commander.Command();
program
.exitOverride()
.helpOption('-c', 'custom help output');
expect(() => {
program.parse(['-c'], { from: 'user' });
}).toThrow('(outputHelp)');
});

test('when helpOption has just custom long flag then custom long flag invokes help', () => {
const program = new commander.Command();
program
.exitOverride()
.helpOption('--custom-help', 'custom help output');
expect(() => {
program.parse(['--custom-help'], { from: 'user' });
}).toThrow('(outputHelp)');
});

test('when helpOption has custom description then helpInformation include custom description', () => {
const program = new commander.Command();
program
.helpOption('-C,--custom-help', 'custom help output');
const helpInformation = program.helpInformation();
expect(helpInformation).toMatch(/-C,--custom-help +custom help output/);
test('when helpOption has custom description then helpInformation include custom description', () => {
const program = new commander.Command();
program
.helpOption('-C,--custom-help', 'custom help output');
const helpInformation = program.helpInformation();
expect(helpInformation).toMatch(/-C,--custom-help +custom help output/);
});
});
10 changes: 9 additions & 1 deletion tests/options.flags.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ const commander = require('../');

// Test the various ways flags can be specified in the first parameter to `.option`

test('when only short flag defined and not specified then value is undefined', () => {
const program = new commander.Command();
program
.option('-p', 'add pepper');
program.parse(['node', 'test']);
expect(program.p).toBeUndefined();
});

// Sanity check that pepper is not true normally, as otherwise all the following tests would pass for thr wrong reasons!
test('when only short flag defined and specified then value is true', () => {
const program = new commander.Command();
program
Expand All @@ -10,7 +19,6 @@ test('when only short flag defined and specified then value is true', () => {
expect(program.p).toBe(true);
});

// Sanity check that pepper is not true normally, as otherwise all the following tests would pass for thr wrong reasons!
test('when only long flag defined and not specified then value is undefined', () => {
const program = new commander.Command();
program
Expand Down
24 changes: 24 additions & 0 deletions tests/options.version.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,18 @@ describe('.version', () => {
}).toThrow(myVersion);
});

test('when specify just custom short flag then display version', () => {
const myVersion = '1.2.3';
const program = new commander.Command();
program
.exitOverride()
.version(myVersion, '-r');

expect(() => {
program.parse(['node', 'test', '-r']);
}).toThrow(myVersion);
});

test('when specify custom long flag then display version', () => {
const myVersion = '1.2.3';
const program = new commander.Command();
Expand All @@ -101,6 +113,18 @@ describe('.version', () => {
}).toThrow(myVersion);
});

test('when specify just custom long flag then display version', () => {
const myVersion = '1.2.3';
const program = new commander.Command();
program
.exitOverride()
.version(myVersion, '--revision');

expect(() => {
program.parse(['node', 'test', '--revision']);
}).toThrow(myVersion);
});

test('when custom .version then helpInformation includes custom version help', () => {
const myVersion = '1.2.3';
const myVersionFlags = '-r, --revision';
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy