Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit a5aae89

Browse files
Moving logic to validate a new branch name to a testable location; Adding unit tests to check boundary conditions
1 parent aeaaf17 commit a5aae89

File tree

6 files changed

+77
-5
lines changed

6 files changed

+77
-5
lines changed

src/GitHub.Api/GitHub.Api.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
<Compile Include="Application\ApplicationManagerBase.cs" />
105105
<Compile Include="Helpers\Constants.cs" />
106106
<Compile Include="Cache\IBranchCache.cs" />
107+
<Compile Include="Helpers\RegularExpressions.cs" />
107108
<Compile Include="Platform\DefaultEnvironment.cs" />
108109
<Compile Include="Extensions\EnvironmentExtensions.cs" />
109110
<Compile Include="Extensions\FileEventExtensions.cs" />
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System.Text.RegularExpressions;
2+
3+
namespace GitHub.Unity.Helpers
4+
{
5+
public static class RegularExpressions
6+
{
7+
public static readonly Regex BranchNameRegex = new Regex(@"^(?<name>[\w\d\/\-_]+)$");
8+
}
9+
10+
public static class BranchNameValidator
11+
{
12+
public static bool IsBranchNameValid(string branchName)
13+
{
14+
return !string.IsNullOrEmpty(branchName)
15+
&& !branchName.StartsWith("/")
16+
&& !branchName.EndsWith("/")
17+
&& RegularExpressions.BranchNameRegex.IsMatch(branchName);
18+
}
19+
}
20+
}

src/UnityExtension/Assets/Editor/GitHub.Unity/Misc/Utility.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ class Utility : ScriptableObject
2828
public static readonly Regex StatusAheadBehindRegex =
2929
new Regex(
3030
@"\[ahead (?<ahead>\d+), behind (?<behind>\d+)\]|\[ahead (?<ahead>\d+)\]|\[behind (?<behind>\d+>)\]");
31-
public static readonly Regex BranchNameRegex = new Regex(@"^(?<name>[\w\d\/\-\_]+)$");
3231

3332
private static bool ready;
3433
private static Action onReady;

src/UnityExtension/Assets/Editor/GitHub.Unity/UI/BranchesView.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using GitHub.Unity.Helpers;
45
using UnityEditor;
56
using UnityEngine;
67
using Debug = System.Diagnostics.Debug;
@@ -453,10 +454,7 @@ private void OnCreateGUI()
453454
var cancelCreate = false;
454455
var cannotCreate = selectedNode == null ||
455456
selectedNode.Type == NodeType.Folder ||
456-
newBranchName == null ||
457-
newBranchName.StartsWith("/") ||
458-
newBranchName.EndsWith("/") ||
459-
!Utility.BranchNameRegex.IsMatch(newBranchName);
457+
!BranchNameValidator.IsBranchNameValid(newBranchName);
460458

461459
// Create on return/enter or cancel on escape
462460
var offsetID = GUIUtility.GetControlID(FocusType.Passive);
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System;
2+
using System.Linq;
3+
using FluentAssertions;
4+
using GitHub.Unity.Helpers;
5+
using NUnit.Framework;
6+
7+
namespace UnitTests
8+
{
9+
[TestFixture]
10+
public class BranchNameValidatorTests
11+
{
12+
[TestCase(true, "feature1", TestName = "Branch name is valid")]
13+
[TestCase(true, "feature-1", TestName = "Branch name with hyphen is valid")]
14+
[TestCase(true, "feature.1", TestName = "Branch name can contain dots")]
15+
[TestCase(true, "feature..1", TestName = "Branch name cannot contain consecutive dots")]
16+
[TestCase(false, "feature 1", TestName = "Branch name cannot contain a space")]
17+
[TestCase(false, "feature~1", TestName = "Branch name cannot contain a ~")]
18+
[TestCase(false, "feature^1", TestName = "Branch name cannot contain a ^")]
19+
[TestCase(false, "feature:1", TestName = "Branch name cannot contain a :")]
20+
[TestCase(false, "feature?1", TestName = "Branch name cannot contain a ?")]
21+
[TestCase(false, "feature*1", TestName = "Branch name cannot contain a *")]
22+
[TestCase(false, "feature[1", TestName = "Branch name cannot contain a [")]
23+
[TestCase(false, "/feature1", TestName = "Branch name cannot begin with a slash")]
24+
[TestCase(false, "feature1/", TestName = "Branch name cannot end with a slash")]
25+
[TestCase(false, "feature1.", TestName = "Branch name cannot end with a dot")]
26+
[TestCase(false, "feature1.lock", TestName = "Branch name cannot end with .lock")]
27+
[TestCase(true, "a", TestName = "Single character is valid")]
28+
[TestCase(false, "@", TestName = "Single character cannot be @")]
29+
[TestCase(false, ".", TestName = "Single character cannot be [period]")]
30+
[TestCase(true, "features/feature-1", TestName = "Folder and branch name is valid")]
31+
[TestCase(true, "features\\feature-1", TestName = "Backslash is not a valid character")]
32+
[TestCase(true, ".hidden", TestName = "Branch name is valid when starting with [period]")]
33+
[TestCase(false, ".features/feature-1", TestName = "Folder and branch name is not valid when starting with [period]")]
34+
[TestCase(false, "features//feature-1", TestName = "Multiple consecutive slashes are not valid")]
35+
[TestCase(false, null, TestName = "null string is not valid")]
36+
[TestCase(false, "", TestName = "Empty string is not valid")]
37+
[TestCase(false, "/", TestName = "Single slash is not valid")]
38+
[TestCase(false, "asdf@{", TestName = "Sequence @{ is not valid")]
39+
public void TestFeatureString(bool isValid, string branch)
40+
{
41+
BranchNameValidator.IsBranchNameValid(branch).Should().Be(isValid);
42+
}
43+
44+
[TestCase(true, 45, 45, 45, TestName = "Can test with ascii values")]
45+
[TestCase(false, 45, 45, 39, TestName = "No individual ASCII value should be < octal(40)")]
46+
[TestCase(false, 45, 45, 177, TestName = "No individual ASCII value should = octal(177)")]
47+
public void TestFeatureStringFromAsciiArray(bool isValid, params int[] asciiValues)
48+
{
49+
var branch = new string(asciiValues.Select(Convert.ToChar).ToArray());
50+
BranchNameValidator.IsBranchNameValid(branch).Should().Be(isValid);
51+
}
52+
}
53+
}

src/tests/UnitTests/UnitTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
</ItemGroup>
8282
<ItemGroup>
8383
<Compile Include="Authentication\KeychainTests.cs" />
84+
<Compile Include="Git\BranchNameValidationTests.cs" />
8485
<Compile Include="Git\GitConfigTests.cs" />
8586
<Compile Include="IO\BranchListOutputProcessorTests.cs" />
8687
<Compile Include="Extensions\EnvironmentExtensionTests.cs" />

0 commit comments

Comments
 (0)