Skip to content

Commit

Permalink
Handle semicolons in packageReferences (#10909)
Browse files Browse the repository at this point in the history
* Handle semicolons in packageReferences
  • Loading branch information
sebasgomez238 authored Nov 18, 2024
1 parent b7ae5f8 commit 4159f9d
Show file tree
Hide file tree
Showing 6 changed files with 424 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,106 @@ await TestDiscoveryAsync(
);
}

[Fact]
public async Task TestDependenciesSeparatedBySemicolon()
{
await TestDiscoveryAsync(
packages:
[
MockNuGetPackage.CreateSimplePackage("Some.Package", "9.0.1", "net8.0"),
MockNuGetPackage.CreateSimplePackage("Some.Package2", "9.0.1", "net8.0"),
],
workspacePath: "src",
files: new[]
{
("src/project.csproj", """
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<SomePackageVersion>9.0.1</SomePackageVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Some.Package;Some.Package2" Version="$(SomePackageVersion)" />
</ItemGroup>
</Project>
""")
},
expectedResult: new()
{
Path = "src",
Projects = [
new()
{
FilePath = "project.csproj",
TargetFrameworks = ["net8.0"],
ReferencedProjectPaths = [],
ExpectedDependencyCount = 3,
Dependencies = [
new("Microsoft.NET.Sdk", null, DependencyType.MSBuildSdk),
new("Some.Package", "9.0.1", DependencyType.PackageReference, TargetFrameworks: ["net8.0"], IsDirect: true),
new("Some.Package2", "9.0.1", DependencyType.PackageReference, TargetFrameworks: ["net8.0"], IsDirect: true),
],
Properties = [
new("SomePackageVersion", "9.0.1", "src/project.csproj"),
new("TargetFramework", "net8.0", "src/project.csproj"),
]
}
]
}
);
}

[Fact]
public async Task TestDependenciesSeparatedBySemicolonWithWhitespace()
{
await TestDiscoveryAsync(
packages:
[
MockNuGetPackage.CreateSimplePackage("Some.Package", "9.0.1", "net8.0"),
MockNuGetPackage.CreateSimplePackage("Some.Package2", "9.0.1", "net8.0"),
],
workspacePath: "src",
files: new[]
{
("src/project.csproj", """
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<SomePackageVersion>9.0.1</SomePackageVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include=" Some.Package ; Some.Package2 " Version="$(SomePackageVersion)" />
</ItemGroup>
</Project>
""")
},
expectedResult: new()
{
Path = "src",
Projects = [
new()
{
FilePath = "project.csproj",
TargetFrameworks = ["net8.0"],
ReferencedProjectPaths = [],
ExpectedDependencyCount = 3,
Dependencies = [
new("Microsoft.NET.Sdk", null, DependencyType.MSBuildSdk),
new("Some.Package", "9.0.1", DependencyType.PackageReference, TargetFrameworks: ["net8.0"], IsDirect: true),
new("Some.Package2", "9.0.1", DependencyType.PackageReference, TargetFrameworks: ["net8.0"], IsDirect: true),
],
Properties = [
new("SomePackageVersion", "9.0.1", "src/project.csproj"),
new("TargetFramework", "net8.0", "src/project.csproj"),
]
}
]
}
);
}

[Fact]
public async Task TestPackageConfig()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,256 @@ await File.WriteAllTextAsync(projectPath, """
);
}

[Fact]
public async Task UpdateHandlesSemicolonsInPackageReference()
{
var repoMetadata = XElement.Parse("""<repository type="git" url="https://nuget.example.com/some-package" />""");
var repoMetadata2 = XElement.Parse("""<repository type="git" url="https://nuget.example.com/some-package2" />""");
await RunAsync(
packages:
[
MockNuGetPackage.CreateSimplePackage("Some.Package", "1.0.0", "net8.0", additionalMetadata: [repoMetadata]),
MockNuGetPackage.CreateSimplePackage("Some.Package", "1.0.1", "net8.0", additionalMetadata: [repoMetadata]),
MockNuGetPackage.CreateSimplePackage("Some.Package2", "1.0.0", "net8.0", additionalMetadata: [repoMetadata2]),
MockNuGetPackage.CreateSimplePackage("Some.Package2", "1.0.1", "net8.0", additionalMetadata: [repoMetadata2]),
],
job: new Job()
{
PackageManager = "nuget",
Source = new()
{
Provider = "github",
Repo = "test/repo",
Directory = "some-dir",
},
AllowedUpdates =
[
new() { UpdateType = "all" }
]
},
files:
[
("some-dir/project.csproj", """
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Some.Package;Some.Package2" Version="1.0.0" />
</ItemGroup>
</Project>
""")
],
discoveryWorker: new TestDiscoveryWorker(_input =>
{
return Task.FromResult(new WorkspaceDiscoveryResult()
{
Path = "some-dir",
Projects =
[
new()
{
FilePath = "project.csproj",
TargetFrameworks = ["net8.0"],
Dependencies =
[
new("Some.Package", "1.0.0", DependencyType.PackageReference, TargetFrameworks: ["net8.0"]),
new("Some.Package2", "1.0.0", DependencyType.PackageReference, TargetFrameworks: ["net8.0"]),
]
}
]
});
}),
analyzeWorker: new TestAnalyzeWorker(input =>
{
return Task.FromResult(new AnalysisResult()
{
UpdatedVersion = "1.0.1",
CanUpdate = true,
UpdatedDependencies =
[
new("Some.Package", "1.0.1", DependencyType.Unknown, TargetFrameworks: ["net8.0"], InfoUrl: "https://nuget.example.com/some-package"),
new("Some.Package2", "1.0.1", DependencyType.Unknown, TargetFrameworks: ["net8.0"], InfoUrl: "https://nuget.example.com/some-package2"),
]
});
}),
updaterWorker: new TestUpdaterWorker(async input =>
{
Assert.Contains(input.Item3, ["Some.Package", "Some.Package2"]);
Assert.Equal("1.0.0", input.Item4);
Assert.Equal("1.0.1", input.Item5);
var projectPath = input.Item1 + input.Item2;
await File.WriteAllTextAsync(projectPath, """
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Some.Package;Some.Package2" Version="1.0.1" />
</ItemGroup>
</Project>
""");
return new UpdateOperationResult();
}),
expectedResult: new RunResult()
{
Base64DependencyFiles =
[
new DependencyFile()
{
Directory = "/some-dir",
Name = "project.csproj",
Content = Convert.ToBase64String(Encoding.UTF8.GetBytes("""
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Some.Package;Some.Package2" Version="1.0.0" />
</ItemGroup>
</Project>
"""))
}
],
BaseCommitSha = "TEST-COMMIT-SHA",
},
expectedApiMessages:
[
new UpdatedDependencyList()
{
Dependencies =
[
new ReportedDependency()
{
Name = "Some.Package",
Version = "1.0.0",
Requirements =
[
new ReportedRequirement()
{
Requirement = "1.0.0",
File = "/some-dir/project.csproj",
Groups = ["dependencies"],
}
]
},
new ReportedDependency()
{
Name = "Some.Package2",
Version = "1.0.0",
Requirements =
[
new ReportedRequirement()
{
Requirement = "1.0.0",
File = "/some-dir/project.csproj",
Groups = ["dependencies"],
}
]
},
],
DependencyFiles = ["/some-dir/project.csproj"],
},
new IncrementMetric()
{
Metric = "updater.started",
Tags = new()
{
["operation"] = "group_update_all_versions"
}
},
new CreatePullRequest()
{
Dependencies =
[
new ReportedDependency()
{
Name = "Some.Package",
Version = "1.0.1",
Requirements =
[
new ReportedRequirement()
{
Requirement = "1.0.1",
File = "/some-dir/project.csproj",
Groups = ["dependencies"],
Source = new()
{
SourceUrl = "https://nuget.example.com/some-package",
Type = "nuget_repo",
}
}
],
PreviousVersion = "1.0.0",
PreviousRequirements =
[
new ReportedRequirement()
{
Requirement = "1.0.0",
File = "/some-dir/project.csproj",
Groups = ["dependencies"],
}
],
},
new ReportedDependency()
{
Name = "Some.Package2",
Version = "1.0.1",
Requirements =
[
new ReportedRequirement()
{
Requirement = "1.0.1",
File = "/some-dir/project.csproj",
Groups = ["dependencies"],
Source = new()
{
SourceUrl = "https://nuget.example.com/some-package2",
Type = "nuget_repo",
}
}
],
PreviousVersion = "1.0.0",
PreviousRequirements =
[
new ReportedRequirement()
{
Requirement = "1.0.0",
File = "/some-dir/project.csproj",
Groups = ["dependencies"],
}
],
},
],
UpdatedDependencyFiles =
[
new DependencyFile()
{
Name = "project.csproj",
Directory = "/some-dir",
Content = """
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Some.Package;Some.Package2" Version="1.0.1" />
</ItemGroup>
</Project>
""",
}

],
BaseCommitSha = "TEST-COMMIT-SHA",
CommitMessage = "TODO: message",
PrTitle = "TODO: title",
PrBody = "TODO: body",
},
new MarkAsProcessed("TEST-COMMIT-SHA")
]
);
}

[Fact]
public async Task PrivateSourceAuthenticationFailureIsForwaredToApiHandler()
{
Expand Down
Loading

0 comments on commit 4159f9d

Please sign in to comment.