Skip to content

Commit 98f986d

Browse files
committed
Merge pull request #142 from rkeithhill/rkeithhill/is141-accept-script-args
Fixex #141 - passes args to script.
2 parents 41f2c3b + 9ec5d11 commit 98f986d

File tree

5 files changed

+93
-5
lines changed

5 files changed

+93
-5
lines changed

src/PowerShellEditorServices.Protocol/Server/DebugAdapter.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,22 @@ protected async Task HandleLaunchRequest(
104104

105105
await editorSession.PowerShellContext.ExecuteCommand(setWorkingDirCommand);
106106

107-
Logger.Write(LogLevel.Verbose, "Working dir set to '" + workingDir + "'");
107+
Logger.Write(LogLevel.Verbose, "Working dir set to: " + workingDir);
108+
109+
// Prepare arguments to the script - if specified
110+
string arguments = null;
111+
if ((launchParams.Args != null) && (launchParams.Args.Length > 0))
112+
{
113+
arguments = string.Join(" ", launchParams.Args);
114+
Logger.Write(LogLevel.Verbose, "Script arguments are: " + arguments);
115+
}
108116

109117
// Execute the given PowerShell script and send the response.
110118
// Note that we aren't waiting for execution to complete here
111119
// because the debugger could stop while the script executes.
112120
Task executeTask =
113121
editorSession.PowerShellContext
114-
.ExecuteScriptAtPath(launchParams.Program)
122+
.ExecuteScriptAtPath(launchParams.Program, arguments)
115123
.ContinueWith(
116124
async (t) => {
117125
Logger.Write(LogLevel.Verbose, "Execution completed, terminating...");

src/PowerShellEditorServices/Session/PowerShellContext.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -432,16 +432,22 @@ public async Task<IEnumerable<object>> ExecuteScriptString(
432432
/// Executes a script file at the specified path.
433433
/// </summary>
434434
/// <param name="scriptPath">The path to the script file to execute.</param>
435+
/// <param name="arguments">Arguments to pass to the script.</param>
435436
/// <returns>A Task that can be awaited for completion.</returns>
436-
public async Task ExecuteScriptAtPath(string scriptPath)
437+
public async Task ExecuteScriptAtPath(string scriptPath, string arguments = null)
437438
{
438439
// If we don't escape wildcard characters in the script path, the script can
439440
// fail to execute if say the script name was foo][.ps1.
440441
// Related to issue #123.
441442
string escapedScriptPath = EscapeWildcardsInPath(scriptPath);
442443

444+
if (arguments != null)
445+
{
446+
escapedScriptPath += " " + arguments;
447+
}
448+
443449
PSCommand command = new PSCommand();
444-
command.AddCommand(escapedScriptPath);
450+
command.AddScript(escapedScriptPath);
445451

446452
await this.ExecuteCommand<object>(command, true);
447453
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
param($Param1, $Param2, [switch]$Force)
2+
3+
"args are $args"

test/PowerShellEditorServices.Test.Shared/PowerShellEditorServices.Test.Shared.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
<None Include="Completion\CompletionExamples.psm1">
6565
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
6666
</None>
67+
<None Include="Debugging\DebugWithParamsTest.ps1" />
6768
<None Include="Debugging\VariableTest.ps1" />
6869
<None Include="SymbolDetails\SymbolDetails.ps1" />
6970
<None Include="Symbols\MultipleSymbols.ps1" />

test/PowerShellEditorServices.Test/Debugging/DebugServiceTests.cs

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55

66
using Microsoft.PowerShell.EditorServices.Utility;
77
using System;
8+
using System.Collections.Generic;
89
using System.Linq;
910
using System.Management.Automation;
1011
using System.Threading;
1112
using System.Threading.Tasks;
1213
using Xunit;
13-
using Xunit.Sdk;
1414

1515
namespace Microsoft.PowerShell.EditorServices.Test.Debugging
1616
{
@@ -69,6 +69,76 @@ public void Dispose()
6969
this.powerShellContext.Dispose();
7070
}
7171

72+
public static IEnumerable<object[]> DebuggerAcceptsScriptArgsTestData
73+
{
74+
get
75+
{
76+
var data = new[]
77+
{
78+
new[] { new []{ "Foo -Param2 @('Bar','Baz') -Force Extra1" }},
79+
new[] { new []{ "Foo", "-Param2", "@('Bar','Baz')", "-Force", "Extra1" }},
80+
};
81+
82+
return data;
83+
}
84+
}
85+
86+
[Theory]
87+
[MemberData("DebuggerAcceptsScriptArgsTestData")]
88+
public async Task DebuggerAcceptsScriptArgs(string[] args)
89+
{
90+
ScriptFile debugWithParamsFile =
91+
this.workspace.GetFile(
92+
@"..\..\..\PowerShellEditorServices.Test.Shared\Debugging\DebugWithParamsTest.ps1");
93+
94+
await this.debugService.SetBreakpoints(
95+
debugWithParamsFile,
96+
new int[] { 3 });
97+
98+
string arguments = string.Join(" ", args);
99+
100+
// Execute the script and wait for the breakpoint to be hit
101+
this.powerShellContext.ExecuteScriptAtPath(
102+
debugWithParamsFile.FilePath, arguments);
103+
104+
await this.AssertDebuggerStopped(debugWithParamsFile.FilePath);
105+
106+
StackFrameDetails[] stackFrames = debugService.GetStackFrames();
107+
108+
VariableDetailsBase[] variables =
109+
debugService.GetVariables(stackFrames[0].LocalVariables.Id);
110+
111+
var var = variables.FirstOrDefault(v => v.Name == "$Param1");
112+
Assert.NotNull(var);
113+
Assert.Equal("\"Foo\"", var.ValueString);
114+
Assert.False(var.IsExpandable);
115+
116+
var = variables.FirstOrDefault(v => v.Name == "$Param2");
117+
Assert.NotNull(var);
118+
Assert.True(var.IsExpandable);
119+
120+
var childVars = debugService.GetVariables(var.Id);
121+
Assert.Equal(9, childVars.Length);
122+
Assert.Equal("\"Bar\"", childVars[0].ValueString);
123+
Assert.Equal("\"Baz\"", childVars[1].ValueString);
124+
125+
var = variables.FirstOrDefault(v => v.Name == "$Force");
126+
Assert.NotNull(var);
127+
Assert.Equal("True", var.ValueString);
128+
Assert.True(var.IsExpandable);
129+
130+
var = variables.FirstOrDefault(v => v.Name == "$args");
131+
Assert.NotNull(var);
132+
Assert.True(var.IsExpandable);
133+
134+
childVars = debugService.GetVariables(var.Id);
135+
Assert.Equal(8, childVars.Length);
136+
Assert.Equal("\"Extra1\"", childVars[0].ValueString);
137+
138+
// Abort execution of the script
139+
this.powerShellContext.AbortExecution();
140+
}
141+
72142
[Fact]
73143
public async Task DebuggerSetsAndClearsBreakpoints()
74144
{

0 commit comments

Comments
 (0)