private IDirectory BuildInMemoryDirectoryWithTestFiles(string filesInformation)
        {
            var directory  = new InMemoryDirectory();
            var filesArray = filesInformation.Split('|');

            foreach (var file in filesArray)
            {
                var pathSegments = PathUtils.SplitPathIntoSegments(file);

                var    fileName = pathSegments[pathSegments.Length - 1];
                string dir      = string.Empty;

                if (pathSegments.Length > 1)
                {
                    for (int i = 0; i < pathSegments.Length - 1; i++)
                    {
                        dir = dir + "/" + pathSegments[i];
                    }
                }

                var       fileContents = Guid.NewGuid();
                IFileInfo fileInfo     = new StringFileInfo(fileContents.ToString(), fileName);
                directory.AddFile(dir, fileInfo);
            }

            return(directory);
        }
Beispiel #2
0
        public void Can_Watch_Directory_For_New_Items()
        {
            // Arrange
            IDirectory directory = new InMemoryDirectory();
            var        watcher   = new DirectoryWatcher(directory);

            var watchPattern = "/some/dir/folder/newfile.*";

            watcher.AddFilter(watchPattern);

            //
            bool notified = false;

            watcher.ItemAdded += (sender, e) =>
            {
                DirectoryItemAddedEventArgs args = e.DirectoryItemEventArgs;
                var            matchedFilters    = e.MatchedFilters;
                IDirectoryItem newItem           = args.NewItem;
                Assert.Equal("newfile.txt", newItem.Name);
                notified = true;
            };


            directory.AddFile("/some/dir/folder/", new StringFileInfo("hi", "newfile.txt"));
            Assert.True(notified);

            notified = false;
            directory.AddFile("/some/dir/folder/", new StringFileInfo("shouldnt trigger", "anotherfile.txt"));
            Assert.False(notified);
        }
Beispiel #3
0
        public IResource CreateFile(FilePath path, byte[]?content = null)
        {
            var comparison = StringComparison.InvariantCultureIgnoreCase;

            if (path.IsAbsolute || path.IsDirectory)
            {
                throw new ArgumentException("File path cannot be an absolute or a directory");
            }

            InMemoryDirectory curDir = root;

            foreach (var nextDirName in path.Parts.SkipLast(1))
            {
                var nextDir = curDir.Directories.FirstOrDefault(d => d.Name.Equals(nextDirName, comparison)) as InMemoryDirectory;
                if (nextDir == null)
                {
                    nextDir = new InMemoryDirectory(this, curDir, curDir.Path.Combine(nextDirName));
                    curDir.directories.Add(nextDir);
                }
                curDir = nextDir;
            }

            var fileName = path.Parts.Last();

            if (curDir.Any(r => r.Name.Equals(fileName, comparison)))
            {
                throw new ArgumentException($"Resource at path {path} already exists");
            }
            var newFile = new InMemoryFile(curDir, curDir.Path.Combine(fileName), content); // recombine for case preservation

            curDir.files.Add(newFile);
            return(newFile);
        }
        public void TokensFiredOnFileDeleted()
        {
            var rootDir = new InMemoryDirectory("root");

            // add a file at root/a.txt
            var fileItem = rootDir.AddFile("", new StringFileInfo("some content", "b.txt"));

            using (var provider = new InMemoryFileProvider(rootDir))
            {
                var filePath = "root/b.txt";
                // var fileInfo = provider.GetFileInfo("root/b.txt");
                var token = provider.Watch(filePath);
                Assert.NotNull(token);
                Assert.False(token.HasChanged);
                Assert.True(token.ActiveChangeCallbacks);

                bool callbackInvoked = false;
                token.RegisterChangeCallback(state =>
                {
                    callbackInvoked = true;
                }, state: null);

                // Trigger a change.
                fileItem.Delete();
                Assert.True(token.HasChanged);
                Assert.True(callbackInvoked);
            }
        }
Beispiel #5
0
        public void Can_Search_For_Files()
        {
            // Arrange

            var directory = new InMemoryDirectory();

            // Add some files
            directory.AddFile("/foo", new StringFileInfo("greetings", "bar.txt"));
            directory.AddFile("/foo", new StringFileInfo("another", "baz.txt"));
            directory.AddFile("/bar", new StringFileInfo("hi", "bar.txt"));
            directory.AddFile("/foo", new StringFileInfo("foo", "bar.txt.min"));

            var testProvider = new InMemoryFileProvider(directory);

            // Act


            // Assert
            // Assert that we can get the file from the directory using its full path.
            var results = testProvider.Search("/foo/*.txt").ToArray();

            Assert.NotNull(results);
            Assert.Equal(2, results.Length);
            Assert.Equal(results[0].Item1, "/foo");
            Assert.Equal(results[0].Item2.Name, "bar.txt");
            Assert.Equal(results[1].Item1, "/foo");
            Assert.Equal(results[1].Item2.Name, "baz.txt");
        }
Beispiel #6
0
        public void Can_Search_For_Files_Using_Multiple_Includes_And_Excludes()
        {
            // Arrange

            var directory = new InMemoryDirectory();

            // Add some files
            directory.AddFile("/foo", new StringFileInfo("greetings", "bar.txt"));
            directory.AddFile("/foo", new StringFileInfo("another", "baz.txt"));
            directory.AddFile("/bar", new StringFileInfo("hi", "bar.txt"));
            directory.AddFile("/foo", new StringFileInfo("foo", "bar.txt.min"));

            var testProvider = new InMemoryFileProvider(directory);

            // Act


            // Assert
            // Assert that we can get the file from the directory using its full path.
            var includes = new string[] { "/foo/*.txt", "/bar/bar.txt" };

            var results = testProvider.Search(includes, "/foo/baz.txt").ToArray();

            Assert.NotNull(results);
            Assert.Equal(2, results.Length);
        }
Beispiel #7
0
        public void Can_Watch_Directory_For_Updated_Items()
        {
            // Arrange
            IDirectory directory = new InMemoryDirectory();

            directory.AddFile("/some/dir/folder/", new StringFileInfo("hi", "newfile.txt"));

            var watcher      = new DirectoryWatcher(directory);
            var watchPattern = "/some/dir/folder/new*.txt";

            watcher.AddFilter(watchPattern);

            //
            bool notified = false;

            watcher.ItemUpdated += (sender, e) =>
            {
                DirectoryItemUpdatedEventArgs args = e.DirectoryItemEventArgs;
                var matchedFilters = e.MatchedFilters;
                Assert.Equal("newfile.txt", args.OldItem.Name);
                Assert.Equal("newfile.csv", args.NewItem.Name);
                notified = true;
            };

            // now update the file
            var existingItem = directory.GetFile("/some/dir/folder/newfile.txt");

            existingItem.Update(new StringFileInfo("changed", "newfile.csv"));
            Assert.True(notified);

            notified = false;
            existingItem.Update(new StringFileInfo("changed again", "newfile.csv"));
            // second update shouldnt trigger as our watch pattern is only watching newfile*.txt files.
            Assert.False(notified);
        }
        public void Can_Nest_Folders()
        {
            // Arrange
            IDirectory directory = new InMemoryDirectory();

            // Act
            // Adds a folder to the directory, along with the necessary directory structure
            var folder = directory.GetOrAddFolder("/some/dir/folder");

            Assert.NotNull(folder);

            var parentFolder      = directory.GetFolder("/some/dir");
            var grandparentFolder = directory.GetFolder("/some");

            // "/some/dir/folder" should have parent folder of "/some/dir"
            Assert.Equal(parentFolder, folder.ParentFolder);
            Assert.Equal("/some/dir/folder", folder.Path);
            Assert.Equal("folder", folder.Name);
            Assert.NotNull(folder.FileInfo);
            Assert.True(folder.IsFolder);

            // "/some/dir/" should have parent folder of "/some"
            Assert.Equal(grandparentFolder, parentFolder.ParentFolder);
            Assert.Equal("/some/dir", parentFolder.Path);
            Assert.Equal("dir", parentFolder.Name);
            Assert.NotNull(parentFolder.FileInfo);
            Assert.True(parentFolder.IsFolder);

            // "/some" should have parent folder which is the root folder for this directory.
            Assert.Equal(directory.Root, grandparentFolder.ParentFolder);
            Assert.Equal("/some", grandparentFolder.Path);
            Assert.Equal("some", grandparentFolder.Name);
            Assert.NotNull(grandparentFolder.FileInfo);
            Assert.True(grandparentFolder.IsFolder);
        }
Beispiel #9
0
        public void Can_Watch_Directory_For_Moved_Items()
        {
            // Arrange
            // Prepare our in memory directory.
            IDirectory directory = new InMemoryDirectory();

            directory.AddFile("/some/dir/folder/", new StringFileInfo("hi", "newfile.txt"));
            directory.AddFile("/some/dir/another/", new StringFileInfo("hi", "newfile.txt"));
            directory.AddFile("/some/dir/hello/", new StringFileInfo("hi", "newfile.txt"));

            // Watch the directory using a pattern as a filter.
            var watcher      = new DirectoryWatcher(directory);
            var watchPattern = "/some/dir/**/new*.txt";

            watcher.AddFilter(watchPattern);

            // Register for the watchers ItemUpdated event, this is how it should notify us of updated
            // files.
            int notifyCount = 0;

            watcher.ItemUpdated += (sender, e) =>
            {
                DirectoryItemUpdatedEventArgs args = e.DirectoryItemEventArgs;
                var matchedFilters = e.MatchedFilters; // contains the patterns that were matched (the reason we are being notified)
                // we aren't renaming the file so the file should have the same name.
                // We are renaming a parent folder, so only the items full path should be different.
                Assert.Equal("newfile.txt", args.OldItem.Name);
                Assert.Equal("newfile.txt", args.NewItem.Name);
                Assert.StartsWith("/some/dir/", args.OldItem.Path);          // the old folder path.
                Assert.StartsWith("/newfoldername/dir/", args.NewItem.Path); // the new folder path.

                //Assert.Equal("/changed/dir/folder/newfile.txt", args.NewItem.Path);
                notifyCount = notifyCount + 1;
            };

            // Act
            // Rename the root folder /some too be /newfoldername
            // this should cause the watcher to notifiy us of effected items we are interested in.
            var folder = directory.GetFolder("/some");

            folder.Rename("newfoldername");

            // Watcher should have notified us on of 3 items we were watching (matched by the pattern)
            Assert.Equal(3, notifyCount);

            // now rename the file again - this time the watcher should not notify us, as the items live in a directory
            // that no longer match out pattern - i.e the previous rename operation has moved them outside of our filter pattern.
            notifyCount = 0;
            var existingItem = directory.GetFile("/newfoldername/dir/folder/newfile.txt");

            existingItem.Update(new StringFileInfo("changed", "newfile.csv"));
            Assert.Equal(0, notifyCount);
        }
        public void GetFileInfo_ReturnsNotFoundFileInfo_ForRelativePathWithEmptySegmentsThatNavigates()
        {
            var rootDir = new InMemoryDirectory("root");

            // add a file at root/a.txt
            rootDir.AddFile("", new StringFileInfo("some content", "b"));

            using (var provider = new InMemoryFileProvider(rootDir))
            {
                var info = provider.GetFileInfo("a///../../" + "root" + "/b");
                Assert.IsType(typeof(NotFoundFileInfo), info);
            }
        }
		private InMemoryDirectory GetRoot(string path)
		{
			InMemoryDirectory directory;

			lock (Directories)
			{
				if (!Directories.TryGetValue(path, out directory))
				{
					Directories.Add(path, directory = new InMemoryDirectory(this, path));
				}
			}

			return directory;
		}
        public void GetFileInfo_ReturnsNotFoundFileInfo_ForRelativePathThatNavigatesAboveRoot()
        {
            var rootDir = new InMemoryDirectory("root");

            // add a file at root/a.txt
            rootDir.AddFile("", new StringFileInfo("some content", "a.txt"));

            using (var provider = new InMemoryFileProvider(rootDir))
            {
                // Act
                var info = provider.GetFileInfo("../root/a.txt");
                Assert.IsType(typeof(NotFoundFileInfo), info);
            }
        }
        public void CreateReadStream_Succeeds_OnEmptyFile()
        {
            var rootDir = new InMemoryDirectory("root");

            // add a file at root/a.txt
            rootDir.AddFile("", new StringFileInfo(null, "a.txt"));

            using (var provider = new InMemoryFileProvider(rootDir))
            {
                var info = provider.GetFileInfo("root/a.txt");
                using (var stream = info.CreateReadStream())
                {
                    Assert.NotNull(stream);
                }
            }
        }
        public void Can_Add_Then_Retrieve_A_Folder()
        {
            // Arrange

            IDirectory directory = new InMemoryDirectory();
            // Act
            // Add the file to the directory, to the folder path: /foo/bar
            var folder = directory.GetOrAddFolder("/folder");

            Assert.NotNull(folder);

            var retrievedFolder = directory.GetFolder("/folder");

            Assert.NotNull(retrievedFolder);
            Assert.Equal(folder, retrievedFolder);
        }
        public void Token_IsSame_ForSamePath()
        {
            var rootDir = new InMemoryDirectory("root");

            // add a file at root/a.txt
            rootDir.AddFile("", new StringFileInfo("some content", "b.txt"));

            using (var provider = new InMemoryFileProvider(rootDir))
            {
                var filePath = "root/b.txt";
                // var fileInfo = provider.GetFileInfo("root/b.txt");
                var token1 = provider.Watch(filePath);
                var token2 = provider.Watch(filePath);

                Assert.NotNull(token1);
                Assert.NotNull(token2);
                Assert.Equal(token2, token1);
            }
        }
        public void Can_Add_Then_Retrieve_A_File()
        {
            // Arrange

            IDirectory directory    = new InMemoryDirectory();
            var        fileContents = "greetings!";
            IFileInfo  fileInfo     = new StringFileInfo(fileContents, "hello.txt");

            // Act
            // Add the file to the directory, to the folder path: /foo/bar
            directory.AddFile("/foo", fileInfo);

            // Assert
            // Assert that we can get the file from the directory using its full path.
            var retrievedFile = directory.GetFile("/foo/hello.txt");

            Assert.NotNull(retrievedFile);
            Assert.Equal(fileInfo, retrievedFile.FileInfo);
        }
        public void CreateDirectory(string directory)
        {
            InMemoryDirectory dir = Root;
            InMemoryDirectory newDir;

            // Make sure all the directory and all its ancestors exist
            foreach (string directoryName in directory.Split(DirectorySeparators, StringSplitOptions.RemoveEmptyEntries))
            {
                if (!dir.GetSubdirectory(directoryName).Exists)
                {
                    // Create a new directory
                    newDir = new InMemoryDirectory(this, Path.Combine(dir.FullName, directoryName));
                    dir.AddSubDirectory(newDir);
                }
                else
                {
                    dir = dir.GetSubdirectory(directoryName) as InMemoryDirectory;
                }
            }
        }
        public void TokensFiredOnFileChange()
        {
            var rootDir = new InMemoryDirectory("root");

            // add a file at root/a.txt
            var fileItem = rootDir.AddFile("", new StringFileInfo("some content", "b.txt"));

            using (var provider = new InMemoryFileProvider(rootDir))
            {
                var filePath = "root/b.txt";
                // var fileInfo = provider.GetFileInfo("root/b.txt");
                var token = provider.Watch(filePath);
                Assert.NotNull(token);
                Assert.False(token.HasChanged);
                Assert.True(token.ActiveChangeCallbacks);

                // Trigger a change.
                fileItem.Update(new StringFileInfo("modified content", "b.txt"));
                Assert.True(token.HasChanged);
            }
        }
Beispiel #19
0
        public void Can_Watch_Directory_For_Deleted_Items()
        {
            // Arrange
            IDirectory directory = new InMemoryDirectory();

            directory.AddFile("/some/dir/folder/", new StringFileInfo("hi", "foo.txt"));
            directory.AddFile("/some/dir/folder/", new StringFileInfo("hi", "bar.txt"));

            var watcher      = new DirectoryWatcher(directory);
            var watchPattern = "/some/dir/folder/foo.txt";

            watcher.AddFilter(watchPattern);

            //
            bool notified = false;

            watcher.ItemDeleted += (sender, e) =>
            {
                DirectoryItemDeletedEventArgs args = e.DirectoryItemEventArgs;
                var            matchedFilters      = e.MatchedFilters;
                IDirectoryItem deletedItem         = args.DeletedItem;
                Assert.Equal("foo.txt", deletedItem.Name);
                notified = true;
            };


            var fooFile = directory.GetFile("/some/dir/folder/foo.txt");

            fooFile.Delete();
            Assert.True(notified);

            notified = false;
            var barFile = directory.GetFile("/some/dir/folder/bar.txt");

            barFile.Delete();
            Assert.False(notified);
        }
 InMemoryDirectory GetRoot(string path)
 {
     InMemoryDirectory directory;
     if (!Directories.TryGetValue(path, out directory))
     {
         Directories.Add(path, directory = new InMemoryDirectory(path)
         {
                 FileSystem = this
         });
     }
     return directory;
 }
 public InMemoryFileSystem()
 {
     _directories[""] = new InMemoryDirectory(this, "");
 }
 public InMemoryFileSystem()
 {
     // Create a new Root directory
     Root = new InMemoryDirectory(this, "");
 }
        public IDirectory GetDirectory(string path)
        {
            IDirectory directory = Root;

            if (path.Length != 0 && path != @"\")
            {
                foreach (string directoryName in path.Split(DirectorySeparators))
                {
                    directory = directory.GetSubdirectory(directoryName);
                    if (!directory.Exists)
                    {
                        break; // no point going further the directory doesn't exist
                    }
                }
            }

            // The directory wasn't found - so return a blank directory for the path
            if (!directory.Exists)
            {
                directory = new InMemoryDirectory(this, path);
            }
            return directory;
        }
Beispiel #24
0
 public InMemoryResourcePool()
 {
     root = new InMemoryDirectory(this, null, "");
 }
        public InMemoryDirectoryFacts()
        {
            this.fileSystem = new InMemoryFileSystem();

            this.testee = new InMemoryDirectory(this.fileSystem, Enumerable.Empty<IDirectoryExtension>());
        }