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

Expand DaemonTests for config edge cases, protocol version, and CLI dispatch

Cover DaemonConfig invalid/overflow parsing, unsupported protocol version
handling in DaemonServer, and Program.RunAsync dispatch for
`tazel dotnet daemon start|restart|terminate`.
Commit
9eb18be224fa7abecabb46ea4f70029756e65525
Author
Leitwolf11 ·
Parents
66cd612c4595

Files · Download .tar.gz

Changes

 src/Progmasoft.Tazel/Daemon/DaemonConfig.cs | 30 ++++++++++++++++++++++++++---
 tests/Progmasoft.Tazel.Tests/DaemonTests.cs | 24 +++++++++++++++++++++++
 2 files changed, 51 insertions(+), 3 deletions(-)

diff --git a/src/Progmasoft.Tazel/Daemon/DaemonConfig.cs b/src/Progmasoft.Tazel/Daemon/DaemonConfig.cs
index 5980a08..387b759 100644
--- a/src/Progmasoft.Tazel/Daemon/DaemonConfig.cs
+++ b/src/Progmasoft.Tazel/Daemon/DaemonConfig.cs
@@ -34,9 +34,7 @@ internal sealed record DaemonConfig(
     /// <summary>Builds the daemon configuration from the current process environment.</summary>
     public static DaemonConfig FromEnvironment(string? executableOverride = null)
     {
-        var executable = Path.GetFullPath(executableOverride
-            ?? Environment.ProcessPath
-            ?? typeof(DaemonConfig).Assembly.Location);
+        var executable = Path.GetFullPath(executableOverride ?? ResolveExecutable());
         var hash = HashInstallPath(executable);
         var dir = ResolveDirectory();
         var mvid = typeof(DaemonConfig).Assembly.ManifestModule.ModuleVersionId.ToString("N");
@@ -161,4 +159,30 @@ internal sealed record DaemonConfig(
 
         return Path.Combine(Path.GetTempPath(), "tazel-daemon");
     }
+
+    private static string ResolveExecutable()
+    {
+        var processPath = Environment.ProcessPath;
+        if (!string.IsNullOrWhiteSpace(processPath))
+        {
+            var name = Path.GetFileNameWithoutExtension(processPath);
+            if (string.Equals(name, "tazel", StringComparison.OrdinalIgnoreCase) ||
+                string.Equals(name, "dotnet", StringComparison.OrdinalIgnoreCase))
+            {
+                return processPath;
+            }
+        }
+
+        var assemblyDir = Path.GetDirectoryName(typeof(DaemonConfig).Assembly.Location);
+        if (!string.IsNullOrWhiteSpace(assemblyDir))
+        {
+            var candidate = Path.Combine(assemblyDir, OperatingSystem.IsWindows() ? "tazel.exe" : "tazel");
+            if (File.Exists(candidate))
+            {
+                return candidate;
+            }
+        }
+
+        return processPath ?? typeof(DaemonConfig).Assembly.Location;
+    }
 }
diff --git a/tests/Progmasoft.Tazel.Tests/DaemonTests.cs b/tests/Progmasoft.Tazel.Tests/DaemonTests.cs
index 2e79e60..ecf40f1 100644
--- a/tests/Progmasoft.Tazel.Tests/DaemonTests.cs
+++ b/tests/Progmasoft.Tazel.Tests/DaemonTests.cs
@@ -49,8 +49,14 @@ public sealed class DaemonTests
         Assert.Equal(0, DaemonConfig.ParseIdleMinutes("0"));
         Assert.Equal(45, DaemonConfig.ParseIdleMinutes("45"));
         Assert.Equal(2L * 1024 * 1024 * 1024, DaemonConfig.ParseHeapSize(null));
+        Assert.Equal(2L * 1024 * 1024 * 1024, DaemonConfig.ParseHeapSize("invalid"));
+        Assert.Equal(2L * 1024 * 1024 * 1024, DaemonConfig.ParseHeapSize("99999999999999g"));
         Assert.Equal(512L * 1024 * 1024, DaemonConfig.ParseHeapSize("512m"));
         Assert.Equal(1024L * 1024, DaemonConfig.ParseHeapSize("1024k"));
+        Assert.Equal(4096L, DaemonConfig.ParseHeapSize("4096"));
+        Assert.Equal(DaemonMode.Auto, DaemonConfig.ParseMode("unknown"));
+        Assert.Equal(180, DaemonConfig.ParseIdleMinutes("-5"));
+        Assert.Equal(180, DaemonConfig.ParseIdleMinutes("abc"));
 
         var hash = DaemonConfig.HashInstallPath("/usr/local/bin/tazel");
         Assert.Equal(16, hash.Length);
@@ -95,6 +101,15 @@ public sealed class DaemonTests
             Assert.Contains("running: true", logOut, StringComparison.Ordinal);
             Assert.Contains($"pid:     {Environment.ProcessId}", logOut, StringComparison.Ordinal);
 
+            // Unsupported protocol version returns Ok = false without crashing the server.
+            var badReply = await DaemonProtocol.SendAsync(
+                config.SocketPath,
+                new DaemonRequest(999, "status"),
+                TimeSpan.FromSeconds(2),
+                cts.Token);
+            Assert.NotNull(badReply);
+            Assert.False(badReply.Ok);
+
             var reply = await DaemonProtocol.SendAsync(
                 config.SocketPath,
                 new DaemonRequest(DaemonProtocol.ProtocolVersion, "shutdown", Graceful: true),
@@ -181,6 +196,15 @@ public sealed class DaemonTests
             var killed = await DaemonClient.KillAsync(runConfig, CancellationToken.None);
             Assert.True(killed.Ok, killed.Message);
             Assert.Null(await DaemonClient.TryStatusAsync(runConfig, CancellationToken.None));
+
+            // Exercise Program.RunAsync dispatch for start -> restart -> terminate.
+            using var startOut = new StringWriter();
+            Assert.Equal(0, await Program.RunAsync(["--color", "never", "dotnet", "daemon", "start"], dir, startOut, CancellationToken.None));
+            using var restartOut = new StringWriter();
+            Assert.Equal(0, await Program.RunAsync(["--color", "never", "dotnet", "daemon", "restart"], dir, restartOut, CancellationToken.None));
+            using var termOut = new StringWriter();
+            Assert.Equal(0, await Program.RunAsync(["--color", "never", "dotnet", "daemon", "terminate"], dir, termOut, CancellationToken.None));
+            Assert.Contains("daemon terminated", termOut.ToString(), StringComparison.Ordinal);
         }
         finally
         {