Skip to content

Commit

Permalink
Make host tests use HostWriter to create apphost to test (#89523)
Browse files Browse the repository at this point in the history
  • Loading branch information
elinor-fung committed Jul 27, 2023
1 parent 5f0938c commit 6089ff6
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 169 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<RuntimeIdentifier>$(TestTargetRid)</RuntimeIdentifier>
<RuntimeFrameworkVersion>$(MNAVersion)</RuntimeFrameworkVersion>
<SelfContained>true</SelfContained>
<FileVersion>$(MNAVersion)</FileVersion>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,7 @@ public TestApp CreateSelfContainedAppWithMockHostPolicy()
testApp.PopulateSelfContained(TestApp.MockedComponent.HostPolicy);

// ./SelfContainedApp.exe
string selfContainedAppExePath = Path.Combine(testAppDir, Binaries.GetExeFileNameForCurrentPlatform("SelfContainedApp"));
File.Copy(
Binaries.AppHost.FilePath,
selfContainedAppExePath);
AppHostExtensions.BindAppHost(selfContainedAppExePath);
testApp.CreateAppHost(copyResources: false);

return testApp;
}
Expand Down
36 changes: 10 additions & 26 deletions src/installer/tests/HostActivation.Tests/PortableAppActivation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,8 @@ public void AppHost_FrameworkDependent_Succeeds()
var fixture = sharedTestState.PortableAppFixture_Published
.Copy();

// Since SDK doesn't support building framework dependent apphost yet, emulate that behavior
// by creating the executable from apphost.exe
var appExe = fixture.TestProject.AppExe;
File.Copy(Binaries.AppHost.FilePath, appExe, overwrite: true);
AppHostExtensions.BindAppHost(appExe);
string appExe = fixture.TestProject.AppExe;
fixture.TestProject.BuiltApp.CreateAppHost();

// Get the framework location that was built
string builtDotnet = fixture.BuiltDotnet.BinPath;
Expand Down Expand Up @@ -174,11 +171,8 @@ public void AppHost_FrameworkDependent_GlobalLocation_Succeeds(bool useRegistere
var fixture = sharedTestState.PortableAppFixture_Published
.Copy();

// Since SDK doesn't support building framework dependent apphost yet, emulate that behavior
// by creating the executable from apphost.exe
var appExe = fixture.TestProject.AppExe;
File.Copy(Binaries.AppHost.FilePath, appExe, overwrite: true);
AppHostExtensions.BindAppHost(appExe);
string appExe = fixture.TestProject.AppExe;
fixture.TestProject.BuiltApp.CreateAppHost();

// Get the framework location that was built
string builtDotnet = fixture.BuiltDotnet.BinPath;
Expand Down Expand Up @@ -331,8 +325,7 @@ public void AppHost_CLI_FrameworkDependent_MissingRuntimeFramework_ErrorReported
.Copy();

string appExe = fixture.TestProject.AppExe;
File.Copy(Binaries.AppHost.FilePath, appExe, overwrite: true);
AppHostExtensions.BindAppHost(appExe);
fixture.TestProject.BuiltApp.CreateAppHost();

string invalidDotNet = SharedFramework.CalculateUniqueTestDirectory(Path.Combine(TestArtifact.TestArtifactsPath, "cliErrors"));
using (new TestArtifact(invalidDotNet))
Expand Down Expand Up @@ -383,9 +376,7 @@ public void AppHost_GUI_FrameworkDependent_MissingRuntimeFramework_ErrorReported
.Copy();

string appExe = fixture.TestProject.AppExe;
File.Copy(Binaries.AppHost.FilePath, appExe, overwrite: true);
AppHostExtensions.BindAppHost(appExe);
AppHostExtensions.SetWindowsGraphicalUserInterfaceBit(appExe);
fixture.TestProject.BuiltApp.CreateAppHost(isWindowsGui: true);

string invalidDotNet = SharedFramework.CalculateUniqueTestDirectory(Path.Combine(TestArtifact.TestArtifactsPath, "guiErrors"));
using (new TestArtifact(invalidDotNet))
Expand Down Expand Up @@ -428,9 +419,7 @@ public void AppHost_GUI_MissingRuntime_ErrorReportedInDialog()
.Copy();

string appExe = fixture.TestProject.AppExe;
File.Copy(Binaries.AppHost.FilePath, appExe, overwrite: true);
AppHostExtensions.BindAppHost(appExe);
AppHostExtensions.SetWindowsGraphicalUserInterfaceBit(appExe);
fixture.TestProject.BuiltApp.CreateAppHost(isWindowsGui: true);

string invalidDotNet = SharedFramework.CalculateUniqueTestDirectory(Path.Combine(TestArtifact.TestArtifactsPath, "guiErrors"));
using (new TestArtifact(invalidDotNet))
Expand Down Expand Up @@ -463,9 +452,7 @@ public void AppHost_GUI_NoCustomErrorWriter_FrameworkMissing_ErrorReportedInDial
.Copy();

string appExe = fixture.TestProject.AppExe;
File.Copy(Binaries.AppHost.FilePath, appExe, overwrite: true);
AppHostExtensions.BindAppHost(appExe);
AppHostExtensions.SetWindowsGraphicalUserInterfaceBit(appExe);
fixture.TestProject.BuiltApp.CreateAppHost(isWindowsGui: true);

string dotnetWithMockHostFxr = SharedFramework.CalculateUniqueTestDirectory(Path.Combine(TestArtifact.TestArtifactsPath, "guiErrors"));
using (new TestArtifact(dotnetWithMockHostFxr))
Expand Down Expand Up @@ -504,9 +491,7 @@ public void AppHost_GUI_FrameworkDependent_DisabledGUIErrors_DialogNotShown()
.Copy();

string appExe = fixture.TestProject.AppExe;
File.Copy(Binaries.AppHost.FilePath, appExe, overwrite: true);
AppHostExtensions.BindAppHost(appExe);
AppHostExtensions.SetWindowsGraphicalUserInterfaceBit(appExe);
fixture.TestProject.BuiltApp.CreateAppHost(isWindowsGui: true);

string invalidDotNet = SharedFramework.CalculateUniqueTestDirectory(Path.Combine(TestArtifact.TestArtifactsPath, "guiErrors"));
using (new TestArtifact(invalidDotNet))
Expand Down Expand Up @@ -568,8 +553,7 @@ public SharedTestState()
MockApp = new TestApp(SharedFramework.CalculateUniqueTestDirectory(Path.Combine(TestArtifact.TestArtifactsPath, "portableAppActivation")), "App");
Directory.CreateDirectory(MockApp.Location);
File.WriteAllText(MockApp.AppDll, string.Empty);
File.Copy(Binaries.AppHost.FilePath, MockApp.AppExe);
AppHostExtensions.BindAppHost(MockApp.AppExe);
MockApp.CreateAppHost(copyResources: false);
}

public void Dispose()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,16 +238,20 @@ public void Running_Publish_Output_Standalone_EXE_with_Bound_AppHost_Succeeds()
.Copy();

string appExe = fixture.TestProject.AppExe;

UseBuiltAppHost(appExe);
AppHostExtensions.BindAppHost(appExe);
fixture.TestProject.BuiltApp.CreateAppHost();

Command.Create(appExe)
.EnableTracingAndCaptureOutputs()
.Execute()
.Should().Pass()
.And.HaveStdOutContaining("Hello World")
.And.HaveStdOutContaining(sharedTestState.RepoDirectories.MicrosoftNETCoreAppVersion);

if (OperatingSystem.IsWindows())
{
// StandaloneApp sets FileVersion to NETCoreApp version. On Windows, this should be copied to apphost resources.
Assert.Equal(System.Diagnostics.FileVersionInfo.GetVersionInfo(appExe).FileVersion, sharedTestState.RepoDirectories.MicrosoftNETCoreAppVersion);
}
}

[Fact]
Expand All @@ -261,7 +265,7 @@ public void Running_AppHost_with_GUI_No_Console()

// Mark the apphost as GUI, but don't bind it to anything - this will cause it to fail
UseBuiltAppHost(appExe);
AppHostExtensions.SetWindowsGraphicalUserInterfaceBit(appExe);
PEUtils.SetWindowsGraphicalUserInterfaceBit(appExe);

Command.Create(appExe)
.CaptureStdErr()
Expand All @@ -282,7 +286,7 @@ public void Running_AppHost_with_GUI_Traces()

// Mark the apphost as GUI, but don't bind it to anything - this will cause it to fail
UseBuiltAppHost(appExe);
AppHostExtensions.SetWindowsGraphicalUserInterfaceBit(appExe);
PEUtils.SetWindowsGraphicalUserInterfaceBit(appExe);

string traceFilePath;
Command.Create(appExe)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public static string UseSingleFileSelfContainedHost(TestProjectFixture testFixtu
var publishedHostPath = BundleHelper.GetHostPath(testFixture);
HostWriter.CreateAppHost(Binaries.SingleFileHost.FilePath,
publishedHostPath,
BundleHelper.GetAppName(testFixture));
BundleHelper.GetAppName(testFixture),
assemblyToCopyResourcesFrom: BundleHelper.GetAppPath(testFixture));
return publishedHostPath;
}

Expand All @@ -31,7 +32,8 @@ public static string UseFrameworkDependentHost(TestProjectFixture testFixture)
var publishedHostPath = BundleHelper.GetHostPath(testFixture);
HostWriter.CreateAppHost(Binaries.AppHost.FilePath,
publishedHostPath,
BundleHelper.GetAppName(testFixture));
BundleHelper.GetAppName(testFixture),
assemblyToCopyResourcesFrom: BundleHelper.GetAppPath(testFixture));
return publishedHostPath;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.DotNet.Cli.Build;
using Microsoft.DotNet.Cli.Build.Framework;
using Microsoft.DotNet.CoreSetup.Test;
using Microsoft.NET.HostModel.AppHost;
using Microsoft.NET.HostModel.Bundle;
using Xunit;

Expand Down Expand Up @@ -92,7 +93,7 @@ public void Bundled_Framework_dependent_App_GUI_DownlevelHostFxr_ErrorDialog(Bun
var fixture = sharedTestState.TestFrameworkDependentFixture.Copy();
UseFrameworkDependentHost(fixture);
var singleFile = BundleHelper.BundleApp(fixture, options);
AppHostExtensions.SetWindowsGraphicalUserInterfaceBit(singleFile);
PEUtils.SetWindowsGraphicalUserInterfaceBit(singleFile);

string dotnetWithMockHostFxr = SharedFramework.CalculateUniqueTestDirectory(Path.Combine(TestArtifact.TestArtifactsPath, "bundleErrors"));
using (new TestArtifact(dotnetWithMockHostFxr))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ private void Can_Run_SingleFile_App_With_StaticHost()
.Pass()
.And
.HaveStdOutContaining("Hello World");

if (OperatingSystem.IsWindows())
{
// StandaloneApp sets FileVersion to NETCoreApp version. On Windows, this should be copied to singlefilehost resources.
Assert.Equal(System.Diagnostics.FileVersionInfo.GetVersionInfo(singleFile).FileVersion, sharedTestState.RepoDirectories.MicrosoftNETCoreAppVersion);
}
}

public class SharedTestState : SharedTestStateBase, IDisposable
Expand Down
130 changes: 0 additions & 130 deletions src/installer/tests/TestUtils/AppHostExtensions.cs

This file was deleted.

12 changes: 12 additions & 0 deletions src/installer/tests/TestUtils/TestApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Reflection;
using System.Reflection.Metadata;
using Microsoft.DotNet.Cli.Build;
using Microsoft.NET.HostModel.AppHost;

namespace Microsoft.DotNet.CoreSetup.Test
{
Expand Down Expand Up @@ -65,6 +66,17 @@ public void PopulateFrameworkDependent(string fxName, string fxVersion, Action<N
builder.Build(this);
}

public void CreateAppHost(bool isWindowsGui = false, bool copyResources = true)
{
// Use the live-built apphost and HostModel to create the apphost to run
HostWriter.CreateAppHost(
Binaries.AppHost.FilePath,
AppExe,
Path.GetFileName(AppDll),
windowsGraphicalUserInterface: isWindowsGui,
assemblyToCopyResourcesFrom: copyResources ? AppDll : null);
}

public enum MockedComponent
{
None, // Product components
Expand Down

0 comments on commit 6089ff6

Please sign in to comment.