Skip to content

Commit

Permalink
Some clean-up of host tests (dotnet#69802)
Browse files Browse the repository at this point in the history
* Use shared test utilities for test artifacts and tracing
* Delete unused test utilities
  • Loading branch information
elinor-fung authored May 26, 2022
1 parent 008f128 commit bc27adc
Show file tree
Hide file tree
Showing 16 changed files with 84 additions and 313 deletions.
15 changes: 6 additions & 9 deletions src/installer/tests/HostActivation.Tests/DotnetArgValidation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void InvalidFileOrCommand_NoSDK_ListsPossibleIssues()
{
string fileName = "NonExistent";
sharedTestState.BuiltDotNet.Exec(fileName)
.WorkingDirectory(sharedTestState.BaseDirectory)
.WorkingDirectory(sharedTestState.BaseDirectory.Location)
.CaptureStdOut()
.CaptureStdErr()
.Execute(expectedToFail: true)
Expand All @@ -93,26 +93,23 @@ public class SharedTestState : IDisposable
public RepoDirectoriesProvider RepoDirectories { get; }

public DotNetCli BuiltDotNet { get; }
public string BaseDirectory { get; }
public TestArtifact BaseDirectory { get; }

public SharedTestState()
{
RepoDirectories = new RepoDirectoriesProvider();
BuiltDotNet = new DotNetCli(RepoDirectories.BuiltDotnet);

BaseDirectory = SharedFramework.CalculateUniqueTestDirectory(Path.Combine(TestArtifact.TestArtifactsPath, "argValidation"));
BaseDirectory = new TestArtifact(SharedFramework.CalculateUniqueTestDirectory(Path.Combine(TestArtifact.TestArtifactsPath, "argValidation")));

// Create an empty global.json file
Directory.CreateDirectory(BaseDirectory);
File.WriteAllText(Path.Combine(BaseDirectory, "global.json"), "{}");
Directory.CreateDirectory(BaseDirectory.Location);
File.WriteAllText(Path.Combine(BaseDirectory.Location, "global.json"), "{}");
}

public void Dispose()
{
if (!TestArtifact.PreserveTestRuns() && Directory.Exists(BaseDirectory))
{
Directory.Delete(BaseDirectory, true);
}
BaseDirectory.Dispose();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public class SharedTestStateBase : IDisposable
private readonly string _builtDotnet;
private readonly RepoDirectoriesProvider _repoDirectories;
private readonly string _baseDir;
private readonly TestArtifact _baseDirArtifact;

public SharedTestStateBase()
{
Expand All @@ -98,6 +99,7 @@ public SharedTestStateBase()

string baseDir = Path.Combine(TestArtifact.TestArtifactsPath, "frameworkResolution");
_baseDir = SharedFramework.CalculateUniqueTestDirectory(baseDir);
_baseDirArtifact = new TestArtifact(_baseDir);
}

public DotNetBuilder DotNet(string name)
Expand Down Expand Up @@ -167,10 +169,7 @@ protected virtual void Dispose(bool disposing)
{
if (disposing)
{
if (!TestArtifact.PreserveTestRuns() && Directory.Exists(_baseDir))
{
Directory.Delete(_baseDir, true);
}
_baseDirArtifact.Dispose();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ private void Latest_Host_Is_Backwards_Compatible_With_Older_Runtime(TestProjectF

// Baseline (no changes)
Command.Create(appExe)
.EnvironmentVariable("COREHOST_TRACE", "1")
.CaptureStdErr()
.CaptureStdOut()
.EnableTracingAndCaptureOutputs()
.Execute()
.Should().Pass()
.And.HaveStdOutContaining("Hello World")
Expand All @@ -59,9 +57,7 @@ private void Latest_Host_Is_Backwards_Compatible_With_Older_Runtime(TestProjectF
// 2) Newer runtime uninstalled (installer preserves newer apphost)
File.Copy(sharedTestState.FixtureLatest.TestProject.AppExe, fixture.TestProject.AppExe, true);
Command.Create(appExe)
.EnvironmentVariable("COREHOST_TRACE", "1")
.CaptureStdErr()
.CaptureStdOut()
.EnableTracingAndCaptureOutputs()
.Execute()
.Should().Pass()
.And.HaveStdOutContaining("Hello World")
Expand All @@ -73,9 +69,7 @@ private void Latest_Host_Is_Backwards_Compatible_With_Older_Runtime(TestProjectF
// 2) A roll-forward to the newer runtime did not occur
File.Copy(sharedTestState.FixtureLatest.TestProject.HostFxrDll, fixture.TestProject.HostFxrDll, true);
Command.Create(appExe)
.EnvironmentVariable("COREHOST_TRACE", "1")
.CaptureStdErr()
.CaptureStdOut()
.EnableTracingAndCaptureOutputs()
.Execute()
.Should().Pass()
.And.HaveStdOutContaining("Hello World")
Expand Down Expand Up @@ -109,9 +103,7 @@ private void Old_Host_Is_Forward_Compatible_With_Latest_Runtime(TestProjectFixtu

// Baseline (no changes)
Command.Create(appExe)
.EnvironmentVariable("COREHOST_TRACE", "1")
.CaptureStdErr()
.CaptureStdOut()
.EnableTracingAndCaptureOutputs()
.Execute()
.Should().Pass()
.And.HaveStdOutContaining("Hello World")
Expand All @@ -125,9 +117,7 @@ private void Old_Host_Is_Forward_Compatible_With_Latest_Runtime(TestProjectFixtu
File.Copy(previousVersionFixture.TestProject.AppExe, fixture.TestProject.AppExe, true);
File.Copy(previousVersionFixture.TestProject.HostFxrDll, fixture.TestProject.HostFxrDll, true);
Command.Create(appExe)
.EnvironmentVariable("COREHOST_TRACE", "1")
.CaptureStdErr()
.CaptureStdOut()
.EnableTracingAndCaptureOutputs()
.Execute()
.Should().Pass()
.And.HaveStdOutContaining("Hello World")
Expand Down
18 changes: 7 additions & 11 deletions src/installer/tests/HostActivation.Tests/LightupAppActivation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class LightupAppActivation : IClassFixture<LightupAppActivation.SharedTes
private const string SystemCollectionsImmutableFileVersion = "88.2.3.4";
private const string SystemCollectionsImmutableAssemblyVersion = "88.0.1.2";

private readonly string _currentWorkingDir;
private readonly TestArtifact _baseDirArtifact;
private readonly string _builtSharedFxDir;
private readonly string _builtSharedUberFxDir;
private readonly string _fxBaseDir;
Expand All @@ -34,13 +34,13 @@ public LightupAppActivation(LightupAppActivation.SharedTestState fixture)

// The dotnetLightupSharedFxLookup dir will contain some folders and files that will be necessary to perform the tests
string sharedLookupDir = Path.Combine(artifactsDir, "dotnetLightupSharedFxLookup");
_currentWorkingDir = SharedFramework.CalculateUniqueTestDirectory(sharedLookupDir);
_fxBaseDir = Path.Combine(_currentWorkingDir, "shared", "Microsoft.NETCore.App");
_uberFxBaseDir = Path.Combine(_currentWorkingDir, "shared", "Microsoft.UberFramework");
_baseDirArtifact = new TestArtifact(SharedFramework.CalculateUniqueTestDirectory(sharedLookupDir));
_fxBaseDir = Path.Combine(_baseDirArtifact.Location, "shared", "Microsoft.NETCore.App");
_uberFxBaseDir = Path.Combine(_baseDirArtifact.Location, "shared", "Microsoft.UberFramework");

SharedFramework.CopyDirectory(builtDotnet, _currentWorkingDir);
SharedFramework.CopyDirectory(builtDotnet, _baseDirArtifact.Location);

var repoDirectories = new RepoDirectoriesProvider(builtDotnet: _currentWorkingDir);
var repoDirectories = new RepoDirectoriesProvider(builtDotnet: _baseDirArtifact.Location);
GlobalLightupClientFixture = new TestProjectFixture("LightupClient", repoDirectories)
.EnsureRestored()
.BuildProject();
Expand All @@ -55,11 +55,7 @@ public LightupAppActivation(LightupAppActivation.SharedTestState fixture)
public void Dispose()
{
GlobalLightupClientFixture.Dispose();

if (!TestProject.PreserveTestRuns())
{
Directory.Delete(_currentWorkingDir, true);
}
_baseDirArtifact.Dispose();
}

// Attempt to run the app with lightup deps.json specified but lightup library missing in the expected
Expand Down
11 changes: 4 additions & 7 deletions src/installer/tests/HostActivation.Tests/MockCoreClrSanity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,20 @@ public class MockCoreClrSanity : IDisposable
{
private readonly DotNetCli DotNet;

private readonly string _dotnetDir;
private readonly TestArtifact _dotnetDirArtifact;

public MockCoreClrSanity()
{
_dotnetDir = Path.Combine(TestArtifact.TestArtifactsPath, "mockCoreclrSanity");
_dotnetDirArtifact = new TestArtifact(Path.Combine(TestArtifact.TestArtifactsPath, "mockCoreclrSanity"));

DotNet = new DotNetBuilder(_dotnetDir, Path.Combine(TestArtifact.TestArtifactsPath, "sharedFrameworkPublish"), "exe")
DotNet = new DotNetBuilder(_dotnetDirArtifact.Location, Path.Combine(TestArtifact.TestArtifactsPath, "sharedFrameworkPublish"), "exe")
.AddMicrosoftNETCoreAppFrameworkMockCoreClr("9999.0.0")
.Build();
}

public void Dispose()
{
if (!TestArtifact.PreserveTestRuns())
{
Directory.Delete(_dotnetDir, true);
}
_dotnetDirArtifact.Dispose();
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ public void RunApp_GetDelegate(string scenario, string checkProperties)
SharedTestState.ConfigPropertyName
};
CommandResult result = sharedState.CreateNativeHostCommand(args.Concat(appArgs), sharedState.DotNetRoot)
.EnvironmentVariable("COREHOST_TRACE_VERBOSITY", "3")
.EnvironmentVariable(Constants.HostTracing.VerbosityEnvironmentVariable, "3")
.EnvironmentVariable("TEST_BLOCK_MOCK_EXECUTE_ASSEMBLY", $"{sharedState.AppPath}.block")
.EnvironmentVariable("TEST_SIGNAL_MOCK_EXECUTE_ASSEMBLY", $"{sharedState.AppPath}.signal")
.Execute();
Expand Down Expand Up @@ -351,7 +351,7 @@ public void CompatibilityCheck_Frameworks(string scenario, FrameworkCompatibilit
try
{
result = sharedState.CreateNativeHostCommand(args, sharedState.DotNetRoot)
.EnvironmentVariable("COREHOST_TRACE_VERBOSITY", "3")
.EnvironmentVariable(Constants.HostTracing.VerbosityEnvironmentVariable, "3")
.EnvironmentVariable("TEST_BLOCK_MOCK_EXECUTE_ASSEMBLY", $"{sharedState.AppPath}.block")
.EnvironmentVariable("TEST_SIGNAL_MOCK_EXECUTE_ASSEMBLY", $"{sharedState.AppPath}.signal")
.Execute();
Expand Down Expand Up @@ -447,7 +447,7 @@ public void CompatibilityCheck_Properties(string scenario, bool hasMultiplePrope
try
{
result = sharedState.CreateNativeHostCommand(args, sharedState.DotNetRoot)
.EnvironmentVariable("COREHOST_TRACE_VERBOSITY", "3")
.EnvironmentVariable(Constants.HostTracing.VerbosityEnvironmentVariable, "3")
.EnvironmentVariable("TEST_BLOCK_MOCK_EXECUTE_ASSEMBLY", $"{sharedState.AppPath}.block")
.EnvironmentVariable("TEST_SIGNAL_MOCK_EXECUTE_ASSEMBLY", $"{sharedState.AppPath}.signal")
.Execute();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,16 +325,18 @@ public void GetHostFxrPath_InvalidParameters()
[Fact]
public void TracingNotBufferedByDefault()
{
string traceFilePath;
CommandResult result = Command.Create(sharedState.NativeHostPath, $"{GetHostFxrPath} false nullptr x")
.EnvironmentVariable("COREHOST_TRACE", "1")
.EnvironmentVariable("COREHOST_TRACEFILE", "Tracing.out")
.EnableHostTracingToFile(out traceFilePath)
.MultilevelLookup(true)
.DotNetRoot(null)
.Execute();

result.Should().Fail()
.And.FileExists("Tracing.out")
.And.FileContains("Tracing.out", "Tracing enabled");
.And.FileExists(traceFilePath)
.And.FileContains(traceFilePath, "Tracing enabled");

FileUtils.DeleteFileIfPossible(traceFilePath);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ public class SharedTestStateBase : IDisposable
public string NethostPath { get; }
public RepoDirectoriesProvider RepoDirectories { get; }

private readonly TestArtifact _baseDirArtifact;

public SharedTestStateBase()
{
BaseDirectory = SharedFramework.CalculateUniqueTestDirectory(Path.Combine(TestArtifact.TestArtifactsPath, "nativeHosting"));
_baseDirArtifact = new TestArtifact(BaseDirectory);
Directory.CreateDirectory(BaseDirectory);

string nativeHostName = RuntimeInformationExtensions.GetExeFileNameForCurrentPlatform("nativehost");
Expand Down Expand Up @@ -49,13 +52,14 @@ public Command CreateNativeHostCommand(IEnumerable<string> args, string dotNetRo
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
if (!TestArtifact.PreserveTestRuns() && Directory.Exists(BaseDirectory))
if (disposing)
{
Directory.Delete(BaseDirectory, true);
_baseDirArtifact.Dispose();
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/installer/tests/HostActivation.Tests/SDKLookup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1050,12 +1050,15 @@ public class SharedTestState : IDisposable

public string TestAssetsPath { get; }

private readonly TestArtifact _baseDirArtifact;

public SharedTestState()
{
// The dotnetSDKLookup dir will contain some folders and files that will be
// necessary to perform the tests
string baseDir = Path.Combine(TestArtifact.TestArtifactsPath, "dotnetSDKLookup");
BaseDir = SharedFramework.CalculateUniqueTestDirectory(baseDir);
_baseDirArtifact = new TestArtifact(BaseDir);

// The three tested locations will be the cwd and the exe dir. cwd is no longer supported.
// All dirs will be placed inside the base folder
Expand Down Expand Up @@ -1084,10 +1087,7 @@ protected virtual void Dispose(bool disposing)
{
if (disposing)
{
if (!TestArtifact.PreserveTestRuns() && Directory.Exists(BaseDir))
{
Directory.Delete(BaseDir, true);
}
_baseDirArtifact.Dispose();
}
}
}
Expand Down
22 changes: 7 additions & 15 deletions src/installer/tests/HostActivation.Tests/SharedFxLookup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class SharedFxLookup : IDisposable
private readonly string _builtSharedUberFxDir;

private readonly string _sharedFxVersion;
private readonly string _baseDir;
private readonly TestArtifact _baseDirArtifact;
private readonly string _builtDotnet;

public SharedFxLookup()
Expand All @@ -39,12 +39,12 @@ public SharedFxLookup()
// The dotnetSharedFxLookup dir will contain some folders and files that will be
// necessary to perform the tests
string baseDir = Path.Combine(artifactsDir, "dotnetSharedFxLookup");
_baseDir = SharedFramework.CalculateUniqueTestDirectory(baseDir);
_baseDirArtifact = new TestArtifact(SharedFramework.CalculateUniqueTestDirectory(baseDir));

// The two tested locations will be the cwd and the exe dir. Both cwd and exe dir
// are easily overwritten, so they will be placed inside the multilevel folder.
_currentWorkingDir = Path.Combine(_baseDir, "cwd");
_executableDir = Path.Combine(_baseDir, "exe");
_currentWorkingDir = Path.Combine(_baseDirArtifact.Location, "cwd");
_executableDir = Path.Combine(_baseDirArtifact.Location, "exe");

RepoDirectories = new RepoDirectoriesProvider(builtDotnet: _executableDir);

Expand Down Expand Up @@ -76,11 +76,7 @@ public SharedFxLookup()
public void Dispose()
{
SharedFxLookupPortableAppFixture.Dispose();

if (!TestProject.PreserveTestRuns())
{
Directory.Delete(_baseDir, true);
}
_baseDirArtifact.Dispose();
}

[Fact]
Expand Down Expand Up @@ -114,9 +110,7 @@ public void Multiple_SharedFxLookup_NetCoreApp_MinorRollForward_Wins_Over_UberFx
// 7777.0.0
dotnet.Exec(appDll)
.WorkingDirectory(_currentWorkingDir)
.EnvironmentVariable("COREHOST_TRACE", "1")
.CaptureStdOut()
.CaptureStdErr()
.EnableTracingAndCaptureOutputs()
.Execute()
.Should().Pass()
.And.HaveStdErrContaining($"Replacing deps entry [{uberFile}, AssemblyVersion:0.0.0.1, FileVersion:0.0.0.2] with [{netCoreAppFile}");
Expand Down Expand Up @@ -148,9 +142,7 @@ public void Multiple_SharedFxLookup_Uber_Wins_Over_NetCoreApp_On_PatchRollForwar
// 7777.0.0
dotnet.Exec(appDll)
.WorkingDirectory(_currentWorkingDir)
.EnvironmentVariable("COREHOST_TRACE", "1")
.CaptureStdOut()
.CaptureStdErr()
.EnableTracingAndCaptureOutputs()
.Execute()
.Should().Pass()
.And.HaveStdErrContaining(Path.Combine("7777.0.0", "System.Collections.Immutable.dll"))
Expand Down
Loading

0 comments on commit bc27adc

Please sign in to comment.