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

Make the tool exception public and test the batch line

ToolException is public, as the other exceptions of Tazel are; the
batch command is built by an overload that takes the platform, so the
cmd.exe line and its refusals are tested on every system.
Commit
ed831ec6cb1f5d17059f54383e0da0759e92c13a
Author
Leitwolf11 ·
Parents
582d22ed7114

Files · Download .tar.gz

Changes

 src/Progmasoft.Tazel/Cli/ToolCommands.cs  | 19 +++++++++++++------
 tests/Progmasoft.Tazel.Tests/ToolTests.cs | 19 +++++++++++++++++--
 2 files changed, 30 insertions(+), 8 deletions(-)

diff --git a/src/Progmasoft.Tazel/Cli/ToolCommands.cs b/src/Progmasoft.Tazel/Cli/ToolCommands.cs
index 9f78f21..cbb38ad 100644
--- a/src/Progmasoft.Tazel/Cli/ToolCommands.cs
+++ b/src/Progmasoft.Tazel/Cli/ToolCommands.cs
@@ -18,17 +18,20 @@ internal sealed record ToolLaunchCommand(OutputOptions Output, string Name, IRea
 internal sealed record ToolWrapperCommand(OutputOptions Output, string Name, string? OutputDirectory) : Command(Output);
 
 /// <summary>A tool Tazel cannot find or start, or an argument it cannot pass on as written.</summary>
-internal sealed class ToolException : Exception
+public sealed class ToolException : Exception
 {
+    /// <summary>What is wrong.</summary>
     public ToolException(string message)
         : base(message)
     {
     }
 
+    /// <summary>Nothing to say.</summary>
     public ToolException()
     {
     }
 
+    /// <summary>What is wrong, and why.</summary>
     public ToolException(string message, Exception inner)
         : base(message, inner)
     {
@@ -104,8 +107,9 @@ internal static class Tools
         OperatingSystem.IsWindows() || OperatingSystem.IsMacOS() ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;
 
     // TAZEL_TOOL_EXECUTABLE: a path, absolute or from the working directory; anything wrong with it is an error.
-    private static string Chosen(string chosen, string workingDirectory)
+    private static string Chosen(string value, string workingDirectory)
     {
+        var chosen = value;
         if (chosen.Trim().Length == 0)
         {
             throw new ToolException($"{ExecutableVariable} is set but empty; name an executable, or unset it to search PATH");
@@ -177,10 +181,14 @@ internal static class Tools
     /// word at <c>"</c> whatever stands around them, so an argument with either, or with a line break or NUL, is
     /// refused rather than changed.
     /// </summary>
-    public static IReadOnlyList<string> Command(string file, IReadOnlyList<string> arguments)
+    public static IReadOnlyList<string> Command(string file, IReadOnlyList<string> arguments) =>
+        Command(file, arguments, OperatingSystem.IsWindows());
+
+    /// <summary><see cref="Command(string, IReadOnlyList{string})"/>, as on Windows when <paramref name="windows"/>.</summary>
+    internal static IReadOnlyList<string> Command(string file, IReadOnlyList<string> arguments, bool windows)
     {
         ArgumentNullException.ThrowIfNull(arguments);
-        if (!IsBatch(file))
+        if (!(windows && IsBatch(file)))
         {
             return [file, .. arguments];
         }
@@ -210,8 +218,7 @@ internal static class Tools
         return [cmd, "/d", "/s", "/c", line.ToString()];
     }
 
-    private static bool IsBatch(string file) =>
-        OperatingSystem.IsWindows() && Path.GetExtension(file).ToLowerInvariant() is ".bat" or ".cmd";
+    private static bool IsBatch(string file) => Path.GetExtension(file).ToLowerInvariant() is ".bat" or ".cmd";
 
     internal static bool IsBatchSafe(string text) => text.IndexOfAny(['"', '%', '\r', '\n', '\0']) < 0;
 
diff --git a/tests/Progmasoft.Tazel.Tests/ToolTests.cs b/tests/Progmasoft.Tazel.Tests/ToolTests.cs
index d2ef71c..aefb986 100644
--- a/tests/Progmasoft.Tazel.Tests/ToolTests.cs
+++ b/tests/Progmasoft.Tazel.Tests/ToolTests.cs
@@ -5,6 +5,7 @@ using System;
 using System.Collections.Generic;
 using System.Diagnostics;
 using System.IO;
+using System.Linq;
 using System.Threading.Tasks;
 using Progmasoft.Tazel.Cli;
 using Xunit;
@@ -43,7 +44,7 @@ public sealed class ToolTests
     }
 
     // A tool that prints each argument in brackets and leaves with status 7.
-    private static string Tool(string directory, string name, bool executable = true)
+    private static void Tool(string directory, string name, bool executable = true)
     {
         var file = Path.Combine(directory, name);
         File.WriteAllText(file, "#!/bin/sh\nfor a in \"$@\"; do printf '[%s]' \"$a\"; done; echo; exit 7\n");
@@ -51,7 +52,6 @@ public sealed class ToolTests
         {
             File.SetUnixFileMode(file, executable ? UnixFileMode.UserRead | UnixFileMode.UserWrite | UnixFileMode.UserExecute : UnixFileMode.UserRead | UnixFileMode.UserWrite);
         }
-        return file;
     }
 
     private static string WithPath(params string[] directories) =>
@@ -86,6 +86,21 @@ public sealed class ToolTests
     [InlineData("a\nb", false)]
     public void KnowsWhatABatchFileCanBeGiven(string argument, bool safe) => Assert.Equal(safe, Tools.IsBatchSafe(argument));
 
+    [Fact]
+    public void ABatchFileRunsThroughCmdWithEveryArgumentQuoted()
+    {
+        var command = Tools.Command(@"C:\Tools (x86)\tazel-fmt.bat", ["", "a b", "x&y", "--"], windows: true);
+        Assert.Equal("cmd.exe", Path.GetFileName(command[0]));
+        Assert.Equal(["/d", "/s", "/c"], command.Skip(1).Take(3));
+        Assert.Equal("\"\"C:\\Tools (x86)\\tazel-fmt.bat\" \"\" \"a b\" \"x&y\" \"--\"\"", command[4]);
+        Assert.Contains("argument 2", Assert.Throws<ToolException>(() => Tools.Command("t.cmd", ["ok", "50%"], windows: true)).Message,
+            StringComparison.Ordinal);
+        Assert.Throws<ToolException>(() => Tools.Command("t.bat", ["say \"hi\""], windows: true));
+        Assert.Throws<ToolException>(() => Tools.Command("C:\\100%\\t.bat", [], windows: true));
+        Assert.Equal(["t.exe", "50%"], Tools.Command("t.exe", ["50%"], windows: true));
+        Assert.Equal(["t.bat", "50%"], Tools.Command("t.bat", ["50%"], windows: false));
+    }
+
     [Fact]
     public void ANativeToolGetsItsArgumentsAsWritten() =>
         Assert.Equal(["/bin/tool", "", "--", "--help", "a b", "%x%"], Tools.Command("/bin/tool", ["", "--", "--help", "a b", "%x%"]));