Skip to content
Myr

command.hook.test.js

This file

Path

tests/command.hook.test.js

417 lines · 13.6 KiB · JavaScript

Last changed by

Switching to esm-only (#2464)John Geebefore adoptionf4bd470

History of this file

Take it

1import * as commander from '../index.js';2import { test, describe } from 'node:test';3import assert from 'node:assert/strict';45describe('Command.hook()', () => {6  test('when hook event wrong then throw', () => {7    const program = new commander.Command();8    assert.throws(() => {9      program.hook('silly', () => {});10    });11  });1213  test('when no action then action hooks not called', (t) => {14    const hook = t.mock.fn();15    const program = new commander.Command();16    program.hook('preAction', hook).hook('postAction', hook);17    program.parse([], { from: 'user' });18    assert.equal(hook.mock.callCount(), 0);19  });2021  describe('action hooks with synchronous hooks, order', () => {22    test('when hook preAction then hook called before action', () => {23      const calls = [];24      const program = new commander.Command();25      program26        .hook('preAction', () => calls.push('before'))27        .action(() => calls.push('action'));28      program.parse([], { from: 'user' });29      assert.deepEqual(calls, ['before', 'action']);30    });3132    test('when hook postAction then hook called after action', () => {33      const calls = [];34      const program = new commander.Command();35      program36        .hook('postAction', () => calls.push('after'))37        .action(() => calls.push('action'));38      program.parse([], { from: 'user' });39      assert.deepEqual(calls, ['action', 'after']);40    });4142    test('when hook preAction twice then hooks called FIFO', () => {43      const calls = [];44      const program = new commander.Command();45      program46        .hook('preAction', () => calls.push('1'))47        .hook('preAction', () => calls.push('2'))48        .action(() => calls.push('action'));49      program.parse([], { from: 'user' });50      assert.deepEqual(calls, ['1', '2', 'action']);51    });5253    test('when hook postAction twice then hooks called LIFO', () => {54      const calls = [];55      const program = new commander.Command();56      program57        .hook('postAction', () => calls.push('1'))58        .hook('postAction', () => calls.push('2'))59        .action(() => calls.push('action'));60      program.parse([], { from: 'user' });61      assert.deepEqual(calls, ['action', '2', '1']);62    });6364    test('when hook preAction at program and sub then hooks called program then sub', () => {65      const calls = [];66      const program = new commander.Command();67      program.hook('preAction', () => calls.push('program'));68      program69        .command('sub')70        .hook('preAction', () => calls.push('sub'))71        .action(() => calls.push('action'));72      program.parse(['sub'], { from: 'user' });73      assert.deepEqual(calls, ['program', 'sub', 'action']);74    });7576    test('when hook postAction at program and sub then hooks called sub then program', () => {77      const calls = [];78      const program = new commander.Command();79      program.hook('postAction', () => calls.push('program'));80      program81        .command('sub')82        .hook('postAction', () => calls.push('sub'))83        .action(() => calls.push('action'));84      program.parse(['sub'], { from: 'user' });85      assert.deepEqual(calls, ['action', 'sub', 'program']);86    });8788    test('when hook everything then hooks called nested', () => {89      const calls = [];90      const program = new commander.Command();91      program92        .hook('preAction', () => calls.push('pb1'))93        .hook('postAction', () => calls.push('pa1'));94      program95        .hook('preAction', () => calls.push('pb2'))96        .hook('postAction', () => calls.push('pa2'));97      program98        .command('sub')99        .hook('preAction', () => calls.push('sb'))100        .hook('postAction', () => calls.push('sa'))101        .action(() => calls.push('action'));102      program.parse(['sub'], { from: 'user' });103      assert.deepEqual(calls, [104        'pb1',105        'pb2',106        'sb',107        'action',108        'sa',109        'pa2',110        'pa1',111      ]);112    });113  });114115  describe('action hooks context', () => {116    test('when hook on program then passed program/program', (t) => {117      const hook = t.mock.fn();118      const program = new commander.Command();119      program.hook('preAction', hook).action(() => {});120      program.parse([], { from: 'user' });121      assert.equal(hook.mock.callCount(), 1);122      const callArgs = hook.mock.calls[0].arguments;123      assert.equal(callArgs[0], program);124      assert.equal(callArgs[1], program);125    });126127    test('when hook on program and call sub then passed program/sub', (t) => {128      const hook = t.mock.fn();129      const program = new commander.Command();130      program.hook('preAction', hook);131      const sub = program.command('sub').action(() => {});132      program.parse(['sub'], { from: 'user' });133      assert.equal(hook.mock.callCount(), 1);134      const callArgs = hook.mock.calls[0].arguments;135      assert.equal(callArgs[0], program);136      assert.equal(callArgs[1], sub);137    });138139    test('when hook on sub and call sub then passed sub/sub', (t) => {140      const hook = t.mock.fn();141      const program = new commander.Command();142      const sub = program143        .command('sub')144        .hook('preAction', hook)145        .action(() => {});146      program.parse(['sub'], { from: 'user' });147      assert.equal(hook.mock.callCount(), 1);148      const callArgs = hook.mock.calls[0].arguments;149      assert.equal(callArgs[0], sub);150      assert.equal(callArgs[1], sub);151    });152153    test('when hook program on preAction then thisCommand has options set', () => {154      const program = new commander.Command();155      program156        .option('--debug')157        .hook('preAction', (thisCommand) => {158          assert.equal(thisCommand.opts().debug, true);159        })160        .action(() => {});161      program.parse(['--debug'], { from: 'user' });162    });163164    test('when hook program on preAction and call sub then thisCommand has program options set', () => {165      const program = new commander.Command();166      program.option('--debug').hook('preAction', (thisCommand) => {167        assert.equal(thisCommand.opts().debug, true);168      });169      program.command('sub').action(() => {});170      program.parse(['sub', '--debug'], { from: 'user' });171    });172173    test('when hook program on preAction and call sub then actionCommand has sub options set', () => {174      const program = new commander.Command();175      program.hook('preAction', (thisCommand, actionCommand) => {176        assert.equal(actionCommand.opts().debug, true);177      });178      program179        .command('sub')180        .option('--debug')181        .action(() => {});182      program.parse(['sub', '--debug'], { from: 'user' });183    });184185    test('when hook program on preAction then actionCommand has args set', () => {186      const program = new commander.Command();187      program188        .argument('[arg]')189        .hook('preAction', (thisCommand, actionCommand) => {190          assert.deepEqual(actionCommand.args, ['value']);191        })192        .action(() => {});193      program.parse(['value'], { from: 'user' });194    });195196    test('when hook program on preAction then actionCommand has args set with options removed', () => {197      const program = new commander.Command();198      program199        .argument('[arg]')200        .option('--debug')201        .hook('preAction', (thisCommand, actionCommand) => {202          assert.deepEqual(actionCommand.args, ['value']);203        })204        .action(() => {});205      program.parse(['value', '--debug'], { from: 'user' });206    });207208    test('when hook program on preAction and call sub then thisCommand has program args set', () => {209      const program = new commander.Command();210      program.argument('[arg]').hook('preAction', (thisCommand) => {211        assert.deepEqual(thisCommand.args, ['sub', 'value']);212      });213      program214        .command('sub')215        .argument('<arg>')216        .action(() => {});217      program.parse(['sub', 'value'], { from: 'user' });218    });219220    test('when hook program on preAction and call sub then actionCommand has sub args set', () => {221      const program = new commander.Command();222      program.hook('preAction', (thisCommand, actionCommand) => {223        assert.deepEqual(actionCommand.args, ['value']);224      });225      program226        .command('sub')227        .argument('<arg>')228        .action(() => {});229      program.parse(['sub', 'value'], { from: 'user' });230    });231  });232233  describe('action hooks async', () => {234    test('when async preAction then async from preAction', async () => {235      const calls = [];236      const program = new commander.Command();237      program238        .hook('preAction', async () => {239          await 0;240          calls.push('before');241        })242        .action(() => calls.push('action'));243      const result = program.parseAsync([], { from: 'user' });244      assert.deepEqual(calls, []);245      await result;246      assert.deepEqual(calls, ['before', 'action']);247    });248249    test('when async postAction then async from postAction', async () => {250      const calls = [];251      const program = new commander.Command();252      program253        .hook('postAction', async () => {254          await 0;255          calls.push('after');256        })257        .action(() => calls.push('action'));258      const result = program.parseAsync([], { from: 'user' });259      assert.deepEqual(calls, ['action']);260      await result;261      assert.deepEqual(calls, ['action', 'after']);262    });263264    test('when async action then async from action', async () => {265      const calls = [];266      const program = new commander.Command();267      program268        .hook('preAction', () => calls.push('before'))269        .hook('postAction', () => calls.push('after'))270        .action(async () => {271          await 0;272          calls.push('action');273        });274      const result = program.parseAsync([], { from: 'user' });275      assert.deepEqual(calls, ['before']);276      await result;277      assert.deepEqual(calls, ['before', 'action', 'after']);278    });279280    test('when async first preAction then async from first preAction', async () => {281      const calls = [];282      const program = new commander.Command();283      program284        .hook('preAction', async () => {285          await 0;286          calls.push('1');287        })288        .hook('preAction', () => calls.push('2'))289        .action(() => calls.push('action'));290      const result = program.parseAsync([], { from: 'user' });291      assert.deepEqual(calls, []);292      await result;293      assert.deepEqual(calls, ['1', '2', 'action']);294    });295296    test('when async second preAction then async from second preAction', async () => {297      const calls = [];298      const program = new commander.Command();299      program300        .hook('preAction', () => calls.push('1'))301        .hook('preAction', async () => {302          await 0;303          calls.push('2');304        })305        .action(() => calls.push('action'));306      const result = program.parseAsync([], { from: 'user' });307      assert.deepEqual(calls, ['1']);308      await result;309      assert.deepEqual(calls, ['1', '2', 'action']);310    });311312    test('when async hook everything then hooks called nested', async () => {313      const calls = [];314      const program = new commander.Command();315      program316        .hook('preAction', async () => {317          await 0;318          calls.push('pb1');319        })320        .hook('postAction', async () => {321          await 0;322          calls.push('pa1');323        });324      program325        .hook('preAction', async () => {326          await 0;327          calls.push('pb2');328        })329        .hook('postAction', async () => {330          await 0;331          calls.push('pa2');332        });333      program334        .command('sub')335        .hook('preAction', async () => {336          await 0;337          calls.push('sb');338        })339        .hook('postAction', async () => {340          await 0;341          calls.push('sa');342        })343        .action(async () => {344          await 0;345          calls.push('action');346        });347      const result = program.parseAsync(['sub'], { from: 'user' });348      assert.deepEqual(calls, []);349      await result;350      assert.deepEqual(calls, [351        'pb1',352        'pb2',353        'sb',354        'action',355        'sa',356        'pa2',357        'pa1',358      ]);359    });360361    test('preSubcommand hook should work', async () => {362      const calls = [];363      const program = new commander.Command();364      program.hook('preSubcommand', async () => {365        await 0;366        calls.push(0);367      });368      program.command('sub').action(async () => {369        await 1;370        calls.push(1);371      });372      program.action(async () => {373        await 2;374        calls.push(2);375      });376      const result = program.parseAsync(['sub'], { from: 'user' });377      assert.deepEqual(calls, []);378      await result;379      assert.deepEqual(calls, [0, 1]);380    });381    test('preSubcommand hook should effective for direct subcommands', async () => {382      const calls = [];383      const program = new commander.Command();384      program.hook('preSubcommand', async (thisCommand, subCommand) => {385        await 'preSubcommand';386        calls.push('preSubcommand');387        calls.push(subCommand.name());388      });389      program390        .command('first')391        .action(async () => {392          await 'first';393          calls.push('first');394        })395        .command('second')396        .action(async () => {397          await 'second';398          calls.push('second');399        })400        .command('third')401        .action(async () => {402          await 'third';403          calls.push('third');404        });405      program.action(async () => {406        await 2;407        calls.push(2);408      });409      const result = program.parseAsync(['first', 'second', 'third'], {410        from: 'user',411      });412      assert.deepEqual(calls, []);413      await result;414      assert.deepEqual(calls, ['preSubcommand', 'first', 'third']);415    });416  });417});