Skip to content

Commit

Permalink
Merge pull request #1063 from shadowspawn/feature/test-main
Browse files Browse the repository at this point in the history
Add tests on action handler for program
  • Loading branch information
abetomo committed Sep 29, 2019
2 parents 9499d47 + 03b0034 commit a45d676
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 6 deletions.
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);
});
});

0 comments on commit a45d676

Please sign in to comment.