Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/GitHub.Api/IO/FileSystemHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ static class FileSystemHelpers
{
public static string FindCommonPath(IEnumerable<string> paths)
{
var pathsArray = paths.Where(s => s != null).Select(s => s.ToNPath().Parent).ToArray();
var maxDepth = pathsArray.Max(path => path.Depth);
var deepestPath = pathsArray.First(path => path.Depth == maxDepth);
var parentPaths = paths.Where(s => !string.IsNullOrEmpty(s)).Select(s => s.ToNPath().Parent);
if (!parentPaths.Any())
return null;

var parentsArray = parentPaths.ToArray();
var maxDepth = parentsArray.Max(path => path.Depth);
var deepestPath = parentsArray.First(path => path.Depth == maxDepth);

var commonParent = deepestPath;
foreach (var path in pathsArray)
foreach (var path in parentsArray)
{
var cp = path.Elements.Any() ? commonParent.GetCommonParent(path) : null;
if (cp != null)
Expand Down
8 changes: 4 additions & 4 deletions src/tests/UnitTests/IO/FileSystemHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ public void TestFixtureTearDown()
}

[Test]
public void ShouldErrorIfEmpty()
public void ShouldNotErrorIfEmpty()
{
Action item;

item = () => { FileSystemHelpers.FindCommonPath(new List<string>()); };
item.ShouldThrow<InvalidOperationException>();
item.ShouldNotThrow<InvalidOperationException>();

item = () => { FileSystemHelpers.FindCommonPath(new List<string> { null }); };
item.ShouldThrow<InvalidOperationException>();
item.ShouldNotThrow<InvalidOperationException>();

item = () => { FileSystemHelpers.FindCommonPath(new List<string> { "" }); };
item.ShouldThrow<InvalidOperationException>();
item.ShouldNotThrow<InvalidOperationException>();
}

[Test]
Expand Down