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); foreach (var child in node.Content.Directories) { root.Push(child.Value); } } while (result.Count > 0) { var directory = result.Pop(); foreach (var file in directory.Content.Files.Select(x => x).ToArray()) { // Delete the file. DeleteFile(file.Value); } // Delete the directory. directory.Parent.Content.Remove(directory); directory.Exists = false; } }
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); }
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; } }
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); }
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)) { if (queue.Count == 0) { // Use the provided directory. current = directory; } else { current = new FakeDirectory(this, path); } current.Parent = parent; current.Hidden = false; children.Add(current); } else { current = children.Directories[path]; } current.Exists = true; children = current.Content; } return(directory); }
public FakeFileSystemTree(ICakeEnvironment environment) { if (environment == null) { throw new ArgumentNullException("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.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); }