From 1f28e381f2f1282efa040ad574b0ddd8c4b1708e Mon Sep 17 00:00:00 2001 From: Stanley Goldman Date: Mon, 31 Jul 2017 17:28:28 -0400 Subject: [PATCH 1/2] Checking for empty paths as well as null --- src/GitHub.Api/IO/FileSystemHelpers.cs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/GitHub.Api/IO/FileSystemHelpers.cs b/src/GitHub.Api/IO/FileSystemHelpers.cs index afa7cb417..fe16cdb6c 100644 --- a/src/GitHub.Api/IO/FileSystemHelpers.cs +++ b/src/GitHub.Api/IO/FileSystemHelpers.cs @@ -7,12 +7,16 @@ static class FileSystemHelpers { public static string FindCommonPath(IEnumerable 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) From e424f46e3caf99434f73aa56c2c35432b49a9349 Mon Sep 17 00:00:00 2001 From: Stanley Goldman Date: Mon, 31 Jul 2017 18:29:26 -0400 Subject: [PATCH 2/2] Empty paths test fix --- src/tests/UnitTests/IO/FileSystemHelperTests.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/tests/UnitTests/IO/FileSystemHelperTests.cs b/src/tests/UnitTests/IO/FileSystemHelperTests.cs index 9b4f8d22d..46408ea3b 100644 --- a/src/tests/UnitTests/IO/FileSystemHelperTests.cs +++ b/src/tests/UnitTests/IO/FileSystemHelperTests.cs @@ -27,18 +27,18 @@ public void TestFixtureTearDown() } [Test] - public void ShouldErrorIfEmpty() + public void ShouldNotErrorIfEmpty() { Action item; item = () => { FileSystemHelpers.FindCommonPath(new List()); }; - item.ShouldThrow(); + item.ShouldNotThrow(); item = () => { FileSystemHelpers.FindCommonPath(new List { null }); }; - item.ShouldThrow(); + item.ShouldNotThrow(); item = () => { FileSystemHelpers.FindCommonPath(new List { "" }); }; - item.ShouldThrow(); + item.ShouldNotThrow(); } [Test]