Skip to content

Commit

Permalink
Simplify parent lookup with Directory.GetParent (Kareadita#1455)
Browse files Browse the repository at this point in the history
* Simplify parent lookup with Directory.GetParent

* Address comments
  • Loading branch information
tjarls authored Aug 20, 2022
1 parent 66a9984 commit 329970f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 22 deletions.
31 changes: 31 additions & 0 deletions API.Tests/Services/DirectoryServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -963,5 +963,36 @@ public void GetAllDirectories_ShouldFindAllNestedDirectories()
}

#endregion

#region GetParentDirectory

[Theory]
[InlineData(@"C:/file.txt", "C:/")]
[InlineData(@"C:/folder/file.txt", "C:/folder")]
[InlineData(@"C:/folder/subfolder/file.txt", "C:/folder/subfolder")]
public void GetParentDirectoryName_ShouldFindParentOfFiles(string path, string expected)
{
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ path, new MockFileData(string.Empty)}
});

var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), fileSystem);
Assert.Equal(expected, ds.GetParentDirectoryName(path));
}
[Theory]
[InlineData(@"C:/folder", "C:/")]
[InlineData(@"C:/folder/subfolder", "C:/folder")]
[InlineData(@"C:/folder/subfolder/another", "C:/folder/subfolder")]
public void GetParentDirectoryName_ShouldFindParentOfDirectories(string path, string expected)
{
var fileSystem = new MockFileSystem();
fileSystem.AddDirectory(path);

var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), fileSystem);
Assert.Equal(expected, ds.GetParentDirectoryName(path));
}

#endregion
}
}
31 changes: 9 additions & 22 deletions API/Services/DirectoryService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
Expand Down Expand Up @@ -554,31 +554,18 @@ public IEnumerable<string> GetAllDirectories(string folderPath)
/// <summary>
/// Returns the parent directories name for a file or folder. Empty string is path is not valid.
/// </summary>
/// <remarks>This does touch I/O with an Attribute lookup</remarks>
/// <param name="fileOrFolder"></param>
/// <returns></returns>
public string GetParentDirectoryName(string fileOrFolder)
{
// TODO: Write Unit tests
try
{
var attr = File.GetAttributes(fileOrFolder);
var isDirectory = attr.HasFlag(FileAttributes.Directory);
if (isDirectory)
{
return Parser.Parser.NormalizePath(FileSystem.DirectoryInfo
.FromDirectoryName(fileOrFolder).Parent
.FullName);
}

return Parser.Parser.NormalizePath(FileSystem.FileInfo
.FromFileName(fileOrFolder).Directory.Parent
.FullName);
}
catch (Exception)
{
return string.Empty;
}
try
{
return Parser.Parser.NormalizePath(Directory.GetParent(fileOrFolder).FullName);
}
catch (Exception)
{
return string.Empty;
}
}

/// <summary>
Expand Down

0 comments on commit 329970f

Please sign in to comment.