From e69a51b46e3f92ce4a52920c0a581c28ac87d2fc Mon Sep 17 00:00:00 2001 From: Sebastian Gomez Date: Mon, 9 Dec 2024 10:34:24 -0500 Subject: [PATCH 1/3] Add argument to parser to allow trailing comma in json. --- .../DiscoveryWorkerTests.GlobalJson.cs | 36 ++++++++++++++++++- .../NuGetUpdater.Core/Utilities/JsonHelper.cs | 3 +- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Discover/DiscoveryWorkerTests.GlobalJson.cs b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Discover/DiscoveryWorkerTests.GlobalJson.cs index 35c252d294..ed2ab846f4 100644 --- a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Discover/DiscoveryWorkerTests.GlobalJson.cs +++ b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Discover/DiscoveryWorkerTests.GlobalJson.cs @@ -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() { @@ -50,7 +84,7 @@ await TestDiscoveryAsync( ("global.json", """ { "sdk": { - "version": "2.2.104", + "version": "2.2.104", INVALID JSON }, "msbuild-sdks": { "Microsoft.Build.Traversal": "1.0.45" diff --git a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Utilities/JsonHelper.cs b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Utilities/JsonHelper.cs index ec1f10aef8..1f0586e34b 100644 --- a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Utilities/JsonHelper.cs +++ b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Utilities/JsonHelper.cs @@ -3,7 +3,6 @@ using System.Text.Encodings.Web; using System.Text.Json; using System.Text.Json.Nodes; - namespace NuGetUpdater.Core.Utilities { internal static class JsonHelper @@ -11,6 +10,7 @@ internal static class JsonHelper public static JsonDocumentOptions DocumentOptions { get; } = new JsonDocumentOptions { CommentHandling = JsonCommentHandling.Skip, + AllowTrailingCommas = true, }; public static JsonNode? ParseNode(string content) @@ -24,6 +24,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); From f5b4debcf5304ec92aa3792145b358a633e309a6 Mon Sep 17 00:00:00 2001 From: Sebastian Gomez Date: Mon, 9 Dec 2024 10:35:56 -0500 Subject: [PATCH 2/3] Nit. --- .../lib/NuGetUpdater/NuGetUpdater.Core/Utilities/JsonHelper.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Utilities/JsonHelper.cs b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Utilities/JsonHelper.cs index 1f0586e34b..5ac73cff53 100644 --- a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Utilities/JsonHelper.cs +++ b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Utilities/JsonHelper.cs @@ -3,6 +3,7 @@ using System.Text.Encodings.Web; using System.Text.Json; using System.Text.Json.Nodes; + namespace NuGetUpdater.Core.Utilities { internal static class JsonHelper From b681567cebc31077955df1ede6b63f6883a29a8e Mon Sep 17 00:00:00 2001 From: Sebastian Gomez Date: Mon, 9 Dec 2024 11:57:16 -0500 Subject: [PATCH 3/3] Add more test coverage --- .../DiscoveryWorkerTests.DotNetToolsJson.cs | 46 +++++++++- .../Update/UpdateWorkerTests.DotNetTools.cs | 84 +++++++++++++++++++ .../Update/UpdateWorkerTests.GlobalJson.cs | 66 +++++++++++++++ 3 files changed, 195 insertions(+), 1 deletion(-) diff --git a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Discover/DiscoveryWorkerTests.DotNetToolsJson.cs b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Discover/DiscoveryWorkerTests.DotNetToolsJson.cs index e7804d091b..aa73a9a039 100644 --- a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Discover/DiscoveryWorkerTests.DotNetToolsJson.cs +++ b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Discover/DiscoveryWorkerTests.DotNetToolsJson.cs @@ -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() { @@ -74,7 +118,7 @@ await TestDiscoveryAsync( "dotnetsay" ] } - } + } INVALID JSON } """), ], diff --git a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Update/UpdateWorkerTests.DotNetTools.cs b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Update/UpdateWorkerTests.DotNetTools.cs index 09c4cb1aa5..185f4db4f9 100644 --- a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Update/UpdateWorkerTests.DotNetTools.cs +++ b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Update/UpdateWorkerTests.DotNetTools.cs @@ -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: """ + + + net8.0 + + + + + + + """, + 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: """ + + + net8.0 + + + + + + + """, + // 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" + ] + } + } + } + """) + ] + ); + } } } diff --git a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Update/UpdateWorkerTests.GlobalJson.cs b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Update/UpdateWorkerTests.GlobalJson.cs index 8e00119fc6..9537f35fb6 100644 --- a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Update/UpdateWorkerTests.GlobalJson.cs +++ b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Update/UpdateWorkerTests.GlobalJson.cs @@ -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: """ + + + net8.0 + + + + + + + """, + additionalFiles: + [ + ("global.json", """ + { + "sdk": { + "version": "6.0.405", + "rollForward": "latestPatch" + }, + "msbuild-sdks": { + "Some.MSBuild.Sdk": "3.2.0" + }, + } + """) + ], + // expected + expectedProjectContents: """ + + + net8.0 + + + + + + + """, + // 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" + } + } + """) + ] + ); + } } }