예제 #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);
        }
예제 #2
0
        public FakeDirectory FindDirectory(DirectoryPath path)
        {
            // Want the root?
            if (path.Segments.Length == 0)
            {
                return(_root);
            }

            var queue = new Queue <string>(path.Segments);

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

            while (queue.Count > 0)
            {
                // Set the parent.
                var parent = current;

                // Calculate the current path.
                var segment = queue.Dequeue();
                path = parent != null?parent.Path.Combine(segment) : new DirectoryPath(segment);

                // Find the current path.
                if (!children.Directories.ContainsKey(path))
                {
                    return(null);
                }

                current  = children.Directories[path];
                children = current.Content;
            }

            return(current);
        }
        /// <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);
        }
예제 #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);
                }

                // Delete the directory.
                directory.Parent.Content.Remove(directory);
                directory.Exists = false;
            }
        }
예제 #5
0
        private static IEnumerable <FakeFile> GetFiles(FakeDirectory current, Regex exression)
        {
            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 (exression.IsMatch(file.Key.GetFilename().FullPath))
                    {
                        result.Add(current.Content.Files[file.Key]);
                    }
                }
            }
            return(result);
        }
예제 #6
0
        public FakeFileSystemTree(ICakeEnvironment 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, "/");
            _root.Create();
        }
 public void Remove(FakeDirectory directory)
 {
     _directories.Remove(directory.Path);
 }
 public void Add(FakeDirectory directory)
 {
     _directories.Add(directory.Path, directory);
 }
 public FakeDirectoryContent(FakeDirectory owner, PathComparer comparer)
 {
     Owner        = owner;
     _directories = new Dictionary <DirectoryPath, FakeDirectory>(comparer);
     _files       = new Dictionary <FilePath, FakeFile>(comparer);
 }
예제 #10
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.Length - 1).DefaultIfEmpty("/"));
            DirectoryPath destinationParentPath    = new DirectoryPath(destinationParentPathStr == string.Empty ? "/" : 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.
                directory.Parent.Content.Remove(directory);
                directory.Exists = false;
            }
        }