|
| 1 | +using System.Diagnostics; |
| 2 | + |
1 | 3 | namespace OpenClaw.Tray.Tests; |
2 | 4 |
|
3 | 5 | /// <summary> |
@@ -198,28 +200,105 @@ public void LocalInstallerBuild_UsesOneIdentitySwitchAndValidatesPayloadMarker() |
198 | 200 | Assert.DoesNotContain("<DevBuild>true</DevBuild>", project); |
199 | 201 | } |
200 | 202 |
|
201 | | - [Fact] |
202 | | - public void ProductionBuild_DisablesComWrapperDiagnosticsWithoutChangingDevBuilds() |
| 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) |
203 | 216 | { |
| 217 | + var repositoryRoot = TestRepositoryPaths.GetRepositoryRoot(); |
204 | 218 | var projectPath = Path.Combine( |
205 | | - TestRepositoryPaths.GetRepositoryRoot(), |
| 219 | + repositoryRoot, |
206 | 220 | "src", |
207 | 221 | "OpenClaw.Tray.WinUI", |
208 | 222 | "OpenClaw.Tray.WinUI.csproj"); |
209 | | - var project = System.Xml.Linq.XDocument.Load(projectPath); |
210 | | - var option = Assert.Single( |
211 | | - project.Descendants("RuntimeHostConfigurationOption"), |
212 | | - element => |
| 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 => |
213 | 287 | string.Equals( |
214 | | - element.Attribute("Include")?.Value, |
| 288 | + option?["Identity"]?.GetValue<string>(), |
215 | 289 | "System.Diagnostics.Debugger.IsSupported", |
216 | | - StringComparison.Ordinal)); |
| 290 | + StringComparison.Ordinal)) |
| 291 | + .ToArray(); |
| 292 | + |
| 293 | + if (!expected) |
| 294 | + { |
| 295 | + Assert.Empty(matchingOptions); |
| 296 | + return; |
| 297 | + } |
217 | 298 |
|
218 | | - Assert.Equal("false", option.Attribute("Value")?.Value); |
219 | | - Assert.Equal("true", option.Attribute("Trim")?.Value); |
220 | | - Assert.Equal( |
221 | | - "'$(Configuration)' == 'Release' and '$(DevBuild)' != 'true'", |
222 | | - option.Parent?.Attribute("Condition")?.Value); |
| 299 | + var matchingOption = Assert.Single(matchingOptions); |
| 300 | + Assert.Equal("false", matchingOption?["Value"]?.GetValue<string>()); |
| 301 | + Assert.Equal("true", matchingOption?["Trim"]?.GetValue<string>()); |
223 | 302 | } |
224 | 303 |
|
225 | 304 | [Fact] |
|
0 commit comments