Exemplo n.º 1
0
        public FakeDirectory CreateDirectory(FakeDirectory directory)
        {
            var path  = directory.Path;
            var queue = new Queue <string>(path.Segments);

            FakeDirectory?current  = null;
            var           children = _root.Content;

            while (queue.Count > 0)
            {
                // Get the segment.
                var currentSegment = queue.Dequeue();
                var parent         = current;

                // Calculate the current path.
                path = parent != null?parent.Path.Combine(currentSegment) : new DirectoryPath(currentSegment);

                if (!children.Directories.ContainsKey(path))
                {
                    current        = queue.Count == 0 ? directory : new FakeDirectory(this, path);
                    current.Parent = parent ?? _root;
                    current.Hidden = false;
                    children.Add(current);
                }
                else
                {
                    current = children.Directories[path];
                }

                current.Exists = true;
                children       = current.Content;
            }

            return(directory);
        }
Exemplo n.º 2
0
        public FakeFileSystemTree(IEnvironment environment)
        {
            if (environment == null)
            {
                throw new ArgumentNullException(nameof(environment));
            }

            if (environment.WorkingDirectory == null)
            {
                throw new ArgumentException("Working directory not set.");
            }

            if (environment.WorkingDirectory.IsRelative)
            {
                throw new ArgumentException("Working directory cannot be relative.");
            }

            Comparer = new PathComparer(environment.Platform.IsUnix());

            _root = new FakeDirectory(this, "/")
            {
                Exists = true
            };
            _root.Create();
        }
Exemplo n.º 3
0
        /// <summary>
        /// Hides the specified directory.
        /// </summary>
        /// <param name="directory">The directory to hide.</param>
        /// <returns>The same <see cref="FakeDirectory"/> instance so that multiple calls can be chained.</returns>
        public static FakeDirectory Hide(this FakeDirectory directory)
        {
            if (directory == null)
            {
                throw new ArgumentNullException(nameof(directory));
            }

            directory.Hidden = true;
            return(directory);
        }
Exemplo n.º 4
0
        public void DeleteDirectory(FakeDirectory fakeDirectory, bool recursive)
        {
            var root   = new Stack <FakeDirectory>();
            var result = new Stack <FakeDirectory>();

            if (fakeDirectory.Exists)
            {
                root.Push(fakeDirectory);
            }

            while (root.Count > 0)
            {
                var node = root.Pop();
                result.Push(node);

                var directories = node.Content.Directories;

                if (directories.Count > 0 && !recursive)
                {
                    throw new IOException("The directory is not empty.");
                }

                foreach (var child in directories)
                {
                    root.Push(child.Value);
                }
            }

            while (result.Count > 0)
            {
                var directory = result.Pop();

                var files = directory.Content.Files.Select(x => x).ToArray();
                if (files.Length > 0 && !recursive)
                {
                    throw new IOException("The directory is not empty.");
                }

                foreach (var file in files)
                {
                    // Delete the file.
                    DeleteFile(file.Value);
                }

                // Remove the directory from the parent.
                if (directory.Parent != null)
                {
                    directory.Parent.Content.Remove(directory);
                }

                // Mark the directory as it doesn't exist.
                directory.Exists = false;
            }
        }
Exemplo n.º 5
0
        private static IEnumerable <FakeFile> GetFiles(FakeDirectory current, Regex expression)
        {
            var result = new List <FakeFile>();

            foreach (var file in current.Content.Files)
            {
                if (file.Value.Exists)
                {
                    // Is this a match? In that case, add it to the result.
                    if (expression.IsMatch(file.Key.GetFilename().FullPath))
                    {
                        result.Add(current.Content.Files[file.Key]);
                    }
                }
            }

            return(result);
        }
Exemplo n.º 6
0
 public void Remove(FakeDirectory directory)
 {
     _directories.Remove(directory.Path);
 }
Exemplo n.º 7
0
 public void Add(FakeDirectory directory)
 {
     _directories.Add(directory.Path, directory);
 }
Exemplo n.º 8
0
 public FakeDirectoryContent(FakeDirectory owner, PathComparer comparer)
 {
     Owner        = owner;
     _directories = new Dictionary <DirectoryPath, FakeDirectory>(comparer);
     _files       = new Dictionary <FilePath, FakeFile>(comparer);
 }
Exemplo n.º 9
0
        public void MoveDirectory(FakeDirectory fakeDirectory, DirectoryPath destination)
        {
            var root   = new Stack <FakeDirectory>();
            var result = new Stack <FakeDirectory>();

            if (string.IsNullOrEmpty(destination.FullPath))
            {
                throw new ArgumentException("The destination directory is empty.");
            }

            if (fakeDirectory.Path.Equals(destination))
            {
                throw new IOException("The directory being moved and the destination directory have the same name.");
            }

            if (FindDirectory(destination) != null)
            {
                throw new IOException("The destination directory already exists.");
            }

            string        destinationParentPathStr = string.Join("/", destination.Segments.Take(destination.Segments.Count - 1).DefaultIfEmpty("/"));
            DirectoryPath destinationParentPath    = new DirectoryPath(string.IsNullOrEmpty(destinationParentPathStr) ? "/" : destinationParentPathStr);

            if (FindDirectory(destinationParentPath) == null)
            {
                throw new DirectoryNotFoundException("The parent destination directory " + destinationParentPath.FullPath + " could not be found.");
            }

            if (fakeDirectory.Exists)
            {
                root.Push(fakeDirectory);
            }

            // Create destination directories and move files
            while (root.Count > 0)
            {
                var node = root.Pop();
                result.Push(node);

                // Create destination directory
                DirectoryPath relativePath    = fakeDirectory.Path.GetRelativePath(node.Path);
                DirectoryPath destinationPath = destination.Combine(relativePath);
                CreateDirectory(destinationPath);

                var files = node.Content.Files.Select(x => x).ToArray();
                foreach (var file in files)
                {
                    // Move the file.
                    MoveFile(file.Value, destinationPath.CombineWithFilePath(file.Key.GetFilename()));
                }

                var directories = node.Content.Directories;
                foreach (var child in directories)
                {
                    root.Push(child.Value);
                }
            }

            // Delete source directories
            while (result.Count > 0)
            {
                var directory = result.Pop();

                // Delete the directory.
                if (directory.Parent != null)
                {
                    directory.Parent.Content.Remove(directory);
                }

                // Mark the directory as it doesn't exist.
                directory.Exists = false;
            }
        }