< Summary - Helper.Tests

Information
Class: Configuration.Helper.FileFolderInfo
Assembly: Configuration.Helper
File(s): D:\a\NuGetPackages\NuGetPackages\src\Helper\Configuration.Helper\FolderInfo\FileFolderInfo.cs
Tag: 3_8508158812
Line coverage
100%
Covered lines: 45
Uncovered lines: 0
Coverable lines: 45
Total lines: 100
Line coverage: 100%
Branch coverage
95%
Covered branches: 19
Total branches: 20
Branch coverage: 95%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor()100%11100%
Create(...)100%44100%
Initialize(...)100%1212100%
FormatLocation(...)75%44100%

File(s)

D:\a\NuGetPackages\NuGetPackages\src\Helper\Configuration.Helper\FolderInfo\FileFolderInfo.cs

#LineLine coverage
 1namespace Configuration.Helper;
 2
 3/// <summary>Folder Information factory implementation using a Windows file system.</summary>
 4public sealed class FileFolderInfo : FolderInfoBase
 5{
 6  #region Factory Pattern
 7
 8  /// <inheritdoc />
 229  private FileFolderInfo()
 4410  { }
 11
 12  /// <summary>Factory method to create an IFolderInfo object from a DirectoryInfo object.</summary>
 13  /// <param name="dirInfo">Root folder as a DirectoryInfo object.</param>
 14  /// <param name="includeFolders">Include sub-folder contents. The default is <see langword="false"/>.</param>
 15  /// <param name="filter">A string array that contains zero or more file name patterns.</param>
 16  /// <returns>A FileFolderInfo object.</returns>
 17  /// <exception cref="ArgumentNullException">Thrown if the required parameter is <see langword="null"/>.</exception>
 18  public static IFolderInfo Create( DirectoryInfo dirInfo,
 19    bool includeFolders = false, params string[] filter )
 520  {
 21    // Check the required parameter
 22    const string cMethod = @"FileFolderInfo.Create";
 723    if( null == dirInfo ) { throw new ArgumentNullException( nameof( dirInfo ), cMethod ); }
 24
 425    return dirInfo.Exists ? Initialize( dirInfo, string.Empty, includeFolders, filter ) : new FileFolderInfo();
 426  }
 27
 28  #endregion
 29
 30  #region Private Methods
 31
 32  /// <summary>
 33  /// Recursively called method to initialize a FileFolderInfo object for a DirectoryInfo object.
 34  /// </summary>
 35  /// <param name="dirInfo">Directory information object.</param>
 36  /// <param name="root">The root folder name.</param>
 37  /// <param name="includeFolders">Include sub-folder contents.</param>
 38  /// <param name="filter">A string array that contains zero or more file name patterns.</param>
 39  /// <returns>A FileFolderInfo object.</returns>
 40  private static FileFolderInfo Initialize( DirectoryInfo dirInfo, string root, bool includeFolders, params string[] fil
 2141  {
 2142    FileFolderInfo retValue = new() { Location = FormatLocation( dirInfo, ref root ) };
 43
 44    // Set a default filter if not supplied
 2145    if( filter.Length == 0 )
 1946    {
 1947      filter = new[] { "*.*" };
 1948    }
 49
 50    // Populate the file list using the filter
 10551    foreach( string ext in filter )
 2152    {
 29753      foreach( FileInfo fi in dirInfo.GetFiles( ext, SearchOption.TopDirectoryOnly ) )
 11754      {
 11755        if( !fi.Name.Contains( @".vshost.", StringComparison.CurrentCultureIgnoreCase ) ) // Ignore Visual Studio debugg
 11756        {
 11757          retValue.FileList.Add( fi.Name, fi.FullName );
 11758        }
 11759      }
 2160    }
 61
 2162    if( includeFolders )
 1963    {
 64      // Process the sub-folders
 9365      foreach( DirectoryInfo di in dirInfo.GetDirectories() )
 1866      {
 1867        retValue.FolderList.Add( di.Name, Initialize( di, root, true ) );
 1868      }
 1969    }
 70
 2171    retValue.Exists = dirInfo.Exists;
 2172    retValue.Source = IOHelper.CheckIfLocal( dirInfo.FullName );
 2173    return retValue;
 2174  }
 75
 76  /// <summary>Formats the folder location.</summary>
 77  /// <param name="dirInfo">Directory information object.</param>
 78  /// <param name="root">The root folder name.</param>
 79  /// <returns>A string containing the formatted folder location.</returns>
 80  private static string FormatLocation( FileSystemInfo dirInfo, ref string root )
 2181  {
 82    string retValue;
 2183    if( root.Length > 0 )
 1884    {
 1885      retValue = dirInfo.FullName.Replace( root, string.Empty );
 86
 87      // Remove leading separator
 1888      if( retValue.StartsWith( _separator ) ) { retValue = retValue[1..]; }
 1889    }
 90    else
 391    {
 392      retValue = dirInfo.FullName;
 393      root = retValue;
 394    }
 95
 2196    return retValue + _separator;
 2197  }
 98
 99  #endregion
 100}