From 00753c7f28ab17e342387a8099cac321e0f36f93 Mon Sep 17 00:00:00 2001 From: Elinor Fung <47805090+elinor-fung@users.noreply.github.com> Date: Sat, 25 May 2019 09:35:36 -0700 Subject: [PATCH] Add tests for using context-based APIs with a self-contained application (dotnet/core-setup#6499) Commit migrated from https://github.com/dotnet/core-setup/commit/a8d828d14d48b7b321bd0e79266d769d166fd883 --- .../NativeHosting/HostContext.cs | 89 +++++++++++++++++++--- .../test/HostActivationTests/RuntimeConfig.cs | 11 ++- 2 files changed, 84 insertions(+), 16 deletions(-) diff --git a/src/installer/test/HostActivationTests/NativeHosting/HostContext.cs b/src/installer/test/HostActivationTests/NativeHosting/HostContext.cs index 6a44fce..dae88c6 100644 --- a/src/installer/test/HostActivationTests/NativeHosting/HostContext.cs +++ b/src/installer/test/HostActivationTests/NativeHosting/HostContext.cs @@ -57,23 +57,43 @@ namespace Microsoft.DotNet.CoreSetup.Test.HostActivation.NativeHosting } [Theory] - [InlineData(CheckProperties.None)] - [InlineData(CheckProperties.Get)] - [InlineData(CheckProperties.Set)] - [InlineData(CheckProperties.Remove)] - [InlineData(CheckProperties.GetAll)] - [InlineData(CheckProperties.GetActive)] - [InlineData(CheckProperties.GetAllActive)] - public void RunApp(string checkProperties) + [InlineData(false, CheckProperties.None)] + [InlineData(false, CheckProperties.Get)] + [InlineData(false, CheckProperties.Set)] + [InlineData(false, CheckProperties.Remove)] + [InlineData(false, CheckProperties.GetAll)] + [InlineData(false, CheckProperties.GetActive)] + [InlineData(false, CheckProperties.GetAllActive)] + [InlineData(true, CheckProperties.None)] + [InlineData(true, CheckProperties.Get)] + [InlineData(true, CheckProperties.Set)] + [InlineData(true, CheckProperties.Remove)] + [InlineData(true, CheckProperties.GetAll)] + [InlineData(true, CheckProperties.GetActive)] + [InlineData(true, CheckProperties.GetAllActive)] + public void RunApp(bool isSelfContained, string checkProperties) { + string expectedAppPath; + string hostFxrPath; + if (isSelfContained) + { + expectedAppPath = sharedState.SelfContainedAppPath; + hostFxrPath = sharedState.SelfContainedHostFxrPath; + } + else + { + expectedAppPath = sharedState.AppPath; + hostFxrPath = sharedState.HostFxrPath; + } + string newPropertyName = "HOST_TEST_PROPERTY"; string[] args = { HostContextArg, Scenario.App, checkProperties, - sharedState.HostFxrPath, - sharedState.AppPath + hostFxrPath, + expectedAppPath }; string[] appArgs = { @@ -89,8 +109,9 @@ namespace Microsoft.DotNet.CoreSetup.Test.HostActivation.NativeHosting .Execute(); result.Should().Pass() - .And.InitializeContextForApp(sharedState.AppPath) - .And.ExecuteAssemblyMock(sharedState.AppPath, appArgs); + .And.InitializeContextForApp(expectedAppPath) + .And.ExecuteAssemblyMock(expectedAppPath, appArgs) + .And.HaveStdErrContaining($"Executing as a {(isSelfContained ? "self-contained" : "framework-dependent")} app"); CheckPropertiesValidation propertyValidation = new CheckPropertiesValidation(checkProperties, LogPrefix.App, SharedTestState.AppPropertyName, SharedTestState.AppPropertyValue); propertyValidation.ValidateActiveContext(result, newPropertyName); @@ -133,6 +154,29 @@ namespace Microsoft.DotNet.CoreSetup.Test.HostActivation.NativeHosting propertyValidation.ValidateActiveContext(result, newPropertyName); } + [Fact] + public void InitializeConfig_SelfContained_Fails() + { + string[] args = + { + HostContextArg, + Scenario.Config, + CheckProperties.None, + sharedState.SelfContainedHostFxrPath, + sharedState.SelfContainedConfigPath + }; + CommandResult result = Command.Create(sharedState.NativeHostPath, args) + .CaptureStdErr() + .CaptureStdOut() + .EnvironmentVariable("COREHOST_TRACE", "1") + .EnvironmentVariable("DOTNET_ROOT", sharedState.DotNetRoot) + .EnvironmentVariable("DOTNET_ROOT(x86)", sharedState.DotNetRoot) + .Execute(); + + result.Should().Fail() + .And.HaveStdErrContaining("Initialization for self-contained components is not supported"); + } + [Theory] [InlineData(CheckProperties.None)] [InlineData(CheckProperties.Get)] @@ -580,6 +624,10 @@ namespace Microsoft.DotNet.CoreSetup.Test.HostActivation.NativeHosting public string RuntimeConfigPath { get; } public string SecondaryRuntimeConfigPath { get; } + public string SelfContainedAppPath { get; } + public string SelfContainedConfigPath { get; } + public string SelfContainedHostFxrPath { get; } + public string AppPath_MultiProperty { get; } public string RuntimeConfigPath_MultiProperty { get; } @@ -630,6 +678,23 @@ namespace Microsoft.DotNet.CoreSetup.Test.HostActivation.NativeHosting .WithProperty(AppMultiPropertyName, AppMultiPropertyValue) .Save(); + string selfContainedDir = Path.Combine(BaseDirectory, "selfContained"); + Directory.CreateDirectory(selfContainedDir); + SelfContainedAppPath = Path.Combine(selfContainedDir, "SelfContained.dll"); + File.WriteAllText(SelfContainedAppPath, string.Empty); + var toCopy = Directory.GetFiles(dotNet.GreatestVersionSharedFxPath) + .Concat(Directory.GetFiles(dotNet.GreatestVersionHostFxrPath)); + foreach (string file in toCopy) + { + File.Copy(file, Path.Combine(selfContainedDir, Path.GetFileName(file))); + } + + SelfContainedHostFxrPath = Path.Combine(selfContainedDir, Path.GetFileName(dotNet.GreatestVersionHostFxrFilePath)); + SelfContainedConfigPath = Path.Combine(selfContainedDir, "SelfContained.runtimeconfig.json"); + RuntimeConfig.FromFile(SelfContainedConfigPath) + .WithProperty(AppPropertyName, AppPropertyValue) + .Save(); + string configDir = Path.Combine(BaseDirectory, "config"); Directory.CreateDirectory(configDir); RuntimeConfigPath = Path.Combine(configDir, "Component.runtimeconfig.json"); diff --git a/src/installer/test/HostActivationTests/RuntimeConfig.cs b/src/installer/test/HostActivationTests/RuntimeConfig.cs index c47ad6f..b30404e 100644 --- a/src/installer/test/HostActivationTests/RuntimeConfig.cs +++ b/src/installer/test/HostActivationTests/RuntimeConfig.cs @@ -198,10 +198,13 @@ namespace Microsoft.DotNet.CoreSetup.Test.HostActivation public void Save() { - JObject runtimeOptions = new JObject() - { - { "frameworks", new JArray(_frameworks.Select(f => f.ToJson()).ToArray()) } - }; + JObject runtimeOptions = new JObject(); + if (_frameworks.Any()) + { + runtimeOptions.Add( + "frameworks", + new JArray(_frameworks.Select(f => f.ToJson()).ToArray())); + } if (_rollForward != null) { -- 2.7.4