From fd54342d071c489200abb93a09dc33fa7c07f1e5 Mon Sep 17 00:00:00 2001 From: John Gee Date: Sun, 29 Sep 2019 22:35:24 +1300 Subject: [PATCH 1/2] Add more tests on action handlers on program --- tests/command.action.test.js | 54 ++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/tests/command.action.test.js b/tests/command.action.test.js index cb3f69862..fa0f85c35 100644 --- a/tests/command.action.test.js +++ b/tests/command.action.test.js @@ -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('') + .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); +}); From 03b00349cf381d9377f379dd3fbb66427399e657 Mon Sep 17 00:00:00 2001 From: John Gee Date: Sun, 29 Sep 2019 22:35:55 +1300 Subject: [PATCH 2/2] Add test for PR #1062 --- tests/openIssues.test.js.skip | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/tests/openIssues.test.js.skip b/tests/openIssues.test.js.skip index 78c4dd14b..202291de5 100644 --- a/tests/openIssues.test.js.skip +++ b/tests/openIssues.test.js.skip @@ -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(); @@ -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(); @@ -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() @@ -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); + }); });