Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep legacy version compliance checks in place for non-SemVer2 versions #3761

Merged
merged 5 commits into from
Apr 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you please leave a comment about the old clients scenario and why this is important

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done e6b2f1b

{
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm assuming there already is, but there should be a test case that checks that it returns no error if the SemVer2 version is complaint with 2.X clients.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SemVer2 cannot ever be compliant with 2.x clients :)

{
var nuspecStream = CreateNuspecStream(string.Format(NuSpecPlaceholderVersion, version));

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

[Fact]
public void ReturnsErrorIfDependencyVersionIsSemVer200WithMetadataPart()
{
Expand Down