Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor and improve MLL tests #66332

Merged
merged 4 commits into from
Mar 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
vitek-karas marked this conversation as resolved.
Show resolved Hide resolved
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