This repository has been archived by the owner on Jul 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b62d265
commit 376ac06
Showing
7 changed files
with
204 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Globalization; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace NuGet.Services.AzureSearch | ||
{ | ||
// TODO: Delete this copy of the PackageIdValidator. | ||
// Tracked by: https://github.com/NuGet/Engineering/issues/3669 | ||
// Forked from: https://github.com/NuGet/NuGet.Client/blob/18863da5be3dc8c7315f4416df1bc9ef96cb7446/src/NuGet.Core/NuGet.Packaging/PackageCreation/Utility/PackageIdValidator.cs | ||
public static class PackageIdValidator | ||
{ | ||
public const int MaxPackageIdLength = 100; | ||
private static readonly Regex IdRegex = new Regex(pattern: @"^\w+([.-]\w+)*$", | ||
options: RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture | RegexOptions.CultureInvariant, | ||
matchTimeout: TimeSpan.FromSeconds(15)); | ||
|
||
public static bool IsValidPackageIdWithTimeout(string packageId) | ||
{ | ||
if (packageId == null) | ||
{ | ||
throw new ArgumentNullException(nameof(packageId)); | ||
} | ||
return IdRegex.IsMatch(packageId); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
168 changes: 168 additions & 0 deletions
168
tests/NuGet.Services.AzureSearch.Tests/PackageIdValidatorFacts.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using Xunit; | ||
|
||
namespace NuGet.Services.AzureSearch | ||
{ | ||
// TODO: Delete this copy of the PackageIdValidator. | ||
// Tracked by: https://github.com/NuGet/Engineering/issues/3669 | ||
// Forked from: https://github.com/NuGet/NuGet.Client/blob/18863da5be3dc8c7315f4416df1bc9ef96cb7446/test/NuGet.Core.Tests/NuGet.Packaging.Test/PackageIdValidatorTest.cs#L8 | ||
public class PackageIdValidatorTest | ||
{ | ||
[Fact] | ||
public void EmptyIsNotValid() | ||
{ | ||
// Arrange | ||
string packageId = ""; | ||
|
||
// Act | ||
bool isValid = PackageIdValidator.IsValidPackageIdWithTimeout(packageId); | ||
|
||
// Assert | ||
Assert.False(isValid); | ||
} | ||
|
||
[Fact] | ||
public void NullThrowsException() | ||
{ | ||
// Arrange | ||
string packageId = null; | ||
|
||
// Act & Assert | ||
Assert.Throws<ArgumentNullException>(paramName: "packageId", | ||
testCode: () => PackageIdValidator.IsValidPackageIdWithTimeout(packageId)); | ||
} | ||
|
||
[Fact] | ||
public void AlphaNumericIsValid() | ||
{ | ||
// Arrange | ||
string packageId = "42This1Is4You"; | ||
|
||
// Act | ||
bool isValid = PackageIdValidator.IsValidPackageIdWithTimeout(packageId); | ||
|
||
// Assert | ||
Assert.True(isValid); | ||
} | ||
|
||
[Fact] | ||
public void MultipleDotSeparatorsAllowed() | ||
{ | ||
// Arrange | ||
string packageId = "I.Like.Writing.Unit.Tests"; | ||
|
||
// Act | ||
bool isValid = PackageIdValidator.IsValidPackageIdWithTimeout(packageId); | ||
|
||
// Assert | ||
Assert.True(isValid); | ||
} | ||
|
||
[Fact] | ||
public void NumbersAndWordsDotSeparatedAllowd() | ||
{ | ||
// Arrange | ||
string packageId = "1.2.3.4.Uno.Dos.Tres.Cuatro"; | ||
|
||
// Act | ||
bool isValid = PackageIdValidator.IsValidPackageIdWithTimeout(packageId); | ||
|
||
// Assert | ||
Assert.True(isValid); | ||
} | ||
|
||
[Fact] | ||
public void UnderscoreDotAndDashSeparatorsAreValid() | ||
{ | ||
// Arrange | ||
string packageId = "Nu_Get.Core-IsCool"; | ||
|
||
// Act | ||
bool isValid = PackageIdValidator.IsValidPackageIdWithTimeout(packageId); | ||
|
||
// Assert | ||
Assert.True(isValid); | ||
} | ||
|
||
[Fact] | ||
public void NonAlphaNumericUnderscoreDotDashIsInvalid() | ||
{ | ||
// Arrange | ||
string packageId = "ILike*Asterisks"; | ||
|
||
// Act | ||
bool isValid = PackageIdValidator.IsValidPackageIdWithTimeout(packageId); | ||
|
||
// Assert | ||
Assert.False(isValid); | ||
} | ||
|
||
[Fact] | ||
public void ConsecutiveSeparatorsNotAllowed() | ||
{ | ||
// Arrange | ||
string packageId = "I_.Like.-Separators"; | ||
|
||
// Act | ||
bool isValid = PackageIdValidator.IsValidPackageIdWithTimeout(packageId); | ||
|
||
// Assert | ||
Assert.False(isValid); | ||
} | ||
|
||
[Fact] | ||
public void StartingWithSeparatorsNotAllowed() | ||
{ | ||
// Arrange | ||
string packageId = "-StartWithSeparator"; | ||
|
||
// Act | ||
bool isValid = PackageIdValidator.IsValidPackageIdWithTimeout(packageId); | ||
|
||
// Assert | ||
Assert.False(isValid); | ||
} | ||
|
||
[Fact] | ||
public void EndingWithSeparatorsNotAllowed() | ||
{ | ||
// Arrange | ||
string packageId = "StartWithSeparator."; | ||
|
||
// Act | ||
bool isValid = PackageIdValidator.IsValidPackageIdWithTimeout(packageId); | ||
|
||
// Assert | ||
Assert.False(isValid); | ||
} | ||
|
||
[Fact] | ||
public void DotToolsIsNotAllowed() | ||
{ | ||
// Arrange | ||
string packageId = ".tools"; | ||
|
||
// Act | ||
bool isValid = PackageIdValidator.IsValidPackageIdWithTimeout(packageId); | ||
|
||
// Assert | ||
Assert.False(isValid); | ||
} | ||
|
||
[Fact] | ||
public void IsValidPackageId_PackageIdWithTwoUnderscores_Success() | ||
{ | ||
// Arrange | ||
string packageId = "Hello__World"; | ||
|
||
// Act | ||
bool isValid = PackageIdValidator.IsValidPackageIdWithTimeout(packageId); | ||
|
||
// Assert | ||
Assert.True(isValid); | ||
} | ||
} | ||
} |