@@ -4,6 +4,7 @@ import { exec } from '../../lib/cli/cli';
44import { CliIoHost } from '../../lib/cli/io-host' ;
55import { Configuration } from '../../lib/cli/user-configuration' ;
66import { TestIoHost } from '../_helpers/io-host' ;
7+ import { Toolkit } from '@aws-cdk/toolkit-lib' ;
78
89// Store original version module exports so we don't conflict with other tests
910const originalVersion = jest . requireActual ( '../../lib/cli/version' ) ;
@@ -65,6 +66,8 @@ jest.mock('../../lib/cli/parse-command-line-arguments', () => ({
6566 _ : [ 'deploy' ] ,
6667 parameters : [ ] ,
6768 } ;
69+ } else if ( args . includes ( 'flags' ) ) {
70+ result = { ...result , _ : [ 'flags' ] } ;
6871 }
6972
7073 // Handle notices flags
@@ -93,6 +96,22 @@ jest.mock('../../lib/cli/parse-command-line-arguments', () => ({
9396 } ) ,
9497} ) ) ;
9598
99+ // Mock FlagCommandHandler to capture constructor calls
100+ const mockFlagCommandHandlerConstructor = jest . fn ( ) ;
101+ const mockProcessFlagsCommand = jest . fn ( ) . mockResolvedValue ( undefined ) ;
102+
103+ jest . mock ( '../../lib/commands/flags/flags' , ( ) => {
104+ return {
105+ FlagCommandHandler : jest . fn ( ) . mockImplementation ( ( ...args ) => {
106+ mockFlagCommandHandlerConstructor ( ...args ) ;
107+ return {
108+ processFlagsCommand : mockProcessFlagsCommand ,
109+ } ;
110+ } ) ,
111+ } ;
112+ } ) ;
113+
114+
96115describe ( 'exec verbose flag tests' , ( ) => {
97116 beforeEach ( ( ) => {
98117 jest . clearAllMocks ( ) ;
@@ -513,3 +532,61 @@ describe('--yes', () => {
513532 execSpy . mockRestore ( ) ;
514533 } ) ;
515534} ) ;
535+
536+ describe ( 'flags command tests' , ( ) => {
537+ let mockConfig : any ;
538+ let flagsSpy : jest . SpyInstance ;
539+
540+ beforeEach ( ( ) => {
541+ jest . clearAllMocks ( ) ;
542+ mockFlagCommandHandlerConstructor . mockClear ( ) ;
543+ mockProcessFlagsCommand . mockClear ( ) ;
544+
545+ flagsSpy = jest . spyOn ( Toolkit . prototype , 'flags' ) . mockResolvedValue ( [ ] ) ;
546+
547+ mockConfig = {
548+ loadConfigFiles : jest . fn ( ) . mockResolvedValue ( undefined ) ,
549+ settings : {
550+ get : jest . fn ( ) . mockImplementation ( ( key : string [ ] ) => {
551+ if ( key [ 0 ] === 'unstable' ) return [ 'flags' ] ;
552+ return undefined ;
553+ } ) ,
554+ } ,
555+ context : {
556+ all : {
557+ 'myContextParam' : 'testValue' ,
558+ } ,
559+ get : jest . fn ( ) . mockReturnValue ( [ ] ) ,
560+ } ,
561+ } ;
562+
563+ Configuration . fromArgsAndFiles = jest . fn ( ) . mockResolvedValue ( mockConfig ) ;
564+ } ) ;
565+
566+ afterEach ( ( ) => {
567+ flagsSpy . mockRestore ( ) ;
568+ } ) ;
569+
570+ test ( 'passes CLI context to FlagCommandHandler' , async ( ) => {
571+ // WHEN
572+ await exec ( [
573+ 'flags' ,
574+ '--unstable=flags' ,
575+ '--set' ,
576+ '--recommended' ,
577+ '--all' ,
578+ '-c' , 'myContextParam=testValue' ,
579+ '--yes' ,
580+ ] ) ;
581+
582+ // THEN
583+ expect ( mockFlagCommandHandlerConstructor ) . toHaveBeenCalledWith (
584+ expect . anything ( ) , // flagsData
585+ expect . anything ( ) , // ioHelper
586+ expect . anything ( ) , // args
587+ expect . anything ( ) , // toolkit
588+ mockConfig . context . all , // cliContextValues
589+ ) ;
590+ expect ( mockProcessFlagsCommand ) . toHaveBeenCalled ( ) ;
591+ } ) ;
592+ } ) ;
0 commit comments