From cf2769936a83f8b4c5c89951ada4d10b95e6e328 Mon Sep 17 00:00:00 2001 From: Davis Goodin Date: Fri, 12 Jul 2019 15:55:21 -0500 Subject: [PATCH] Use file, not env, for test context variables This is the recommended Arcade way to transfer this information. The Arcade SDK doens't support passing env vars while running tests. Commit migrated from https://github.com/dotnet/core-setup/commit/fb43a6a3abab1b25356b7ee146586d14bb20b195 --- src/installer/test/Directory.Build.targets | 183 +++++---------------- .../test/HostActivation.Tests/DotnetTestXunit.cs | 2 +- .../HostActivation.Tests/LightupAppActivation.cs | 2 +- .../MultilevelSharedFxLookup.cs | 2 +- .../test/HostActivation.Tests/SharedFxLookup.cs | 2 +- src/installer/test/TestUtils/Command.cs | 2 +- .../test/TestUtils/RepoDirectoriesProvider.cs | 74 +++++++-- src/installer/test/TestUtils/TestArtifact.cs | 22 +-- src/installer/test/TestUtils/TestProjectFixture.cs | 4 +- 9 files changed, 121 insertions(+), 172 deletions(-) diff --git a/src/installer/test/Directory.Build.targets b/src/installer/test/Directory.Build.targets index e3ca9ae..a87be0c 100644 --- a/src/installer/test/Directory.Build.targets +++ b/src/installer/test/Directory.Build.targets @@ -21,169 +21,74 @@ SourceFiles="@(SharedFrameworkPublishFiles)" DestinationFiles="@(SharedFrameworkPublishFiles->'$(TestsOutputDir)sharedFrameworkPublish/%(RecursiveDir)%(Filename)%(Extension)')" /> - + + - - - - - - - $(_HostRid) - $(MSBuildProjectName) - - $(ArtifactsDir)tests/$(ConfigurationGroup)/ - $(TestsOutputRootDir)$(TestsOutputName)/ - - - - + BeforeTargets="Build"> - + $([System.IO.Path]::DirectorySeparatorChar) $([System.String]::Copy('$(TestsOutputDir)').Replace('/', '$(DirectorySeparatorChar)')) $([System.String]::Copy('$(SystemPathTestsOutputDir)').Replace('\', '$(DirectorySeparatorChar)')) - - - - - - - - - - - - - - - - - - - - - - - - - <_TestEnvironment>%(TestToRun.EnvironmentDisplay) - <_TestAssembly>%(TestToRun.Identity) - <_TestRuntime>%(TestToRun.TestRuntime) - <_TestRunnerAdditionalArguments>%(TestToRun.TestRunnerAdditionalArguments) - - - <_TestRunnerTargetFramework>net472 - <_TestRunnerTargetFramework Condition="'$(_TestRuntime)' == 'Core'">netcoreapp2.0 - <_TestRunnerTargetFramework Condition="%(TestToRun.TargetFramework) == 'netcoreapp1.1' or %(TestToRun.TargetFramework) == 'netcoreapp1.0'">netcoreapp1.0 - - - - <_TargetFileNameNoExt>$([System.IO.Path]::GetFileNameWithoutExtension('$(_TestAssembly)')) - <_TargetDir>$([System.IO.Path]::GetDirectoryName('$(_TestAssembly)'))\ - <_CoreRuntimeConfigPath>$(_TargetDir)$(_TargetFileNameNoExt).runtimeconfig.json - <_CoreDepsPath>$(_TargetDir)$(_TargetFileNameNoExt).deps.json - - <_TestRunner Condition="'%(TestToRun.Architecture)'=='x86' And Exists('$(DotNetRoot)x86\dotnet.exe')">$(DotNetRoot)x86\dotnet.exe - <_TestRunner Condition="'$(_TestRunner)'==''">$(DotNetTool) - - <_TestRunnerArgs>exec --depsfile "$(_CoreDepsPath)" --runtimeconfig "$(_CoreRuntimeConfigPath)" $(TestRuntimeAdditionalArguments) "$(NuGetPackageRoot)xunit.runner.console/$(XUnitVersion)/tools/$(_TestRunnerTargetFramework)/xunit.console.dll" "$(_TestAssembly)" -noautoreporters -xml "%(TestToRun.ResultsXmlPath)" -html "%(TestToRun.ResultsHtmlPath)" $(_TestRunnerAdditionalArguments) - - - - <_XUnitConsoleExe>xunit.console.exe - <_XUnitConsoleExe Condition="'%(TestToRun.Architecture)' == 'x86'">xunit.console.x86.exe - <_XUnitConsoleExePath>$(NuGetPackageRoot)xunit.runner.console\$(XUnitVersion)\tools\$(_TestRunnerTargetFramework)\$(_XUnitConsoleExe) - - <_TestRunnerArgs>"$(_TestAssembly)" -noshadow -xml "%(TestToRun.ResultsXmlPath)" -html "%(TestToRun.ResultsHtmlPath)" $(_TestRunnerAdditionalArguments) - <_TestRunnerArgs Condition="'$(_TestRuntime)' == 'Mono'">$(TestRuntimeAdditionalArguments) "$(_XUnitConsoleExePath)" $(_TestRunnerArgs) - <_TestRunner Condition="'$(_TestRuntime)' == 'Mono'">$(MonoTool) - <_TestRunner Condition="'$(_TestRuntime)' != 'Mono'">$(_XUnitConsoleExePath) - - - - <_TestRunnerCommand>"$(_TestRunner)" $(_TestRunnerArgs) - - - <_TestRunnerCommand Condition="'$(TestCaptureOutput)' != 'false'">$(_TestRunnerCommand) > "%(TestToRun.ResultsStdOutPath)" 2>&1 + + $(NetCoreRoot) + - <_OutputFiles Include="%(TestToRun.ResultsXmlPath)" /> - <_OutputFiles Include="%(TestToRun.ResultsHtmlPath)" /> - <_OutputFiles Include="%(TestToRun.ResultsStdOutPath)" /> + + + + + + + + + - - - - - - - - - - - - + + - - + + + + - <_ResultsFileToDisplay>%(TestToRun.ResultsHtmlPath) - <_ResultsFileToDisplay Condition="!Exists('$(_ResultsFileToDisplay)')">%(TestToRun.ResultsStdOutPath) - - - - + $(_HostRid) + $(MSBuildProjectName) - - - + $(ArtifactsDir)tests/$(ConfigurationGroup)/ + $(TestsOutputRootDir)$(TestsOutputName)/ + - + diff --git a/src/installer/test/HostActivation.Tests/DotnetTestXunit.cs b/src/installer/test/HostActivation.Tests/DotnetTestXunit.cs index a580023..8adaf88 100644 --- a/src/installer/test/HostActivation.Tests/DotnetTestXunit.cs +++ b/src/installer/test/HostActivation.Tests/DotnetTestXunit.cs @@ -85,7 +85,7 @@ namespace Microsoft.DotNet.Tools.Publish.Tests if ( ! File.Exists(dotnetTestXunitDll)) { throw new Exception( - $"Unable to find dotnet-test-xunit.dll, ensure {nameof(DotnetTestXunitVersion)} is updated to the version in Portable/StandaloneTestApp"); + $"Unable to find '{dotnetTestXunitDll}', ensure {nameof(DotnetTestXunitVersion)} is updated to the version in Portable/StandaloneTestApp"); } return dotnetTestXunitDll; diff --git a/src/installer/test/HostActivation.Tests/LightupAppActivation.cs b/src/installer/test/HostActivation.Tests/LightupAppActivation.cs index 2cd68bd..e3dd30e 100644 --- a/src/installer/test/HostActivation.Tests/LightupAppActivation.cs +++ b/src/installer/test/HostActivation.Tests/LightupAppActivation.cs @@ -30,7 +30,7 @@ namespace Microsoft.DotNet.CoreSetup.Test.HostActivation // From the artifacts dir, it's possible to find where the sharedFrameworkPublish folder is. We need // to locate it because we'll copy its contents into other folders - string artifactsDir = Environment.GetEnvironmentVariable("TEST_ARTIFACTS"); + string artifactsDir = new RepoDirectoriesProvider().GetTestContextVariable("TEST_ARTIFACTS"); string builtDotnet = Path.Combine(artifactsDir, "sharedFrameworkPublish"); // The dotnetLightupSharedFxLookup dir will contain some folders and files that will be necessary to perform the tests diff --git a/src/installer/test/HostActivation.Tests/MultilevelSharedFxLookup.cs b/src/installer/test/HostActivation.Tests/MultilevelSharedFxLookup.cs index 2956d90..2b05a3e 100644 --- a/src/installer/test/HostActivation.Tests/MultilevelSharedFxLookup.cs +++ b/src/installer/test/HostActivation.Tests/MultilevelSharedFxLookup.cs @@ -48,7 +48,7 @@ namespace Microsoft.DotNet.CoreSetup.Test.HostActivation { // From the artifacts dir, it's possible to find where the sharedFrameworkPublish folder is. We need // to locate it because we'll copy its contents into other folders - string artifactsDir = Environment.GetEnvironmentVariable("TEST_ARTIFACTS"); + string artifactsDir = new RepoDirectoriesProvider().GetTestContextVariable("TEST_ARTIFACTS"); _builtDotnet = Path.Combine(artifactsDir, "sharedFrameworkPublish"); // The dotnetMultilevelSharedFxLookup dir will contain some folders and files that will be diff --git a/src/installer/test/HostActivation.Tests/SharedFxLookup.cs b/src/installer/test/HostActivation.Tests/SharedFxLookup.cs index 63390d4..e7cd16d 100644 --- a/src/installer/test/HostActivation.Tests/SharedFxLookup.cs +++ b/src/installer/test/HostActivation.Tests/SharedFxLookup.cs @@ -34,7 +34,7 @@ namespace Microsoft.DotNet.CoreSetup.Test.HostActivation { // From the artifacts dir, it's possible to find where the sharedFrameworkPublish folder is. We need // to locate it because we'll copy its contents into other folders - string artifactsDir = Environment.GetEnvironmentVariable("TEST_ARTIFACTS"); + string artifactsDir = new RepoDirectoriesProvider().GetTestContextVariable("TEST_ARTIFACTS"); _builtDotnet = Path.Combine(artifactsDir, "sharedFrameworkPublish"); // The dotnetSharedFxLookup dir will contain some folders and files that will be diff --git a/src/installer/test/TestUtils/Command.cs b/src/installer/test/TestUtils/Command.cs index f75c683..cb178a6 100644 --- a/src/installer/test/TestUtils/Command.cs +++ b/src/installer/test/TestUtils/Command.cs @@ -251,7 +251,7 @@ namespace Microsoft.DotNet.Cli.Build.Framework public Command WithUserProfile(string userprofile) { string userDir; - if (RuntimeEnvironment.OperatingSystemPlatform == Platform.Windows) + if (InternalAbstractions.RuntimeEnvironment.OperatingSystemPlatform == Platform.Windows) { userDir = "USERPROFILE"; } diff --git a/src/installer/test/TestUtils/RepoDirectoriesProvider.cs b/src/installer/test/TestUtils/RepoDirectoriesProvider.cs index ed6252b..f30695f 100644 --- a/src/installer/test/TestUtils/RepoDirectoriesProvider.cs +++ b/src/installer/test/TestUtils/RepoDirectoriesProvider.cs @@ -1,6 +1,9 @@ using System; using System.Collections; +using System.Collections.Generic; +using System.Collections.Immutable; using System.IO; +using System.Linq; namespace Microsoft.DotNet.CoreSetup.Test { @@ -10,7 +13,11 @@ namespace Microsoft.DotNet.CoreSetup.Test public string BuildArchitecture { get; } public string TargetRID { get; } public string MicrosoftNETCoreAppVersion { get; } + public string Configuration { get; } public string RepoRoot { get; } + public string BaseArtifactsFolder { get; } + public string BaseBinFolder { get; } + public string BaseObjFolder { get; } public string Artifacts { get; } public string HostArtifacts { get; } public string BuiltDotnet { get; } @@ -18,6 +25,9 @@ namespace Microsoft.DotNet.CoreSetup.Test public string CorehostPackages { get; } public string DotnetSDK { get; } + private string _testContextVariableFilePath { get; } + private ImmutableDictionary _testContextVariables { get; } + public RepoDirectoriesProvider( string repoRoot = null, string artifacts = null, @@ -29,34 +39,72 @@ namespace Microsoft.DotNet.CoreSetup.Test { RepoRoot = repoRoot ?? GetRepoRootDirectory(); - string baseArtifactsFolder = artifacts ?? Path.Combine(RepoRoot, "artifacts"); - string baseBinFolder = artifacts ?? Path.Combine(baseArtifactsFolder, "bin"); - string baseObjFolder = artifacts ?? Path.Combine(baseArtifactsFolder, "obj"); + _testContextVariableFilePath = Path.Combine( + Directory.GetCurrentDirectory(), + "TestContextVariables.txt"); + + _testContextVariables = File.ReadAllLines(_testContextVariableFilePath) + .ToImmutableDictionary( + line => line.Substring(0, line.IndexOf('=')), + line => line.Substring(line.IndexOf('=') + 1), + StringComparer.OrdinalIgnoreCase); + + BaseArtifactsFolder = artifacts ?? Path.Combine(RepoRoot, "artifacts"); + BaseBinFolder = artifacts ?? Path.Combine(BaseArtifactsFolder, "bin"); + BaseObjFolder = artifacts ?? Path.Combine(BaseArtifactsFolder, "obj"); - TargetRID = Environment.GetEnvironmentVariable("TEST_TARGETRID"); - BuildRID = Environment.GetEnvironmentVariable("BUILDRID"); - BuildArchitecture = Environment.GetEnvironmentVariable("BUILD_ARCHITECTURE"); - MicrosoftNETCoreAppVersion = microsoftNETCoreAppVersion ?? Environment.GetEnvironmentVariable("MNA_VERSION"); + TargetRID = GetTestContextVariable("TEST_TARGETRID"); + BuildRID = GetTestContextVariable("BUILDRID"); + BuildArchitecture = GetTestContextVariable("BUILD_ARCHITECTURE"); + MicrosoftNETCoreAppVersion = microsoftNETCoreAppVersion ?? GetTestContextVariable("MNA_VERSION"); - string configuration = Environment.GetEnvironmentVariable("BUILD_CONFIGURATION"); - string osPlatformConfig = $"{BuildRID}.{configuration}"; + Configuration = GetTestContextVariable("BUILD_CONFIGURATION"); + string osPlatformConfig = $"{BuildRID}.{Configuration}"; - DotnetSDK = dotnetSdk ?? Environment.GetEnvironmentVariable("DOTNET_SDK_PATH"); + DotnetSDK = dotnetSdk ?? GetTestContextVariable("DOTNET_SDK_PATH"); if (!Directory.Exists(DotnetSDK)) { throw new InvalidOperationException("ERROR: Test SDK folder not found."); } - Artifacts = Path.Combine(baseBinFolder, osPlatformConfig); + Artifacts = Path.Combine(BaseBinFolder, osPlatformConfig); HostArtifacts = artifacts ?? Path.Combine(Artifacts, "corehost"); NugetPackages = nugetPackages ?? - Environment.GetEnvironmentVariable("NUGET_PACKAGES") ?? + GetTestContextVariable("NUGET_PACKAGES") ?? Path.Combine(RepoRoot, ".packages"); CorehostPackages = corehostPackages ?? Path.Combine(Artifacts, "corehost"); - BuiltDotnet = builtDotnet ?? Path.Combine(baseObjFolder, osPlatformConfig, "sharedFrameworkPublish"); + BuiltDotnet = builtDotnet ?? Path.Combine(BaseObjFolder, osPlatformConfig, "sharedFrameworkPublish"); + } + + public string GetTestContextVariable(string name) + { + return GetTestContextVariableOrNull(name) ?? throw new ArgumentException( + $"Unable to find variable '{name}' in " + + $"test context variable file '{_testContextVariableFilePath}'"); + } + + public 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) + { + return envValue; + } + } + + if (_testContextVariables.TryGetValue(name, out string value)) + { + return value; + } + + return null; } private static string GetRepoRootDirectory() diff --git a/src/installer/test/TestUtils/TestArtifact.cs b/src/installer/test/TestUtils/TestArtifact.cs index e49b520..d989a37 100644 --- a/src/installer/test/TestUtils/TestArtifact.cs +++ b/src/installer/test/TestUtils/TestArtifact.cs @@ -10,20 +10,21 @@ namespace Microsoft.DotNet.CoreSetup.Test { public class TestArtifact : IDisposable { + private static readonly Lazy _repoDirectoriesProvider = + new Lazy(() => new RepoDirectoriesProvider()); + + private static readonly Lazy _preserveTestRuns = new Lazy(() => + _repoDirectoriesProvider.Value.GetTestContextVariableOrNull("PRESERVE_TEST_RUNS") == "1"); + private static readonly string TestArtifactDirectoryEnvironmentVariable = "TEST_ARTIFACTS"; private static readonly Lazy _testArtifactsPath = new Lazy(() => { - return Environment.GetEnvironmentVariable(TestArtifactDirectoryEnvironmentVariable) + return _repoDirectoriesProvider.Value.GetTestContextVariable(TestArtifactDirectoryEnvironmentVariable) ?? Path.Combine(AppContext.BaseDirectory, TestArtifactDirectoryEnvironmentVariable); }); - public static string TestArtifactsPath - { - get - { - return _testArtifactsPath.Value; - } - } + public static bool PreserveTestRuns() => _preserveTestRuns.Value; + public static string TestArtifactsPath => _testArtifactsPath.Value; public string Location { get; } public string Name { get; } @@ -66,11 +67,6 @@ namespace Microsoft.DotNet.CoreSetup.Test _copies.Clear(); } - public static bool PreserveTestRuns() - { - return Environment.GetEnvironmentVariable("PRESERVE_TEST_RUNS") == "1"; - } - protected static string GetNewTestArtifactPath(string artifactName) { int projectCount = 0; diff --git a/src/installer/test/TestUtils/TestProjectFixture.cs b/src/installer/test/TestUtils/TestProjectFixture.cs index 6d4bfa3..e69ef93 100644 --- a/src/installer/test/TestUtils/TestProjectFixture.cs +++ b/src/installer/test/TestUtils/TestProjectFixture.cs @@ -36,10 +36,10 @@ namespace Microsoft.DotNet.CoreSetup.Test { ValidateRequiredDirectories(repoDirectoriesProvider); - Framework = framework ?? Environment.GetEnvironmentVariable("MNA_TFM"); - RepoDirProvider = repoDirectoriesProvider; + Framework = framework ?? RepoDirProvider.GetTestContextVariable("MNA_TFM"); + SdkDotnet = new DotNetCli(repoDirectoriesProvider.DotnetSDK); CurrentRid = repoDirectoriesProvider.TargetRID; -- 2.7.4