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

fix: handling of empty packages sequence #1195

Merged
merged 7 commits into from
Feb 26, 2024
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
16 changes: 13 additions & 3 deletions src/CTA.Rules.ProjectFile/ProjectFileCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,19 @@ private void UpdatePackageReferences()
//Select existing packages and deduplicate them by package name:
var existingPackages = _projectFileXml.Descendants()
.Where(d => d.Name == "PackageReference")
.Select(d => new { Name = d.Attributes("Include").First().Value, Version = d.Attributes("Version").First().Value })
.GroupBy(d => d.Name).Select(d => d.FirstOrDefault())
.ToDictionary(p => p.Name, p => p.Version);
.Select(d => new
{
Name = d.Attributes("Include")
.FirstOrDefault()?
.Value ?? "",
Version = d.Attributes("Version")
.FirstOrDefault()?
.Value ?? ""
})
.GroupBy(d => d.Name)
.Select(d => d.FirstOrDefault())
.ToDictionary(p => p.Name,
p => p.Version);

_packages = _packages.Where(p => existingPackages.Keys?.Contains(p.Key) == false).ToDictionary(d => d.Key, d => d.Value);

Expand Down
114 changes: 110 additions & 4 deletions tst/CTA.Rules.Test/Actions/ProjectFileActionsTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CTA.Rules.Actions;
using Amazon.Runtime.Internal.Transform;
using CTA.Rules.Actions;
using CTA.Rules.Config;
using CTA.Rules.Models;
using NUnit.Framework;
Expand Down Expand Up @@ -52,9 +53,101 @@ public void ProjectFileCreationWebClassLibrary()
StringAssert.Contains("Microsoft.AspNetCore.App", result);
}

private string CreateNewFile(ProjectType projectType, List<string> targetVersions, Dictionary<string, string> packageReferences, List<string> projectReferences)
[Test]
public void ProjectFileCreationNoPackages()
{
var result = CreateNewFile(
ProjectType.CoreMvc,
new List<string>() { SupportedFrameworks.Net8 },
new Dictionary<string, string>(), new List<string>(),
isNetCore: true);
StringAssert.Contains(SupportedFrameworks.Net8, result);
}

[Test]
public void ProjectFileCreationHandleExistingPackages()
{
var result = CreateNewFile(
ProjectType.CoreMvc,
new List<string>() { SupportedFrameworks.Net8 },
new Dictionary<string, string>()
{
{"Microsoft.EntityFrameworkCore.Design", "2.2.6"}
}, new List<string>(),
isNetCore: true);
StringAssert.Contains(SupportedFrameworks.Net8, result);
}

[Test]
public void ProjectFileCreationAddsPackages()
{
var result = CreateNewFile(
ProjectType.CoreMvc,
new List<string>() { SupportedFrameworks.Net8 },
new Dictionary<string, string>()
{
{"Newtonsoft.Json", "2.2.6"}
}, new List<string>(),
isNetCore: true);
StringAssert.Contains(SupportedFrameworks.Net8, result);
//verify both new and old package reference remain
StringAssert.Contains("<PackageReference Include=\"Newtonsoft.Json\" Version=\"2.2.6\" />", result);
StringAssert.Contains("<PackageReference Include=\"Microsoft.EntityFrameworkCore.Design\" Version=\"2.2.6\" />", result);
}

[Test]
public void ProjectFileCreationHandlesNoExistingPackages()
{
ResetProjectFile();
const string projectFile = @"
<Project Sdk=""Microsoft.NET.Sdk"">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
</Project>";

var result = CreateNewFile(
ProjectType.CoreMvc,
new List<string>() { SupportedFrameworks.Net8 },
new Dictionary<string, string>()
{
{"Newtonsoft.Json", "2.2.6"}
}, new List<string>(),
newContent: projectFile);
StringAssert.Contains(SupportedFrameworks.Net8, result);
//verify both new and old package reference remain
StringAssert.Contains("<PackageReference Include=\"Newtonsoft.Json\" Version=\"2.2.6\" />", result);
}

[Test]
public void ProjectFileCreationHandlePackageReferenceNoVersion()
{
const string projectFile = @"
<Project Sdk=""Microsoft.NET.Sdk"">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include=""Microsoft.EntityFrameworkCore.Design""/>
</ItemGroup>
</Project>";

var result = CreateNewFile(
ProjectType.CoreMvc,
new List<string>() { SupportedFrameworks.Net8 },
new Dictionary<string, string>()
{
{"Newtonsoft.Json", "2.2.6"}
}, new List<string>(),
newContent: projectFile);
StringAssert.Contains(SupportedFrameworks.Net8, result);
//verify both new and old package reference remain
StringAssert.Contains("<PackageReference Include=\"Newtonsoft.Json\" Version=\"2.2.6\" />", result);
StringAssert.Contains("<PackageReference Include=\"Microsoft.EntityFrameworkCore.Design\" />", result);
}

private string CreateNewFile(ProjectType projectType, List<string> targetVersions, Dictionary<string, string> packageReferences, List<string> projectReferences, bool isNetCore = false, string newContent="")
{
ResetProjectFile(newContent: newContent, isNetCore: isNetCore);
var migrationProjectFileAction = _projectFileActions.GetMigrateProjectFileAction("");
var metaRefs = new List<string>
{
Expand All @@ -64,12 +157,25 @@ private string CreateNewFile(ProjectType projectType, List<string> targetVersion
return string.Concat(result, File.ReadAllText(_projectFile));
}

private void ResetProjectFile(string newContent = "")
private void ResetProjectFile(string newContent = "", bool isNetCore = false)
{
if (!string.IsNullOrEmpty(newContent))
{
File.WriteAllText(_projectFile, newContent);
}
else if (isNetCore)
{
File.WriteAllText(_projectFile,
@"
<Project Sdk=""Microsoft.NET.Sdk"">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include=""Microsoft.EntityFrameworkCore.Design"" Version=""2.2.6"" />
</ItemGroup>
</Project>");
}
else
{
File.WriteAllText(_projectFile,
Expand Down
4 changes: 2 additions & 2 deletions tst/CTA.Rules.Test/WebFormsFullTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public void Setup()
[TestCase(SupportedFrameworks.Netcore31)]
[TestCase(SupportedFrameworks.Net5)]
[TestCase(SupportedFrameworks.Net6)]
[TestCase(SupportedFrameworks.Net7)]
public void TestProjectFilePortingResults(string version)
{
var results = _resultsDict[version];
Expand All @@ -59,9 +60,8 @@ public void TestProjectFilePortingResults(string version)
StringAssert.Contains(@"<PackageReference Include=""Microsoft.Data.SqlClient"" Version=""5.0.1"" />", webFormsFullResult.CsProjectContent);
}

[TestCase(SupportedFrameworks.Net7)]
[TestCase(SupportedFrameworks.Net8)]
public void TestProjectFilePortingResults_Dotnet7AndAbove(string version)
public void TestProjectFilePortingResults_Dotnet8AndAbove(string version)
{
var results = _resultsDict[version];
var webFormsFullResult = results.ProjectResults.First(proj => proj.CsProjectPath.EndsWith("WebFormsFull.csproj"));
Expand Down
4 changes: 2 additions & 2 deletions tst/CTA.Rules.Test/WebFormsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public void TestAspNetWebFormsProject(string version)
[TestCase(SupportedFrameworks.Netcore31)]
[TestCase(SupportedFrameworks.Net5)]
[TestCase(SupportedFrameworks.Net6)]
[TestCase(SupportedFrameworks.Net7)]
public void TestSolutionWithMvcAndDualWebForms(string version)
{
var results = _mvcAndDualWebFormsSolution[version];
Expand All @@ -101,9 +102,8 @@ public void TestSolutionWithMvcAndDualWebForms(string version)
VerifyMvc(someMvcResult);
}

[TestCase(SupportedFrameworks.Net7)]
[TestCase(SupportedFrameworks.Net8)]
public void TestSolutionWithMvcAndDualWebForms_Dotnet7AndAbove(string version)
public void TestSolutionWithMvcAndDualWebForms_Dotnet8AndAbove(string version)
{
var results = _mvcAndDualWebFormsSolution[version];

Expand Down
Loading