Skip to content

Commit

Permalink
Refactor and improve MLL tests (dotnet#66332)
Browse files Browse the repository at this point in the history
* Remove duplicated code - I know that tests should be descriptive, but repeating 100 times that we want to capture output doesn't feel necessary.
* For some of them move more stuff into the shared test state - this improves perf as we avoid repeating the setup (copying files around) for each test case, we do it once for the entire class
I also changed some of the tests to "Theory" as it's easier to read that way.
  * `SDKLookup.cs` - moved most of the state in to the shared state to speed up the tests.

* Adding more cases into the "theory" tests
  * Most notably for the framework resolution I added variations on the TFM (which will be needed when we implement disable of MLL)
* Adding new tests mostly around "list runtimes" (various forms), "list sdks" (various forms) and errors (which also list runtimes or sdks)

* Ported all of the `MultiLevelLookupFramework` tests over to the `FrameworkResolution` and `DependencyResolutions` suites which have a more robust test infra and can run the same tests much faster. Along the way I added lot more variations on top of the existing tests:
  * `PerAssemblyVersionResolutionMultipleFrameworks.cs` - this is actually not an MLL test, but I moved it to the new infra to make it much faster
  * `MultipleHives.cs` - MLL framework resolution tests

For SDK resolution I kept the `MultiLevelSDKLookup.cs` just removed code duplication and added new variants.

For the core reviewers: I promise I didn't remove any single test case in spirit with these exceptions:

* We had tests which validated that framework resolution is not affected by frameworks in current directory and also by frameworks in the user's directory. I left some basic test for the current directory check, but I completely removed the user's directory variant as the product simply doesn't have any code around that anymore.

* Remove "user" directory from SDK resolution tests

The product simply doesn't have any knowledge about user directory SDK lookup, so there's little value in testing for it (even though the test expects it to not have any impact).

Co-authored-by: Elinor Fung <[email protected]>
  • Loading branch information
vitek-karas and elinor-fung authored Mar 11, 2022
1 parent 5ce2b9f commit f82fe1d
Show file tree
Hide file tree
Showing 17 changed files with 829 additions and 1,208 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,29 @@ public abstract class ComponentSharedTestStateBase : SharedTestStateBase

public ComponentSharedTestStateBase()
{
DotNetWithNetCoreApp = DotNet("WithNetCoreApp")
.AddMicrosoftNETCoreAppFrameworkMockCoreClr("4.0.0", builder => CustomizeDotNetWithNetCoreAppMicrosoftNETCoreApp(builder))
.Build();
var dotNetBuilder = DotNet("WithNetCoreApp")
.AddMicrosoftNETCoreAppFrameworkMockCoreClr("4.0.0", builder => CustomizeDotNetWithNetCoreAppMicrosoftNETCoreApp(builder));
CustomizeDotNetWithNetCoreApp(dotNetBuilder);
DotNetWithNetCoreApp = dotNetBuilder.Build();

TestApp app = CreateFrameworkReferenceApp(MicrosoftNETCoreApp, "4.0.0");
TestApp app = CreateTestFrameworkReferenceApp();
FrameworkReferenceApp = NetCoreAppBuilder.PortableForNETCoreApp(app)
.WithProject(p => p.WithAssemblyGroup(null, g => g.WithMainAssembly()))
.Build(app);

_nativeHostingState = new NativeHosting.SharedTestStateBase();
}

protected virtual TestApp CreateTestFrameworkReferenceApp() => CreateFrameworkReferenceApp(MicrosoftNETCoreApp, "4.0.0");

protected virtual void CustomizeDotNetWithNetCoreAppMicrosoftNETCoreApp(NetCoreAppBuilder builder)
{
}

protected virtual void CustomizeDotNetWithNetCoreApp(DotNetBuilder builder)
{
}

public CommandResult RunComponentResolutionTest(TestApp component, Action<Command> commandCustomizer = null)
{
return RunComponentResolutionTest(component.AppDll, FrameworkReferenceApp, DotNetWithNetCoreApp.GreatestVersionHostFxrPath, commandCustomizer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,10 @@ public PerAssemblyVersionResolutionBase(SharedTestState fixture)
[InlineData(TestAssemblyWithFileVersion, null, "3.2.2.2", false)]
public void AppWithSameAssemblyAsFramework(string testAssemblyName, string appAsmVersion, string appFileVersion, bool appWins)
{
RunTest(b => b
.WithPackage(TestVersionsPackage, "1.0.0", lib => lib
.WithAssemblyGroup(null, g => g
.WithAsset(testAssemblyName + ".dll", rf => rf
.WithVersion(appAsmVersion, appFileVersion)))),
testAssemblyName, appAsmVersion, appFileVersion, appWins);
RunTest(testAssemblyName, appAsmVersion, appFileVersion, appWins);
}

protected abstract void RunTest(Action<NetCoreAppBuilder> customizer, string testAssemblyName, string appAsmVersion, string appFileVersion, bool appWins);
protected abstract void RunTest(string testAssemblyName, string appAsmVersion, string appFileVersion, bool appWins);

public class SharedTestState : ComponentSharedTestStateBase
{
Expand Down Expand Up @@ -105,7 +100,7 @@ public AppPerAssemblyVersionResolution(SharedTestState sharedState)
{
}

protected override void RunTest(Action<NetCoreAppBuilder> customizer, string testAssemblyName, string appAsmVersion, string appFileVersion, bool appWins)
protected override void RunTest(string testAssemblyName, string appAsmVersion, string appFileVersion, bool appWins)
{
var app = SharedState.CreateTestFrameworkReferenceApp(b => b
.WithPackage(TestVersionsPackage, "1.0.0", lib => lib
Expand Down Expand Up @@ -133,7 +128,7 @@ public ComponentPerAssemblyVersionResolution(SharedTestState sharedState)
{
}

protected override void RunTest(Action<NetCoreAppBuilder> customizer, string testAssemblyName, string appAsmVersion, string appFileVersion, bool appWins)
protected override void RunTest(string testAssemblyName, string appAsmVersion, string appFileVersion, bool appWins)
{
var component = SharedState.CreateComponentWithNoDependencies(b => b
.WithPackage(TestVersionsPackage, "1.0.0", lib => lib
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
// 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;
using Xunit;

namespace Microsoft.DotNet.CoreSetup.Test.HostActivation.DependencyResolution
{
public abstract class PerAssemblyVersionResolutionMultipleFrameworksBase :
ComponentDependencyResolutionBase,
IClassFixture<PerAssemblyVersionResolutionBase.SharedTestState>
{
protected readonly SharedTestState SharedState;

public PerAssemblyVersionResolutionMultipleFrameworksBase(SharedTestState fixture)
{
SharedState = fixture;
}

protected const string HighWare = "HighWare";
protected const string TestVersionsPackage = "Test.Versions.Package";

// The test framework above has 4 assemblies in it each with different set of assembly and file versions.
// NetCoreApp HighWare
// - TestAssemblyWithNoVersions: null , null null , null
// - TestAssemblyWithAssemblyVersion: 2.1.1.1, null 2.1.1.2, null
// - TestAssemblyWithFileVersion: null , 3.2.2.2 null , 3.2.2.2
// - TestAssemblyWithBothVersions: 2.1.1.1, 3.2.2.2 2.1.1.0, 3.2.2.0
private const string TestAssemblyWithNoVersions = "Test.Assembly.NoVersions";
private const string TestAssemblyWithAssemblyVersion = "Test.Assembly.AssemblyVersion";
private const string TestAssemblyWithFileVersion = "Test.Assembly.FileVersion";
private const string TestAssemblyWithBothVersions = "Test.Assembly.BothVersions";

[Theory]
[InlineData(TestAssemblyWithBothVersions, null, null, MicrosoftNETCoreApp)] // NetCoreApp has higher version than HighWare
[InlineData(TestAssemblyWithBothVersions, "1.0.0.0", "1.0.0.0", MicrosoftNETCoreApp)]
[InlineData(TestAssemblyWithBothVersions, "3.0.0.0", "4.0.0.0", null)] // App has higher version than any framework
[InlineData(TestAssemblyWithBothVersions, "2.1.1.1", "3.3.0.0", null)] // App has higher file version
[InlineData(TestAssemblyWithBothVersions, "2.1.1.1", "3.2.2.2", MicrosoftNETCoreApp)] // Lower level framework always wins on equality (this is intentional)
[InlineData(TestAssemblyWithBothVersions, null, "4.0.0.0", MicrosoftNETCoreApp)] // The one with version wins
[InlineData(TestAssemblyWithBothVersions, null, "2.0.0.0", MicrosoftNETCoreApp)] // The one with version wins
[InlineData(TestAssemblyWithBothVersions, "3.0.0.0", null, null)]
[InlineData(TestAssemblyWithBothVersions, "2.1.1.1", null, MicrosoftNETCoreApp)]
[InlineData(TestAssemblyWithNoVersions, null, null, MicrosoftNETCoreApp)] // No versions are treated as equal (so lower one wins)
[InlineData(TestAssemblyWithNoVersions, "1.0.0.0", null, null)] // The one with version wins
[InlineData(TestAssemblyWithNoVersions, "1.0.0.0", "1.0.0.0", null)] // The one with version wins
[InlineData(TestAssemblyWithNoVersions, null, "1.0.0.0", null)] // The one with version wins
[InlineData(TestAssemblyWithAssemblyVersion, null, null, HighWare)] // Highware has higher version than NetCoreApp
[InlineData(TestAssemblyWithAssemblyVersion, "1.0.0.0", null, HighWare)]
[InlineData(TestAssemblyWithAssemblyVersion, null, "1.0.0.0", HighWare)]
[InlineData(TestAssemblyWithAssemblyVersion, "3.0.0.0", "1.0.0.0", null)] // App has higher version than any framework
[InlineData(TestAssemblyWithAssemblyVersion, "2.1.1.2", null, HighWare)] // Both are exactly the same, so lower level wins
[InlineData(TestAssemblyWithAssemblyVersion, "2.1.1.2", "1.0.0.0", null)]
[InlineData(TestAssemblyWithFileVersion, null, null, MicrosoftNETCoreApp)] // Frameworks both have the same version - lower one wins
[InlineData(TestAssemblyWithFileVersion, "1.0.0.0", null, null)] // App has assembly version, no framework has it - so app wins
[InlineData(TestAssemblyWithFileVersion, null, "1.0.0.0", MicrosoftNETCoreApp)]
[InlineData(TestAssemblyWithFileVersion, null, "4.0.0.0", null)] // App has higher version than either framework
[InlineData(TestAssemblyWithFileVersion, null, "3.2.2.2", MicrosoftNETCoreApp)] // Exactly equal - lower one wins
public void AppWithSameAssemblyAsFramework(string testAssemblyName, string appAsmVersion, string appFileVersion, string frameWorkWins)
{
RunTest(null, testAssemblyName, appAsmVersion, appFileVersion, frameWorkWins);
}

[Theory]
[InlineData("1.1.1")] // Exact match - no roll forward
[InlineData("1.1.0")] // Patch roll forward
[InlineData("1.0.0")] // Minor
[InlineData("0.0.0")] // Major
public void AppWithExactlySameAssemblyAsFrameworkWithRollForward(string frameworkReferenceVersion)
{
RunTest(
runtimeConfig => runtimeConfig
.WithFramework(HighWare, frameworkReferenceVersion)
.WithRollForward(Constants.RollForwardSetting.Major),
TestAssemblyWithBothVersions, "2.1.1.1", "3.2.2.2", MicrosoftNETCoreApp);
}

protected abstract void RunTest(Action<RuntimeConfig> runtimeConfigCustomizer, string testAssemblyName, string appAsmVersion, string appFileVersion, string frameWorkWins);

public class SharedTestState : ComponentSharedTestStateBase
{
public SharedTestState()
{
}

protected override TestApp CreateTestFrameworkReferenceApp() => CreateFrameworkReferenceApp(HighWare, "1.1.1");


protected override void CustomizeDotNetWithNetCoreAppMicrosoftNETCoreApp(NetCoreAppBuilder builder)
{
builder
.WithPackage(TestVersionsPackage, "1.1.1", b => b
.WithAssemblyGroup(null, g => g
.WithAsset(TestAssemblyWithNoVersions + ".dll")
.WithAsset(TestAssemblyWithAssemblyVersion + ".dll", rf => rf.WithVersion("2.1.1.1", null))
.WithAsset(TestAssemblyWithFileVersion + ".dll", rf => rf.WithVersion(null, "3.2.2.2"))
.WithAsset(TestAssemblyWithBothVersions + ".dll", rf => rf.WithVersion("2.1.1.1", "3.2.2.2"))));
}

protected override void CustomizeDotNetWithNetCoreApp(DotNetBuilder builder)
{
builder.AddFramework(
HighWare,
"1.1.1",
runtimeConfig => runtimeConfig.WithFramework(MicrosoftNETCoreApp, "4.0.0"),
path => NetCoreAppBuilder.ForNETCoreApp(HighWare, RepoDirectories.TargetRID)
.WithProject(HighWare, "1.1.1", p => p
.WithAssemblyGroup(null, g => g
.WithAsset(TestAssemblyWithNoVersions + ".dll")
.WithAsset(TestAssemblyWithAssemblyVersion + ".dll", rf => rf.WithVersion("2.1.1.2", null))
.WithAsset(TestAssemblyWithFileVersion + ".dll", rf => rf.WithVersion(null, "3.2.2.2"))
.WithAsset(TestAssemblyWithBothVersions + ".dll", rf => rf.WithVersion("2.1.1.0", "3.2.2.0"))))
.Build(new TestApp(path, HighWare)));
}

public TestApp CreateTestFrameworkReferenceApp(Action<NetCoreAppBuilder> customizer)
{
TestApp testApp = FrameworkReferenceApp.Copy();
NetCoreAppBuilder builder = NetCoreAppBuilder.PortableForNETCoreApp(testApp);
builder.WithProject(p => p
.WithAssemblyGroup(null, g => g.WithMainAssembly()));
customizer(builder);
return builder.Build(testApp);
}
}
}

public class AppPerAssemblyVersionResolutionMultipleFrameworks :
PerAssemblyVersionResolutionMultipleFrameworksBase,
IClassFixture<PerAssemblyVersionResolutionMultipleFrameworksBase.SharedTestState>
{
public AppPerAssemblyVersionResolutionMultipleFrameworks(SharedTestState sharedState)
: base(sharedState)
{
}

protected override void RunTest(Action<RuntimeConfig> runtimeConfigCustomizer, string testAssemblyName, string appAsmVersion, string appFileVersion, string frameworkWins)
{
var app = SharedState.CreateTestFrameworkReferenceApp(b => b
.WithPackage(TestVersionsPackage, "1.0.0", lib => lib
.WithAssemblyGroup(null, g => g
.WithAsset(testAssemblyName + ".dll", rf => rf
.WithVersion(appAsmVersion, appFileVersion)))));
if (runtimeConfigCustomizer is not null)
{
var runtimeConfig = new RuntimeConfig(app.RuntimeConfigJson);
runtimeConfigCustomizer(runtimeConfig);
runtimeConfig.Save();
}

string expectedBaseLocation = frameworkWins switch
{
MicrosoftNETCoreApp => SharedState.DotNetWithNetCoreApp.GreatestVersionSharedFxPath,
HighWare => Path.Combine(SharedState.DotNetWithNetCoreApp.BinPath, "shared", HighWare, "1.1.1"),
_ => app.Location,
};
string expectedTestAssemblyPath = Path.Combine(expectedBaseLocation, testAssemblyName + ".dll");

SharedState.DotNetWithNetCoreApp.Exec(app.AppDll)
.EnableTracingAndCaptureOutputs()
.Execute()
.Should().Pass()
.And.HaveResolvedAssembly(expectedTestAssemblyPath);
}
}

public class ComponentPerAssemblyVersionResolutionMultipleFrameworks :
PerAssemblyVersionResolutionMultipleFrameworksBase,
IClassFixture<PerAssemblyVersionResolutionMultipleFrameworksBase.SharedTestState>
{
public ComponentPerAssemblyVersionResolutionMultipleFrameworks(SharedTestState sharedState)
: base(sharedState)
{
}

protected override void RunTest(Action<RuntimeConfig> runtimeConfigCustomizer, string testAssemblyName, string appAsmVersion, string appFileVersion, string frameworkWins)
{
var component = SharedState.CreateComponentWithNoDependencies(b => b
.WithPackage(TestVersionsPackage, "1.0.0", lib => lib
.WithAssemblyGroup(null, g => g
.WithAsset(testAssemblyName + ".dll", rf => rf
.WithVersion(appAsmVersion, appFileVersion)))));
if (runtimeConfigCustomizer is not null)
{
var runtimeConfig = RuntimeConfig.FromFile(component.RuntimeConfigJson);
runtimeConfigCustomizer(runtimeConfig);
runtimeConfig.Save();
}

// For component dependency resolution, frameworks are not considered, so the assembly from the component always wins
string expectedTestAssemblyPath = Path.Combine(component.Location, testAssemblyName + ".dll");

SharedState.RunComponentResolutionTest(component)
.Should().Pass()
.And.HaveSuccessfullyResolvedComponentDependencies()
.And.HaveResolvedComponentDependencyAssembly($"{component.AppDll};{expectedTestAssemblyPath}");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// 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 Microsoft.DotNet.Cli.Build.Framework;
using System;
using System.IO;
using System.Linq;
using Microsoft.DotNet.Cli.Build;
using Microsoft.DotNet.Cli.Build.Framework;

namespace Microsoft.DotNet.CoreSetup.Test.HostActivation.FrameworkResolution
{
Expand All @@ -24,18 +24,21 @@ protected CommandResult RunTest(
TestApp app,
TestSettings settings,
Action<CommandResult> resultAction = null,
bool multiLevelLookup = false)
bool? multiLevelLookup = false)
{
using (DotNetCliExtensions.DotNetCliCustomizer dotnetCustomizer = settings.DotnetCustomizer == null ? null : dotnet.Customize())
{
settings.DotnetCustomizer?.Invoke(dotnetCustomizer);

if (settings.RuntimeConfigCustomizer != null)
if (app is not null)
{
settings.RuntimeConfigCustomizer(RuntimeConfig.Path(app.RuntimeConfigJson)).Save();
}
if (settings.RuntimeConfigCustomizer != null)
{
settings.RuntimeConfigCustomizer(RuntimeConfig.Path(app.RuntimeConfigJson)).Save();
}

settings.WithCommandLine(app.AppDll);
settings.WithCommandLine(app.AppDll);
}

Command command = dotnet.Exec(settings.CommandLine.First(), settings.CommandLine.Skip(1).ToArray());

Expand Down Expand Up @@ -156,9 +159,18 @@ public TestApp CreateSelfContainedAppWithMockHostPolicy()

public void Dispose()
{
if (!TestArtifact.PreserveTestRuns() && Directory.Exists(_baseDir))
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
if (disposing)
{
Directory.Delete(_baseDir, true);
if (!TestArtifact.PreserveTestRuns() && Directory.Exists(_baseDir))
{
Directory.Delete(_baseDir, true);
}
}
}
}
Expand Down
Loading

0 comments on commit f82fe1d

Please sign in to comment.