Skip to content

Commit

Permalink
Keep legacy version compliance checks in place for non-SemVer2 versio…
Browse files Browse the repository at this point in the history
…ns (#3761)

* Keep legacy version compatibility checks in place for non-semver2 versions

* Added comment to clarify the reasons behind the legacy version check.

* Fix typo

* Rename test for clarity

* code review feedback
  • Loading branch information
xavierdecoster committed May 16, 2017
1 parent 6055211 commit 52152db
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/NuGetGallery.Core/NuGetVersionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Text.RegularExpressions;
using NuGet.Versioning;

namespace NuGetGallery
Expand All @@ -22,9 +23,19 @@ public static string Normalize(string version)

public static class NuGetVersionExtensions
{
private const RegexOptions SemanticVersionRegexFlags = RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture;
private static readonly Regex SemanticVersionRegex = new Regex(@"^(?<Version>\d+(\s*\.\s*\d+){0,3})(?<Release>-[a-z][0-9a-z-]*)?$", SemanticVersionRegexFlags);

public static string ToNormalizedStringSafe(this NuGetVersion self)
{
return self != null ? self.ToNormalizedString() : String.Empty;
}

public static bool IsValidVersionForLegacyClients(this NuGetVersion self)
{
var match = SemanticVersionRegex.Match(self.ToString().Trim());

return match.Success;
}
}
}
28 changes: 27 additions & 1 deletion src/NuGetGallery.Core/Packaging/ManifestValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ private static IEnumerable<ValidationResult> ValidateCore(PackageMetadata packag
version));
}

var versionValidationResult = ValidateVersionForLegacyClients(packageMetadata.Version);
if (versionValidationResult != null)
{
yield return versionValidationResult;
}

// Check framework reference groups
var frameworkReferenceGroups = packageMetadata.GetFrameworkReferenceGroups();
if (frameworkReferenceGroups != null)
Expand Down Expand Up @@ -160,6 +166,26 @@ private static IEnumerable<ValidationResult> ValidateCore(PackageMetadata packag
}
}

/// <summary>
/// Checks whether the provided version is consumable by legacy 2.x clients,
/// which do not support a `.` in release labels, or release labels starting with numeric characters.
/// See also https://github.com/NuGet/NuGetGallery/issues/3226.
/// </summary>
/// <param name="version">The <see cref="NuGetVersion"/> to check for 2.x client compatibility.</param>
/// <returns>Returns a <see cref="ValidationResult"/> when non-compliant; otherwise <c>null</c>.</returns>
private static ValidationResult ValidateVersionForLegacyClients(NuGetVersion version)
{
if (!version.IsSemVer2 && !version.IsValidVersionForLegacyClients())
{
return new ValidationResult(string.Format(
CultureInfo.CurrentCulture,
CoreStrings.Manifest_InvalidVersion,
version));
}

return null;
}

private static ValidationResult ValidateDependencyVersion(NuGetVersion version)
{
if (version.HasMetadata)
Expand All @@ -170,7 +196,7 @@ private static ValidationResult ValidateDependencyVersion(NuGetVersion version)
version.ToFullString()));
}

return null;
return ValidateVersionForLegacyClients(version);
}

private static IEnumerable<ValidationResult> CheckUrls(params string[] urls)
Expand Down
10 changes: 10 additions & 0 deletions tests/NuGetGallery.Core.Facts/Packaging/ManifestValidatorFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,16 @@ public void ReturnsNoErrorIfDependencyVersionIsSemVer200WithoutMetadataPart()
Assert.Empty(GetErrors(nuspecStream));
}

[Theory]
[InlineData("1.0.0-10")]
[InlineData("1.0.0--")]
public void ReturnsErrorIfNonSemVer2VersionIsNotCompliantWith2XClients(string version)
{
var nuspecStream = CreateNuspecStream(string.Format(NuSpecPlaceholderVersion, version));

Assert.Equal(new[] {String.Format(CoreStrings.Manifest_InvalidVersion, version)}, GetErrors(nuspecStream));
}

[Fact]
public void ReturnsErrorIfDependencyVersionIsSemVer200WithMetadataPart()
{
Expand Down

0 comments on commit 52152db

Please sign in to comment.