-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
replace string with ProjectCode in HgService to force validation and …
…to avoid requiring validation anywhere in HgService. Also add some tests for ProjectCode validation. closes #823
- Loading branch information
Showing
4 changed files
with
121 additions
and
60 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,43 @@ | ||
using System.Text.RegularExpressions; | ||
|
||
namespace LexCore.Entities; | ||
|
||
public readonly partial record struct ProjectCode | ||
{ | ||
public ProjectCode() | ||
{ | ||
throw new NotSupportedException("Default constructor is not supported."); | ||
} | ||
|
||
public ProjectCode(string value) | ||
{ | ||
AssertIsSafeRepoName(value); | ||
Value = value; | ||
} | ||
|
||
public string Value { get; } | ||
public static implicit operator ProjectCode(string code) => new(code); | ||
|
||
public override string ToString() | ||
{ | ||
return Value; | ||
} | ||
|
||
public const string DELETED_REPO_FOLDER = "_____deleted_____"; | ||
public const string TEMP_REPO_FOLDER = "_____temp_____"; | ||
public static readonly string[] SpecialDirectoryNames = [DELETED_REPO_FOLDER, TEMP_REPO_FOLDER]; | ||
|
||
private static readonly HashSet<string> InvalidRepoNames = | ||
new([.. SpecialDirectoryNames, "api"], StringComparer.OrdinalIgnoreCase); | ||
|
||
private void AssertIsSafeRepoName(string name) | ||
{ | ||
if (InvalidRepoNames.Contains(name)) | ||
throw new ArgumentException($"Invalid repo name: {name}."); | ||
if (!ProjectCodeRegex().IsMatch(name)) | ||
throw new ArgumentException($"Invalid repo name: {name}."); | ||
} | ||
|
||
[GeneratedRegex(Project.ProjectCodeRegex)] | ||
private static partial Regex ProjectCodeRegex(); | ||
} |
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,35 @@ | ||
using LexCore.Entities; | ||
using Shouldly; | ||
|
||
namespace Testing.LexCore; | ||
|
||
public class ProjectCodeTests | ||
{ | ||
[Theory] | ||
[InlineData("_____deleted_____")] | ||
[InlineData("_____temp_____")] | ||
[InlineData("api")] | ||
[InlineData("../hacker")] | ||
[InlineData("hacker/test")] | ||
[InlineData("/hacker")] | ||
[InlineData(@"hacker\test")] | ||
[InlineData("❌")] | ||
[InlineData("!")] | ||
[InlineData("#")] | ||
[InlineData("-not-start-with-dash")] | ||
public void InvalidCodesThrows(string code) | ||
{ | ||
Assert.Throws<ArgumentException>(() => new ProjectCode(code)); | ||
} | ||
|
||
[Theory] | ||
[InlineData("test-name123")] | ||
[InlineData("123-name")] | ||
[InlineData("test")] | ||
public void ValidCodes(string code) | ||
{ | ||
var projectCode = new ProjectCode(code); | ||
projectCode.Value.ShouldBe(code); | ||
projectCode.ToString().ShouldBe(code); | ||
} | ||
} |