Skip to content

Commit afddf5f

Browse files
authored
Merge pull request #38 from xt0rted/cmd-escaping
Remove caret escaping from cmd commands
2 parents 1edb2e9 + 2bf3425 commit afddf5f

File tree

5 files changed

+28
-18
lines changed

5 files changed

+28
-18
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ jobs:
159159
name: integration-test-results
160160
path: ./.coverage/*.trx
161161

162+
- run: dotnet r integration:ci --verbose -- --logger "trx;LogFilePrefix=ci test"
163+
162164
release:
163165
if: github.event_name == 'push'
164166

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
- fixed: Don't escape the script passed to `cmd.exe`, just the double dash arguments
6+
37
## [0.2.0](https://github.com/xt0rted/dotnet-run-script/compare/v0.1.0...v0.2.0) - 2022-04-23
48

9+
> ℹ️ This version broke conditional script execution (`cmd1 && cmd2`) in `cmd.exe`
10+
511
- added: Force color output with the `DOTNET_SYSTEM_CONSOLE_ALLOW_ANSI_COLOR_REDIRECTION` environment variable.
612
- Note: this tool with output color on all platforms including when output is redirected, but the dotnet cli only supports this on Unix platforms currently. This means script results might not be colored in places like GitHub Actions build logs when using the Windows VMs.
713
- added: Added `-v` alias to enable verbose output.

global.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
"test:release": "dotnet r test -- --configuration Release",
2020
"pack:release": "dotnet r pack -- --configuration Release",
2121

22+
// integration tests
23+
"integration:ci": "dotnet r clean && dotnet r clean:bin && dotnet build && dotnet test --no-build",
24+
2225
// test scripts
2326
"info": "dotnet r dotnet:version && dotnet r dotnet:info",
2427
"dotnet:info": "dotnet --info",

src/ArgumentBuilder.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public static string EscapeAndConcatenateCommandAndArgArrayForCmdProcessStart(
6262
{
6363
var sb = new ValueStringBuilder(stackalloc char[256]);
6464

65-
EscapeArgForCmd(ref sb, command, quoteValue: false);
65+
sb.Append(command);
6666

6767
if (args is not null)
6868
{
@@ -71,7 +71,7 @@ public static string EscapeAndConcatenateCommandAndArgArrayForCmdProcessStart(
7171
sb.Append(Caret);
7272
sb.Append(Space);
7373

74-
EscapeArgForCmd(ref sb, args[i], quoteValue: true);
74+
EscapeArgForCmd(ref sb, args[i]);
7575
}
7676
}
7777

@@ -165,10 +165,9 @@ private static void EscapeSingleArg(ref ValueStringBuilder sb, ReadOnlySpan<char
165165
/// </remarks>
166166
/// <param name="sb">The <seealso cref="ValueStringBuilder"/> to append to.</param>
167167
/// <param name="argument">The argument to escape.</param>
168-
/// <param name="quoteValue">If the value should be wrapped in quotes if it contains whitespace.</param>
169-
private static void EscapeArgForCmd(ref ValueStringBuilder sb, ReadOnlySpan<char> argument, bool quoteValue)
168+
private static void EscapeArgForCmd(ref ValueStringBuilder sb, ReadOnlySpan<char> argument)
170169
{
171-
var quoted = quoteValue && ArgumentContainsWhitespace(argument);
170+
var quoted = ArgumentContainsWhitespace(argument);
172171

173172
if (quoted)
174173
{

test/ArgumentBuilderTests.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,19 @@ public void EscapeAndConcatenateCommandAndArgArrayForProcessStart(string command
2727
}
2828

2929
[Theory]
30-
[InlineData("cmd", null, "^c^m^d")]
31-
[InlineData("cm \"d\"", null, "^c^m^ ^\"^d^\"")]
32-
[InlineData("c m d", null, "^c^ ^m^ ^d")]
33-
[InlineData("c m d", new string[0], "^c^ ^m^ ^d")]
34-
[InlineData("c m d", new[] { "one", "two", "three" }, "^c^ ^m^ ^d^ ^o^n^e^ ^t^w^o^ ^t^h^r^e^e")]
35-
[InlineData("c m d", new[] { "line1\nline2", "word1\tword2" }, "^c^ ^m^ ^d^ ^\"^l^i^n^e^1^\n^l^i^n^e^2^\"^ ^\"^w^o^r^d^1^\t^w^o^r^d^2^\"")]
36-
[InlineData("c m d", new[] { "with spaces" }, "^c^ ^m^ ^d^ ^\"^w^i^t^h^ ^s^p^a^c^e^s^\"")]
37-
[InlineData("c m d", new[] { @"with\backslash" }, @"^c^ ^m^ ^d^ ^w^i^t^h^\^b^a^c^k^s^l^a^s^h")]
38-
[InlineData("c m d", new[] { @"""quotedwith\backslash""" }, @"^c^ ^m^ ^d^ ^""^q^u^o^t^e^d^w^i^t^h^\^b^a^c^k^s^l^a^s^h^""")]
39-
[InlineData("c m d", new[] { @"C:\Users\" }, @"^c^ ^m^ ^d^ ^C^:^\^U^s^e^r^s^\")]
40-
[InlineData("c m d", new[] { @"C:\Program Files\dotnet\" }, @"^c^ ^m^ ^d^ ^""^C^:^\^P^r^o^g^r^a^m^ ^F^i^l^e^s^\^d^o^t^n^e^t^\^""")]
41-
[InlineData("c m d", new[] { @"backslash\""preceedingquote" }, @"^c^ ^m^ ^d^ ^b^a^c^k^s^l^a^s^h^\^""^p^r^e^c^e^e^d^i^n^g^q^u^o^t^e")]
42-
[InlineData("c m d", new[] { @""" hello """ }, @"^c^ ^m^ ^d^ ^""^""^ ^h^e^l^l^o^ ^""^""")]
30+
[InlineData("cmd", null, "cmd")]
31+
[InlineData("cm \"d\"", null, "cm \"d\"")]
32+
[InlineData("c m d", null, "c m d")]
33+
[InlineData("c m d", new string[0], "c m d")]
34+
[InlineData("c m d", new[] { "one", "two", "three" }, "c m d^ ^o^n^e^ ^t^w^o^ ^t^h^r^e^e")]
35+
[InlineData("c m d", new[] { "line1\nline2", "word1\tword2" }, "c m d^ ^\"^l^i^n^e^1^\n^l^i^n^e^2^\"^ ^\"^w^o^r^d^1^\t^w^o^r^d^2^\"")]
36+
[InlineData("c m d", new[] { "with spaces" }, "c m d^ ^\"^w^i^t^h^ ^s^p^a^c^e^s^\"")]
37+
[InlineData("c m d", new[] { @"with\backslash" }, @"c m d^ ^w^i^t^h^\^b^a^c^k^s^l^a^s^h")]
38+
[InlineData("c m d", new[] { @"""quotedwith\backslash""" }, @"c m d^ ^""^q^u^o^t^e^d^w^i^t^h^\^b^a^c^k^s^l^a^s^h^""")]
39+
[InlineData("c m d", new[] { @"C:\Users\" }, @"c m d^ ^C^:^\^U^s^e^r^s^\")]
40+
[InlineData("c m d", new[] { @"C:\Program Files\dotnet\" }, @"c m d^ ^""^C^:^\^P^r^o^g^r^a^m^ ^F^i^l^e^s^\^d^o^t^n^e^t^\^""")]
41+
[InlineData("c m d", new[] { @"backslash\""preceedingquote" }, @"c m d^ ^b^a^c^k^s^l^a^s^h^\^""^p^r^e^c^e^e^d^i^n^g^q^u^o^t^e")]
42+
[InlineData("c m d", new[] { @""" hello """ }, @"c m d^ ^""^""^ ^h^e^l^l^o^ ^""^""")]
4343
public void EscapeAndConcatenateCommandAndArgArrayForCmdProcessStart(string command, string[] args, string expected)
4444
{
4545
// Given / When

0 commit comments

Comments
 (0)