From 5be87280bf4a2597d4c157324be30d204b50a871 Mon Sep 17 00:00:00 2001 From: Juan Carlos Fiorenzano Date: Wed, 6 Mar 2024 15:31:08 -0800 Subject: [PATCH] Ignore leading and trailing spaces on versions (#1025) --- .../pip/PythonVersionUtilities.cs | 5 +++-- .../PythonVersionTests.cs | 1 + 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.ComponentDetection.Detectors/pip/PythonVersionUtilities.cs b/src/Microsoft.ComponentDetection.Detectors/pip/PythonVersionUtilities.cs index cc8a275ba..46d9533c8 100644 --- a/src/Microsoft.ComponentDetection.Detectors/pip/PythonVersionUtilities.cs +++ b/src/Microsoft.ComponentDetection.Detectors/pip/PythonVersionUtilities.cs @@ -95,9 +95,10 @@ private static bool VersionValidForSpec(string version, string spec) } var op = spec[..i]; + var specVerSection = spec[i..].Trim(); var targetVer = PythonVersion.Create(version); - var specVer = PythonVersion.Create(spec[i..]); + var specVer = PythonVersion.Create(specVerSection); if (!targetVer.Valid) { @@ -106,7 +107,7 @@ private static bool VersionValidForSpec(string version, string spec) if (!specVer.Valid) { - throw new ArgumentException($"The version specification {spec[i..]} is not a valid python version"); + throw new ArgumentException($"The version specification {specVerSection} is not a valid python version"); } return op switch diff --git a/test/Microsoft.ComponentDetection.Detectors.Tests/PythonVersionTests.cs b/test/Microsoft.ComponentDetection.Detectors.Tests/PythonVersionTests.cs index 4a436e7b5..17ed3d2f1 100644 --- a/test/Microsoft.ComponentDetection.Detectors.Tests/PythonVersionTests.cs +++ b/test/Microsoft.ComponentDetection.Detectors.Tests/PythonVersionTests.cs @@ -86,6 +86,7 @@ public void TestVersionValidForSpec() IList<(IList, IList, IList)> testCases = new List<(IList, IList, IList)> { (new List { "==1.0" }, new List { "1.0" }, new List { "1.0.1", "2.0", "0.1" }), + (new List { "== 1.0 " }, new List { "1.0" }, new List { "1.0.1", "2.0", "0.1" }), (new List { "==1.4.*" }, new List { "1.4", "1.4.1", "1.4.2", "1.4.3" }, new List { "1.0.1", "2.0", "0.1", "1.5", "1.5.0" }), (new List { ">=1.0" }, new List { "1.0", "1.1", "1.5" }, new List { "0.9" }), (new List { ">=1.0", "<=1.4" }, new List { "1.0", "1.1", "1.4" }, new List { "0.9", "1.5" }),