Skip to content

Commit aaf3c3e

Browse files
committed
Add RegexSanitizeParticipant for comparison.
1 parent ed7fbb1 commit aaf3c3e

File tree

3 files changed

+72
-5
lines changed

3 files changed

+72
-5
lines changed

src/GitVersion.Core.Tests/Helpers/ParticipantConverterTests.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,50 @@ public void SanitizeValidParticipant_ShouldReturnExpectedResult(string input, st
4949
actual.ShouldBe(expected);
5050
}
5151

52+
[TestCase("feature/1234-is-id-with-something-kebab", "feature_1234_is_id_with_something_kebab")]
53+
[TestCase("feature/1234-IsSomethingPascalCase", "feature_1234_IsSomethingPascalCase")]
54+
[TestCase("feature/Caps-lower-something-kebab", "feature_Caps_lower_something_kebab")]
55+
[TestCase("feature/Caps-lower-is-kebab", "feature_Caps_lower_is_kebab")]
56+
[TestCase("kebab-folder/1234-is-id-with-something-kebab", "kebab_folder_1234_is_id_with_something_kebab")]
57+
[TestCase("kebab-folder/1234-IsSomethingPascalCase", "kebab_folder_1234_IsSomethingPascalCase")]
58+
[TestCase("kebab-folder/Caps-lower-something-kebab", "kebab_folder_Caps_lower_something_kebab")]
59+
[TestCase("kebab-folder/Caps-lower-is-kebab", "kebab_folder_Caps_lower_is_kebab")]
60+
[TestCase("PascalCaseFolder/1234-is-id-with-something-kebab", "PascalCaseFolder_1234_is_id_with_something_kebab")]
61+
[TestCase("PascalCaseFolder/1234-IsSomethingPascalCase", "PascalCaseFolder_1234_IsSomethingPascalCase")]
62+
[TestCase("PascalCaseFolder/Caps-lower-something-kebab", "PascalCaseFolder_Caps_lower_something_kebab")]
63+
[TestCase("PascalCaseFolder/Caps-lower-is-kebab", "PascalCaseFolder_Caps_lower_is_kebab")]
64+
[TestCase("1234-is-id-with-something-kebab", "1234_is_id_with_something_kebab")]
65+
[TestCase("1234-IsSomethingPascalCase", "1234_IsSomethingPascalCase")]
66+
[TestCase("Caps-lower-something-kebab", "Caps_lower_something_kebab")]
67+
[TestCase("Caps-lower-is-kebab", "Caps_lower_is_kebab")]
68+
[TestCase("feature/all-lower-is-kebab", "feature_all_lower_is_kebab")]
69+
[TestCase("feature/24321-Upperjustoneword", "feature_24321_Upperjustoneword")]
70+
[TestCase("feature/justoneword", "feature_justoneword")]
71+
[TestCase("feature/PascalCase", "feature_PascalCase")]
72+
[TestCase("feature/PascalCase-with-kebab", "feature_PascalCase_with_kebab")]
73+
[TestCase("feature/12414", "feature_12414")]
74+
[TestCase("feature/12414/12342-FeatureStoryTaskWithShortDescription", "feature_12414_12342_FeatureStoryTaskWithShortDescription")]
75+
[TestCase("feature/12414/12342-Short-description", "feature_12414_12342_Short_description")]
76+
[TestCase("feature/12414/12342-short-description", "feature_12414_12342_short_description")]
77+
[TestCase("feature/12414/12342-Short-Description", "feature_12414_12342_Short_Description")]
78+
[TestCase("release/1.0.0", "release_1_0_0")]
79+
[TestCase("releases", "releases")]
80+
[TestCase("feature", "feature")]
81+
[TestCase("feature/tfs1-Short-description", "feature_tfs1_Short_description")]
82+
[TestCase("feature/f2-Short-description", "feature_f2_Short_description")]
83+
[TestCase("feature/bug1", "feature_bug1")]
84+
[TestCase("f2", "f2")]
85+
[TestCase("feature/f2", "feature_f2")]
86+
[TestCase("feature/story2", "feature_story2")]
87+
[TestCase("master", "master")]
88+
[TestCase("develop", "develop")]
89+
[TestCase("main", "main")]
90+
public void RegexSanitizeValidParticipant_ShouldReturnExpectedResult(string input, string expected)
91+
{
92+
var actual = ParticipantSanitizer.RegexSanitizeParticipant(input);
93+
actual.ShouldBe(expected);
94+
}
95+
5296
[TestCase("")]
5397
[TestCase(" ")]
5498
public void SanitizeEmptyOrWhitespaceParticipant_ShouldThrow(string value)

src/GitVersion.Testing/Helpers/ParticipantSanitizer.cs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System;
2+
13
namespace GitVersion.Testing.Helpers;
24

35
public static class ParticipantSanitizer
@@ -11,11 +13,7 @@ public static class ParticipantSanitizer
1113
/// PascalCase. Otherwise, the input is returned unchanged.</returns>
1214
public static string SanitizeParticipant(string participant)
1315
{
14-
ArgumentException.ThrowIfNullOrWhiteSpace(participant);
15-
if (participant.EndsWith('/'))
16-
{
17-
throw new ArgumentException("The value cannot end with a folder separator ('/').", nameof(participant));
18-
}
16+
GuardAgainstInvalidParticipants(participant);
1917

2018
var folderIndex = participant.IndexOf('/');
2119
if (folderIndex > -1)
@@ -31,6 +29,13 @@ public static string SanitizeParticipant(string participant)
3129
return participant;
3230
}
3331

32+
public static string RegexSanitizeParticipant(string participant)
33+
{
34+
GuardAgainstInvalidParticipants(participant);
35+
36+
return RegexReplacer.NonAlphanumericRegex().Replace(participant, "_");
37+
}
38+
3439
private static string SplitOnFolderAndRecurseSuffix(int folderIndex, string input)
3540
{
3641
var folder = input[..folderIndex];
@@ -95,4 +100,13 @@ private static string ToPascalCase(string[] parts)
95100

96101
return sb.ToString();
97102
}
103+
104+
private static void GuardAgainstInvalidParticipants(string participant)
105+
{
106+
ArgumentException.ThrowIfNullOrWhiteSpace(participant);
107+
if (participant.EndsWith('/'))
108+
{
109+
throw new ArgumentException("The value cannot end with a folder separator ('/').", nameof(participant));
110+
}
111+
}
98112
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System.Text.RegularExpressions;
2+
3+
namespace GitVersion.Testing.Helpers;
4+
5+
public static partial class RegexReplacer
6+
{
7+
[GeneratedRegex("[^a-zA-Z0-9]")]
8+
public static partial Regex NonAlphanumericRegex();
9+
}

0 commit comments

Comments
 (0)