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

Update referenced projects during a run of NuGetUpdater. #9097

Merged
merged 6 commits into from
Feb 23, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,61 @@

using Xunit;

using TestFile = (string Path, string Content);
using TestProject = (string Path, string Content, System.Guid ProjectId);

namespace NuGetUpdater.Core.Test.Update;

public abstract class UpdateWorkerTestBase
{
protected static Task TestNoChange(
string dependencyName,
string oldVersion,
string newVersion,
bool useSolution,
string projectContents,
bool isTransitive = false,
(string Path, string Content)[]? additionalFiles = null,
string projectFilePath = "test-project.csproj")
{
return useSolution
? TestNoChangeforSolution(dependencyName, oldVersion, newVersion, projectFiles: [(projectFilePath, projectContents)], isTransitive, additionalFiles)
: TestNoChangeforProject(dependencyName, oldVersion, newVersion, projectContents, isTransitive, additionalFiles, projectFilePath);
}

protected static Task TestUpdate(
string dependencyName,
string oldVersion,
string newVersion,
bool useSolution,
string projectContents,
string expectedProjectContents,
bool isTransitive = false,
TestFile[]? additionalFiles = null,
TestFile[]? additionalFilesExpected = null,
string projectFilePath = "test-project.csproj")
{
return useSolution
? TestUpdateForSolution(dependencyName, oldVersion, newVersion, projectFiles: [(projectFilePath, projectContents)], projectFilesExpected: [(projectFilePath, expectedProjectContents)], isTransitive, additionalFiles, additionalFilesExpected)
: TestUpdateForProject(dependencyName, oldVersion, newVersion, projectFile: (projectFilePath, projectContents), expectedProjectContents, isTransitive, additionalFiles, additionalFilesExpected);
}

protected static Task TestUpdate(
string dependencyName,
string oldVersion,
string newVersion,
bool useSolution,
TestFile projectFile,
string expectedProjectContents,
bool isTransitive = false,
TestFile[]? additionalFiles = null,
TestFile[]? additionalFilesExpected = null)
{
return useSolution
? TestUpdateForSolution(dependencyName, oldVersion, newVersion, projectFiles: [projectFile], projectFilesExpected: [(projectFile.Path, expectedProjectContents)], isTransitive, additionalFiles, additionalFilesExpected)
: TestUpdateForProject(dependencyName, oldVersion, newVersion, projectFile, expectedProjectContents, isTransitive, additionalFiles, additionalFilesExpected);
}

protected static Task TestNoChangeforProject(
string dependencyName,
string oldVersion,
Expand All @@ -35,8 +86,8 @@ public abstract class UpdateWorkerTestBase
string projectContents,
string expectedProjectContents,
bool isTransitive = false,
(string Path, string Content)[]? additionalFiles = null,
(string Path, string Content)[]? additionalFilesExpected = null,
TestFile[]? additionalFiles = null,
TestFile[]? additionalFilesExpected = null,
string projectFilePath = "test-project.csproj")
=> TestUpdateForProject(
dependencyName,
Expand All @@ -52,42 +103,92 @@ public abstract class UpdateWorkerTestBase
string dependencyName,
string oldVersion,
string newVersion,
(string Path, string Content) projectFile,
TestFile projectFile,
string expectedProjectContents,
bool isTransitive = false,
(string Path, string Content)[]? additionalFiles = null,
(string Path, string Content)[]? additionalFilesExpected = null)
TestFile[]? additionalFiles = null,
TestFile[]? additionalFilesExpected = null)
{
additionalFiles ??= [];
additionalFilesExpected ??= [];

var projectFilePath = projectFile.Path;
var projectName = Path.GetFileNameWithoutExtension(projectFilePath);
var testFiles = new[] { projectFile }.Concat(additionalFiles).ToArray();

var actualResult = await RunUpdate(testFiles, async temporaryDirectory =>
{
var worker = new UpdaterWorker(new Logger(verbose: true));
await worker.RunAsync(temporaryDirectory, projectFilePath, dependencyName, oldVersion, newVersion, isTransitive);
});

var expectedResult = additionalFilesExpected.Prepend((projectFilePath, expectedProjectContents)).ToArray();

AssertContainsFiles(expectedResult, actualResult);
}

protected static Task TestNoChangeforSolution(
string dependencyName,
string oldVersion,
string newVersion,
TestFile[] projectFiles,
bool isTransitive = false,
TestFile[]? additionalFiles = null)
=> TestUpdateForSolution(
dependencyName,
oldVersion,
newVersion,
projectFiles,
projectFilesExpected: projectFiles,
isTransitive,
additionalFiles,
additionalFilesExpected: additionalFiles);

protected static async Task TestUpdateForSolution(
string dependencyName,
string oldVersion,
string newVersion,
TestFile[] projectFiles,
TestFile[] projectFilesExpected,
bool isTransitive = false,
TestFile[]? additionalFiles = null,
TestFile[]? additionalFilesExpected = null)
{
additionalFiles ??= [];
additionalFilesExpected ??= [];

var testProjects = projectFiles.Select(file => new TestProject(file.Path, file.Content, Guid.NewGuid())).ToArray();
var projectDeclarations = testProjects.Select(project => $$"""
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "{{Path.GetFileNameWithoutExtension(project.Path)}}", "{{project.Path}}", "{{project.ProjectId}}"
EndProject
""");
var debugConfiguration = testProjects.Select(project => $$"""
{{project.ProjectId}}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{{project.ProjectId}}.Debug|Any CPU.Build.0 = Debug|Any CPU
{{project.ProjectId}}..Release|Any CPU.ActiveCfg = Release|Any CPU
{{project.ProjectId}}..Release|Any CPU.Build.0 = Release|Any CPU
""");

var slnName = "test-solution.sln";
var slnContent = $$"""
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.22705.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "{{projectName}}", "{{projectFilePath}}", "{782E0C0A-10D3-444D-9640-263D03D2B20C}"
EndProject
{{string.Join(Environment.NewLine, projectDeclarations)}}
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{782E0C0A-10D3-444D-9640-263D03D2B20C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{782E0C0A-10D3-444D-9640-263D03D2B20C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{782E0C0A-10D3-444D-9640-263D03D2B20C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{782E0C0A-10D3-444D-9640-263D03D2B20C}.Release|Any CPU.Build.0 = Release|Any CPU
{{string.Join(Environment.NewLine, debugConfiguration)}}
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
""";
var testFiles = new[] { (slnName, slnContent), projectFile }.Concat(additionalFiles).ToArray();
var testFiles = new[] { (slnName, slnContent) }.Concat(projectFiles).Concat(additionalFiles).ToArray();

var actualResult = await RunUpdate(testFiles, async temporaryDirectory =>
{
Expand All @@ -96,7 +197,7 @@ public abstract class UpdateWorkerTestBase
await worker.RunAsync(temporaryDirectory, slnPath, dependencyName, oldVersion, newVersion, isTransitive);
});

var expectedResult = additionalFilesExpected.Prepend((projectFilePath, expectedProjectContents)).ToArray();
var expectedResult = projectFilesExpected.Concat(additionalFilesExpected).ToArray();

AssertContainsFiles(expectedResult, actualResult);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
Expand All @@ -24,7 +22,7 @@ public async Task UpdateSingleDependencyInDirsProj()
// initial
projectContents: """
<Project Sdk="Microsoft.Build.NoTargets">

<ItemGroup>
<ProjectReference Include="src/test-project.csproj" />
</ItemGroup>
Expand All @@ -39,7 +37,7 @@ public async Task UpdateSingleDependencyInDirsProj()
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
</ItemGroup>
Expand All @@ -49,7 +47,7 @@ public async Task UpdateSingleDependencyInDirsProj()
// expected
expectedProjectContents: """
<Project Sdk="Microsoft.Build.NoTargets">

<ItemGroup>
<ProjectReference Include="src/test-project.csproj" />
</ItemGroup>
Expand All @@ -64,7 +62,7 @@ public async Task UpdateSingleDependencyInDirsProj()
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>
Expand Down Expand Up @@ -101,7 +99,7 @@ public async Task UpdateSingleDependencyInNestedDirsProj()
// initial
projectContents: """
<Project Sdk="Microsoft.Build.NoTargets">

<ItemGroup>
<ProjectReference Include="src/dirs.proj" />
</ItemGroup>
Expand All @@ -113,7 +111,7 @@ public async Task UpdateSingleDependencyInNestedDirsProj()
("src/dirs.proj",
"""
<Project Sdk="Microsoft.Build.NoTargets">

<ItemGroup>
<ProjectReference Include="test-project/test-project.csproj" />
</ItemGroup>
Expand All @@ -126,7 +124,7 @@ public async Task UpdateSingleDependencyInNestedDirsProj()
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
</ItemGroup>
Expand All @@ -136,7 +134,7 @@ public async Task UpdateSingleDependencyInNestedDirsProj()
// expected
expectedProjectContents: """
<Project Sdk="Microsoft.Build.NoTargets">

<ItemGroup>
<ProjectReference Include="src/dirs.proj" />
</ItemGroup>
Expand All @@ -148,7 +146,7 @@ public async Task UpdateSingleDependencyInNestedDirsProj()
("src/dirs.proj",
"""
<Project Sdk="Microsoft.Build.NoTargets">

<ItemGroup>
<ProjectReference Include="test-project/test-project.csproj" />
</ItemGroup>
Expand All @@ -161,7 +159,7 @@ public async Task UpdateSingleDependencyInNestedDirsProj()
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>
Expand All @@ -177,7 +175,7 @@ public async Task UpdateSingleDependencyInNestedDirsProjUsingWildcard()
// initial
projectContents: """
<Project Sdk="Microsoft.Build.NoTargets">

<ItemGroup>
<ProjectReference Include="src/*.proj" />
</ItemGroup>
Expand All @@ -189,7 +187,7 @@ public async Task UpdateSingleDependencyInNestedDirsProjUsingWildcard()
("src/dirs.proj",
"""
<Project Sdk="Microsoft.Build.NoTargets">

<ItemGroup>
<ProjectReference Include="test-project/test-project.csproj" />
</ItemGroup>
Expand All @@ -202,7 +200,7 @@ public async Task UpdateSingleDependencyInNestedDirsProjUsingWildcard()
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
</ItemGroup>
Expand All @@ -212,7 +210,7 @@ public async Task UpdateSingleDependencyInNestedDirsProjUsingWildcard()
// expected
expectedProjectContents: """
<Project Sdk="Microsoft.Build.NoTargets">

<ItemGroup>
<ProjectReference Include="src/*.proj" />
</ItemGroup>
Expand All @@ -224,7 +222,7 @@ public async Task UpdateSingleDependencyInNestedDirsProjUsingWildcard()
("src/dirs.proj",
"""
<Project Sdk="Microsoft.Build.NoTargets">

<ItemGroup>
<ProjectReference Include="test-project/test-project.csproj" />
</ItemGroup>
Expand All @@ -237,7 +235,7 @@ public async Task UpdateSingleDependencyInNestedDirsProjUsingWildcard()
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>
Expand All @@ -253,7 +251,7 @@ public async Task UpdateSingleDependencyInNestedDirsProjUsingRecursiveWildcard()
// initial
projectContents: """
<Project Sdk="Microsoft.Build.NoTargets">

<ItemGroup>
<ProjectReference Include="**/*.proj" />
</ItemGroup>
Expand All @@ -265,7 +263,7 @@ public async Task UpdateSingleDependencyInNestedDirsProjUsingRecursiveWildcard()
("src/dirs.proj",
"""
<Project Sdk="Microsoft.Build.NoTargets">

<ItemGroup>
<ProjectReference Include="test-project/test-project.csproj" />
</ItemGroup>
Expand All @@ -278,7 +276,7 @@ public async Task UpdateSingleDependencyInNestedDirsProjUsingRecursiveWildcard()
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
</ItemGroup>
Expand All @@ -288,7 +286,7 @@ public async Task UpdateSingleDependencyInNestedDirsProjUsingRecursiveWildcard()
// expected
expectedProjectContents: """
<Project Sdk="Microsoft.Build.NoTargets">

<ItemGroup>
<ProjectReference Include="**/*.proj" />
</ItemGroup>
Expand All @@ -300,7 +298,7 @@ public async Task UpdateSingleDependencyInNestedDirsProjUsingRecursiveWildcard()
("src/dirs.proj",
"""
<Project Sdk="Microsoft.Build.NoTargets">

<ItemGroup>
<ProjectReference Include="test-project/test-project.csproj" />
</ItemGroup>
Expand All @@ -313,7 +311,7 @@ public async Task UpdateSingleDependencyInNestedDirsProjUsingRecursiveWildcard()
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>
Expand Down