Forge
csharpdeeb25a2
1using System.Diagnostics;
2using System.Text.Json;
3using Xunit;
4
5namespace CascadeIDE.Tests;
6
7[Trait("Category", "BuildVerifyWorker")]
8public sealed class BuildVerifyWorkerCliTests
9{
10 private static string RepoRoot()
11 {
12 var dir = new DirectoryInfo(AppContext.BaseDirectory);
13 while (dir is not null)
14 {
15 if (File.Exists(Path.Combine(dir.FullName, "CascadeIDE.sln")))
16 return dir.FullName;
17 dir = dir.Parent;
18 }
19
20 throw new InvalidOperationException("CascadeIDE.sln not found from test output path.");
21 }
22
23 private static string WorkerDllPath()
24 {
25 var inOutput = Path.Combine(AppContext.BaseDirectory, "CascadeIDE.BuildVerifyWorker.dll");
26 if (File.Exists(inOutput))
27 return inOutput;
28
29 var fallback = Path.Combine(
30 RepoRoot(),
31 "tools",
32 "CascadeIDE.BuildVerifyWorker",
33 "bin",
34 "Debug",
35 "net10.0",
36 "CascadeIDE.BuildVerifyWorker.dll");
37 if (File.Exists(fallback))
38 return fallback;
39
40 throw new FileNotFoundException(
41 "BuildVerifyWorker assembly not found; build tools/CascadeIDE.BuildVerifyWorker first.");
42 }
43
44 [Fact]
45 public void Worker_missing_args_returns_exit_2()
46 {
47 var (exit, _, _) = RunWorker([]);
48 Assert.Equal(2, exit);
49 }
50
51 [Fact]
52 public void Worker_unknown_mode_returns_exit_2()
53 {
54 var csproj = SampleCsprojPath();
55 var (exit, _, _) = RunWorker(["publish", csproj]);
56 Assert.Equal(2, exit);
57 }
58
59 [Fact]
60 public void Worker_missing_project_returns_exit_2()
61 {
62 var missing = Path.Combine(RepoRoot(), "samples", "DebugTarget", "does-not-exist.csproj");
63 var (exit, _, _) = RunWorker(["build", missing]);
64 Assert.Equal(2, exit);
65 }
66
67 [Fact]
68 public void Worker_build_sample_csproj_returns_success_json()
69 {
70 var csproj = SampleCsprojPath();
71 var (exit, stdout, _) = RunWorker(["build", csproj]);
72
73 Assert.Equal(0, exit);
74 using var doc = JsonDocument.Parse(stdout);
75 Assert.True(doc.RootElement.GetProperty("success").GetBoolean());
76 }
77
78 private static string SampleCsprojPath() =>
79 Path.Combine(RepoRoot(), "samples", "DebugTarget", "DebugTarget.csproj");
80
81 private static (int ExitCode, string StdOut, string StdErr) RunWorker(string[] workerArgs)
82 {
83 var dll = WorkerDllPath();
84 var psi = new ProcessStartInfo("dotnet", $"exec \"{dll}\" {string.Join(' ', workerArgs.Select(QuoteArg))}")
85 {
86 RedirectStandardOutput = true,
87 RedirectStandardError = true,
88 UseShellExecute = false,
89 CreateNoWindow = true,
90 };
91
92 using var proc = Process.Start(psi) ?? throw new InvalidOperationException("Failed to start worker process.");
93 var stdout = proc.StandardOutput.ReadToEnd();
94 var stderr = proc.StandardError.ReadToEnd();
95 proc.WaitForExit(TimeSpan.FromMinutes(5));
96 return (proc.ExitCode, stdout.Trim(), stderr.Trim());
97 }
98
99 private static string QuoteArg(string arg) =>
100 arg.Contains(' ', StringComparison.Ordinal) || arg.Contains('"', StringComparison.Ordinal)
101 ? $"\"{arg.Replace("\"", "\\\"", StringComparison.Ordinal)}\""
102 : arg;
103}
104
View only · write via MCP/CIDE