Exemple #1
0
        public void IsLegalAbsoluteOrRelative(string path, string paramName)
        {
            if (path == null)
            {
                throw new ArgumentNullException(paramName, StringResources.Manager.GetString("VALUE_CANNOT_BE_NULL"));
            }

            if (path == string.Empty)
            {
                throw new ArgumentException("Empty file name is not legal.", paramName);
            }

            if (path.Trim() == string.Empty)
            {
                throw CommonExceptions.PathIsNotOfALegalForm(paramName);
            }

            if (XFS.IsWindowsPlatform() && !IsValidUseOfVolumeSeparatorChar(path))
            {
                throw CommonExceptions.InvalidUseOfVolumeSeparator();
            }

            if (ExtractFileName(path).IndexOfAny(_mockFileDataAccessor.Path.GetInvalidFileNameChars()) > -1)
            {
                throw CommonExceptions.IllegalCharactersInPath();
            }

            var filePath = ExtractFilePath(path);

            if (HasIllegalCharacters(filePath, checkAdditional: false))
            {
                throw CommonExceptions.IllegalCharactersInPath();
            }
        }
        public void IsLegalAbsoluteOrRelative(string path, string paramName)
        {
            if (path == null)
            {
                throw new ArgumentNullException(paramName, StringResources.Manager.GetString("VALUE_CANNOT_BE_NULL"));
            }

            if (path == string.Empty)
            {
                throw new ArgumentException("Empty file name is not legal.", paramName);
            }

            if (path.Trim() == string.Empty)
            {
                throw new ArgumentException(StringResources.Manager.GetString("THE_PATH_IS_NOT_OF_A_LEGAL_FORM"), paramName);
            }

            if (XFS.IsWindowsPlatform() && !IsValidUseOfVolumeSeparatorChar(path))
            {
                throw new NotSupportedException(StringResources.Manager.GetString("THE_PATH_IS_NOT_OF_A_LEGAL_FORM"));
            }

            if (ExtractFileName(path).IndexOfAny(_mockFileDataAccessor.Path.GetInvalidFileNameChars()) > -1)
            {
                throw new ArgumentException(StringResources.Manager.GetString("ILLEGAL_CHARACTERS_IN_PATH_EXCEPTION"));
            }

            var filePath = ExtractFilePath(path);

            if (HasIllegalCharacters(filePath, checkAdditional: false))
            {
                throw new ArgumentException(StringResources.Manager.GetString("ILLEGAL_CHARACTERS_IN_PATH_EXCEPTION"));
            }
        }
        public static string NormalizeSlashes(this string path)
        {
            path = path.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
            var sep       = Path.DirectorySeparatorChar.ToString();
            var doubleSep = sep + sep;

            var prefixSeps = new string(path.TakeWhile(c => c == Path.DirectorySeparatorChar).ToArray());

            path = path.Substring(prefixSeps.Length);

            // UNC Paths start with double slash but no reason
            // to have more than 2 slashes at the start of a path
            if (XFS.IsWindowsPlatform() && prefixSeps.Length >= 2)
            {
                prefixSeps = prefixSeps.Substring(0, 2);
            }
            else if (prefixSeps.Length > 1)
            {
                prefixSeps = prefixSeps.Substring(0, 1);
            }

            while (true)
            {
                var newPath = path.Replace(doubleSep, sep);

                if (path == newPath)
                {
                    return(prefixSeps + path);
                }

                path = newPath;
            }
        }
        public static string TrimSlashes(this string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                return(path);
            }

            var trimmed = path.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);

            if (XFS.IsUnixPlatform() &&
                (path[0] == Path.DirectorySeparatorChar || path[0] == Path.AltDirectorySeparatorChar) &&
                trimmed == "")
            {
                return(Path.DirectorySeparatorChar.ToString());
            }

            if (XFS.IsWindowsPlatform() &&
                trimmed.Length == 2 &&
                char.IsLetter(trimmed[0]) &&
                trimmed[1] == ':')
            {
                return(trimmed + Path.DirectorySeparatorChar);
            }

            return(trimmed);
        }
        private IDirectoryInfo CreateDirectoryInternal(string path, DirectorySecurity directorySecurity)
        {
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }

            if (path.Length == 0)
            {
                throw new ArgumentException(StringResources.Manager.GetString("PATH_CANNOT_BE_THE_EMPTY_STRING_OR_ALL_WHITESPACE"), "path");
            }

            path = mockFileDataAccessor.Path.GetFullPath(path).TrimSlashes();
            if (XFS.IsWindowsPlatform())
            {
                path = path.TrimEnd(' ');
            }

            if (!Exists(path))
            {
                mockFileDataAccessor.AddDirectory(path);
            }

            var created = new MockDirectoryInfo(mockFileDataAccessor, path);

            if (directorySecurity != null)
            {
                created.SetAccessControl(directorySecurity);
            }

            return(created);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="MockDirectoryInfo"/> class.
        /// </summary>
        /// <param name="mockFileDataAccessor">The mock file data accessor.</param>
        /// <param name="directoryPath">The directory path.</param>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="mockFileDataAccessor"/> or <paramref name="directoryPath"/> is <see langref="null"/>.</exception>
        public MockDirectoryInfo(IMockFileDataAccessor mockFileDataAccessor, string directoryPath) : base(mockFileDataAccessor?.FileSystem)
        {
            this.mockFileDataAccessor = mockFileDataAccessor ?? throw new ArgumentNullException(nameof(mockFileDataAccessor));

            originalPath  = directoryPath;
            directoryPath = mockFileDataAccessor.Path.GetFullPath(directoryPath);

            directoryPath = directoryPath.TrimSlashes();
            if (XFS.IsWindowsPlatform())
            {
                directoryPath = directoryPath.TrimEnd(' ');
            }
            this.directoryPath = directoryPath;
        }