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

Commit 8e42a1e

Browse files
authored
Merge pull request #155 from github-for-unity/fixes/file-system-helper-empty-paths
Fix for FindCommonPath to checking for empty paths as well as null
2 parents a9481b5 + e424f46 commit 8e42a1e

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

src/GitHub.Api/IO/FileSystemHelpers.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@ static class FileSystemHelpers
77
{
88
public static string FindCommonPath(IEnumerable<string> paths)
99
{
10-
var pathsArray = paths.Where(s => s != null).Select(s => s.ToNPath().Parent).ToArray();
11-
var maxDepth = pathsArray.Max(path => path.Depth);
12-
var deepestPath = pathsArray.First(path => path.Depth == maxDepth);
10+
var parentPaths = paths.Where(s => !string.IsNullOrEmpty(s)).Select(s => s.ToNPath().Parent);
11+
if (!parentPaths.Any())
12+
return null;
13+
14+
var parentsArray = parentPaths.ToArray();
15+
var maxDepth = parentsArray.Max(path => path.Depth);
16+
var deepestPath = parentsArray.First(path => path.Depth == maxDepth);
1317

1418
var commonParent = deepestPath;
15-
foreach (var path in pathsArray)
19+
foreach (var path in parentsArray)
1620
{
1721
var cp = path.Elements.Any() ? commonParent.GetCommonParent(path) : null;
1822
if (cp != null)

src/tests/UnitTests/IO/FileSystemHelperTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,18 @@ public void TestFixtureTearDown()
2727
}
2828

2929
[Test]
30-
public void ShouldErrorIfEmpty()
30+
public void ShouldNotErrorIfEmpty()
3131
{
3232
Action item;
3333

3434
item = () => { FileSystemHelpers.FindCommonPath(new List<string>()); };
35-
item.ShouldThrow<InvalidOperationException>();
35+
item.ShouldNotThrow<InvalidOperationException>();
3636

3737
item = () => { FileSystemHelpers.FindCommonPath(new List<string> { null }); };
38-
item.ShouldThrow<InvalidOperationException>();
38+
item.ShouldNotThrow<InvalidOperationException>();
3939

4040
item = () => { FileSystemHelpers.FindCommonPath(new List<string> { "" }); };
41-
item.ShouldThrow<InvalidOperationException>();
41+
item.ShouldNotThrow<InvalidOperationException>();
4242
}
4343

4444
[Test]

0 commit comments

Comments
 (0)