Progmasoft Git

tazel

A build system for C, C++, Objective-C, Objective-C++ and Zig, with C# project files.

Clonegit clone https://git.progmasoft.com/tazel

Pass a line for cmd.exe unchanged, and read test output with CRLF

Processes.AddArguments gives cmd.exe /d /s /c "line" as written instead of
through ArgumentList, whose escapes cmd.exe does not read; every process
start uses it. The step test reads its output with either line ending.
Commit
9b468e9f7448b5a14afdda85f09f47e678af6348
Author
Leitwolf11 ·
Parents
576359cd4986

Files · Download .tar.gz

Changes

 src/Progmasoft.Tazel/Driver/Processes.cs    | 39 ++++++++++++++++++++---------
 tests/Progmasoft.Tazel.Tests/PluginTests.cs |  2 +-
 2 files changed, 28 insertions(+), 13 deletions(-)

diff --git a/src/Progmasoft.Tazel/Driver/Processes.cs b/src/Progmasoft.Tazel/Driver/Processes.cs
index 196e9ab..623a790 100644
--- a/src/Progmasoft.Tazel/Driver/Processes.cs
+++ b/src/Progmasoft.Tazel/Driver/Processes.cs
@@ -5,6 +5,7 @@ using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Diagnostics;
+using System.IO;
 using System.Text;
 using System.Threading;
 using System.Threading.Tasks;
@@ -45,10 +46,7 @@ internal static class Processes
             StandardOutputEncoding = Encoding.UTF8,
             StandardErrorEncoding = Encoding.UTF8,
         };
-        for (var i = 1; i < command.Count; i++)
-        {
-            start.ArgumentList.Add(command[i]);
-        }
+        AddArguments(start, command);
 
         start.Environment.Clear();
         foreach (var (name, value) in environment)
@@ -94,6 +92,29 @@ internal static class Processes
         return new ProcessResult(process.ExitCode, Text(output));
     }
 
+    /// <summary>
+    /// Gives <paramref name="start"/> the arguments of <paramref name="command"/>. A line for cmd.exe, as
+    /// <c>cmd.exe /d /s /c "line"</c>, is passed as written: cmd.exe does not read the escapes other programs read, so
+    /// the line reaches it unchanged.
+    /// </summary>
+    public static void AddArguments(ProcessStartInfo start, IReadOnlyList<string> command)
+    {
+        ArgumentNullException.ThrowIfNull(start);
+        ArgumentNullException.ThrowIfNull(command);
+        if (OperatingSystem.IsWindows() && command.Count == 5 &&
+            string.Equals(Path.GetFileName(command[0]), "cmd.exe", StringComparison.OrdinalIgnoreCase) &&
+            command[1] == "/d" && command[2] == "/s" && command[3] == "/c")
+        {
+            start.Arguments = $"/d /s /c {command[4]}";
+            return;
+        }
+
+        for (var i = 1; i < command.Count; i++)
+        {
+            start.ArgumentList.Add(command[i]);
+        }
+    }
+
     /// <summary>Runs <paramref name="command"/> and keeps what it prints apart from its errors.</summary>
     public static async Task<(int ExitCode, string Output, string Errors)> CaptureAsync(
         IReadOnlyList<string> command, string directory, IReadOnlyDictionary<string, string> environment, CancellationToken cancel)
@@ -109,10 +130,7 @@ internal static class Processes
             UseShellExecute = false,
             CreateNoWindow = true,
         };
-        for (var i = 1; i < command.Count; i++)
-        {
-            start.ArgumentList.Add(command[i]);
-        }
+        AddArguments(start, command);
 
         start.Environment.Clear();
         foreach (var (name, value) in environment)
@@ -143,10 +161,7 @@ internal static class Processes
         ArgumentNullException.ThrowIfNull(command);
         ArgumentNullException.ThrowIfNull(environment);
         var start = new ProcessStartInfo(command[0]) { WorkingDirectory = directory, UseShellExecute = false };
-        for (var i = 1; i < command.Count; i++)
-        {
-            start.ArgumentList.Add(command[i]);
-        }
+        AddArguments(start, command);
 
         start.Environment.Clear();
         foreach (var (name, value) in environment)
diff --git a/tests/Progmasoft.Tazel.Tests/PluginTests.cs b/tests/Progmasoft.Tazel.Tests/PluginTests.cs
index 740e285..1aca61e 100644
--- a/tests/Progmasoft.Tazel.Tests/PluginTests.cs
+++ b/tests/Progmasoft.Tazel.Tests/PluginTests.cs
@@ -42,7 +42,7 @@ public sealed class StepTests
             .Write("MODULE.csx", TemporaryModule.ModuleFile).Write("BUILD.csx", Build).Write("in.txt", "hello");
         var (status, output) = await module.TazelAsync(".", "build", "//...");
         Assert.True(status == 0, output);
-        Assert.Contains("Running //:copy\ncopying", output, StringComparison.Ordinal);
+        Assert.Contains("Running //:copy\ncopying", output.Replace("\r\n", "\n", StringComparison.Ordinal), StringComparison.Ordinal);
         var made = Directory.GetFiles(Path.Combine(module.Root, ".tazel"), "out.txt", SearchOption.AllDirectories).Single();
         Assert.Equal("HELLO", await File.ReadAllTextAsync(made, TestContext.Current.CancellationToken));