@@ -13,19 +13,23 @@ import {
1313import { ExtContext } from '../../../shared/extensions'
1414import { AwsSamDebuggerConfiguration } from '../../../shared/sam/debugger/awsSamDebugConfiguration'
1515import assert from 'assert'
16- import sinon from 'sinon'
1716import * as picker from '../../../shared/ui/picker'
17+ import * as input from '../../../shared/ui/input'
1818import * as utils from '../../../lambda/utils'
1919import { HttpResourceFetcher } from '../../../shared/resourcefetcher/httpResourceFetcher'
2020import * as vscode from 'vscode'
2121import path from 'path'
22- import { fs , makeTemporaryToolkitFolder } from '../../../shared'
22+ import { addCodiconToString , fs , makeTemporaryToolkitFolder } from '../../../shared'
2323import { LaunchConfiguration } from '../../../shared/debug/launchConfiguration'
2424import { getTestWindow } from '../..'
2525import { getLogger } from '../../../shared/logger'
2626import * as extensionUtilities from '../../../shared/extensionUtilities'
2727import * as samInvokeBackend from '../../../lambda/vue/configEditor/samInvokeBackend'
2828import { SamDebugConfigProvider } from '../../../shared/sam/debugger/awsSamDebugger'
29+ import sinon from 'sinon'
30+ import * as nls from 'vscode-nls'
31+
32+ const localize = nls . loadMessageBundle ( )
2933
3034function createMockWorkspaceFolder ( uriPath : string ) : vscode . WorkspaceFolder {
3135 return {
@@ -552,7 +556,7 @@ describe('SamInvokeWebview', () => {
552556 }
553557 templateRegistryStub . resolves ( { items : [ mockTemplate ] } )
554558 createQuickPickStub . returns ( { } )
555- promptUserStub . resolves ( undefined ) // No selection made
559+ promptUserStub . resolves ( undefined )
556560 verifySinglePickerOutputStub . returns ( undefined )
557561
558562 const result = await samInvokeWebview . getTemplate ( )
@@ -615,4 +619,217 @@ describe('SamInvokeWebview', () => {
615619 assert ( SamDebugConfigProviderStub . called )
616620 } )
617621 } )
622+ describe ( 'saveLaunchConfig' , function ( ) {
623+ let sandbox : sinon . SinonSandbox
624+ let mockFolder : vscode . WorkspaceFolder
625+ let mockUri : vscode . Uri
626+ let getUriFromLaunchConfigStub : sinon . SinonStub
627+ let workspaceFoldersStub : sinon . SinonStub
628+ let getUriStub : sinon . SinonStub
629+ let launchConfigurationsStub : sinon . SinonStub
630+ let getLaunchConfigQuickPickItemsStub : sinon . SinonStub
631+ let editDebugConfigurationStub : sinon . SinonStub
632+ let verifySinglePickerOutputStub : sinon . SinonStub
633+ let createQuickPickStub : sinon . SinonStub
634+
635+ this . beforeEach ( async function ( ) {
636+ sandbox = sinon . createSandbox ( )
637+ mockFolder = createMockWorkspaceFolder ( '/mock-path' )
638+ mockUri = mockFolder . uri
639+
640+ getUriStub = sandbox . stub ( samInvokeWebview as any , 'getUriFromLaunchConfig' )
641+ getUriFromLaunchConfigStub = sinon . stub ( )
642+ sandbox . stub ( vscode . workspace , 'getWorkspaceFolder' ) . returns ( mockFolder )
643+ workspaceFoldersStub = sandbox . stub ( vscode . workspace , 'workspaceFolders' )
644+ launchConfigurationsStub = sandbox . stub ( LaunchConfiguration . prototype , 'getDebugConfigurations' )
645+ editDebugConfigurationStub = sandbox . stub ( LaunchConfiguration . prototype , 'editDebugConfiguration' )
646+ getLaunchConfigQuickPickItemsStub = sandbox . stub ( )
647+
648+ verifySinglePickerOutputStub = sandbox . stub ( picker , 'verifySinglePickerOutput' )
649+ createQuickPickStub = sandbox . stub ( picker , 'createQuickPick' )
650+ } )
651+ afterEach ( ( ) => {
652+ sandbox . restore ( )
653+ } )
654+
655+ it ( 'should create quick pick with correct items' , async ( ) => {
656+ getUriFromLaunchConfigStub . resolves ( mockUri )
657+ getLaunchConfigQuickPickItemsStub . resolves ( [ { label : 'Existing Config' , index : 0 } ] )
658+ verifySinglePickerOutputStub . returns ( undefined )
659+ createQuickPickStub . returns ( { } )
660+ sandbox . stub ( picker , 'promptUser' ) . resolves ( [ ] )
661+
662+ sandbox . replace ( samInvokeWebview as any , 'getLaunchConfigQuickPickItems' , getLaunchConfigQuickPickItemsStub )
663+ sandbox . replace ( samInvokeWebview as any , 'getUriFromLaunchConfig' , getUriFromLaunchConfigStub )
664+
665+ await samInvokeWebview . saveLaunchConfig ( mockConfig )
666+ assert ( getLaunchConfigQuickPickItemsStub . called )
667+
668+ sinon . assert . calledWith (
669+ createQuickPickStub ,
670+ sinon . match ( {
671+ items : sinon . match . array . deepEquals ( [
672+ {
673+ label : addCodiconToString (
674+ 'add' ,
675+ localize (
676+ 'AWS.command.addSamDebugConfiguration' ,
677+ 'Add Local Invoke and Debug Configuration'
678+ )
679+ ) ,
680+ index : - 1 ,
681+ alwaysShow : true ,
682+ } ,
683+ { label : 'Existing Config' , index : 0 } ,
684+ ] ) ,
685+ } )
686+ )
687+ } )
688+ describe ( 'Save Launch Config with no URI' , ( ) => {
689+ let promptUserStub : sinon . SinonStub
690+ let testConfig : any
691+ beforeEach ( ( ) => {
692+ const testConfig = {
693+ label : '$(add) Add Local Invoke and Debug Configuration' ,
694+ index : - 1 ,
695+ alwaysShow : true ,
696+ }
697+ // Create a stub for the promptUser function
698+ promptUserStub = sinon . stub ( picker , 'promptUser' )
699+
700+ // Configure the stub to return the desired array
701+ promptUserStub . resolves ( [ testConfig ] )
702+ } )
703+ afterEach ( ( ) => {
704+ // Restore the original function after each test
705+ promptUserStub . restore ( )
706+ } )
707+ it ( 'should not save launch config' , async ( ) => {
708+ workspaceFoldersStub . value ( [ mockFolder ] )
709+ sandbox . stub ( extensionUtilities , 'isCloud9' ) . returns ( false )
710+ sandbox . replace ( samInvokeWebview as any , 'getUriFromLaunchConfig' , getUriFromLaunchConfigStub )
711+ const launchConfigItems = launchConfigurationsStub . resolves ( [ ] )
712+ getUriFromLaunchConfigStub . resolves ( mockUri )
713+
714+ const mockPickerItems = [
715+ {
716+ label : addCodiconToString (
717+ 'add' ,
718+ localize ( 'AWS.command.addSamDebugConfiguration' , 'Add Local Invoke and Debug Configuration' )
719+ ) ,
720+ index : - 1 ,
721+ alwaysShow : true ,
722+ } ,
723+ launchConfigItems ,
724+ ]
725+ verifySinglePickerOutputStub . resolves ( [ testConfig ] )
726+ getLaunchConfigQuickPickItemsStub . resolves ( mockPickerItems )
727+ sandbox . replace (
728+ samInvokeWebview as any ,
729+ 'getLaunchConfigQuickPickItems' ,
730+ getLaunchConfigQuickPickItemsStub
731+ )
732+ sinon . stub ( input , 'createInputBox' ) . resolves ( 'testConfig' )
733+ const updateStub = sandbox . stub ( vscode . workspace , 'updateWorkspaceFolders' )
734+
735+ await samInvokeWebview . saveLaunchConfig ( mockConfig )
736+ assert ( getLaunchConfigQuickPickItemsStub . called )
737+ assert ( verifySinglePickerOutputStub . called )
738+ assert ( updateStub . notCalled )
739+ } )
740+ } )
741+
742+ describe ( 'Save Launch Config with no URI' , ( ) => {
743+ it ( 'should not save if no URI is found' , async ( ) => {
744+ getUriStub . resolves ( undefined )
745+ await samInvokeWebview . saveLaunchConfig ( mockConfig )
746+ assert ( launchConfigurationsStub . notCalled )
747+ } )
748+ } )
749+ describe ( 'Save Launch Config' , ( ) => {
750+ let promptUserStub : sinon . SinonStub
751+ let mockSavedConfig : AwsSamDebuggerConfiguration
752+
753+ beforeEach ( ( ) => {
754+ mockSavedConfig = {
755+ type : 'aws-sam' ,
756+ request : 'direct-invoke' ,
757+ invokeTarget : {
758+ target : 'template' ,
759+ logicalId : 'HelloWorldFunction' ,
760+ templatePath : 'template.yaml' ,
761+ } ,
762+ lambda : {
763+ runtime : 'python3.9' ,
764+ } ,
765+ sam : {
766+ containerBuild : false ,
767+ localArguments : [ '-e' , '/tester/events.json' ] ,
768+ skipNewImageCheck : false ,
769+ } ,
770+ api : {
771+ path : 'asdad' ,
772+ httpMethod : 'get' ,
773+ } ,
774+ name : 'tester' ,
775+ }
776+ // Create a stub for the promptUser function
777+ promptUserStub = sinon . stub ( picker , 'promptUser' )
778+
779+ // Configure the stub to return the desired array
780+ promptUserStub . resolves ( [ mockSavedConfig ] )
781+ } )
782+
783+ afterEach ( ( ) => {
784+ // Restore the original function after each test
785+ promptUserStub . restore ( )
786+ } )
787+
788+ it ( 'should save launch config' , async ( ) => {
789+ workspaceFoldersStub . value ( [ mockFolder ] )
790+ sandbox . stub ( extensionUtilities , 'isCloud9' ) . returns ( false )
791+ getUriFromLaunchConfigStub . resolves ( mockUri )
792+ sandbox . replace ( samInvokeWebview as any , 'getUriFromLaunchConfig' , getUriFromLaunchConfigStub )
793+ const launchConfigItems = launchConfigurationsStub . resolves ( [
794+ {
795+ config : mockSavedConfig ,
796+ index : 0 ,
797+ label : 'tester' ,
798+ } ,
799+ ] )
800+ const mockPickerItems = [
801+ {
802+ label : addCodiconToString (
803+ 'add' ,
804+ localize ( 'AWS.command.addSamDebugConfiguration' , 'Add Local Invoke and Debug Configuration' )
805+ ) ,
806+ index : - 1 ,
807+ alwaysShow : true ,
808+ } ,
809+ launchConfigItems ,
810+ ]
811+ verifySinglePickerOutputStub . resolves ( [
812+ {
813+ label : 'tester' ,
814+ index : 0 ,
815+ alwaysShow : true ,
816+ } ,
817+ ] )
818+ getLaunchConfigQuickPickItemsStub . resolves ( mockPickerItems )
819+ sandbox . replace (
820+ samInvokeWebview as any ,
821+ 'getLaunchConfigQuickPickItems' ,
822+ getLaunchConfigQuickPickItemsStub
823+ )
824+ const updateStub = sandbox . stub ( vscode . workspace , 'updateWorkspaceFolders' )
825+
826+ await samInvokeWebview . saveLaunchConfig ( mockSavedConfig )
827+
828+ assert ( getLaunchConfigQuickPickItemsStub . called )
829+ assert ( editDebugConfigurationStub . called )
830+ assert ( verifySinglePickerOutputStub . called )
831+ assert ( updateStub . notCalled )
832+ } )
833+ } )
834+ } )
618835} )
0 commit comments