From 0e8522505abf396a3725490b0ffc8447b35ac981 Mon Sep 17 00:00:00 2001 From: Nikolche Kolev Date: Tue, 2 Jul 2024 13:54:56 -0700 Subject: [PATCH 1/2] Add a test --- .../NuGet.LibraryModel.Tests/LibraryRangeTests.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/NuGet.Core.Tests/NuGet.LibraryModel.Tests/LibraryRangeTests.cs b/test/NuGet.Core.Tests/NuGet.LibraryModel.Tests/LibraryRangeTests.cs index 34ec3f8a035..00965ebb921 100644 --- a/test/NuGet.Core.Tests/NuGet.LibraryModel.Tests/LibraryRangeTests.cs +++ b/test/NuGet.Core.Tests/NuGet.LibraryModel.Tests/LibraryRangeTests.cs @@ -17,13 +17,14 @@ public class LibraryRangeTests [InlineData("[1.0.0 , 2.0.0]", "packageA >= 1.0.0 <= 2.0.0")] [InlineData("(1.0.0 , 2.0.0)", "packageA > 1.0.0 < 2.0.0")] [InlineData("(1.0.0 , 2.0.0]", "packageA > 1.0.0 <= 2.0.0")] + [InlineData(null, "")] public void LibraryRange_ToLockFileDependencyGroupString(string versionRange, string expected) { // Arrange LibraryRange range = new LibraryRange() { Name = "packageA", - VersionRange = VersionRange.Parse(versionRange), + VersionRange = versionRange != null ? VersionRange.Parse(versionRange) : null, TypeConstraint = LibraryDependencyTarget.Project }; From b65eac1de0d962e43e3422da979c0d07ef78741b Mon Sep 17 00:00:00 2001 From: Nikolche Kolev Date: Tue, 2 Jul 2024 15:29:49 -0700 Subject: [PATCH 2/2] Don't write nulls for LockFileDependencyGroupString --- .../NuGet.LibraryModel/LibraryRange.cs | 48 +++++++++---------- .../NuGet.LibraryModel/PublicAPI.Shipped.txt | 2 +- .../LibraryRangeTests.cs | 2 +- 3 files changed, 25 insertions(+), 27 deletions(-) diff --git a/src/NuGet.Core/NuGet.LibraryModel/LibraryRange.cs b/src/NuGet.Core/NuGet.LibraryModel/LibraryRange.cs index b6dec564528..1ce89227294 100644 --- a/src/NuGet.Core/NuGet.LibraryModel/LibraryRange.cs +++ b/src/NuGet.Core/NuGet.LibraryModel/LibraryRange.cs @@ -73,42 +73,40 @@ public override string ToString() return output; } - public string? ToLockFileDependencyGroupString() + public string ToLockFileDependencyGroupString() { - if (VersionRange is null) - { - return null; - } - StringBuilder sb = StringBuilderPool.Shared.Rent(256); sb.Append(Name); - if (VersionRange.HasLowerBound) + if (VersionRange != null) { - if (VersionRange.IsMinInclusive) - { - sb.Append(" >= "); - } - else + if (VersionRange.HasLowerBound) { - sb.Append(" > "); + if (VersionRange.IsMinInclusive) + { + sb.Append(" >= "); + } + else + { + sb.Append(" > "); + } + + if (VersionRange.IsFloating) + { + VersionRange.Float.ToString(sb); + } + else + { + sb.Append(VersionRange.MinVersion.ToNormalizedString()); + } } - if (VersionRange.IsFloating) + if (VersionRange.HasUpperBound) { - VersionRange.Float.ToString(sb); + sb.Append(VersionRange.IsMaxInclusive ? " <= " : " < "); + sb.Append(VersionRange.MaxVersion.ToNormalizedString()); } - else - { - sb.Append(VersionRange.MinVersion.ToNormalizedString()); - } - } - - if (VersionRange.HasUpperBound) - { - sb.Append(VersionRange.IsMaxInclusive ? " <= " : " < "); - sb.Append(VersionRange.MaxVersion.ToNormalizedString()); } return StringBuilderPool.Shared.ToStringAndReturn(sb); diff --git a/src/NuGet.Core/NuGet.LibraryModel/PublicAPI.Shipped.txt b/src/NuGet.Core/NuGet.LibraryModel/PublicAPI.Shipped.txt index ca845a4626b..94c06f6699d 100644 --- a/src/NuGet.Core/NuGet.LibraryModel/PublicAPI.Shipped.txt +++ b/src/NuGet.Core/NuGet.LibraryModel/PublicAPI.Shipped.txt @@ -121,7 +121,7 @@ NuGet.LibraryModel.LibraryRange.LibraryRange(string! name, NuGet.LibraryModel.Li NuGet.LibraryModel.LibraryRange.LibraryRange(string! name, NuGet.Versioning.VersionRange? versionRange, NuGet.LibraryModel.LibraryDependencyTarget typeConstraint) -> void NuGet.LibraryModel.LibraryRange.Name.get -> string! NuGet.LibraryModel.LibraryRange.Name.set -> void -NuGet.LibraryModel.LibraryRange.ToLockFileDependencyGroupString() -> string? +NuGet.LibraryModel.LibraryRange.ToLockFileDependencyGroupString() -> string! NuGet.LibraryModel.LibraryRange.TypeConstraint.get -> NuGet.LibraryModel.LibraryDependencyTarget NuGet.LibraryModel.LibraryRange.TypeConstraint.set -> void NuGet.LibraryModel.LibraryRange.TypeConstraintAllows(NuGet.LibraryModel.LibraryDependencyTarget flag) -> bool diff --git a/test/NuGet.Core.Tests/NuGet.LibraryModel.Tests/LibraryRangeTests.cs b/test/NuGet.Core.Tests/NuGet.LibraryModel.Tests/LibraryRangeTests.cs index 00965ebb921..a21661439d6 100644 --- a/test/NuGet.Core.Tests/NuGet.LibraryModel.Tests/LibraryRangeTests.cs +++ b/test/NuGet.Core.Tests/NuGet.LibraryModel.Tests/LibraryRangeTests.cs @@ -17,7 +17,7 @@ public class LibraryRangeTests [InlineData("[1.0.0 , 2.0.0]", "packageA >= 1.0.0 <= 2.0.0")] [InlineData("(1.0.0 , 2.0.0)", "packageA > 1.0.0 < 2.0.0")] [InlineData("(1.0.0 , 2.0.0]", "packageA > 1.0.0 <= 2.0.0")] - [InlineData(null, "")] + [InlineData(null, "packageA")] public void LibraryRange_ToLockFileDependencyGroupString(string versionRange, string expected) { // Arrange