From 7d6fe2055fbf6b8243c18880f841af27c22d4d48 Mon Sep 17 00:00:00 2001 From: Elinor Fung Date: Fri, 15 Mar 2024 14:16:06 -0700 Subject: [PATCH 1/2] Delete TestProjectFixture and associated helpers --- .../HostActivation.Tests/NativeUnitTests.cs | 6 +- .../RegisteredInstallLocationOverride.cs | 2 +- .../NETCoreTests.cs | 2 +- .../Assertions/CommandResultAssertions.cs | 14 - .../Assertions/DirectoryInfoExtensions.cs | 5 - .../tests/TestUtils/BuildFailureException.cs | 22 - .../tests/TestUtils/CommandExtensions.cs | 2 +- .../tests/TestUtils/CommandResult.cs | 25 -- src/installer/tests/TestUtils/DotNetCli.cs | 10 +- .../TestUtils/RepoDirectoriesProvider.cs | 26 +- src/installer/tests/TestUtils/TestApp.cs | 11 +- src/installer/tests/TestUtils/TestArtifact.cs | 5 +- src/installer/tests/TestUtils/TestContext.cs | 17 +- src/installer/tests/TestUtils/TestProject.cs | 76 ---- .../tests/TestUtils/TestProjectFixture.cs | 408 ------------------ 15 files changed, 13 insertions(+), 618 deletions(-) delete mode 100644 src/installer/tests/TestUtils/BuildFailureException.cs delete mode 100644 src/installer/tests/TestUtils/TestProject.cs delete mode 100644 src/installer/tests/TestUtils/TestProjectFixture.cs diff --git a/src/installer/tests/HostActivation.Tests/NativeUnitTests.cs b/src/installer/tests/HostActivation.Tests/NativeUnitTests.cs index a98e323d6ef1e..74c3f7a1e4c58 100644 --- a/src/installer/tests/HostActivation.Tests/NativeUnitTests.cs +++ b/src/installer/tests/HostActivation.Tests/NativeUnitTests.cs @@ -8,16 +8,14 @@ using Microsoft.DotNet.CoreSetup.Test; using Microsoft.DotNet.Cli.Build.Framework; -namespace Microsoft.DotNet.CoreSetup.Test.HostActivation.NativeUnitTests +namespace HostActivation.Tests { public class NativeUnitTests { [Fact] public void Native_Test_Fx_Ver() { - RepoDirectoriesProvider repoDirectoriesProvider = new RepoDirectoriesProvider(); - - string testPath = Path.Combine(repoDirectoriesProvider.HostTestArtifacts, Binaries.GetExeFileNameForCurrentPlatform("test_fx_ver")); + string testPath = Path.Combine(RepoDirectoriesProvider.Default.HostTestArtifacts, Binaries.GetExeFileNameForCurrentPlatform("test_fx_ver")); Command testCommand = Command.Create(testPath); testCommand diff --git a/src/installer/tests/HostActivation.Tests/RegisteredInstallLocationOverride.cs b/src/installer/tests/HostActivation.Tests/RegisteredInstallLocationOverride.cs index fcb29470d9eeb..66c0a2c9763c9 100644 --- a/src/installer/tests/HostActivation.Tests/RegisteredInstallLocationOverride.cs +++ b/src/installer/tests/HostActivation.Tests/RegisteredInstallLocationOverride.cs @@ -56,7 +56,7 @@ public RegisteredInstallLocationOverride(string productBinaryPath) // On Linux/macOS the install location is registered in a file which is normally // located in /etc/dotnet/install_location // So we need to redirect it to a different place here. - string directory = Path.Combine(TestArtifact.TestArtifactsPath, "installLocationOverride" + Process.GetCurrentProcess().Id.ToString()); + string directory = Path.Combine(TestContext.TestArtifactsPath, "installLocationOverride" + Process.GetCurrentProcess().Id.ToString()); if (Directory.Exists(directory)) Directory.Delete(directory, true); Directory.CreateDirectory(directory); diff --git a/src/installer/tests/Microsoft.DotNet.CoreSetup.Packaging.Tests/NETCoreTests.cs b/src/installer/tests/Microsoft.DotNet.CoreSetup.Packaging.Tests/NETCoreTests.cs index a50624ecb6e10..bcb276907b33f 100644 --- a/src/installer/tests/Microsoft.DotNet.CoreSetup.Packaging.Tests/NETCoreTests.cs +++ b/src/installer/tests/Microsoft.DotNet.CoreSetup.Packaging.Tests/NETCoreTests.cs @@ -8,7 +8,7 @@ namespace Microsoft.DotNet.CoreSetup.Packaging.Tests { public class NETCoreTests { - private readonly RepoDirectoriesProvider dirs = new RepoDirectoriesProvider(); + private readonly RepoDirectoriesProvider dirs = RepoDirectoriesProvider.Default; [Fact] public void NETCoreTargetingPackIsValid() diff --git a/src/installer/tests/TestUtils/Assertions/CommandResultAssertions.cs b/src/installer/tests/TestUtils/Assertions/CommandResultAssertions.cs index f312913aff8a0..f6958a1a5391b 100644 --- a/src/installer/tests/TestUtils/Assertions/CommandResultAssertions.cs +++ b/src/installer/tests/TestUtils/Assertions/CommandResultAssertions.cs @@ -155,19 +155,5 @@ public string GetDiagnosticsInfo() $"StdOut:{Environment.NewLine}{Result.StdOut}{Environment.NewLine}" + $"StdErr:{Environment.NewLine}{Result.StdErr}{Environment.NewLine}"; } - - public AndConstraint HaveSkippedProjectCompilation(string skippedProject, string frameworkFullName) - { - Result.StdOut.Should().Contain("Project {0} ({1}) was previously compiled. Skipping compilation.", skippedProject, frameworkFullName); - - return new AndConstraint(this); - } - - public AndConstraint HaveCompiledProject(string compiledProject, string frameworkFullName) - { - Result.StdOut.Should().Contain($"Project {0} ({1}) will be compiled", compiledProject, frameworkFullName); - - return new AndConstraint(this); - } } } diff --git a/src/installer/tests/TestUtils/Assertions/DirectoryInfoExtensions.cs b/src/installer/tests/TestUtils/Assertions/DirectoryInfoExtensions.cs index 16471d542dd4d..0f240d6a91bc2 100644 --- a/src/installer/tests/TestUtils/Assertions/DirectoryInfoExtensions.cs +++ b/src/installer/tests/TestUtils/Assertions/DirectoryInfoExtensions.cs @@ -11,10 +11,5 @@ public static DirectoryInfoAssertions Should(this DirectoryInfo dir) { return new DirectoryInfoAssertions(dir); } - - public static DirectoryInfo Sub(this DirectoryInfo dir, string name) - { - return new DirectoryInfo(Path.Combine(dir.FullName, name)); - } } } diff --git a/src/installer/tests/TestUtils/BuildFailureException.cs b/src/installer/tests/TestUtils/BuildFailureException.cs deleted file mode 100644 index f23406cce10a9..0000000000000 --- a/src/installer/tests/TestUtils/BuildFailureException.cs +++ /dev/null @@ -1,22 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using System; - -namespace Microsoft.DotNet.Cli.Build.Framework -{ - public partial class BuildFailureException : Exception - { - public BuildFailureException() - { - } - - public BuildFailureException(string message) : base(message) - { - } - - public BuildFailureException(string message, Exception innerException) : base(message, innerException) - { - } - } -} diff --git a/src/installer/tests/TestUtils/CommandExtensions.cs b/src/installer/tests/TestUtils/CommandExtensions.cs index cfa42d1fab4ac..1adf97e7b7b67 100644 --- a/src/installer/tests/TestUtils/CommandExtensions.cs +++ b/src/installer/tests/TestUtils/CommandExtensions.cs @@ -17,7 +17,7 @@ public static Command EnableHostTracing(this Command command) public static Command EnableHostTracingToFile(this Command command, out string filePath) { - filePath = Path.Combine(TestArtifact.TestArtifactsPath, "trace" + Guid.NewGuid().ToString() + ".log"); + filePath = Path.Combine(TestContext.TestArtifactsPath, "trace" + Guid.NewGuid().ToString() + ".log"); if (File.Exists(filePath)) { File.Delete(filePath); diff --git a/src/installer/tests/TestUtils/CommandResult.cs b/src/installer/tests/TestUtils/CommandResult.cs index de7961be7043f..36af034a3cb51 100644 --- a/src/installer/tests/TestUtils/CommandResult.cs +++ b/src/installer/tests/TestUtils/CommandResult.cs @@ -9,8 +9,6 @@ namespace Microsoft.DotNet.Cli.Build.Framework { public struct CommandResult { - public static readonly CommandResult Empty = new CommandResult(); - public ProcessStartInfo StartInfo { get; } public int ExitCode { get; } public string StdOut { get; } @@ -23,28 +21,5 @@ public CommandResult(ProcessStartInfo startInfo, int exitCode, string stdOut, st StdOut = stdOut; StdErr = stdErr; } - - public void EnsureSuccessful(bool suppressOutput = false) - { - if (ExitCode != 0) - { - StringBuilder message = new StringBuilder($"Command failed with exit code {ExitCode}: {StartInfo.FileName} {StartInfo.Arguments}"); - - if (!suppressOutput) - { - if (!string.IsNullOrEmpty(StdOut)) - { - message.AppendLine($"{Environment.NewLine}Standard Output:{Environment.NewLine}{StdOut}"); - } - - if (!string.IsNullOrEmpty(StdErr)) - { - message.AppendLine($"{Environment.NewLine}Standard Error:{Environment.NewLine}{StdErr}"); - } - } - - throw new BuildFailureException(message.ToString()); - } - } } } diff --git a/src/installer/tests/TestUtils/DotNetCli.cs b/src/installer/tests/TestUtils/DotNetCli.cs index ff90b47e7a8ac..385f596dc3474 100644 --- a/src/installer/tests/TestUtils/DotNetCli.cs +++ b/src/installer/tests/TestUtils/DotNetCli.cs @@ -47,15 +47,7 @@ public Command Exec(string command, params string[] args) return Command.Create(DotnetExecutablePath, newArgs) .EnvironmentVariable("DOTNET_SKIP_FIRST_TIME_EXPERIENCE", "1") - .EnvironmentVariable("DOTNET_MULTILEVEL_LOOKUP", "0"); // Avoid looking at machine state by default + .MultilevelLookup(false); // Avoid looking at machine state by default } - - public Command Restore(params string[] args) => Exec("restore", args); - public Command Build(params string[] args) => Exec("build", args); - public Command Pack(params string[] args) => Exec("pack", args); - public Command Test(params string[] args) => Exec("test", args); - public Command Publish(params string[] args) => Exec("publish", args); - - public Command Store(params string[] args) => Exec("store", args); } } diff --git a/src/installer/tests/TestUtils/RepoDirectoriesProvider.cs b/src/installer/tests/TestUtils/RepoDirectoriesProvider.cs index c0d7fd755b86e..d5db123099efc 100644 --- a/src/installer/tests/TestUtils/RepoDirectoriesProvider.cs +++ b/src/installer/tests/TestUtils/RepoDirectoriesProvider.cs @@ -1,9 +1,5 @@ using System; -using System.Collections; -using System.Collections.Generic; -using System.Collections.Immutable; using System.IO; -using System.Linq; namespace Microsoft.DotNet.CoreSetup.Test { @@ -11,21 +7,12 @@ public sealed class RepoDirectoriesProvider { public static readonly RepoDirectoriesProvider Default = new RepoDirectoriesProvider(); - // Values from test context can be overridden in constructor - public string BuiltDotnet { get; } - // Paths computed by looking for the repo root public string BaseArtifactsFolder { get; } public string HostArtifacts { get; } public string HostTestArtifacts { get; } - // Paths used for building/publishing projects - public string TestAssetsFolder { get; } - public string NugetPackages { get; } - public string DotnetSDK { get; } - - public RepoDirectoriesProvider( - string builtDotnet = null) + private RepoDirectoriesProvider() { string repoRoot = GetRepoRootDirectory(); BaseArtifactsFolder = Path.Combine(repoRoot, "artifacts"); @@ -34,17 +21,6 @@ public RepoDirectoriesProvider( string artifacts = Path.Combine(BaseArtifactsFolder, "bin", osPlatformConfig); HostArtifacts = Path.Combine(artifacts, "corehost"); HostTestArtifacts = Path.Combine(artifacts, "corehost_test"); - - TestAssetsFolder = TestContext.GetTestContextVariable("TEST_ASSETS"); - DotnetSDK = TestContext.GetTestContextVariable("DOTNET_SDK_PATH"); - if (!Directory.Exists(DotnetSDK)) - { - throw new InvalidOperationException("ERROR: Test SDK folder not found."); - } - - NugetPackages = TestContext.GetTestContextVariable("NUGET_PACKAGES"); - - BuiltDotnet = builtDotnet ?? TestContext.BuiltDotNet.BinPath; } private static string GetRepoRootDirectory() diff --git a/src/installer/tests/TestUtils/TestApp.cs b/src/installer/tests/TestUtils/TestApp.cs index ab5df6be6c700..37936d9506a10 100644 --- a/src/installer/tests/TestUtils/TestApp.cs +++ b/src/installer/tests/TestUtils/TestApp.cs @@ -2,14 +2,9 @@ // The .NET Foundation licenses this file to you under the MIT license. using System; -using System.Collections.Generic; using System.Diagnostics; -using System.Globalization; using System.IO; -using System.Linq; -using System.Reflection; -using System.Reflection.Metadata; -using Microsoft.DotNet.Cli.Build; + using Microsoft.NET.HostModel.AppHost; namespace Microsoft.DotNet.CoreSetup.Test @@ -21,9 +16,7 @@ public class TestApp : TestArtifact public string DepsJson { get; private set; } public string RuntimeConfigJson { get; private set; } public string RuntimeDevConfigJson { get; private set; } - public string HostPolicyDll { get; private set; } public string HostFxrDll { get; private set; } - public string CoreClrDll { get; private set; } public string AssemblyName { get; } @@ -194,9 +187,7 @@ private void LoadAssets() DepsJson = Path.Combine(Location, $"{AssemblyName}.deps.json"); RuntimeConfigJson = Path.Combine(Location, $"{AssemblyName}.runtimeconfig.json"); RuntimeDevConfigJson = Path.Combine(Location, $"{AssemblyName}.runtimeconfig.dev.json"); - HostPolicyDll = Path.Combine(Location, Binaries.HostPolicy.FileName); HostFxrDll = Path.Combine(Location, Binaries.HostFxr.FileName); - CoreClrDll = Path.Combine(Location, Binaries.CoreClr.FileName); } } } diff --git a/src/installer/tests/TestUtils/TestArtifact.cs b/src/installer/tests/TestUtils/TestArtifact.cs index 2506bb5fc29ac..f11ee4db747fc 100644 --- a/src/installer/tests/TestUtils/TestArtifact.cs +++ b/src/installer/tests/TestUtils/TestArtifact.cs @@ -14,10 +14,9 @@ namespace Microsoft.DotNet.CoreSetup.Test public class TestArtifact : IDisposable { private static readonly Lazy _preserveTestRuns = new Lazy(() => - TestContext.GetTestContextVariableOrNull("PRESERVE_TEST_RUNS") == "1"); + Environment.GetEnvironmentVariable("PRESERVE_TEST_RUNS") == "1"); public static bool PreserveTestRuns() => _preserveTestRuns.Value; - public static string TestArtifactsPath => TestContext.TestArtifactsPath; public string Location { get; } public string Name { get; } @@ -121,7 +120,7 @@ protected static (string, string) GetNewTestArtifactPath(string artifactName) Exception? lastException = null; for (int i = 0; i < 10; i++) { - var parentPath = Path.Combine(TestArtifactsPath, Path.GetRandomFileName()); + var parentPath = Path.Combine(TestContext.TestArtifactsPath, Path.GetRandomFileName()); // Create a lock file next to the target folder var lockPath = parentPath + ".lock"; var artifactPath = Path.Combine(parentPath, artifactName); diff --git a/src/installer/tests/TestUtils/TestContext.cs b/src/installer/tests/TestUtils/TestContext.cs index e716ba9c31174..3c8ffe53f4711 100644 --- a/src/installer/tests/TestUtils/TestContext.cs +++ b/src/installer/tests/TestUtils/TestContext.cs @@ -50,22 +50,11 @@ static TestContext() } public static string GetTestContextVariable(string name) - { - return GetTestContextVariableOrNull(name) ?? throw new ArgumentException( - $"Unable to find variable '{name}' in test context variable file '{_testContextVariableFilePath}'"); - } - - public static string GetTestContextVariableOrNull(string name) { // Allow env var override, although normally the test context variables file is used. - // Don't accept NUGET_PACKAGES env override specifically: Arcade sets this and it leaks - // in during build.cmd/sh runs, replacing the test-specific dir. - if (!name.Equals("NUGET_PACKAGES", StringComparison.OrdinalIgnoreCase)) + if (Environment.GetEnvironmentVariable(name) is string envValue) { - if (Environment.GetEnvironmentVariable(name) is string envValue) - { - return envValue; - } + return envValue; } if (_testContextVariables.TryGetValue(name, out string value)) @@ -73,7 +62,7 @@ public static string GetTestContextVariableOrNull(string name) return value; } - return null; + throw new ArgumentException($"Unable to find variable '{name}' in test context variable file '{_testContextVariableFilePath}'"); } } } diff --git a/src/installer/tests/TestUtils/TestProject.cs b/src/installer/tests/TestUtils/TestProject.cs deleted file mode 100644 index 967c58e69d975..0000000000000 --- a/src/installer/tests/TestUtils/TestProject.cs +++ /dev/null @@ -1,76 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using System.IO; - -namespace Microsoft.DotNet.CoreSetup.Test -{ - public class TestProject : TestArtifact - { - public string ProjectDirectory { get => Location; } - public string ProjectName { get => Name; } - - public string AssemblyName { get; private set; } - public string OutputDirectory { get; set; } - public string ProjectFile { get; private set; } - public string ProjectAssetsJson { get; private set; } - public string RuntimeConfigJson { get => BuiltApp?.RuntimeConfigJson; } - public string RuntimeDevConfigJson { get => BuiltApp?.RuntimeDevConfigJson; } - public string DepsJson { get => BuiltApp?.DepsJson; } - public string AppDll { get => BuiltApp?.AppDll; } - public string AppExe { get => BuiltApp?.AppExe; } - public string HostPolicyDll { get => BuiltApp?.HostPolicyDll; } - public string HostFxrDll { get => BuiltApp?.HostFxrDll; } - public string CoreClrDll { get => BuiltApp?.CoreClrDll; } - - public TestApp BuiltApp { get; private set; } - - public TestProject( - string projectDirectory, - string outputDirectory = null, - string assemblyName = null) - : base(projectDirectory) - { - Initialize(outputDirectory, assemblyName); - } - - public TestProject(TestProject source) - : base(source) - { - Initialize(null, source.AssemblyName); - } - - public TestProject Copy() - { - return new TestProject(this); - } - - private void Initialize(string outputDirectory, string assemblyName) - { - AssemblyName = assemblyName ?? ProjectName; - ProjectFile = Path.Combine(ProjectDirectory, $"{ProjectName}.csproj"); - ProjectAssetsJson = Path.Combine(ProjectDirectory, "obj", "project.assets.json"); - - OutputDirectory = outputDirectory ?? Path.Combine(ProjectDirectory, "bin"); - if (Directory.Exists(OutputDirectory)) - { - LoadOutputFiles(); - } - } - - public void LoadOutputFiles() - { - BuiltApp = new TestApp(OutputDirectory, AssemblyName); - } - - public bool IsRestored() - { - if (string.IsNullOrEmpty(ProjectAssetsJson)) - { - return false; - } - - return File.Exists(ProjectAssetsJson); - } - } -} diff --git a/src/installer/tests/TestUtils/TestProjectFixture.cs b/src/installer/tests/TestUtils/TestProjectFixture.cs deleted file mode 100644 index 8f40b301a38c0..0000000000000 --- a/src/installer/tests/TestUtils/TestProjectFixture.cs +++ /dev/null @@ -1,408 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using Microsoft.DotNet.Cli.Build; -using System; -using System.Collections.Generic; -using System.IO; -using System.Threading; - -namespace Microsoft.DotNet.CoreSetup.Test -{ - /* - * TestProjectFixture is an abstraction around a TestProject which manages - * setup of the TestProject, copying test projects for perf on build/restore, - * and building/publishing/restoring test projects where necessary. - */ - public class TestProjectFixture : IDisposable - { - private string _assemblyName; - private TestProject _sourceTestProject; - - public DotNetCli SdkDotnet { get; } - public DotNetCli BuiltDotnet { get; } - public TestProject TestProject { get; private set; } - - public string CurrentRid { get; private set; } - public string Framework { get; private set; } - public RepoDirectoriesProvider RepoDirProvider { get; } - - public TestProjectFixture( - string testProjectName, - RepoDirectoriesProvider repoDirectoriesProvider, - string framework = null, - string assemblyName = null) - { - ValidateRequiredDirectories(repoDirectoriesProvider); - - RepoDirProvider = repoDirectoriesProvider; - - Framework = framework ?? TestContext.Tfm; - - SdkDotnet = new DotNetCli(repoDirectoriesProvider.DotnetSDK); - CurrentRid = TestContext.TargetRID; - - BuiltDotnet = new DotNetCli(repoDirectoriesProvider.BuiltDotnet); - - _assemblyName = assemblyName; - - var sourceTestProjectPath = Path.Combine(repoDirectoriesProvider.TestAssetsFolder, "TestProjects", testProjectName); - _sourceTestProject = new TestProject( - sourceTestProjectPath, - assemblyName: _assemblyName); - - TestProject = CopyTestProject(_sourceTestProject); - } - - public TestProjectFixture(TestProjectFixture fixtureToCopy) - { - RepoDirProvider = fixtureToCopy.RepoDirProvider; - SdkDotnet = fixtureToCopy.SdkDotnet; - CurrentRid = fixtureToCopy.CurrentRid; - BuiltDotnet = fixtureToCopy.BuiltDotnet; - _sourceTestProject = fixtureToCopy._sourceTestProject; - Framework = fixtureToCopy.Framework; - _assemblyName = fixtureToCopy._assemblyName; - - TestProject = CopyTestProject(fixtureToCopy.TestProject); - } - - public void Dispose() - { - if (TestProject != null) - { - TestProject.Dispose(); - TestProject = null; - } - } - - private readonly static object s_buildFilesLock = new object(); - - private TestProject CopyTestProject(TestProject sourceTestProject) - { - lock (s_buildFilesLock) - { - // Prevent in-process race condition since the TestArtifactsPath is shared by the current - // assembly - EnsureDirectoryBuildFiles(RepoDirProvider.TestAssetsFolder, TestArtifact.TestArtifactsPath); - } - - return sourceTestProject.Copy(); - - static void EnsureDirectoryBuildFiles(string testAssetsFolder, string testArtifactDirectory) - { - Directory.CreateDirectory(testArtifactDirectory); - - // write an empty Directory.Build.* file to ensure that msbuild doesn't pick up - // the repo's root Directory.Build.*. - EnsureTestProjectsFileContent(testAssetsFolder, testArtifactDirectory, "props"); - EnsureTestProjectsFileContent(testAssetsFolder, testArtifactDirectory, "targets"); - - static void EnsureTestProjectsFileContent(string testAssetsFolder, string dir, string type) - { - var fileName = Path.Combine(dir, $"Directory.Build.{type}"); - if (File.Exists(fileName)) - { - return; - } - - File.WriteAllText( - fileName, - string.Join( - Environment.NewLine, - "", - $" ", - "")); - } - } - } - - private void ValidateRequiredDirectories(RepoDirectoriesProvider repoDirectoriesProvider) - { - if ( ! Directory.Exists(repoDirectoriesProvider.BuiltDotnet)) - { - throw new Exception($"Unable to find built host and sharedfx, please ensure the build has been run: {repoDirectoriesProvider.BuiltDotnet}"); - } - - if ( ! Directory.Exists(repoDirectoriesProvider.HostArtifacts)) - { - throw new Exception($"Unable to find host artifacts directory, please ensure the build has been run: {repoDirectoriesProvider.HostArtifacts}"); - } - } - - public TestProjectFixture BuildProject( - DotNetCli dotnet = null, - string runtime = null, - string framework = null, - string outputDirectory = null, - bool restore = false) - { - dotnet = dotnet ?? SdkDotnet; - outputDirectory = outputDirectory ?? TestProject.OutputDirectory; - TestProject.OutputDirectory = outputDirectory; - framework = framework ?? Framework; - Framework = framework; - - var buildArgs = new List - { - "/bl:BuildProject.binlog" - }; - - if (restore != true) - { - buildArgs.Add("--no-restore"); - } - - if (runtime != null) - { - buildArgs.Add("--runtime"); - buildArgs.Add(runtime); - } - - if (framework != null) - { - buildArgs.Add("--framework"); - buildArgs.Add(framework); - } - - buildArgs.Add($"/p:TestTargetRid={TestContext.TargetRID}"); - buildArgs.Add($"/p:MNAVersion={TestContext.MicrosoftNETCoreAppVersion}"); - - if (outputDirectory != null) - { - buildArgs.Add("-o"); - buildArgs.Add(outputDirectory); - } - - dotnet.Build(buildArgs.ToArray()) - .WorkingDirectory(TestProject.ProjectDirectory) - .Environment("NUGET_PACKAGES", RepoDirProvider.NugetPackages) - .Environment("VERSION", "") // Generate with package version 1.0.0, not %VERSION% - .CaptureStdErr() - .CaptureStdOut() - .Execute() - .EnsureSuccessful(); - - TestProject.LoadOutputFiles(); - - return this; - } - - public TestProjectFixture StoreProject( - DotNetCli dotnet = null, - string runtime = null, - string framework = null, - string manifest = null, - string outputDirectory = null) - { - dotnet = dotnet ?? SdkDotnet; - outputDirectory = outputDirectory ?? TestProject.OutputDirectory; - framework = framework ?? Framework; - Framework = framework; - - var storeArgs = new List - { - "--runtime" - }; - - if (runtime != null) - { - storeArgs.Add(runtime); - } - else - { - storeArgs.Add(CurrentRid); - } - - if (framework != null) - { - storeArgs.Add("--framework"); - storeArgs.Add(framework); - } - - storeArgs.Add("--manifest"); - if (manifest != null) - { - storeArgs.Add(manifest); - } - else - { - storeArgs.Add(_sourceTestProject.ProjectFile); - } - - if (outputDirectory != null) - { - storeArgs.Add("-o"); - storeArgs.Add(outputDirectory); - } - - storeArgs.Add($"/p:MNAVersion={TestContext.MicrosoftNETCoreAppVersion}"); - storeArgs.Add($"/p:NetCoreAppCurrent={Framework}"); - - // Ensure the project's OutputType isn't 'Exe', since that causes issues with 'dotnet store' - storeArgs.Add("/p:OutputType=Library"); - - dotnet.Store(storeArgs.ToArray()) - .WorkingDirectory(TestProject.ProjectDirectory) - .Environment("NUGET_PACKAGES", RepoDirProvider.NugetPackages) - .CaptureStdErr() - .CaptureStdOut() - .Execute() - .EnsureSuccessful(); - - TestProject.LoadOutputFiles(); - - return this; - } - - public TestProjectFixture PublishProject( - DotNetCli dotnet = null, - string runtime = null, - string framework = null, - bool? selfContained = null, - string outputDirectory = null, - bool singleFile = false, - bool restore = false, - params string[] extraArgs) - { - dotnet = dotnet ?? SdkDotnet; - outputDirectory = outputDirectory ?? TestProject.OutputDirectory; - TestProject.OutputDirectory = outputDirectory; - framework = framework ?? Framework; - Framework = framework; - - var publishArgs = new List - { - "/bl:PublishProject.binlog" - }; - - if (restore != true) - { - publishArgs.Add("--no-restore"); - } - - if (runtime != null) - { - publishArgs.Add("--runtime"); - publishArgs.Add(runtime); - - if (selfContained == null) - { - // This is to prevent bugs caused by SDK defaulting self-contained differently for various configurations. - // We still want to allow selfContained to remain unspecified for simple cases, for example for building libraries. - throw new ArgumentException("If runtime is specified, then the caller also has to specify selfContained value."); - } - } - - if (framework != null) - { - publishArgs.Add("--framework"); - publishArgs.Add(framework); - publishArgs.Add($"/p:NetCoreAppCurrent={framework}"); - } - - if (selfContained != null) - { - publishArgs.Add("--self-contained"); - publishArgs.Add(selfContained.Value ? "true" : "false"); - - // Workaround for https://github.com/dotnet/sdk/issues/25062 - // If self-contained is specified via the command line, also specify the - // runtime identifier (if we didn't already). Otherwise, the SDK ends up - // passing the runtime identifier of the SDK such that the one specified - // in the project file is ignored. - if (selfContained.Value && runtime == null) - { - publishArgs.Add("--runtime"); - publishArgs.Add(TestContext.TargetRID); - } - } - - if (outputDirectory != null) - { - publishArgs.Add("-o"); - publishArgs.Add(outputDirectory); - } - - if (singleFile) - { - publishArgs.Add("/p:PublishSingleFile=true"); - } - - publishArgs.Add($"/p:TestTargetRid={TestContext.TargetRID}"); - publishArgs.Add($"/p:MNAVersion={TestContext.MicrosoftNETCoreAppVersion}"); - - foreach (var arg in extraArgs) - { - publishArgs.Add(arg); - } - - dotnet.Publish(publishArgs.ToArray()) - .WorkingDirectory(TestProject.ProjectDirectory) - .Environment("NUGET_PACKAGES", RepoDirProvider.NugetPackages) - .CaptureStdErr() - .CaptureStdOut() - .Execute() - .EnsureSuccessful(); - - TestProject.LoadOutputFiles(); - - return this; - } - - public TestProjectFixture RestoreProject(string[] fallbackSources, string extraMSBuildProperties = null) - { - var restoreArgs = new List(); - foreach (var fallbackSource in fallbackSources) - { - restoreArgs.Add("--source"); - restoreArgs.Add(fallbackSource); - } - restoreArgs.Add("--disable-parallel"); - - restoreArgs.Add($"/p:MNAVersion={TestContext.MicrosoftNETCoreAppVersion}"); - restoreArgs.Add($"/p:NetCoreAppCurrent={Framework}"); - - if (extraMSBuildProperties != null) - { - restoreArgs.Add(extraMSBuildProperties); - } - - SdkDotnet.Restore(restoreArgs.ToArray()) - .WorkingDirectory(TestProject.ProjectDirectory) - .CaptureStdErr() - .CaptureStdOut() - .Environment("NUGET_PACKAGES", RepoDirProvider.NugetPackages) - .Execute() - .EnsureSuccessful(); - - return this; - } - - public TestProjectFixture EnsureRestored(params string[] fallbackSources) - { - if (!TestProject.IsRestored()) - { - RestoreProject(fallbackSources); - } - - return this; - } - - public TestProjectFixture EnsureRestoredForRid(string rid, params string[] fallbackSources) - { - if (!TestProject.IsRestored()) - { - string extraMSBuildProperties = $"/p:TestTargetRid={rid}"; - RestoreProject(fallbackSources, extraMSBuildProperties); - } - - return this; - } - - public TestProjectFixture Copy() - { - return new TestProjectFixture(this); - } - } -} From 52ee239e258627c11f00672bff0f185a0bac204c Mon Sep 17 00:00:00 2001 From: Elinor Fung Date: Fri, 15 Mar 2024 16:07:44 -0700 Subject: [PATCH 2/2] Remove complicated console logging --- .../tests/TestUtils/AnsiColorExtensions.cs | 52 ------- src/installer/tests/TestUtils/AnsiConsole.cs | 145 ------------------ .../tests/TestUtils/BuildReporter.cs | 38 ----- src/installer/tests/TestUtils/Command.cs | 35 ++--- src/installer/tests/TestUtils/Reporter.cs | 55 ------- 5 files changed, 16 insertions(+), 309 deletions(-) delete mode 100644 src/installer/tests/TestUtils/AnsiColorExtensions.cs delete mode 100644 src/installer/tests/TestUtils/AnsiConsole.cs delete mode 100644 src/installer/tests/TestUtils/BuildReporter.cs delete mode 100644 src/installer/tests/TestUtils/Reporter.cs diff --git a/src/installer/tests/TestUtils/AnsiColorExtensions.cs b/src/installer/tests/TestUtils/AnsiColorExtensions.cs deleted file mode 100644 index 97c104516c67c..0000000000000 --- a/src/installer/tests/TestUtils/AnsiColorExtensions.cs +++ /dev/null @@ -1,52 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -namespace Microsoft.DotNet.Cli.Build.Framework -{ - public static class AnsiColorExtensions - { - public static string Black(this string text) - { - return "\x1B[30m" + text + "\x1B[39m"; - } - - public static string Red(this string text) - { - return "\x1B[31m" + text + "\x1B[39m"; - } - public static string Green(this string text) - { - return "\x1B[32m" + text + "\x1B[39m"; - } - - public static string Yellow(this string text) - { - return "\x1B[33m" + text + "\x1B[39m"; - } - - public static string Blue(this string text) - { - return "\x1B[34m" + text + "\x1B[39m"; - } - - public static string Magenta(this string text) - { - return "\x1B[35m" + text + "\x1B[39m"; - } - - public static string Cyan(this string text) - { - return "\x1B[36m" + text + "\x1B[39m"; - } - - public static string White(this string text) - { - return "\x1B[37m" + text + "\x1B[39m"; - } - - public static string Bold(this string text) - { - return "\x1B[1m" + text + "\x1B[22m"; - } - } -} diff --git a/src/installer/tests/TestUtils/AnsiConsole.cs b/src/installer/tests/TestUtils/AnsiConsole.cs deleted file mode 100644 index 2a8d6f3ad5a72..0000000000000 --- a/src/installer/tests/TestUtils/AnsiConsole.cs +++ /dev/null @@ -1,145 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using System; -using System.IO; - -namespace Microsoft.DotNet.Cli.Build.Framework -{ - public class AnsiConsole - { - private AnsiConsole(TextWriter writer) - { - Writer = writer; - - OriginalForegroundColor = Console.ForegroundColor; - } - - private int _boldRecursion; - - public static AnsiConsole GetOutput() - { - return new AnsiConsole(Console.Out); - } - - public static AnsiConsole GetError() - { - return new AnsiConsole(Console.Error); - } - - public TextWriter Writer { get; } - - public ConsoleColor OriginalForegroundColor { get; } - - private void SetColor(ConsoleColor color) - { - const int Light = 0x08; - int c = (int)color; - - Console.ForegroundColor = - c < 0 ? color : // unknown, just use it - _boldRecursion > 0 ? (ConsoleColor)(c | Light) : // ensure color is light - (ConsoleColor)(c & ~Light); // ensure color is dark - } - - private void SetBold(bool bold) - { - _boldRecursion += bold ? 1 : -1; - if (_boldRecursion > 1 || (_boldRecursion == 1 && !bold)) - { - return; - } - - // switches on _boldRecursion to handle boldness - SetColor(Console.ForegroundColor); - } - - public void WriteLine(string message) - { - Write(message); - Writer.WriteLine(); - } - - - public void Write(string message) - { - var escapeScan = 0; - for (;;) - { - var escapeIndex = message.IndexOf("\x1b[", escapeScan, StringComparison.Ordinal); - if (escapeIndex == -1) - { - var text = message.Substring(escapeScan); - Writer.Write(text); - break; - } - else - { - var startIndex = escapeIndex + 2; - var endIndex = startIndex; - while (endIndex != message.Length && - message[endIndex] >= 0x20 && - message[endIndex] <= 0x3f) - { - endIndex += 1; - } - - var text = message.Substring(escapeScan, escapeIndex - escapeScan); - Writer.Write(text); - if (endIndex == message.Length) - { - break; - } - - switch (message[endIndex]) - { - case 'm': - int value; - if (int.TryParse(message.Substring(startIndex, endIndex - startIndex), out value)) - { - switch (value) - { - case 1: - SetBold(true); - break; - case 22: - SetBold(false); - break; - case 30: - SetColor(ConsoleColor.Black); - break; - case 31: - SetColor(ConsoleColor.Red); - break; - case 32: - SetColor(ConsoleColor.Green); - break; - case 33: - SetColor(ConsoleColor.Yellow); - break; - case 34: - SetColor(ConsoleColor.Blue); - break; - case 35: - SetColor(ConsoleColor.Magenta); - break; - case 36: - SetColor(ConsoleColor.Cyan); - break; - case 37: - SetColor(ConsoleColor.Gray); - break; - case 39: - Console.ForegroundColor = OriginalForegroundColor; - break; - } - } - break; - } - - escapeScan = endIndex + 1; - } - } - } - } -} diff --git a/src/installer/tests/TestUtils/BuildReporter.cs b/src/installer/tests/TestUtils/BuildReporter.cs deleted file mode 100644 index e87987074ec96..0000000000000 --- a/src/installer/tests/TestUtils/BuildReporter.cs +++ /dev/null @@ -1,38 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using System; - -namespace Microsoft.DotNet.Cli.Build.Framework -{ - public static class BuildReporter - { - private const string TimeSpanFormat = @"hh\:mm\:ss\.fff"; - private static DateTime _initialTime = DateTime.Now; - - public static void BeginSection(string type, string name) - { - Reporter.Output.WriteLine($"[{type.PadRight(10)} >]".Green() + $" [....] [{(DateTime.Now - _initialTime).ToString(TimeSpanFormat)}]".Blue() + $" {name}"); - } - - public static void SectionComment(string type, string comment) - { - Reporter.Output.WriteLine($"[{type.PadRight(10)} -]".Green() + $" [....] [{(DateTime.Now - _initialTime).ToString(TimeSpanFormat)}]".Blue() + $" {comment}"); - } - - public static void EndSection(string type, string name, bool success) - { - var header = $"[{type.PadRight(10)} <]"; - if (success) - { - header = header.Green(); - } - else - { - header = header.Red(); - } - var successString = success ? " OK " : "FAIL"; - Reporter.Output.WriteLine(header + $" [{successString}] [{(DateTime.Now - _initialTime).ToString(TimeSpanFormat)}]".Blue() + $" {name}"); - } - } -} diff --git a/src/installer/tests/TestUtils/Command.cs b/src/installer/tests/TestUtils/Command.cs index 2624e60e0487a..1f403c4c4000c 100644 --- a/src/installer/tests/TestUtils/Command.cs +++ b/src/installer/tests/TestUtils/Command.cs @@ -6,9 +6,7 @@ using System.ComponentModel; using System.Diagnostics; using System.IO; -using System.Linq; using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; using System.Threading; namespace Microsoft.DotNet.Cli.Build.Framework @@ -315,33 +313,32 @@ private string FormatProcessInfo(ProcessStartInfo info, bool includeWorkingDirec return prefix + " " + info.Arguments; } + private static DateTime _initialTime = DateTime.Now; + + private string GetFormattedTime() + { + const string TimeSpanFormat = @"hh\:mm\:ss\.fff"; + return (DateTime.Now - _initialTime).ToString(TimeSpanFormat); + } + private void ReportExecBegin() { - BuildReporter.BeginSection("EXEC", FormatProcessInfo(Process.StartInfo, includeWorkingDirectory: false)); + string message = FormatProcessInfo(Process.StartInfo, includeWorkingDirectory: false); + Console.WriteLine($"[EXEC >] [....] [{GetFormattedTime()}] {message}"); } private void ReportExecWaitOnExit() { - BuildReporter.SectionComment("EXEC", $"Waiting for process {Process.Id} to exit..."); + string message = $"Waiting for process {Process.Id} to exit..."; + Console.WriteLine($"[EXEC -] [....] [{GetFormattedTime()}] {message}"); } private void ReportExecEnd(int exitCode, bool fExpectedToFail) { - bool success = exitCode == 0; - string msgExpectedToFail = ""; - - if (fExpectedToFail) - { - success = !success; - msgExpectedToFail = "failed as expected and "; - } - - var message = $"{FormatProcessInfo(Process.StartInfo, includeWorkingDirectory: !success)} {msgExpectedToFail}exited with {exitCode}"; - - BuildReporter.EndSection( - "EXEC", - success ? message.Green() : message.Red().Bold(), - success); + bool success = fExpectedToFail ? exitCode != 0 : exitCode == 0; + var status = success ? " OK " : "FAIL"; + var message = $"{FormatProcessInfo(Process.StartInfo, includeWorkingDirectory: !success)} exited with {exitCode}. Expected: {(fExpectedToFail ? "non-zero" : "0")}"; + Console.WriteLine($"[EXEC <] [{status}] [{GetFormattedTime()}] {message}"); } private void ThrowIfRunning([CallerMemberName] string memberName = null) diff --git a/src/installer/tests/TestUtils/Reporter.cs b/src/installer/tests/TestUtils/Reporter.cs deleted file mode 100644 index c32353027b6ae..0000000000000 --- a/src/installer/tests/TestUtils/Reporter.cs +++ /dev/null @@ -1,55 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -namespace Microsoft.DotNet.Cli.Build.Framework -{ - // Stupid-simple console manager - internal class Reporter - { - private static readonly Reporter Null = new Reporter(console: null); - private static object _lock = new object(); - - private readonly AnsiConsole _console; - - private Reporter(AnsiConsole console) - { - _console = console; - } - - public static Reporter Output { get; } = new Reporter(AnsiConsole.GetOutput()); - public static Reporter Error { get; } = new Reporter(AnsiConsole.GetOutput()); - public static Reporter Verbose { get; } = new Reporter(AnsiConsole.GetOutput()); - - public void WriteLine(string message) - { - lock (_lock) - { - _console?.WriteLine(message); - } - } - - public void WriteLine() - { - lock (_lock) - { - _console?.Writer?.WriteLine(); - } - } - - public void Write(string message) - { - lock (_lock) - { - _console?.Writer?.Write(message); - } - } - - public void WriteBanner(string content) - { - string border = new string('*', content.Length + 6); - WriteLine($@"{border} -* {content} * -{border}".Green()); - } - } -}