Skip to content

Commit 79d0601

Browse files
author
Tushar Bhatnagar
committed
Fix parameter forwarding for pwsh
1 parent acad68b commit 79d0601

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

src/ArgumentBuilder.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,35 @@ public static string EscapeAndConcatenateCommandAndArgArrayForProcessStart(
5050
return sb.ToString();
5151
}
5252

53+
/// <summary>
54+
/// Undo the processing which took place to create string[] args in Main, so that the next process will receive the same string[] args.
55+
/// </summary>
56+
/// <remarks>
57+
/// Note that pwsh does not require any special escaping of arguments. We can simply concatenate the list of strings with a space between each.
58+
/// </remarks>
59+
/// <param name="command">The base command.</param>
60+
/// <param name="args">List of arguments to escape.</param>
61+
/// <returns>An escaped string of the <paramref name="command"/> and <paramref name="args"/>.</returns>
62+
public static string ConcatenateCommandAndArgArrayForPwshProcessStart(
63+
string? command,
64+
string[]? args)
65+
{
66+
var sb = new ValueStringBuilder(stackalloc char[256]);
67+
68+
sb.Append(command);
69+
70+
if (args is not null)
71+
{
72+
for (var i = 0; i < args.Length; i++)
73+
{
74+
sb.Append(Space);
75+
sb.Append(args[i]);
76+
}
77+
}
78+
79+
return sb.ToString();
80+
}
81+
5382
/// <summary>
5483
/// Undo the processing which took place to create string[] args in Main, so that the next process will receive the same string[] args.
5584
/// </summary>

src/CommandRunner.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ public async Task<int> RunAsync(string name, string cmd, string[]? args)
5656
ArgumentBuilder.EscapeAndConcatenateCommandAndArgArrayForCmdProcessStart(cmd, args),
5757
"\"");
5858
}
59+
else if (_processContext.Shell.Equals("pwsh", StringComparison.OrdinalIgnoreCase))
60+
{
61+
process.StartInfo.ArgumentList.Add("-c");
62+
process.StartInfo.ArgumentList.Add(ArgumentBuilder.ConcatenateCommandAndArgArrayForPwshProcessStart(cmd, args));
63+
}
5964
else
6065
{
6166
process.StartInfo.ArgumentList.Add("-c");

test/ArgumentBuilderTests.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,29 @@ public void EscapeAndConcatenateCommandAndArgArrayForProcessStart(string command
2727
result.ShouldBe(expected);
2828
}
2929

30+
[Theory]
31+
[InlineData("pwsh", null, "pwsh")]
32+
[InlineData("pws \"h\"", null, "pws \"h\"")]
33+
[InlineData("p w s h", null, "p w s h")]
34+
[InlineData("pwsh", new string[0], "pwsh")]
35+
[InlineData("pwsh", new[] { "one", "two", "three" }, "pwsh one two three")]
36+
[InlineData("pwsh", new[] { "line1\nline2", "word1\tword2" }, "pwsh line1\nline2 word1\tword2")]
37+
[InlineData("pwsh", new[] { "with spaces" }, "pwsh with spaces")]
38+
[InlineData("pwsh", new[] { @"with\backslash" }, @"pwsh with\backslash")]
39+
[InlineData("pwsh", new[] { @"""quotedwith\backslash""" }, @"pwsh ""quotedwith\backslash""" )]
40+
[InlineData("pwsh", new[] { @"C:\Users\" }, @"pwsh C:\Users\")]
41+
[InlineData("pwsh", new[] { @"C:\Program Files\dotnet\" }, @"pwsh C:\Program Files\dotnet\")]
42+
[InlineData("pwsh", new[] { @"backslash\""preceedingquote" }, @"pwsh backslash\""preceedingquote")]
43+
[InlineData("pwsh", new[] { @""" hello """ }, @"pwsh "" hello """)]
44+
public void ConcatenateCommandAndArgArrayForPwshProcessStart(string command, string[]? args, string expected)
45+
{
46+
// Given / When
47+
var result = ArgumentBuilder.ConcatenateCommandAndArgArrayForPwshProcessStart(command, args);
48+
49+
// Then
50+
result.ShouldBe(expected);
51+
}
52+
3053
[Theory]
3154
[InlineData("cmd", null, "cmd")]
3255
[InlineData("cm \"d\"", null, "cm \"d\"")]

0 commit comments

Comments
 (0)