CheckDisableTransitiveFailingExtensionMethodCommand.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT License.
  3. // See the LICENSE file in the project root for more information.
  4. using Corvus.Json;
  5. using RxGauntlet.Build;
  6. using RxGauntlet.CommandLine;
  7. using RxGauntlet.LogModel;
  8. using Spectre.Console.Cli;
  9. using System.Diagnostics;
  10. using System.Text.Json;
  11. namespace CheckDisableTransitiveFailingExtensionMethod;
  12. /// <summary>
  13. /// Implements the only command offered by this program.
  14. /// </summary>
  15. /// <remarks>
  16. /// <para>
  17. /// This builds a list of all the scenarios that need to be tested. Executes each one, and appends
  18. /// the results into the output JSON file.
  19. /// </para>
  20. /// <para>
  21. /// For each scenario, this creates a copy of the <c>CheckDisableTransitiveFailingExtensionMethod</c>
  22. /// project, modifying settings according to the scenario being tested, and then tries to build it.
  23. /// It reports any build failures.
  24. /// </para>
  25. /// </remarks>
  26. internal sealed class CheckDisableTransitiveFailingExtensionMethodCommand : TestCommandBase<TestSettings>
  27. {
  28. protected override string DefaultOutputFilename => "CheckExtensionMethodsWorkaround.json";
  29. protected override async Task<int> ExecuteTestAsync(
  30. TestDetails testDetails, CommandContext context, TestSettings settings, Utf8JsonWriter jsonWriter)
  31. {
  32. // TODO: check that using only the main package is the right thing to do here.
  33. PackageIdAndVersion[] replaceSystemReactiveWith = [settings.RxMainPackageParsed];
  34. var templateProjectFolder =
  35. Path.Combine(AppContext.BaseDirectory, "../../../../ExtensionMethods.DisableTransitiveWorkaroundFail/");
  36. string[] baseNetTfms = ["net8.0"];
  37. string?[] windowsVersions = [null, "windows10.0.19041.0"];
  38. bool?[] boolsWithNull = [null, true, false];
  39. bool[] bools = [true, false];
  40. var scenarios =
  41. from baseNetTfm in baseNetTfms
  42. from windowsVersion in windowsVersions
  43. from useWpf in (windowsVersion is null ? [false] : boolsWithNull)
  44. from useWindowsForms in (windowsVersion is null ? [false] : boolsWithNull)
  45. from useTransitiveFrameworksWorkaround in bools
  46. select new Scenario(baseNetTfm, windowsVersion, useWpf, useWindowsForms, useTransitiveFrameworksWorkaround);
  47. jsonWriter.WriteStartArray();
  48. foreach (var scenario in scenarios)
  49. {
  50. var result = await RunScenario(scenario);
  51. result.WriteTo(jsonWriter);
  52. jsonWriter.Flush();
  53. }
  54. jsonWriter.WriteEndArray();
  55. return 0;
  56. async Task<ExtensionMethodsWorkaroundTestRun> RunScenario(Scenario scenario)
  57. {
  58. Console.WriteLine(scenario);
  59. var tfm = scenario.WindowsVersion is string windowsVersion
  60. ? $"{scenario.BaseNetTfm}-{windowsVersion}"
  61. : scenario.BaseNetTfm;
  62. string rxPackage, rxVersion;
  63. rxPackage = rxVersion = string.Empty;
  64. #pragma warning disable IDE0063 // Use simple 'using' statement
  65. using (var projectClone = ModifiedProjectClone.Create(
  66. templateProjectFolder,
  67. "CheckDisableTransitiveFailingExtensionMethod",
  68. (project) =>
  69. {
  70. project.SetTargetFramework(tfm);
  71. if (replaceSystemReactiveWith is not null)
  72. {
  73. project.ReplacePackageReference("System.Reactive", replaceSystemReactiveWith);
  74. (rxPackage, rxVersion) = (replaceSystemReactiveWith[0].PackageId, replaceSystemReactiveWith[0].Version);
  75. }
  76. project.AddUseUiFrameworksIfRequired(scenario.UseWpf, scenario.UseWindowsForms);
  77. if (scenario.EmitDisableTransitiveFrameworkReferences)
  78. {
  79. project.AddPropertyGroup([new("DisableTransitiveFrameworkReferences", "True")]);
  80. }
  81. },
  82. settings.PackageSource is string packageSource ? [("loc", packageSource)] : null))
  83. {
  84. var buildResult = await projectClone.RunDotnetBuild("ExtensionMethods.DisableTransitiveWorkaroundFail.csproj");
  85. Console.WriteLine($"{scenario}: {buildResult}");
  86. (var includesWpf, var includesWindowsForms) = buildResult.CheckForUiComponentsInOutput();
  87. Debug.Assert(!string.IsNullOrWhiteSpace(rxPackage), "rxPackage should not be null or empty.");
  88. Debug.Assert(!string.IsNullOrWhiteSpace(rxVersion), "rxVersion should not be null or empty.");
  89. var rxVersionPackage = NuGetPackage.Create(
  90. id: rxPackage,
  91. version: rxVersion,
  92. packageSource: settings.PackageSource.AsNullableJsonString());
  93. var config = TestRunConfigWithUiFrameworkSettings.Create(
  94. baseNetTfm: scenario.BaseNetTfm,
  95. emitDisableTransitiveFrameworkReferences: scenario.EmitDisableTransitiveFrameworkReferences,
  96. rxVersion: rxVersionPackage,
  97. useWindowsForms: scenario.UseWindowsForms,
  98. windowsVersion: scenario.WindowsVersion.AsNullableJsonString(),
  99. useWpf: scenario.UseWpf);
  100. if (scenario.WindowsVersion is string wv)
  101. {
  102. config = config.WithWindowsVersion(wv);
  103. }
  104. return ExtensionMethodsWorkaroundTestRun.Create(
  105. config: config,
  106. buildSucceeded: buildResult.BuildSucceeded,
  107. deployedWindowsForms: includesWindowsForms,
  108. deployedWpf: includesWpf,
  109. testRunDateTime: testDetails.TestRunDateTime,
  110. testRunId: testDetails.TestRunId);
  111. }
  112. #pragma warning restore IDE0063 // Use simple 'using' statement
  113. }
  114. }
  115. internal record Scenario(
  116. string BaseNetTfm,
  117. string? WindowsVersion,
  118. bool? UseWpf,
  119. bool? UseWindowsForms,
  120. bool EmitDisableTransitiveFrameworkReferences);
  121. }