public MockFileSystem(IDictionary <string, MockFileData> files, string currentDirectory = "")
        {
            if (string.IsNullOrEmpty(currentDirectory))
            {
                currentDirectory = XFS.Path(DEFAULT_CURRENT_DIRECTORY);
            }

            StringOperations = new StringOperations(XFS.IsUnixPlatform());
            pathVerifier     = new PathVerifier(this);
            this.files       = new Dictionary <string, MockFileData>(StringOperations.Comparer);

            Path              = new MockPath(this);
            File              = new MockFile(this);
            Directory         = new MockDirectory(this, currentDirectory);
            FileInfo          = new MockFileInfoFactory(this);
            FileStream        = new MockFileStreamFactory(this);
            DirectoryInfo     = new MockDirectoryInfoFactory(this);
            DriveInfo         = new MockDriveInfoFactory(this);
            FileSystemWatcher = new MockFileSystemWatcherFactory();

            if (files != null)
            {
                foreach (var entry in files)
                {
                    AddFile(entry.Key, entry.Value);
                }
            }

            if (!FileExists(currentDirectory))
            {
                AddDirectory(currentDirectory);
            }
        }
        public void AddDirectory(string path)
        {
            var fixedPath = FixPath(path, true);
            var separator = Path.DirectorySeparatorChar.ToString();

            lock (files)
            {
                if (FileExists(fixedPath) &&
                    (GetFile(fixedPath).Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
                {
                    throw new UnauthorizedAccessException(string.Format(CultureInfo.InvariantCulture, StringResources.Manager.GetString("ACCESS_TO_THE_PATH_IS_DENIED"), fixedPath));
                }

                var lastIndex = 0;
                var isUnc     =
                    StringOperations.StartsWith(fixedPath, @"\\") ||
                    StringOperations.StartsWith(fixedPath, @"//");

                if (isUnc)
                {
                    //First, confirm they aren't trying to create '\\server\'
                    lastIndex = StringOperations.IndexOf(fixedPath, separator, 2);

                    if (lastIndex < 0)
                    {
                        throw new ArgumentException(@"The UNC path should be of the form \\server\share.", "path");
                    }

                    /*
                     * Although CreateDirectory(@"\\server\share\") is not going to work in real code, we allow it here for the purposes of setting up test doubles.
                     * See PR https://github.com/System-IO-Abstractions/System.IO.Abstractions/pull/90 for conversation
                     */
                }

                while ((lastIndex = StringOperations.IndexOf(fixedPath, separator, lastIndex + 1)) > -1)
                {
                    var segment = fixedPath.Substring(0, lastIndex + 1);
                    if (!Directory.Exists(segment))
                    {
                        SetEntry(segment, new MockDirectoryData());
                    }
                }

                var s = StringOperations.EndsWith(fixedPath, separator) ? fixedPath : fixedPath + separator;
                SetEntry(s, new MockDirectoryData());
            }
        }
Ejemplo n.º 3
0
        public void AddDirectory(string path)
        {
            var fixedPath = FixPath(path, true);
            var separator = Path.DirectorySeparatorChar.ToString();

            lock (files)
            {
                if (FileExists(fixedPath) &&
                    (GetFile(fixedPath).Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
                {
                    throw CommonExceptions.AccessDenied(fixedPath);
                }

                var lastIndex = 0;
                var isUnc     =
                    StringOperations.StartsWith(fixedPath, @"\\") ||
                    StringOperations.StartsWith(fixedPath, @"//");

                if (isUnc)
                {
                    //First, confirm they aren't trying to create '\\server\'
                    lastIndex = StringOperations.IndexOf(fixedPath, separator, 2);

                    if (lastIndex < 0)
                    {
                        throw CommonExceptions.InvalidUncPath(nameof(path));
                    }

                    /*
                     * Although CreateDirectory(@"\\server\share\") is not going to work in real code, we allow it here for the purposes of setting up test doubles.
                     * See PR https://github.com/System-IO-Abstractions/System.IO.Abstractions/pull/90 for conversation
                     */
                }

                while ((lastIndex = StringOperations.IndexOf(fixedPath, separator, lastIndex + 1)) > -1)
                {
                    var segment = fixedPath.Substring(0, lastIndex + 1);
                    if (!Directory.Exists(segment))
                    {
                        SetEntry(segment, new MockDirectoryData());
                    }
                }

                var s = StringOperations.EndsWith(fixedPath, separator) ? fixedPath : fixedPath + separator;
                SetEntry(s, new MockDirectoryData());
            }
        }
        public MockFileSystem(IDictionary <string, MockFileData> files, string currentDirectory = "")
        {
            if (string.IsNullOrEmpty(currentDirectory))
            {
                currentDirectory = XFS.Path(DEFAULT_CURRENT_DIRECTORY);
            }
            else if (!System.IO.Path.IsPathRooted(currentDirectory))
            {
                throw new ArgumentException("Current directory needs to be rooted.", nameof(currentDirectory));
            }

            var defaultTempDirectory = XFS.Path(TEMP_DIRECTORY);

            StringOperations = new StringOperations(XFS.IsUnixPlatform());
            pathVerifier     = new PathVerifier(this);
            this.files       = new Dictionary <string, MockFileData>(StringOperations.Comparer);

            Path              = new MockPath(this, defaultTempDirectory);
            File              = new MockFile(this);
            Directory         = new MockDirectory(this, currentDirectory);
            FileInfo          = new MockFileInfoFactory(this);
            FileStream        = new MockFileStreamFactory(this);
            DirectoryInfo     = new MockDirectoryInfoFactory(this);
            DriveInfo         = new MockDriveInfoFactory(this);
            FileSystemWatcher = new MockFileSystemWatcherFactory();

            if (files != null)
            {
                foreach (var entry in files)
                {
                    AddFile(entry.Key, entry.Value);
                }
            }

            if (!FileExists(currentDirectory))
            {
                AddDirectory(currentDirectory);
            }

            if (!FileExists(defaultTempDirectory))
            {
                AddDirectory(defaultTempDirectory);
            }
        }
        public void MoveDirectory(string sourcePath, string destPath)
        {
            sourcePath = FixPath(sourcePath);
            destPath   = FixPath(destPath);

            lock (files)
            {
                var affectedPaths = files.Keys
                                    .Where(p => StringOperations.StartsWith(p, sourcePath))
                                    .ToList();

                foreach (var path in affectedPaths)
                {
                    var newPath = StringOperations.Replace(path, sourcePath, destPath);
                    files[newPath] = files[path];
                    files.Remove(path);
                }
            }
        }
        public void MoveDirectory(string sourcePath, string destPath)
        {
            sourcePath = FixPath(sourcePath);
            destPath   = FixPath(destPath);

            var sourcePathSequence = sourcePath.Split(new[] { Path.DirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries);

            lock (files)
            {
                var affectedPaths = files.Keys
                                    .Where(p => PathStartsWith(p, sourcePathSequence))
                                    .ToList();

                foreach (var path in affectedPaths)
                {
                    var newPath = StringOperations.Replace(path, sourcePath, destPath);
                    files[newPath] = files[path];
                    files.Remove(path);
                }
            }

            bool PathStartsWith(string path, string[] minMatch)
            {
                var pathSequence = path.Split(new[] { Path.DirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries);

                if (pathSequence.Length < minMatch.Length)
                {
                    return(false);
                }

                for (var i = 0; i < minMatch.Length; i++)
                {
                    if (!StringOperations.Equals(minMatch[i], pathSequence[i]))
                    {
                        return(false);
                    }
                }

                return(true);
            }
        }
        //If C:\foo exists, ensures that trying to save a file to "C:\FOO\file.txt" instead saves it to "C:\foo\file.txt".
        private string GetPathWithCorrectDirectoryCapitalization(string fullPath)
        {
            string[] splitPath = fullPath.Split(Path.DirectorySeparatorChar);
            string   leftHalf  = fullPath;
            string   rightHalf = "";

            for (int i = splitPath.Length - 1; i > 1; i--)
            {
                rightHalf = i == splitPath.Length - 1 ? splitPath[i] : splitPath[i] + Path.DirectorySeparatorChar + rightHalf;
                int lastSeparator = leftHalf.LastIndexOf(Path.DirectorySeparatorChar);
                leftHalf = lastSeparator > 0 ? leftHalf.Substring(0, lastSeparator) : leftHalf;

                if (Directory.Exists(leftHalf))
                {
                    leftHalf = Path.GetFullPath(leftHalf).TrimSlashes();
                    string baseDirectory = AllDirectories.First(dir => StringOperations.Equals(dir, leftHalf));
                    return(baseDirectory + Path.DirectorySeparatorChar + rightHalf);
                }
            }

            return(fullPath.TrimSlashes());
        }