< Summary

Information
Class: Renci.SshNet.Common.PosixPath
Assembly: Renci.SshNet
File(s): \home\appveyor\projects\ssh-net\src\Renci.SshNet\Common\PosixPath.cs
Line coverage
100%
Covered lines: 65
Uncovered lines: 0
Coverable lines: 65
Total lines: 152
Line coverage: 100%
Branch coverage
100%
Covered branches: 26
Total branches: 26
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
.ctor()100%1100%
get_Directory()100%1100%
get_File()100%1100%
CreateAbsoluteOrRelativeFilePath(...)100%12100%
GetFileName(...)100%6100%
GetDirectoryName(...)100%8100%

File(s)

\home\appveyor\projects\ssh-net\src\Renci.SshNet\Common\PosixPath.cs

#LineLine coverage
 1using System;
 2
 3namespace Renci.SshNet.Common
 4{
 5    /// <summary>
 6    /// Represents a POSIX path.
 7    /// </summary>
 8    internal sealed class PosixPath
 9    {
 29010        private PosixPath()
 29011        {
 29012        }
 13
 14        /// <summary>
 15        /// Gets the directory of the path.
 16        /// </summary>
 17        /// <value>
 18        /// The directory of the path.
 19        /// </value>
 57420        public string Directory { get; private set; }
 21
 22        /// <summary>
 23        /// Gets the file part of the path.
 24        /// </summary>
 25        /// <value>
 26        /// The file part of the path, or <see langword="null"/> if the path represents a directory.
 27        /// </value>
 70528        public string File { get; private set; }
 29
 30        /// <summary>
 31        /// Create a <see cref="PosixPath"/> from the specified path.
 32        /// </summary>
 33        /// <param name="path">The path.</param>
 34        /// <returns>
 35        /// A <see cref="PosixPath"/> created from the specified path.
 36        /// </returns>
 37        /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>
 38        /// <exception cref="ArgumentException"><paramref name="path"/> is empty ("").</exception>
 39        public static PosixPath CreateAbsoluteOrRelativeFilePath(string path)
 29340        {
 29341            if (path is null)
 342            {
 343                throw new ArgumentNullException(nameof(path));
 44            }
 45
 29046            var posixPath = new PosixPath();
 47
 29048            var pathEnd = path.LastIndexOf('/');
 29049            if (pathEnd == -1)
 5250            {
 5251                if (path.Length == 0)
 352                {
 353                    throw new ArgumentException("The path is a zero-length string.", nameof(path));
 54                }
 55
 4956                posixPath.Directory = ".";
 4957                posixPath.File = path;
 4958            }
 23859            else if (pathEnd == 0)
 1560            {
 1561                posixPath.Directory = "/";
 1562                if (path.Length > 1)
 1263                {
 1264                    posixPath.File = path.Substring(pathEnd + 1);
 1265                }
 1566            }
 67            else
 22368            {
 22369                posixPath.Directory = path.Substring(0, pathEnd);
 22370                if (pathEnd < path.Length - 1)
 22071                {
 22072                    posixPath.File = path.Substring(pathEnd + 1);
 22073                }
 22374            }
 75
 28776            return posixPath;
 28777        }
 78
 79        /// <summary>
 80        /// Gets the file name part of a given POSIX path.
 81        /// </summary>
 82        /// <param name="path">The POSIX path to get the file name for.</param>
 83        /// <returns>
 84        /// The file name part of <paramref name="path"/>.
 85        /// </returns>
 86        /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>
 87        /// <remarks>
 88        /// <para>
 89        /// If <paramref name="path"/> contains no forward slash, then <paramref name="path"/>
 90        /// is returned.
 91        /// </para>
 92        /// <para>
 93        /// If path has a trailing slash, <see cref="GetFileName(string)"/> return a zero-length string.
 94        /// </para>
 95        /// </remarks>
 96        public static string GetFileName(string path)
 4097        {
 4098            if (path is null)
 399            {
 3100                throw new ArgumentNullException(nameof(path));
 101            }
 102
 37103            var pathEnd = path.LastIndexOf('/');
 37104            if (pathEnd == -1)
 10105            {
 10106                return path;
 107            }
 108
 27109            if (pathEnd == path.Length - 1)
 6110            {
 6111                return string.Empty;
 112            }
 113
 21114            return path.Substring(pathEnd + 1);
 37115        }
 116
 117        /// <summary>
 118        /// Gets the directory name part of a given POSIX path.
 119        /// </summary>
 120        /// <param name="path">The POSIX path to get the directory name for.</param>
 121        /// <returns>
 122        /// The directory part of the specified <paramref name="path"/>, or <c>.</c> if <paramref name="path"/>
 123        /// does not contain any directory information.
 124        /// </returns>
 125        /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>
 126        public static string GetDirectoryName(string path)
 39127        {
 39128            if (path is null)
 3129            {
 3130                throw new ArgumentNullException(nameof(path));
 131            }
 132
 36133            var pathEnd = path.LastIndexOf('/');
 36134            if (pathEnd == -1)
 9135            {
 9136                return ".";
 137            }
 138
 27139            if (pathEnd == 0)
 15140            {
 15141                return "/";
 142            }
 143
 12144            if (pathEnd == path.Length - 1)
 3145            {
 3146                return path.Substring(0, pathEnd);
 147            }
 148
 9149            return path.Substring(0, pathEnd);
 36150        }
 151    }
 152}