Skip to content

Commit

Permalink
make tests SDK agnostic
Browse files Browse the repository at this point in the history
  • Loading branch information
brettfo committed Dec 20, 2024
1 parent 3ea8ae7 commit 0095890
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,17 @@ public static MockNuGetPackage WellKnownReferencePackage(string packageName, str
return WellKnownPackages[key];
}

public static MockNuGetPackage GetMicrosoftNETCoreAppRefPackage(int majorRuntimeVersion)
{
return WellKnownReferencePackage("Microsoft.NETCore.App", $"net{majorRuntimeVersion}.0",
[
("data/FrameworkList.xml", Encoding.UTF8.GetBytes($"""
<FileList TargetFrameworkIdentifier=".NETCoreApp" TargetFrameworkVersion="{majorRuntimeVersion}.0" FrameworkName="Microsoft.NETCore.App" Name=".NET Runtime">
</FileList>
"""))
]);
}

public static MockNuGetPackage WellKnownHostPackage(string packageName, string targetFramework, (string Path, byte[] Content)[]? files = null)
{
string key = $"{packageName}/{targetFramework}";
Expand Down Expand Up @@ -437,34 +448,10 @@ public static MockNuGetPackage WellKnownHostPackage(string packageName, string t
WellKnownReferencePackage("Microsoft.AspNetCore.App", "net7.0"),
WellKnownReferencePackage("Microsoft.AspNetCore.App", "net8.0"),
WellKnownReferencePackage("Microsoft.AspNetCore.App", "net9.0"),
WellKnownReferencePackage("Microsoft.NETCore.App", "net6.0",
[
("data/FrameworkList.xml", Encoding.UTF8.GetBytes("""
<FileList TargetFrameworkIdentifier=".NETCoreApp" TargetFrameworkVersion="6.0" FrameworkName="Microsoft.NETCore.App" Name=".NET Runtime">
</FileList>
"""))
]),
WellKnownReferencePackage("Microsoft.NETCore.App", "net7.0",
[
("data/FrameworkList.xml", Encoding.UTF8.GetBytes("""
<FileList TargetFrameworkIdentifier=".NETCoreApp" TargetFrameworkVersion="7.0" FrameworkName="Microsoft.NETCore.App" Name=".NET Runtime">
</FileList>
"""))
]),
WellKnownReferencePackage("Microsoft.NETCore.App", "net8.0",
[
("data/FrameworkList.xml", Encoding.UTF8.GetBytes("""
<FileList TargetFrameworkIdentifier=".NETCoreApp" TargetFrameworkVersion="8.0" FrameworkName="Microsoft.NETCore.App" Name=".NET Runtime">
</FileList>
"""))
]),
WellKnownReferencePackage("Microsoft.NETCore.App", "net9.0",
[
("data/FrameworkList.xml", Encoding.UTF8.GetBytes("""
<FileList TargetFrameworkIdentifier=".NETCoreApp" TargetFrameworkVersion="9.0" FrameworkName="Microsoft.NETCore.App" Name=".NET Runtime">
</FileList>
"""))
]),
GetMicrosoftNETCoreAppRefPackage(6),
GetMicrosoftNETCoreAppRefPackage(7),
GetMicrosoftNETCoreAppRefPackage(8),
GetMicrosoftNETCoreAppRefPackage(9),
WellKnownReferencePackage("Microsoft.WindowsDesktop.App", "net6.0"),
WellKnownReferencePackage("Microsoft.WindowsDesktop.App", "net7.0"),
WellKnownReferencePackage("Microsoft.WindowsDesktop.App", "net8.0"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3491,16 +3491,20 @@ await TestUpdateForProject("Some.Package", "1.0.0", "1.1.0",
[Fact]
public async Task UpdateSdkManagedPackage_DirectDependency()
{
// To avoid a unit test that's tightly coupled to the installed SDK, the package correlation file is faked.
// Doing this requires a temporary file and environment variable override.
// To avoid a unit test that's tightly coupled to the installed SDK, several values are simulated,
// including the runtime major version, the current Microsoft.NETCore.App.Ref package, and the package
// correlation file. Doing this requires a temporary file and environment variable override.
var runtimeMajorVersion = Environment.Version.Major;
var netCoreAppRefPackage = MockNuGetPackage.GetMicrosoftNETCoreAppRefPackage(runtimeMajorVersion);
using var tempDirectory = new TemporaryDirectory();
var packageCorrelationFile = Path.Combine(tempDirectory.DirectoryPath, "dotnet-package-correlation.json");
await File.WriteAllTextAsync(packageCorrelationFile, """
await File.WriteAllTextAsync(packageCorrelationFile, $$"""
{
"Runtimes": {
"1": {
"{{runtimeMajorVersion}}.0.0": {
"Packages": {
"System.Text.Json": "8.0.98"
"{{netCoreAppRefPackage.Id}}": "{{netCoreAppRefPackage.Version}}",
"System.Text.Json": "{{runtimeMajorVersion}}.0.98"
}
}
}
Expand All @@ -3510,52 +3514,68 @@ await File.WriteAllTextAsync(packageCorrelationFile, """

// In the `packages` section below, we fake a `System.Text.Json` package with a low assembly version that
// will always trigger the replacement so that can be detected and then the equivalent version is pulled
// from the correlation file specified above. In the original project contents, package version `8.0.98`
// is reported which makes the update to `8.0.99` always possible.
await TestUpdateForProject("System.Text.Json", "8.0.0", "8.0.99",
// from the correlation file specified above. In the original project contents, package version `x.0.98`
// is reported which makes the update to `x.0.99` always possible.
await TestUpdateForProject("System.Text.Json", $"{runtimeMajorVersion}.0.98", $"{runtimeMajorVersion}.0.99",
experimentsManager: new ExperimentsManager() { UseDirectDiscovery = true, InstallDotnetSdks = true },
packages:
[
MockNuGetPackage.CreatePackageWithAssembly("System.Text.Json", "8.0.0", "net8.0", assemblyVersion: "8.0.0.0"), // this assembly version is lower than what the SDK will have
MockNuGetPackage.CreatePackageWithAssembly("System.Text.Json", "8.0.99", "net8.0", assemblyVersion: "8.99.99.99"), // this assembly version is greater than what the SDK will have
// this assembly version is lower than what the SDK will have
MockNuGetPackage.CreatePackageWithAssembly("System.Text.Json", $"{runtimeMajorVersion}.0.0", $"net{runtimeMajorVersion}.0", assemblyVersion: $"{runtimeMajorVersion}.0.0.0"),
// this assembly version is greater than what the SDK will have
MockNuGetPackage.CreatePackageWithAssembly("System.Text.Json", $"{runtimeMajorVersion}.0.99", $"net{runtimeMajorVersion}.0", assemblyVersion: $"{runtimeMajorVersion}.99.99.99"),
],
projectContents: """
projectContents: $"""
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net{runtimeMajorVersion}.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Text.Json" Version="8.0.0" />
<PackageReference Include="System.Text.Json" Version="{runtimeMajorVersion}.0.0" />
</ItemGroup>
</Project>
""",
expectedProjectContents: """
additionalFiles: [
("global.json", $$"""
{
"sdk": {
"version": "{{runtimeMajorVersion}}.0.100",
"allowPrerelease": true,
"rollForward": "latestMinor"
}
}
""")
],
expectedProjectContents: $"""
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net{runtimeMajorVersion}.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Text.Json" Version="8.0.99" />
<PackageReference Include="System.Text.Json" Version="{runtimeMajorVersion}.0.99" />
</ItemGroup>
</Project>
"""
);
}

[Fact(Skip = "https://github.com/dependabot/dependabot-core/issues/11140")]
[Fact]
public async Task UpdateSdkManagedPackage_TransitiveDependency()
{
// To avoid a unit test that's tightly coupled to the installed SDK, the package correlation file is faked.
// Doing this requires a temporary file and environment variable override. Note that SDK version 8.0.100
// or greater is required.
// To avoid a unit test that's tightly coupled to the installed SDK, several values are simulated,
// including the runtime major version, the current Microsoft.NETCore.App.Ref package, and the package
// correlation file. Doing this requires a temporary file and environment variable override.
var runtimeMajorVersion = Environment.Version.Major;
var netCoreAppRefPackage = MockNuGetPackage.GetMicrosoftNETCoreAppRefPackage(runtimeMajorVersion);
using var tempDirectory = new TemporaryDirectory();
var packageCorrelationFile = Path.Combine(tempDirectory.DirectoryPath, "dotnet-package-correlation.json");
await File.WriteAllTextAsync(packageCorrelationFile, """
await File.WriteAllTextAsync(packageCorrelationFile, $$"""
{
"Sdks": {
"8.0.100": {
"Runtimes": {
"{{runtimeMajorVersion}}.0.0": {
"Packages": {
"System.Text.Json": "8.0.98"
"{{netCoreAppRefPackage.Id}}": "{{netCoreAppRefPackage.Version}}",
"System.Text.Json": "{{runtimeMajorVersion}}.0.98"
}
}
}
Expand All @@ -3565,42 +3585,45 @@ await File.WriteAllTextAsync(packageCorrelationFile, """

// In the `packages` section below, we fake a `System.Text.Json` package with a low assembly version that
// will always trigger the replacement so that can be detected and then the equivalent version is pulled
// from the correlation file specified above. In the original project contents, package version `8.0.98`
// is reported which makes the update to `8.0.99` always possible.
await TestUpdateForProject("System.Text.Json", "8.0.98", "8.0.99",
// from the correlation file specified above. In the original project contents, package version `x.0.98`
// is reported which makes the update to `x.0.99` always possible.
await TestUpdateForProject("System.Text.Json", $"{runtimeMajorVersion}.0.98", $"{runtimeMajorVersion}.0.99",
isTransitive: true,
experimentsManager: new ExperimentsManager() { UseDirectDiscovery = true, InstallDotnetSdks = true },
packages:
[
MockNuGetPackage.CreateSimplePackage("Some.Package", "1.0.0", "net8.0", [(null, [("System.Text.Json", "[8.0.0]")])]),
MockNuGetPackage.CreateSimplePackage("Some.Package", "2.0.0", "net8.0", [(null, [("System.Text.Json", "[8.0.99]")])]),
MockNuGetPackage.CreatePackageWithAssembly("System.Text.Json", "8.0.0", "net8.0", assemblyVersion: "8.0.0.0"), // this assembly version is lower than what the SDK will have
MockNuGetPackage.CreatePackageWithAssembly("System.Text.Json", "8.0.99", "net8.0", assemblyVersion: "8.99.99.99"), // this assembly version is greater than what the SDK will have
MockNuGetPackage.CreateSimplePackage("Some.Package", "1.0.0", $"net{runtimeMajorVersion}.0", [(null, [("System.Text.Json", $"[{runtimeMajorVersion}.0.0]")])]),
MockNuGetPackage.CreateSimplePackage("Some.Package", "2.0.0", $"net{runtimeMajorVersion}.0", [(null, [("System.Text.Json", $"[{runtimeMajorVersion}.0.99]")])]),
// this assembly version is lower than what the SDK will have
MockNuGetPackage.CreatePackageWithAssembly("System.Text.Json", $"{runtimeMajorVersion}.0.0", $"net{runtimeMajorVersion}.0", assemblyVersion: $"{runtimeMajorVersion}.0.0.0"),
// this assembly version is greater than what the SDK will have
MockNuGetPackage.CreatePackageWithAssembly("System.Text.Json", $"{runtimeMajorVersion}.0.99", $"net{runtimeMajorVersion}.0", assemblyVersion: $"{runtimeMajorVersion}.99.99.99"),
],
projectContents: """
projectContents: $"""
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net{runtimeMajorVersion}.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Some.Package" Version="1.0.0" />
</ItemGroup>
</Project>
""",
additionalFiles: [
("global.json", """
("global.json", $$"""
{
"sdk": {
"version": "8.0.100",
"version": "{{runtimeMajorVersion}}.0.100",
"allowPrerelease": true,
"rollForward": "latestMinor"
}
}
""")
],
expectedProjectContents: """
expectedProjectContents: $"""
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net{runtimeMajorVersion}.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Some.Package" Version="2.0.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,8 @@ runtimePackageVersion is not null &&
var replacementPackageVersion = packageMapper.GetPackageVersionThatShippedWithOtherPackage(runtimePackageName, parsedRuntimePackageVersion, removedPackageName);
if (replacementPackageVersion is not null)
{
var packagesPerProject = packagesReplacedBySdkPerProject.GetOrAdd(projectEvaluation.ProjectFile, () => new(PathComparer.Instance));
var packagesPerTfm = packagesPerProject.GetOrAdd(tfm, () => new(StringComparer.OrdinalIgnoreCase));
var packagesPerThisProject = packagesReplacedBySdkPerProject.GetOrAdd(projectEvaluation.ProjectFile, () => new(PathComparer.Instance));
var packagesPerTfm = packagesPerThisProject.GetOrAdd(tfm, () => new(StringComparer.OrdinalIgnoreCase));
packagesPerTfm[removedPackageName] = replacementPackageVersion.ToString();
}
}
Expand Down

0 comments on commit 0095890

Please sign in to comment.