|
| 1 | +using System.Diagnostics; |
| 2 | + |
1 | 3 | namespace OpenClaw.Tray.Tests; |
2 | 4 |
|
3 | 5 | /// <summary> |
@@ -198,6 +200,107 @@ public void LocalInstallerBuild_UsesOneIdentitySwitchAndValidatesPayloadMarker() |
198 | 200 | Assert.DoesNotContain("<DevBuild>true</DevBuild>", project); |
199 | 201 | } |
200 | 202 |
|
| 203 | + [Theory] |
| 204 | + [InlineData("Release", false, false, "win-x64", true)] |
| 205 | + [InlineData("Release", false, true, "win-x64", true)] |
| 206 | + [InlineData("Release", false, false, "win-arm64", true)] |
| 207 | + [InlineData("Release", false, true, "win-arm64", true)] |
| 208 | + [InlineData("Release", true, false, "win-x64", false)] |
| 209 | + [InlineData("Debug", false, false, "win-x64", false)] |
| 210 | + public async Task ComWrapperDiagnosticsSwitch_EvaluatesOnlyForProductionBuilds( |
| 211 | + string configuration, |
| 212 | + bool devBuild, |
| 213 | + bool packageMsix, |
| 214 | + string runtimeIdentifier, |
| 215 | + bool expected) |
| 216 | + { |
| 217 | + var repositoryRoot = TestRepositoryPaths.GetRepositoryRoot(); |
| 218 | + var projectPath = Path.Combine( |
| 219 | + repositoryRoot, |
| 220 | + "src", |
| 221 | + "OpenClaw.Tray.WinUI", |
| 222 | + "OpenClaw.Tray.WinUI.csproj"); |
| 223 | + var startInfo = new ProcessStartInfo("dotnet") |
| 224 | + { |
| 225 | + WorkingDirectory = repositoryRoot, |
| 226 | + RedirectStandardOutput = true, |
| 227 | + RedirectStandardError = true, |
| 228 | + UseShellExecute = false, |
| 229 | + CreateNoWindow = true, |
| 230 | + }; |
| 231 | + startInfo.ArgumentList.Add("msbuild"); |
| 232 | + startInfo.ArgumentList.Add(projectPath); |
| 233 | + startInfo.ArgumentList.Add("-nologo"); |
| 234 | + startInfo.ArgumentList.Add("-v:q"); |
| 235 | + startInfo.ArgumentList.Add("-getItem:RuntimeHostConfigurationOption"); |
| 236 | + startInfo.ArgumentList.Add($"-p:Configuration={configuration}"); |
| 237 | + startInfo.ArgumentList.Add($"-p:DevBuild={devBuild.ToString().ToLowerInvariant()}"); |
| 238 | + startInfo.ArgumentList.Add($"-p:PackageMsix={packageMsix.ToString().ToLowerInvariant()}"); |
| 239 | + startInfo.ArgumentList.Add($"-p:RuntimeIdentifier={runtimeIdentifier}"); |
| 240 | + |
| 241 | + using var process = Process.Start(startInfo); |
| 242 | + Assert.NotNull(process); |
| 243 | + var standardOutput = process.StandardOutput.ReadToEndAsync(); |
| 244 | + var standardError = process.StandardError.ReadToEndAsync(); |
| 245 | + using var timeout = new CancellationTokenSource(TimeSpan.FromMinutes(2)); |
| 246 | + try |
| 247 | + { |
| 248 | + await process.WaitForExitAsync(timeout.Token); |
| 249 | + } |
| 250 | + catch (OperationCanceledException) when (timeout.IsCancellationRequested) |
| 251 | + { |
| 252 | + if (!process.HasExited) |
| 253 | + process.Kill(entireProcessTree: true); |
| 254 | + await process.WaitForExitAsync(); |
| 255 | + |
| 256 | + throw new TimeoutException( |
| 257 | + $"MSBuild evaluation timed out.{Environment.NewLine}" + |
| 258 | + $"Standard output:{Environment.NewLine}{await standardOutput}{Environment.NewLine}" + |
| 259 | + $"Standard error:{Environment.NewLine}{await standardError}"); |
| 260 | + } |
| 261 | + |
| 262 | + var output = await standardOutput; |
| 263 | + var error = await standardError; |
| 264 | + |
| 265 | + Assert.True( |
| 266 | + process.ExitCode == 0, |
| 267 | + $"MSBuild evaluation failed with exit code {process.ExitCode}.{Environment.NewLine}" + |
| 268 | + $"Standard output:{Environment.NewLine}{output}{Environment.NewLine}" + |
| 269 | + $"Standard error:{Environment.NewLine}{error}"); |
| 270 | + |
| 271 | + JsonNode? result; |
| 272 | + try |
| 273 | + { |
| 274 | + result = JsonNode.Parse(output); |
| 275 | + } |
| 276 | + catch (System.Text.Json.JsonException exception) |
| 277 | + { |
| 278 | + throw new InvalidDataException( |
| 279 | + $"MSBuild evaluation returned invalid JSON:{Environment.NewLine}{output}", |
| 280 | + exception); |
| 281 | + } |
| 282 | + |
| 283 | + var options = result?["Items"]?["RuntimeHostConfigurationOption"]?.AsArray(); |
| 284 | + Assert.NotNull(options); |
| 285 | + var matchingOptions = options |
| 286 | + .Where(option => |
| 287 | + string.Equals( |
| 288 | + option?["Identity"]?.GetValue<string>(), |
| 289 | + "System.Diagnostics.Debugger.IsSupported", |
| 290 | + StringComparison.Ordinal)) |
| 291 | + .ToArray(); |
| 292 | + |
| 293 | + if (!expected) |
| 294 | + { |
| 295 | + Assert.Empty(matchingOptions); |
| 296 | + return; |
| 297 | + } |
| 298 | + |
| 299 | + var matchingOption = Assert.Single(matchingOptions); |
| 300 | + Assert.Equal("false", matchingOption?["Value"]?.GetValue<string>()); |
| 301 | + Assert.Equal("true", matchingOption?["Trim"]?.GetValue<string>()); |
| 302 | + } |
| 303 | + |
201 | 304 | [Fact] |
202 | 305 | public void MsixManifest_IsGeneratedUnderObjWithoutMutatingTrackedSource() |
203 | 306 | { |
|
0 commit comments