Skip to content
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

Add tests on action handler for program #1063

Merged
merged 2 commits into from Sep 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
54 changes: 54 additions & 0 deletions tests/command.action.test.js
Expand Up @@ -34,3 +34,57 @@ test('when .action called with extra arguments then extras also passed to action
program.parse(['node', 'test', 'info', 'my-file', 'a']);
expect(actionMock).toHaveBeenCalledWith('my-file', cmd, ['a']);
});

test('when .action on program with argument then action called', () => {
const actionMock = jest.fn();
const program = new commander.Command();
program
.arguments('<file>')
.action(actionMock);
program.parse(['node', 'test', 'my-file']);
expect(actionMock).toHaveBeenCalledWith('my-file', program);
});

// Changes made in #729 to call program action handler
test('when .action on program and no arguments then action called', () => {
const actionMock = jest.fn();
const program = new commander.Command();
program
.action(actionMock);
program.parse(['node', 'test']);
expect(actionMock).toHaveBeenCalledWith(program);
});

test('when .action on program with optional argument supplied then action called', () => {
const actionMock = jest.fn();
const program = new commander.Command();
program
.arguments('[file]')
.action(actionMock);
program.parse(['node', 'test', 'my-file']);
expect(actionMock).toHaveBeenCalledWith('my-file', program);
});

test('when .action on program without optional argument supplied then action called', () => {
const actionMock = jest.fn();
const program = new commander.Command();
program
.arguments('[file]')
.action(actionMock);
program.parse(['node', 'test']);
expect(actionMock).toHaveBeenCalledWith(undefined, program);
});

test('when .action on program with subcommand and program argument then program action called', () => {
const actionMock = jest.fn();
const program = new commander.Command();
program
.arguments('[file]')
.action(actionMock);
program
.command('subcommand');

program.parse(['node', 'test', 'a']);

expect(actionMock).toHaveBeenCalledWith('a', program);
});
27 changes: 21 additions & 6 deletions tests/openIssues.test.js.skip
Expand Up @@ -4,7 +4,7 @@ const path = require('path');

describe('open issues', () => {
// https://github.com/tj/commander.js/issues/1039
test('#1039: when unknown option then unknown option detected', () => {
test.skip('#1039: when unknown option then unknown option detected', () => {
const program = new commander.Command();
program
.exitOverride();
Expand All @@ -13,7 +13,7 @@ describe('open issues', () => {
}).toThrow();
});

test('#1039: when unknown option and multiple arguments then unknown option detected', () => {
test.skip('#1039: when unknown option and multiple arguments then unknown option detected', () => {
const program = new commander.Command();
program
.exitOverride();
Expand All @@ -33,20 +33,20 @@ describe('open issues', () => {
return program;
};

test('#1032: when specify subcommand then args not empty', () => {
test.skip('#1032: when specify subcommand then args not empty', () => {
const program = createProgram1032();
program.parse(['node', 'test.js', 'doit', 'myid']);
expect(program.args.length).toBeGreaterThan(0);
});

test('#1032: when specify subcommand and option then args not empty', () => {
test.skip('#1032: when specify subcommand and option then args not empty', () => {
const program = createProgram1032();
program.parse(['node', 'test.js', 'doit', '--better', 'myid']);
expect(program.args.length).toBeGreaterThan(0);
});

// https://github.com/tj/commander.js/issues/561
test('#561: when specify argument and unknown option then error', () => {
test.skip('#561: when specify argument and unknown option then error', () => {
const program = new commander.Command();
program
.exitOverride()
Expand All @@ -59,11 +59,26 @@ describe('open issues', () => {
});

// https://github.com/tj/commander.js/issues/508
test('#508: when arguments to executable include option flags then argument order preserved', (done) => {
test.skip('#508: when arguments to executable include option flags then argument order preserved', (done) => {
const pm = path.join(__dirname, 'fixtures/pm');
childProcess.execFile('node', [pm, 'echo', '1', '2', '--dry-run', '3', '4', '5', '6'], function(_error, stdout, stderr) {
expect(stdout).toBe("[ '1', '2', '--dry-run', '3', '4', '5', '6' ]\n");
done();
});
});

// https://github.com/tj/commander.js/pull/1062
test.skip('#1062 when .action on program with subcommands and no program argument then program action called', () => {
const actionMock = jest.fn();
const program = new commander.Command();
program
.arguments('[file]')
.action(actionMock);
program
.command('subcommand');

program.parse(['node', 'test']);

expect(actionMock).toHaveBeenCalledWith(undefined, program);
});
});