@@ -6,7 +6,12 @@ import { inject, runInInjectionContext } from "../../lib/common/di";
66import { COMMAND_OWNER } from "../../lib/common/contracts/command-registry" ;
77import { CommandsService } from "../../lib/common/services/commands-service" ;
88import { HooksService } from "../../lib/common/services/hooks-service" ;
9- import { defineCommand , stringOption } from "../../lib/common/define-command" ;
9+ import {
10+ defineCommand ,
11+ defineOptions ,
12+ stringOption ,
13+ } from "../../lib/common/define-command" ;
14+ import { COMMAND_LINE_EXPECTED } from "../../lib/common/contracts/command-line-expected" ;
1015import { registerCommand } from "../../lib/common/services/command-definition-adapter" ;
1116import { Options } from "../../lib/options" ;
1217import { LoggerStub } from "../stubs" ;
@@ -24,7 +29,11 @@ interface IDispatchHarness {
2429}
2530
2631const createHarness = (
27- opts : { disableCommandHooks ?: boolean ; realOptions ?: boolean } = { } ,
32+ opts : {
33+ disableCommandHooks ?: boolean ;
34+ realOptions ?: boolean ;
35+ commandLine ?: boolean ;
36+ } = { } ,
2837) : IDispatchHarness => {
2938 const testInjector = new Yok ( ) ;
3039 const harness = < IDispatchHarness > ( < unknown > {
@@ -73,6 +82,10 @@ const createHarness = (
7382 trackOptions : async ( ) : Promise < void > => undefined ,
7483 } ) ;
7584 if ( opts . realOptions ) {
85+ testInjector . register ( {
86+ provide : COMMAND_LINE_EXPECTED ,
87+ useValue : ! ! opts . commandLine ,
88+ } ) ;
7689 testInjector . register ( "hostInfo" , { } ) ;
7790 testInjector . register ( "settingsService" , {
7891 setSettings : ( ) : any => undefined ,
@@ -82,6 +95,7 @@ const createHarness = (
8295 } else {
8396 testInjector . register ( "options" , {
8497 validateOptions : ( ) : void => undefined ,
98+ recordCommandLine : ( ) : void => undefined ,
8599 } ) ;
86100 }
87101 testInjector . register ( "commandsService" , CommandsService ) ;
@@ -272,6 +286,55 @@ describe("CommandsService in-process dispatch", () => {
272286 assert . deepEqual ( inProcess . hooks , commandLine . hooks ) ;
273287 } ) ;
274288
289+ it ( "gives option groups the subcommand's values before the parent's hooks" , async ( ) => {
290+ const argv = process . argv ;
291+ process . argv = [
292+ "node" ,
293+ "ns" ,
294+ "dctest-groups" ,
295+ "child" ,
296+ "--level" ,
297+ "typed" ,
298+ ] ;
299+ try {
300+ const harness = createHarness ( {
301+ realOptions : true ,
302+ commandLine : true ,
303+ } ) ;
304+ const Level = defineOptions ( {
305+ level : stringOption ( { default : "group" } ) ,
306+ } ) ;
307+ const Unlisted = defineOptions ( {
308+ level : stringOption ( { default : "unlisted" } ) ,
309+ } ) ;
310+ const level = harness . injector . get ( Level ) ;
311+ const unlisted = harness . injector . get ( Unlisted ) ;
312+ const seen : string [ ] = [ ] ;
313+ harness . injector . resolve ( "hooksService" ) . executeBeforeHooks = async (
314+ name : string ,
315+ ) : Promise < any [ ] > => {
316+ seen . push ( `${ name } : ${ level . level } ${ unlisted . level } ` ) ;
317+ return [ ] ;
318+ } ;
319+ register ( harness . injector , {
320+ name : "dctest-groups|child" ,
321+ options : [ Level ] ,
322+ run : ( ) : void => undefined ,
323+ } ) ;
324+
325+ await harness . commandsService . tryExecuteCommand ( "dctest-groups" , [
326+ "child" ,
327+ ] ) ;
328+
329+ assert . deepEqual ( seen , [
330+ "dctest-groups-child: typed unlisted" ,
331+ "dctest-groups|child: typed unlisted" ,
332+ ] ) ;
333+ } finally {
334+ process . argv = argv ;
335+ }
336+ } ) ;
337+
275338 it ( "rejects with the subcommand's failure instead of exiting" , async ( ) => {
276339 const harness = createHarness ( ) ;
277340 register ( harness . injector , {
0 commit comments