1212using Microsoft . Extensions . Logging . Abstractions ;
1313using Microsoft . PowerShell . EditorServices . Services ;
1414using Microsoft . PowerShell . EditorServices . Services . DebugAdapter ;
15- using Microsoft . PowerShell . EditorServices . Services . PowerShell . Execution ;
1615using Microsoft . PowerShell . EditorServices . Services . PowerShell . Host ;
1716using Microsoft . PowerShell . EditorServices . Services . TextDocument ;
1817using Microsoft . PowerShell . EditorServices . Test . Shared ;
18+ using Microsoft . PowerShell . EditorServices . Utility ;
1919using Xunit ;
2020
2121namespace Microsoft . PowerShell . EditorServices . Test . Debugging
@@ -92,12 +92,18 @@ public async Task DebuggerAcceptsInlineScript()
9292 await debugService . SetCommandBreakpointsAsync (
9393 new [ ] { CommandBreakpointDetails . Create ( "Get-Random" ) } ) . ConfigureAwait ( false ) ;
9494
95- Task executeTask = _psesHost . ExecutePSCommandAsync (
96- new PSCommand ( ) . AddScript ( "Get-Random -Maximum 100" ) , CancellationToken . None ) ;
95+ Task < IReadOnlyList < int > > executeTask = _psesHost . ExecutePSCommandAsync < int > (
96+ new PSCommand ( ) . AddScript ( "Get-Random -SetSeed 42 - Maximum 100" ) , CancellationToken . None ) ;
9797
9898 AssertDebuggerStopped ( "" , 1 ) ;
99+
100+ // NOTE: Continuing the debugger cancels the current task, as it expects the integrated
101+ // console to be running. So we run the prompt so our actual debug task is not canceled.
102+ await ExecutePowerShellCommand ( "prompt" ) . ConfigureAwait ( false ) ;
99103 debugService . Continue ( ) ;
100104
105+ Assert . Equal ( 17 , ( await executeTask . ConfigureAwait ( false ) ) [ 0 ] ) ;
106+
101107 StackFrameDetails [ ] stackFrames = await debugService . GetStackFramesAsync ( ) . ConfigureAwait ( false ) ;
102108 Assert . Equal ( StackFrameDetails . NoFileScriptPath , stackFrames [ 0 ] . ScriptPath ) ;
103109
@@ -110,49 +116,40 @@ await debugService.SetCommandBreakpointsAsync(
110116 Assert . Equal ( "[ArrayList: 0]" , var . ValueString ) ;
111117 }
112118
113- public static IEnumerable < object [ ] > DebuggerAcceptsScriptArgsTestData
114- {
115- get
116- {
117- return new [ ]
118- {
119- new [ ] { new [ ] { "Foo -Param2 @('Bar','Baz') -Force Extra1" } } ,
120- new [ ] { new [ ] { "Foo" , "-Param2" , "@('Bar','Baz')" , "-Force" , "Extra1" } } ,
121- } ;
122- }
123- }
124-
125119 [ Trait ( "Category" , "DebugService" ) ]
126- [ Theory ]
127- [ MemberData ( nameof ( DebuggerAcceptsScriptArgsTestData ) ) ]
128- public async Task DebuggerAcceptsScriptArgs ( string [ ] args )
120+ [ Fact ]
121+ public async Task DebuggerAcceptsScriptArgs ( )
129122 {
130123 // The path is intentionally odd (some escaped chars but not all) because we are testing
131124 // the internal path escaping mechanism - it should escape certains chars ([, ] and space) but
132125 // it should not escape already escaped chars.
133126 ScriptFile debugWithParamsFile = GetDebugScript ( "Debug W&ith Params [Test].ps1" ) ;
134127
135- await debugService . SetLineBreakpointsAsync (
128+ BreakpointDetails [ ] breakpoints = await debugService . SetLineBreakpointsAsync (
136129 debugWithParamsFile ,
137130 new [ ] { BreakpointDetails . Create ( debugWithParamsFile . FilePath , 3 ) } ) . ConfigureAwait ( false ) ;
138131
139- // Execute the script and wait for the breakpoint to be hit
140- Task executeTask = _psesHost . ExecutePSCommandAsync (
141- new PSCommand ( ) . AddCommand ( debugWithParamsFile . FilePath ) . AddArgument ( string . Join ( " " , args ) ) ,
142- CancellationToken . None ) ;
132+ Assert . Single ( breakpoints ) ;
133+ Assert . Collection ( breakpoints , ( breakpoint ) =>
134+ {
135+ Assert . Equal ( debugWithParamsFile . FilePath , breakpoint . Source ) ;
136+ Assert . Equal ( 3 , breakpoint . LineNumber ) ;
137+ Assert . True ( breakpoint . Verified ) ;
138+ } ) ;
143139
144- AssertDebuggerStopped ( debugWithParamsFile . FilePath ) ;
140+ // TODO: This test used to also pass the args as a single string, but that doesn't seem
141+ // to work any more. Perhaps that's a bug?
142+ var args = new [ ] { "Foo" , "-Param2" , "@('Bar','Baz')" , "-Force" , "Extra1" } ;
143+ Task executeTask = ExecutePowerShellCommand ( debugWithParamsFile . FilePath , args ) ;
145144
146- StackFrameDetails [ ] stackFrames = await debugService . GetStackFramesAsync ( ) . ConfigureAwait ( false ) ;
145+ AssertDebuggerStopped ( debugWithParamsFile . FilePath , 3 ) ;
147146
148- VariableDetailsBase [ ] variables =
149- debugService . GetVariables ( stackFrames [ 0 ] . AutoVariables . Id ) ;
147+ StackFrameDetails [ ] stackFrames = await debugService . GetStackFramesAsync ( ) . ConfigureAwait ( false ) ;
148+ VariableDetailsBase [ ] variables = debugService . GetVariables ( stackFrames [ 0 ] . AutoVariables . Id ) ;
150149
151150 var var = Array . Find ( variables , v => v . Name == "$Param1" ) ;
152151 Assert . NotNull ( var ) ;
153- // TODO: Double-check this is intended.
154- Assert . StartsWith ( "\" Foo" , var . ValueString ) ;
155- // Assert.Equal("\"Foo\"", var.ValueString);
152+ Assert . Equal ( "\" Foo\" " , var . ValueString ) ;
156153 Assert . False ( var . IsExpandable ) ;
157154
158155 var = Array . Find ( variables , v => v . Name == "$Param2" ) ;
@@ -169,6 +166,9 @@ await debugService.SetLineBreakpointsAsync(
169166 Assert . Equal ( "True" , var . ValueString ) ;
170167 Assert . True ( var . IsExpandable ) ;
171168
169+ // NOTE: $args are not longer found in AutoVariables.
170+ variables = debugService . GetVariables ( stackFrames [ 0 ] . CommandVariables . Id ) ;
171+
172172 var = Array . Find ( variables , v => v . Name == "$args" ) ;
173173 Assert . NotNull ( var ) ;
174174 Assert . True ( var . IsExpandable ) ;
@@ -179,7 +179,6 @@ await debugService.SetLineBreakpointsAsync(
179179
180180 // Abort script execution early and wait for completion
181181 debugService . Abort ( ) ;
182- await executeTask . ConfigureAwait ( false ) ;
183182 }
184183
185184 [ Trait ( "Category" , "DebugService" ) ]
@@ -1008,6 +1007,10 @@ await debugService.SetLineBreakpointsAsync(
10081007 debugService . Abort ( ) ;
10091008 await executeTask . ConfigureAwait ( false ) ;
10101009 }
1010+ private Task ExecutePowerShellCommand ( string command , params string [ ] args )
1011+ {
1012+ return _psesHost . ExecutePSCommandAsync ( PSCommandHelpers . BuildCommandFromArguments ( command , args ) , CancellationToken . None ) ;
1013+ }
10111014
10121015 private void AssertDebuggerPaused ( )
10131016 {
@@ -1024,6 +1027,8 @@ private void AssertDebuggerStopped(
10241027 DebuggerStoppedEventArgs eventArgs =
10251028 debuggerStoppedQueue . Take ( new CancellationTokenSource ( 10000 ) . Token ) ;
10261029
1030+ // Assert.True(_psesHost.DebugContext.IsStopped);
1031+
10271032 // TODO: Why does the casing of the path change? Specifically the Drive letter on Windows.
10281033 if ( eventArgs . ScriptPath is not null )
10291034 {
0 commit comments