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

Add argument to parser to allow trailing comma in json. #11086

Merged
merged 3 commits into from
Dec 10, 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
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,50 @@ await TestDiscoveryAsync(
);
}

[Fact]
public async Task DiscoversDependenciesTrailingComma()
{
await TestDiscoveryAsync(
packages: [],
workspacePath: "",
files: [
(".config/dotnet-tools.json", """
{
"version": 1,
"isRoot": true,
"tools": {
"botsay": {
"version": "1.0.0",
"commands": [
"botsay"
],
},
"dotnetsay": {
"version": "1.0.0",
"commands": [
"dotnetsay"
],
}
}
}
"""),
],
expectedResult: new()
{
Path = "",
DotNetToolsJson = new()
{
FilePath = ".config/dotnet-tools.json",
Dependencies = [
new("botsay", "1.0.0", DependencyType.DotNetTool),
new("dotnetsay", "1.0.0", DependencyType.DotNetTool),
]
},
ExpectedProjectCount = 0,
}
);
}

[Fact]
public async Task ReportsFailure()
{
Expand All @@ -74,7 +118,7 @@ await TestDiscoveryAsync(
"dotnetsay"
]
}
}
} INVALID JSON
}
"""),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,40 @@ await TestDiscoveryAsync(
);
}

[Fact]
public async Task DiscoversDependencies_HandlesTrailingComma()
{
await TestDiscoveryAsync(
packages: [],
workspacePath: "",
files: [
("global.json", """
{
"sdk": {
"version": "2.2.104"
},
"msbuild-sdks": {
"Microsoft.Build.Traversal": "1.0.45"
},
}
"""),
],
expectedResult: new()
{
Path = "",
GlobalJson = new()
{
FilePath = "global.json",
Dependencies = [
new("Microsoft.NET.Sdk", "2.2.104", DependencyType.MSBuildSdk),
new("Microsoft.Build.Traversal", "1.0.45", DependencyType.MSBuildSdk),
]
},
ExpectedProjectCount = 0,
}
);
}

[Fact]
public async Task ReportsFailure()
{
Expand All @@ -50,7 +84,7 @@ await TestDiscoveryAsync(
("global.json", """
{
"sdk": {
"version": "2.2.104",
"version": "2.2.104", INVALID JSON
sebasgomez238 marked this conversation as resolved.
Show resolved Hide resolved
},
"msbuild-sdks": {
"Microsoft.Build.Traversal": "1.0.45"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,5 +287,89 @@ await TestUpdateForProject("Some.DotNet.Tool", "1.0.0", "1.1.0",
]
);
}

[Fact]
public async Task UpdateSingleDependencyWithTrailingComma()
{
await TestUpdateForProject("Some.DotNet.Tool", "1.0.0", "1.1.0",
packages:
[
MockNuGetPackage.CreateSimplePackage("Some.Package", "13.0.3", "net8.0"),
MockNuGetPackage.CreateDotNetToolPackage("Some.DotNet.Tool", "1.0.0", "net8.0"),
MockNuGetPackage.CreateDotNetToolPackage("Some.DotNet.Tool", "1.1.0", "net8.0"),
],
// initial
projectContents: """
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Some.Package" Version="13.0.3" />
</ItemGroup>
</Project>
""",
additionalFiles:
[
(".config/dotnet-tools.json", """
{
"version": 1,
"isRoot": true,
"tools": {
"some.dotnet.tool": {
"version": "1.0.0",
"commands": [
"some.dotnet.tool"
],
},
"some-other-tool": {
"version": "2.1.3",
"commands": [
"some-other-tool"
],
}
}
}
""")
],
// expected
expectedProjectContents: """
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Some.Package" Version="13.0.3" />
</ItemGroup>
</Project>
""",
// expected files no longer have trailing commas in the json
additionalFilesExpected:
[
(".config/dotnet-tools.json", """
{
"version": 1,
"isRoot": true,
"tools": {
"some.dotnet.tool": {
"version": "1.1.0",
"commands": [
"some.dotnet.tool"
]
},
"some-other-tool": {
"version": "2.1.3",
"commands": [
"some-other-tool"
]
}
}
}
""")
]
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -226,5 +226,71 @@ await TestUpdateForProject("Some.MSBuild.Sdk", "3.2.0", "4.1.0",
]
);
}

[Fact]
public async Task UpdateDependencyWithTrailingComma()
{
await TestUpdateForProject("Some.MSBuild.Sdk", "3.2.0", "4.1.0",
packages:
[
MockNuGetPackage.CreateSimplePackage("Some.Package", "13.0.3", "net8.0"),
MockNuGetPackage.CreateMSBuildSdkPackage("Some.MSBuild.Sdk", "3.2.0"),
MockNuGetPackage.CreateMSBuildSdkPackage("Some.MSBuild.Sdk", "4.1.0"),
],
// initial
projectContents: """
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Some.Package" Version="13.0.3" />
</ItemGroup>
</Project>
""",
additionalFiles:
[
("global.json", """
{
"sdk": {
"version": "6.0.405",
"rollForward": "latestPatch"
},
"msbuild-sdks": {
"Some.MSBuild.Sdk": "3.2.0"
},
}
""")
],
// expected
expectedProjectContents: """
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Some.Package" Version="13.0.3" />
</ItemGroup>
</Project>
""",
// expected file no longer has the trailing comma because the parser removes it.
additionalFilesExpected:
[
("global.json", """
{
"sdk": {
"version": "6.0.405",
"rollForward": "latestPatch"
},
"msbuild-sdks": {
"Some.MSBuild.Sdk": "4.1.0"
}
}
""")
]
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ internal static class JsonHelper
public static JsonDocumentOptions DocumentOptions { get; } = new JsonDocumentOptions
{
CommentHandling = JsonCommentHandling.Skip,
AllowTrailingCommas = true,
};

public static JsonNode? ParseNode(string content)
Expand All @@ -24,6 +25,7 @@ public static string UpdateJsonProperty(string json, string[] propertyPath, stri
var readerOptions = new JsonReaderOptions
{
CommentHandling = JsonCommentHandling.Allow,
AllowTrailingCommas = true,
};
var bytes = Encoding.UTF8.GetBytes(json);
var reader = new Utf8JsonReader(bytes, readerOptions);
Expand Down
Loading