< Summary - SonghayCore

Information
Class: Songhay.Extensions.DirectoryInfoExtensions
Assembly: SonghayCore
File(s): /home/rasx/sourceRoot/SonghayCore/SonghayCore/Extensions/DirectoryInfoExtensions.cs
Line coverage
35%
Covered lines: 14
Uncovered lines: 25
Coverable lines: 39
Total lines: 104
Line coverage: 35.8%
Branch coverage
25%
Covered branches: 6
Total branches: 24
Branch coverage: 25%
Method coverage

Method coverage is only available for sponsors.

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
FindDirectory(...)0%40%
FindFile(...)0%40%
GetParentDirectory(...)50%2100%
GetParentDirectoryInfo(...)83.33%6100%
ToCombinedPath(...)100%1100%
VerifyDirectory(...)0%80%

File(s)

/home/rasx/sourceRoot/SonghayCore/SonghayCore/Extensions/DirectoryInfoExtensions.cs

#LineLine coverage
 1namespace Songhay.Extensions;
 2
 3/// <summary>Extensions of <see cref="DirectoryInfo"/>.</summary>
 4public static class DirectoryInfoExtensions
 5{
 6    /// <summary>
 7    /// Finds the specified target <see cref="DirectoryInfo"/>
 8    /// under the specified root <see cref="DirectoryInfo"/>.
 9    /// </summary>
 10    /// <param name="directoryInfo">The specified root <see cref="DirectoryInfo"/>.</param>
 11    /// <param name="expectedDirectoryName">The specified target <see cref="DirectoryInfo.Name"/>.</param>
 12    public static DirectoryInfo? FindDirectory(this DirectoryInfo? directoryInfo, string? expectedDirectoryName)
 013    {
 014        expectedDirectoryName.ThrowWhenNullOrWhiteSpace();
 15
 016        if (directoryInfo == null)
 017            throw new DirectoryNotFoundException("The expected root directory is not here.");
 18
 019        if (!directoryInfo.Exists)
 020            throw new DirectoryNotFoundException("The expected root directory does not exist.");
 21
 022        return directoryInfo.GetDirectories(expectedDirectoryName).FirstOrDefault();
 023    }
 24
 25    /// <summary>
 26    /// Finds the specified <see cref="FileInfo"/>
 27    /// under the specified <see cref="DirectoryInfo"/>.
 28    /// </summary>
 29    /// <param name="directoryInfo">The specified <see cref="DirectoryInfo"/>.</param>
 30    /// <param name="expectedFileName">The specified <see cref="FileInfo.Name"/>.</param>
 31    public static FileInfo? FindFile(this DirectoryInfo? directoryInfo, string? expectedFileName)
 032    {
 033        expectedFileName.ThrowWhenNullOrWhiteSpace();
 34
 035        if (directoryInfo == null)
 036            throw new DirectoryNotFoundException("The expected directory is not here.");
 37
 038        if (!directoryInfo.Exists)
 039            throw new DirectoryNotFoundException("The expected directory does not exist.");
 40
 041        return directoryInfo.GetFiles(expectedFileName).FirstOrDefault();
 042    }
 43
 44    /// <summary>Gets the parent directory.</summary>
 45    /// <param name="directoryInfo">The specified <see cref="DirectoryInfo"/>.</param>
 46    /// <param name="levels">The levels.</param>
 47    /// <returns>Returns a <see cref="string"/> representing the directory.</returns>
 48    public static string? GetParentDirectory(this DirectoryInfo? directoryInfo, int levels) =>
 2449        directoryInfo.GetParentDirectoryInfo(levels)?.FullName;
 50
 51    /// <summary>Gets the parent <see cref="DirectoryInfo"/>.</summary>
 52    /// <param name="directoryInfo">The specified <see cref="DirectoryInfo"/>.</param>
 53    /// <param name="levels">The levels.</param>
 54    public static DirectoryInfo? GetParentDirectoryInfo(this DirectoryInfo? directoryInfo, int levels)
 8455    {
 8456        ArgumentNullException.ThrowIfNull(directoryInfo);
 57
 8458        levels = Math.Abs(levels);
 8559        if (levels == 0) return directoryInfo;
 60
 8361        var parentDirectoryInfo = directoryInfo.Parent;
 8362        if (parentDirectoryInfo == null) return directoryInfo;
 63
 8364        --levels;
 65
 8366        return levels >= 1 ? parentDirectoryInfo.GetParentDirectoryInfo(levels) : parentDirectoryInfo;
 8467    }
 68
 69    /// <summary>
 70    /// Combines path and root based
 71    /// on the current value of <see cref="Path.DirectorySeparatorChar"/>
 72    /// of the current OS or passes through a drive-letter rooted path.</summary>
 73    /// <param name="directoryInfo">The specified <see cref="DirectoryInfo"/>.</param>
 74    /// <param name="path">The path.</param>
 75    /// <remarks>
 76    /// For detail, see https://github.com/BryanWilhite/SonghayCore/issues/14
 77    /// and <see cref="ProgramFileUtility.GetCombinedPath(string, string)" />.
 78    /// </remarks>
 79    public static string ToCombinedPath(this DirectoryInfo? directoryInfo, string? path)
 2880    {
 2881        ArgumentNullException.ThrowIfNull(directoryInfo);
 82
 2883        return ProgramFileUtility.GetCombinedPath(directoryInfo.FullName, path);
 2884    }
 85
 86    /// <summary>
 87    /// Verifies the specified <see cref="DirectoryInfo"/>
 88    /// with conventional error handling.
 89    /// </summary>
 90    /// <param name="directoryInfo">The specified <see cref="DirectoryInfo"/>.</param>
 91    /// <param name="expectedDirectoryName">The expected directory name.</param>
 92    public static void VerifyDirectory(this DirectoryInfo? directoryInfo, string? expectedDirectoryName)
 093    {
 094        if (directoryInfo == null)
 095            throw new DirectoryNotFoundException("The expected directory is not here.");
 96
 097        if (!directoryInfo.Exists)
 098            throw new DirectoryNotFoundException("The expected directory does not exist.");
 99
 0100        if (!directoryInfo.Name.EqualsInvariant(expectedDirectoryName))
 0101            throw new DirectoryNotFoundException(
 0102                $"The expected directory is not here. [actual: {expectedDirectoryName ?? "[name]"}");
 0103    }
 104}