Esempio 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);
        }
Esempio n. 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);
        }
Esempio n. 3
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;
            }
        }
Esempio n. 4
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);
        }
Esempio n. 5
0
        public FakeFileSystemTree(IFileSystemEnvironment 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();
        }
Esempio n. 6
0
 public void Remove(FakeDirectory directory)
 {
     _directories.Remove(directory.Path);
 }
Esempio n. 7
0
 public void Add(FakeDirectory directory)
 {
     _directories.Add(directory.Path, directory);
 }
Esempio n. 8
0
 public FakeDirectoryContent(FakeDirectory owner, PathComparer comparer)
 {
     Owner        = owner;
     _directories = new Dictionary <DirectoryPath, FakeDirectory>(comparer);
     _files       = new Dictionary <FilePath, FakeFile>(comparer);
 }