From 42558faf0c7ae2376c59ebaf9da099cefa6079e5 Mon Sep 17 00:00:00 2001 From: Joel Verhagen Date: Tue, 24 Oct 2017 09:30:54 -0700 Subject: [PATCH] In feed2catalog, default to empty string if the version range is missing or invalid (#253) Fix https://github.com/NuGet/NuGetGallery/issues/3482 --- src/Catalog/Helpers/NuGetVersionUtility.cs | 6 +- src/Catalog/Helpers/XsltHelper.cs | 9 +- tests/CatalogTests/CatalogTests.csproj | 24 +++++ .../Helpers/NuGetVersionUtilityTests.cs | 21 ++++- tests/CatalogTests/PackageCatalogItemTests.cs | 4 + .../EmptyDependencyVersionRange.0.1.0.json | 84 ++++++++++++++++++ .../EmptyDependencyVersionRange.0.1.0.nupkg | Bin 0 -> 450 bytes .../InvalidDependencyVersionRange.0.1.0.json | 84 ++++++++++++++++++ .../InvalidDependencyVersionRange.0.1.0.nupkg | Bin 0 -> 473 bytes .../MissingDependencyVersionRange.0.1.0.json | 84 ++++++++++++++++++ .../MissingDependencyVersionRange.0.1.0.nupkg | Bin 0 -> 450 bytes ...hitespaceDependencyVersionRange.0.1.0.json | 84 ++++++++++++++++++ ...itespaceDependencyVersionRange.0.1.0.nupkg | Bin 0 -> 464 bytes 13 files changed, 394 insertions(+), 6 deletions(-) create mode 100644 tests/CatalogTests/TestData/EmptyDependencyVersionRange.0.1.0.json create mode 100644 tests/CatalogTests/TestData/EmptyDependencyVersionRange.0.1.0.nupkg create mode 100644 tests/CatalogTests/TestData/InvalidDependencyVersionRange.0.1.0.json create mode 100644 tests/CatalogTests/TestData/InvalidDependencyVersionRange.0.1.0.nupkg create mode 100644 tests/CatalogTests/TestData/MissingDependencyVersionRange.0.1.0.json create mode 100644 tests/CatalogTests/TestData/MissingDependencyVersionRange.0.1.0.nupkg create mode 100644 tests/CatalogTests/TestData/WhitespaceDependencyVersionRange.0.1.0.json create mode 100644 tests/CatalogTests/TestData/WhitespaceDependencyVersionRange.0.1.0.nupkg diff --git a/src/Catalog/Helpers/NuGetVersionUtility.cs b/src/Catalog/Helpers/NuGetVersionUtility.cs index e99457418..e4224b52b 100644 --- a/src/Catalog/Helpers/NuGetVersionUtility.cs +++ b/src/Catalog/Helpers/NuGetVersionUtility.cs @@ -21,13 +21,13 @@ public static string NormalizeVersion(string version) return parsedVersion.ToNormalizedString(); } - - public static string NormalizeVersionRange(string versionRange) + + public static string NormalizeVersionRange(string versionRange, string defaultValue) { VersionRange parsedVersionRange; if (!VersionRange.TryParse(versionRange, out parsedVersionRange)) { - return versionRange; + return defaultValue; } return parsedVersionRange.ToNormalizedString(); diff --git a/src/Catalog/Helpers/XsltHelper.cs b/src/Catalog/Helpers/XsltHelper.cs index 54f8c92a2..3d15b71e4 100644 --- a/src/Catalog/Helpers/XsltHelper.cs +++ b/src/Catalog/Helpers/XsltHelper.cs @@ -12,6 +12,13 @@ namespace NuGet.Services.Metadata.Catalog { public class XsltHelper { + /// + /// Default to an empty string if the dependency version range is invalid or missing. This is meant to be a + /// predictable signal to the client that they need to handle this invalid version case. The official NuGet + /// client treats this as a dependency of any version. + /// + private static readonly string DefaultVersionRange = string.Empty; + public XPathNavigator Split(string original) { char[] trimChar = { ',', ' ', '\t', '|', ';' }; @@ -52,7 +59,7 @@ public string GetFullVersionString(string original) public string NormalizeVersionRange(string original) { - return NuGetVersionUtility.NormalizeVersionRange(original); + return NuGetVersionUtility.NormalizeVersionRange(original, DefaultVersionRange); } public string IsPrerelease(string original) diff --git a/tests/CatalogTests/CatalogTests.csproj b/tests/CatalogTests/CatalogTests.csproj index b9990092a..15ff394b8 100644 --- a/tests/CatalogTests/CatalogTests.csproj +++ b/tests/CatalogTests/CatalogTests.csproj @@ -194,6 +194,24 @@ PreserveNewest + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + PreserveNewest @@ -224,6 +242,12 @@ PreserveNewest + + PreserveNewest + + + PreserveNewest + diff --git a/tests/CatalogTests/Helpers/NuGetVersionUtilityTests.cs b/tests/CatalogTests/Helpers/NuGetVersionUtilityTests.cs index 41277e82a..b7da54bff 100644 --- a/tests/CatalogTests/Helpers/NuGetVersionUtilityTests.cs +++ b/tests/CatalogTests/Helpers/NuGetVersionUtilityTests.cs @@ -34,13 +34,30 @@ public void NormalizeVersion(string input, string expected) [InlineData("invalid", "invalid")] public void NormalizeVersionRange(string input, string expected) { - // Arrange & Act - var actual = NuGetVersionUtility.NormalizeVersionRange(input); + // Arrange + var defaultValue = input; + + // Arrange + var actual = NuGetVersionUtility.NormalizeVersionRange(input, defaultValue); // Assert Assert.Equal(expected, actual); } + [Fact] + public void NormalizeVersionRange_UsesDifferentDefault() + { + // Arrange + var input = "invalid"; + var defaultValue = "(, )"; + + // Act + var actual = NuGetVersionUtility.NormalizeVersionRange(input, defaultValue); + + // Assert + Assert.Equal(defaultValue, actual); + } + [Theory] [InlineData("1.0.0-alpha.1", "1.0.0-alpha.1")] [InlineData("1.0.0-alpha+githash", "1.0.0-alpha+githash")] diff --git a/tests/CatalogTests/PackageCatalogItemTests.cs b/tests/CatalogTests/PackageCatalogItemTests.cs index 39cdef1aa..d43e43f0c 100644 --- a/tests/CatalogTests/PackageCatalogItemTests.cs +++ b/tests/CatalogTests/PackageCatalogItemTests.cs @@ -22,6 +22,10 @@ public class PackageCatalogItemTests [InlineData("OneValidDependencyOneEmptyId.0.1.0")] // One valid dependency and one with empty string ID [InlineData("OneValidDependencyOneEmptyIdWithGroups.0.1.0")] // Using dependency groups, one valid dependency and one with empty string ID [InlineData("WhitespaceDependencyId.0.1.0")] // One dependency with an ID only containing whitespace + [InlineData("EmptyDependencyVersionRange.0.1.0")] // A dependency with a version range that is an empty string + [InlineData("InvalidDependencyVersionRange.0.1.0")] //A dependency with a version range that is invalid + [InlineData("MissingDependencyVersionRange.0.1.0")] // A dependency with no version range attribute + [InlineData("WhitespaceDependencyVersionRange.0.1.0")] // A dependency with a version range that is whitespace public void CreateContent_ProducesExpectedJson(string packageName) { // Arrange diff --git a/tests/CatalogTests/TestData/EmptyDependencyVersionRange.0.1.0.json b/tests/CatalogTests/TestData/EmptyDependencyVersionRange.0.1.0.json new file mode 100644 index 000000000..b0dfaca17 --- /dev/null +++ b/tests/CatalogTests/TestData/EmptyDependencyVersionRange.0.1.0.json @@ -0,0 +1,84 @@ +{ + "@id": "http://example/data/2017.01.04.08.15.00/emptydependencyversionrange.0.1.0.json", + "@type": [ + "PackageDetails", + "catalog:Permalink" + ], + "authors": "EmptyDependencyVersionRange", + "catalog:commitId": "4aee0ef4-a039-4460-bd5f-98f944e33289", + "catalog:commitTimeStamp": "2017-01-04T08:15:00Z", + "created": "2017-01-01T08:15:00Z", + "description": "EmptyDependencyVersionRange", + "id": "EmptyDependencyVersionRange", + "isPrerelease": false, + "lastEdited": "2017-01-02T08:15:00Z", + "listed": true, + "packageHash": "J1NrM0aeDXk3kdjY3Aby8duP+mAt1JN/srVW5AMDIFXJXhWjZgmwAnAKBBrO6VDWXg2hi1x+uaWLHI0dUXraUg==", + "packageHashAlgorithm": "SHA512", + "packageSize": 450, + "published": "2017-01-03T08:15:00Z", + "title": "EmptyDependencyVersionRange", + "verbatimVersion": "0.1.0", + "version": "0.1.0", + "dependencyGroups": [ + { + "@id": "http://example/data/2017.01.04.08.15.00/emptydependencyversionrange.0.1.0.json#dependencygroup", + "@type": "PackageDependencyGroup", + "dependencies": [ + { + "@id": "http://example/data/2017.01.04.08.15.00/emptydependencyversionrange.0.1.0.json#dependencygroup/nuget.versioning", + "@type": "PackageDependency", + "id": "NuGet.Versioning", + "range": "" + } + ] + } + ], + "packageEntries": [ + { + "@id": "http://example/data/2017.01.04.08.15.00/emptydependencyversionrange.0.1.0.json#EmptyDependencyVersionRange.nuspec", + "@type": "PackageEntry", + "compressedLength": 248, + "fullName": "EmptyDependencyVersionRange.nuspec", + "length": 504, + "name": "EmptyDependencyVersionRange.nuspec" + } + ], + "@context": { + "@vocab": "http://schema.nuget.org/schema#", + "catalog": "http://schema.nuget.org/catalog#", + "xsd": "http://www.w3.org/2001/XMLSchema#", + "dependencies": { + "@id": "dependency", + "@container": "@set" + }, + "dependencyGroups": { + "@id": "dependencyGroup", + "@container": "@set" + }, + "packageEntries": { + "@id": "packageEntry", + "@container": "@set" + }, + "supportedFrameworks": { + "@id": "supportedFramework", + "@container": "@set" + }, + "tags": { + "@id": "tag", + "@container": "@set" + }, + "published": { + "@type": "xsd:dateTime" + }, + "created": { + "@type": "xsd:dateTime" + }, + "lastEdited": { + "@type": "xsd:dateTime" + }, + "catalog:commitTimeStamp": { + "@type": "xsd:dateTime" + } + } +} \ No newline at end of file diff --git a/tests/CatalogTests/TestData/EmptyDependencyVersionRange.0.1.0.nupkg b/tests/CatalogTests/TestData/EmptyDependencyVersionRange.0.1.0.nupkg new file mode 100644 index 0000000000000000000000000000000000000000..72de5fefd16786d6aa62a2902ad0bb88b0029a40 GIT binary patch literal 450 zcmWIWW@Zs#U|`^2aPW-qer@T^^aIHI!N|a%1f*Sa3rZ?oQVUY^Qd0AhE5lNYiZk=` zf)ex6Q}yyniwjbdYbV z$qDao-IkuZ<>v+24R82we3++U>(#l8eSKg>`mv5e|3fbN;%4rrey>`W8hg?pkx7{^ zqmnP#r&1x;^yT7Hw{CvBsXlAIg5!prm;UUVQgeABv+C!46PDIQcixFRsdQb}QYyjv zRmYk=^H%gL=UH=YsnS^>8M}xl=l8h-_3yi^B0lLYH!j}xaQ$WO2^x_keh=D+BwaUAMoyYAx6k6uwG`D1EXk-Yqrk>B!5xU)_ zo>*+g)ptbHK$?A8ZcyLfz5f2j@$&PtHouQDa+)9WL3FB50Mk8|*MHV6V$Rdg)9_re z_iFBkfQy_$cdtB8*UI|5tlXjK-tsQ9j*rtmr%1ghNt$b1>yg-frls$EPS_`JMb+s| zzZG2#a^~xtHF64JU$NG$cE+bSIe#Cx-?ge{7u<7p>9XfLpH2Q|9bhBes{1wjU{U?i z(>wkwIxKcAg+0Ub1>@`s%eVR*oHnI3rApk8Is2sY7xTRf_|hK#zusvPeew81xe21- zaq53}-|60y6cl58Fo}P0^Y?JCA4<>Fzs+C1xK%r@FS4FFz?+fDo&i^If&vr-6u=~L ufy)I|&B!3ZkTqxHsz*MT8J(qXJu&n|&;j18Y#<{Ufv^ln`vN17fdK$BxwTCI literal 0 HcmV?d00001 diff --git a/tests/CatalogTests/TestData/MissingDependencyVersionRange.0.1.0.json b/tests/CatalogTests/TestData/MissingDependencyVersionRange.0.1.0.json new file mode 100644 index 000000000..0482a483b --- /dev/null +++ b/tests/CatalogTests/TestData/MissingDependencyVersionRange.0.1.0.json @@ -0,0 +1,84 @@ +{ + "@id": "http://example/data/2017.01.04.08.15.00/missingdependencyversionrange.0.1.0.json", + "@type": [ + "PackageDetails", + "catalog:Permalink" + ], + "authors": "MissingDependencyVersionRange", + "catalog:commitId": "4aee0ef4-a039-4460-bd5f-98f944e33289", + "catalog:commitTimeStamp": "2017-01-04T08:15:00Z", + "created": "2017-01-01T08:15:00Z", + "description": "MissingDependencyVersionRange", + "id": "MissingDependencyVersionRange", + "isPrerelease": false, + "lastEdited": "2017-01-02T08:15:00Z", + "listed": true, + "packageHash": "7K0E8VKZX8of+E4LU2gpnQbjA74nubVDhVDPLOdiRuYyMecuOu3eZ33QmVJ+ZsqLpZB/jnVEUUmUvL7sA4Y+JA==", + "packageHashAlgorithm": "SHA512", + "packageSize": 450, + "published": "2017-01-03T08:15:00Z", + "title": "MissingDependencyVersionRange", + "verbatimVersion": "0.1.0", + "version": "0.1.0", + "dependencyGroups": [ + { + "@id": "http://example/data/2017.01.04.08.15.00/missingdependencyversionrange.0.1.0.json#dependencygroup", + "@type": "PackageDependencyGroup", + "dependencies": [ + { + "@id": "http://example/data/2017.01.04.08.15.00/missingdependencyversionrange.0.1.0.json#dependencygroup/nuget.versioning", + "@type": "PackageDependency", + "id": "NuGet.Versioning", + "range": "" + } + ] + } + ], + "packageEntries": [ + { + "@id": "http://example/data/2017.01.04.08.15.00/missingdependencyversionrange.0.1.0.json#MissingDependencyVersionRange.nuspec", + "@type": "PackageEntry", + "compressedLength": 244, + "fullName": "MissingDependencyVersionRange.nuspec", + "length": 501, + "name": "MissingDependencyVersionRange.nuspec" + } + ], + "@context": { + "@vocab": "http://schema.nuget.org/schema#", + "catalog": "http://schema.nuget.org/catalog#", + "xsd": "http://www.w3.org/2001/XMLSchema#", + "dependencies": { + "@id": "dependency", + "@container": "@set" + }, + "dependencyGroups": { + "@id": "dependencyGroup", + "@container": "@set" + }, + "packageEntries": { + "@id": "packageEntry", + "@container": "@set" + }, + "supportedFrameworks": { + "@id": "supportedFramework", + "@container": "@set" + }, + "tags": { + "@id": "tag", + "@container": "@set" + }, + "published": { + "@type": "xsd:dateTime" + }, + "created": { + "@type": "xsd:dateTime" + }, + "lastEdited": { + "@type": "xsd:dateTime" + }, + "catalog:commitTimeStamp": { + "@type": "xsd:dateTime" + } + } +} \ No newline at end of file diff --git a/tests/CatalogTests/TestData/MissingDependencyVersionRange.0.1.0.nupkg b/tests/CatalogTests/TestData/MissingDependencyVersionRange.0.1.0.nupkg new file mode 100644 index 0000000000000000000000000000000000000000..b86910a322dfc1ed9b975d745f311567108131b5 GIT binary patch literal 450 zcmWIWW@Zs#U|`^2DD;f*zI=UE=NBOFDOLs{vNX<)0%}cHfOD!tS z%+Cu-%u7$z%PTD|NKI~?Xy|vuK)|(HWV7XZc$Lw&vn3jn72 zYQHBbT*6NMCyq&)J=Z*YZ>#@24NHED#DbMqe(kziW4O?nxAR2Ky8B^TU$0L%{^`^_ z_B-6x&PNTnzZWjERZl)z7223+9=3ONu^0F%UpA{SIOBZCCPs>eGIJ@UEC=q!EfiJ>Qg V4)A7W0~yH(gk?b53>Zrc3;?_)t6cy9 literal 0 HcmV?d00001 diff --git a/tests/CatalogTests/TestData/WhitespaceDependencyVersionRange.0.1.0.json b/tests/CatalogTests/TestData/WhitespaceDependencyVersionRange.0.1.0.json new file mode 100644 index 000000000..f839fc8d2 --- /dev/null +++ b/tests/CatalogTests/TestData/WhitespaceDependencyVersionRange.0.1.0.json @@ -0,0 +1,84 @@ +{ + "@id": "http://example/data/2017.01.04.08.15.00/whitespacedependencyversionrange.0.1.0.json", + "@type": [ + "PackageDetails", + "catalog:Permalink" + ], + "authors": "WhitespaceDependencyVersionRange", + "catalog:commitId": "4aee0ef4-a039-4460-bd5f-98f944e33289", + "catalog:commitTimeStamp": "2017-01-04T08:15:00Z", + "created": "2017-01-01T08:15:00Z", + "description": "WhitespaceDependencyVersionRange", + "id": "WhitespaceDependencyVersionRange", + "isPrerelease": false, + "lastEdited": "2017-01-02T08:15:00Z", + "listed": true, + "packageHash": "+a19ygbSmG7q/Ehq8KjBSMf7ToLxNDUcYkkeDQ338dqm9FOsaICc0VemK9smCMcejAMaeWlV0va1u75LHJWBLw==", + "packageHashAlgorithm": "SHA512", + "packageSize": 464, + "published": "2017-01-03T08:15:00Z", + "title": "WhitespaceDependencyVersionRange", + "verbatimVersion": "0.1.0", + "version": "0.1.0", + "dependencyGroups": [ + { + "@id": "http://example/data/2017.01.04.08.15.00/whitespacedependencyversionrange.0.1.0.json#dependencygroup", + "@type": "PackageDependencyGroup", + "dependencies": [ + { + "@id": "http://example/data/2017.01.04.08.15.00/whitespacedependencyversionrange.0.1.0.json#dependencygroup/nuget.versioning", + "@type": "PackageDependency", + "id": "NuGet.Versioning", + "range": "" + } + ] + } + ], + "packageEntries": [ + { + "@id": "http://example/data/2017.01.04.08.15.00/whitespacedependencyversionrange.0.1.0.json#WhitespaceDependencyVersionRange.nuspec", + "@type": "PackageEntry", + "compressedLength": 252, + "fullName": "WhitespaceDependencyVersionRange.nuspec", + "length": 530, + "name": "WhitespaceDependencyVersionRange.nuspec" + } + ], + "@context": { + "@vocab": "http://schema.nuget.org/schema#", + "catalog": "http://schema.nuget.org/catalog#", + "xsd": "http://www.w3.org/2001/XMLSchema#", + "dependencies": { + "@id": "dependency", + "@container": "@set" + }, + "dependencyGroups": { + "@id": "dependencyGroup", + "@container": "@set" + }, + "packageEntries": { + "@id": "packageEntry", + "@container": "@set" + }, + "supportedFrameworks": { + "@id": "supportedFramework", + "@container": "@set" + }, + "tags": { + "@id": "tag", + "@container": "@set" + }, + "published": { + "@type": "xsd:dateTime" + }, + "created": { + "@type": "xsd:dateTime" + }, + "lastEdited": { + "@type": "xsd:dateTime" + }, + "catalog:commitTimeStamp": { + "@type": "xsd:dateTime" + } + } +} \ No newline at end of file diff --git a/tests/CatalogTests/TestData/WhitespaceDependencyVersionRange.0.1.0.nupkg b/tests/CatalogTests/TestData/WhitespaceDependencyVersionRange.0.1.0.nupkg new file mode 100644 index 0000000000000000000000000000000000000000..6922bafda5ea990120865041a3675f47c1525735 GIT binary patch literal 464 zcmWIWW@Zs#U|`^2xZ@S!tr6L<`45mM#Kgd$4y3~~GD}j63lfu4T~Z5D^HNgtk}Jbf zi;6Sz^MVrd(o^;FN{b6plY1vx`yDY5XscFvm$38r^P0A50#kGEx%Op~gx|lHTcTig zaHbyHnkPLfUKAJbR*H$Md=umCov;Z*y zHK_}$m@Q|fIK7a&X>s^j#L`>4f9?I+GiB3{7G}2Tzh>Q^<`?gq5#ZcpG*5j6_f)qF z6Hb4fZd<sX-7s74*;k+wNC&5 literal 0 HcmV?d00001