Example #1
0
        public async Task ShouldRenameFilesAndPreserveDirectoryStructureIfTargetFilesExist()
        {
            TestFileContext.CreateFile("source/test.txt", "testing");
            TestFileContext.CreateFile("source/sub/test.txt", "testing");
            TestFileContext.CreateFile("target/sub/test.txt", "existing");

            var results = await File.Copy(
                new CopyInput
            {
                Directory       = TestFileContext.GetAbsolutePath("source"),
                Pattern         = "**/*.txt",
                TargetDirectory = TestFileContext.GetAbsolutePath("target")
            },
                new CopyOptions
            {
                PreserveDirectoryStructure = true,
                CreateTargetDirectories    = true,
                IfTargetFileExists         = FileExistsAction.Rename
            },
                CancellationToken.None);

            Assert.Equal(2, results.Count);
            Assert.Equal(results.Select(r => r.Path), new[] {
                TestFileContext.GetAbsolutePath("target/test.txt"),
                TestFileContext.GetAbsolutePath("target/sub/test(1).txt")
            });

            Assert.True(TestFileContext.FileExists("target/sub/test(1).txt"), "Output file should have been written");
        }
Example #2
0
        public async Task ShouldCopyFilesFromSubDirectories()
        {
            TestFileContext.CreateFile("dir/sub/test1.txt", "testing");
            TestFileContext.CreateFile("dir/sub/test2.txt", "testing");
            TestFileContext.CreateFile("dir/sub/other1.xml", "testing");
            TestFileContext.CreateFile("dir/sub/nestedSub/test3.txt", "testing");
            TestFileContext.CreateFile("dir/sub/nestedSub/other2.xml", "testing");

            var results = await File.Copy(
                new CopyInput
            {
                Directory       = TestFileContext.GetAbsolutePath("dir"),
                Pattern         = "**/*.txt",
                TargetDirectory = TestFileContext.GetAbsolutePath("out"),
            },
                new CopyOptions { CreateTargetDirectories = true },
                CancellationToken.None);

            Assert.Equal(3, results.Count);
            Assert.Equal(results.Select(r => Path.GetFileName(r.Path)), new[] { "test1.txt", "test2.txt", "test3.txt" });

            Assert.True(TestFileContext.FileExists("out/test1.txt"), "Output file should have been written");
            Assert.True(TestFileContext.FileExists("out/test2.txt"), "Output file should have been written");
            Assert.True(TestFileContext.FileExists("out/test3.txt"), "Output file should have been written");
        }
Example #3
0
        public async Task ShouldOverwriteExistingFilesInSubDirectories()
        {
            var expectedFileContents = "testing " + DateTime.UtcNow.ToString("o");

            TestFileContext.CreateFile("source/test.txt", "testing");
            TestFileContext.CreateFile("source/sub/test.txt", expectedFileContents);
            TestFileContext.CreateFile("target/sub/test.txt", "existing");

            var results = await File.Copy(
                new CopyInput
            {
                Directory       = TestFileContext.GetAbsolutePath("source"),
                Pattern         = "**/*.txt",
                TargetDirectory = TestFileContext.GetAbsolutePath("target")
            },
                new CopyOptions
            {
                PreserveDirectoryStructure = true,
                CreateTargetDirectories    = true,
                IfTargetFileExists         = FileExistsAction.Overwrite
            },
                CancellationToken.None);

            Assert.Equal(2, results.Count);
            Assert.Equal(results.Select(r => r.Path), new[] {
                TestFileContext.GetAbsolutePath("target/test.txt"),
                TestFileContext.GetAbsolutePath("target/sub/test.txt")
            });

            Assert.True(TestFileContext.FileExists("target/sub/test.txt"), "Output file should have been written");

            var resultFileContents = System.IO.File.ReadAllText(TestFileContext.GetAbsolutePath("target/sub/test.txt"));

            Assert.Equal(expectedFileContents, resultFileContents);
        }
Example #4
0
        public async Task ReadFileContent()
        {
            var fileContent = "Well this is content with some extra nice ümlauts: ÄÖåå 你好!";

            TestFileContext.CreateFile("Folder/test.txt", fileContent);
            var result = await File.Read(new ReadInput()
            {
                Path = Path.Combine(TestFileContext.RootPath, "folder/test.txt")
            }, new ReadOption()
            {
            });

            Assert.Equal(fileContent, result.Content);
        }
Example #5
0
        public async Task ShouldNotCopyFilesIfNoMatch()
        {
            TestFileContext.CreateFile("dir/sub/test.txt", "testing");

            var results = await File.Copy(
                new CopyInput
            {
                Directory       = TestFileContext.GetAbsolutePath("dir"),
                Pattern         = "**/*.xml",
                TargetDirectory = TestFileContext.GetAbsolutePath("out")
            },
                new CopyOptions { CreateTargetDirectories = true },
                CancellationToken.None);

            Assert.Empty(results);
        }
Example #6
0
        public async Task FileMoveOverWrite()
        {
            const string contentForFileToBeOverwritten = "firstFile";

            TestFileContext.CreateFile("folder/test.xml", contentForFileToBeOverwritten);
            var createdFile = System.IO.File.ReadAllText(Path.Combine(TestFileContext.RootPath, "folder/test.xml"));

            Assert.Equal(contentForFileToBeOverwritten, createdFile);

            var results = await File.Move(
                new MoveInput()
            {
                Directory       = TestFileContext.RootPath,
                Pattern         = "**/sub/*.xml",
                TargetDirectory = Path.Combine(TestFileContext.RootPath, "folder")
            },
                new MoveOptions()
            {
                IfTargetFileExists = FileExistsAction.Overwrite
            },
                CancellationToken.None);

            var overWrittenFIle = System.IO.File.ReadAllText(Path.Combine(TestFileContext.RootPath, "folder/test.xml"));

            Assert.NotEqual(contentForFileToBeOverwritten, overWrittenFIle);
            Assert.Equal(2, results.Count);

            var destinationFilesLength = Directory.GetFiles(Path.Combine(TestFileContext.RootPath, "folder")).Length;

            Assert.Equal(2, destinationFilesLength);

            var secondMoveShouldBeEmpty = await File.Move(
                new MoveInput()
            {
                Directory       = TestFileContext.RootPath,
                Pattern         = "**/sub/*.xml",
                TargetDirectory = Path.Combine(TestFileContext.RootPath, "folder")
            },
                new MoveOptions()
            {
                IfTargetFileExists = FileExistsAction.Overwrite
            },
                CancellationToken.None);

            Assert.Empty(secondMoveShouldBeEmpty);
        }
Example #7
0
        public async Task FileMoveCopy()
        {
            const string contentForOriginalFile = "firstFile";

            TestFileContext.CreateFile("folder/test.xml", contentForOriginalFile);

            var results = await File.Move(
                new MoveInput()
            {
                Directory       = TestFileContext.RootPath,
                Pattern         = "**/sub/*.xml",
                TargetDirectory = Path.Combine(TestFileContext.RootPath, "folder")
            },
                new MoveOptions()
            {
                IfTargetFileExists = FileExistsAction.Rename
            },
                CancellationToken.None);

            Assert.Equal(2, results.Count);
            var originalFile = System.IO.File.ReadAllText(Path.Combine(TestFileContext.RootPath, "folder\\test.xml"));
            var copiedFile   = System.IO.File.ReadAllText(Path.Combine(TestFileContext.RootPath, "folder\\test(1).xml"));

            Assert.Equal(contentForOriginalFile, originalFile);
            Assert.StartsWith("Automatically generated for testing on", copiedFile);

            var destinationFilesLength = Directory.GetFiles(Path.Combine(TestFileContext.RootPath, "folder")).Length;

            Assert.Equal(3, destinationFilesLength);

            var secondMoveShouldBeEmpty = await File.Move(
                new MoveInput()
            {
                Directory       = TestFileContext.RootPath,
                Pattern         = "**/sub/*.xml",
                TargetDirectory = Path.Combine(TestFileContext.RootPath, "folder")
            },
                new MoveOptions()
            {
                IfTargetFileExists = FileExistsAction.Rename
            },
                CancellationToken.None);

            Assert.Empty(secondMoveShouldBeEmpty);
        }
Example #8
0
        public async Task WriteFileOverWrite()
        {
            TestFileContext.CreateFile("test.txt", "old content");
            var result = await File.Write(
                new WriteInput()
            {
                Content = "new content",
                Path    = Path.Combine(TestFileContext.RootPath, "test.txt")
            },
                new WriteOption()
            {
                WriteBehaviour = WriteBehaviour.Overwrite
            });

            var fileContent = System.IO.File.ReadAllText(result.Path);

            Assert.Equal("new content", fileContent);
        }
Example #9
0
        public async Task ShouldThrowErrorIfTargetDirectoryDoesNotExist()
        {
            TestFileContext.CreateFile("dir/sub/test.txt", "testing");

            var error = await Assert.ThrowsAsync <DirectoryNotFoundException>(async() => await File.Copy(
                                                                                  new CopyInput
            {
                Directory       = TestFileContext.GetAbsolutePath("dir/sub"),
                Pattern         = "test.txt",
                TargetDirectory = TestFileContext.GetAbsolutePath("out")
            },
                                                                                  new CopyOptions {
                CreateTargetDirectories = false
            },
                                                                                  CancellationToken.None));

            Assert.Contains(TestFileContext.GetAbsolutePath("out"), error.Message);
        }
Example #10
0
        public async Task WriteFileThrow()
        {
            TestFileContext.CreateFile("test.txt", "old content");
            var ex = await Assert.ThrowsAsync <IOException>(async() => await File.Write(
                                                                new WriteInput()
            {
                Content = "new content",
                Path    = Path.Combine(TestFileContext.RootPath, "test.txt")
            },
                                                                new WriteOption()
            {
                WriteBehaviour = WriteBehaviour.Throw
            }));

            var fileContent = System.IO.File.ReadAllText(Path.Combine(TestFileContext.RootPath, "test.txt"));

            Assert.Equal("old content", fileContent);
            Assert.Equal($"File already exists: {Path.Combine(TestFileContext.RootPath, "test.txt")}", ex.Message);
        }
Example #11
0
        public async Task ShouldCopySingleFile()
        {
            TestFileContext.CreateFile("dir/sub/test.txt", "testing");

            var results = await File.Copy(
                new CopyInput
            {
                Directory       = TestFileContext.GetAbsolutePath("dir/sub"),
                Pattern         = "test.txt",
                TargetDirectory = TestFileContext.GetAbsolutePath("out")
            },
                new CopyOptions { CreateTargetDirectories = true },
                CancellationToken.None);

            Assert.Single(results);
            var result = results[0];

            Assert.Equal(TestFileContext.GetAbsolutePath("out/test.txt"), result.Path);
            Assert.Equal(TestFileContext.GetAbsolutePath("dir/sub/test.txt"), result.SourcePath);

            Assert.True(TestFileContext.FileExists("out/test.txt"), "Output file should have been written");
        }
Example #12
0
        public async Task FileMoveThrowShouldThrowIfFIleExistsAtDestination()
        {
            const string contentForOriginalFile = "firstFile";

            TestFileContext.CreateFile("folder/test.xml", contentForOriginalFile);

            var ex = await Assert.ThrowsAsync <IOException>(async() => await File.Move(
                                                                new MoveInput()
            {
                Directory       = TestFileContext.RootPath,
                Pattern         = "**/sub/*.xml",
                TargetDirectory = Path.Combine(TestFileContext.RootPath, "folder")
            },
                                                                new MoveOptions()
            {
                IfTargetFileExists = FileExistsAction.Throw
            },
                                                                CancellationToken.None));

            Assert.Equal($"File '{Path.Combine(TestFileContext.RootPath, "folder\\test.xml")}' already exists. No files moved.", ex.Message);

            var originalFile = System.IO.File.ReadAllText(Path.Combine(TestFileContext.RootPath, "folder\\test.xml"));

            Assert.Equal(contentForOriginalFile, originalFile);

            var destinationFilesLength = Directory.GetFiles(Path.Combine(TestFileContext.RootPath, "folder"));

            Assert.Single(destinationFilesLength);

            var sourceFolder1 = Directory.GetFiles(Path.Combine(TestFileContext.RootPath, "folder/foo/sub/"));

            Assert.Single(sourceFolder1);

            var sourceFolder2 = Directory.GetFiles(Path.Combine(TestFileContext.RootPath, "folder/bar/sub/"));

            Assert.Single(sourceFolder2);
        }
Example #13
0
        public async Task ShouldThrowAndRollbackIfTargetFilesExist()
        {
            TestFileContext.CreateFile("source/test.txt", "testing");
            TestFileContext.CreateFile("source/sub/test.txt", "testing");
            TestFileContext.CreateFile("target/sub/test.txt", "existing");

            await Assert.ThrowsAsync <IOException>(async() => await File.Copy(
                                                       new CopyInput
            {
                Directory       = TestFileContext.GetAbsolutePath("source"),
                Pattern         = "**/*.txt",
                TargetDirectory = TestFileContext.GetAbsolutePath("target")
            },
                                                       new CopyOptions
            {
                PreserveDirectoryStructure = true,
                CreateTargetDirectories    = true,
                IfTargetFileExists         = FileExistsAction.Throw
            },
                                                       CancellationToken.None));

            Assert.False(TestFileContext.FileExists("target/test.txt"), "Output file should have been rolled back");
            Assert.False(TestFileContext.FileExists("target/test(2).txt"), "Output file should have been rolled back");
        }
Example #14
0
        public void RenameFile()
        {
            var resultsOverWrite = File.Rename(
                new RenameInput()
            {
                Path        = Path.Combine(TestFileContext.RootPath, "folder/foo/sub/test.xml"),
                NewFileName = "newTest.xml"
            },
                new RenameOption()
            {
                RenameBehaviour = FileExistsAction.Overwrite
            });

            Assert.Equal(Path.Combine(TestFileContext.RootPath, "folder\\foo\\sub\\newTest.xml"), resultsOverWrite.Path);

            var resultsCopy = File.Rename(
                new RenameInput()
            {
                Path        = Path.Combine(TestFileContext.RootPath, "folder/foo/sub/newTest.xml"),
                NewFileName = "newTest.xml"
            },
                new RenameOption()
            {
                RenameBehaviour = FileExistsAction.Rename
            });

            Assert.Equal(Path.Combine(TestFileContext.RootPath, "folder\\foo\\sub\\newTest(1).xml"), resultsCopy.Path);

            var results = File.Rename(
                new RenameInput()
            {
                Path        = Path.Combine(TestFileContext.RootPath, "folder/foo/sub/newTest(1).xml"),
                NewFileName = "newTest.xml"
            },
                new RenameOption()
            {
                RenameBehaviour = FileExistsAction.Throw
            });

            Assert.Equal(Path.Combine(TestFileContext.RootPath, "folder\\foo\\sub\\newTest.xml"), results.Path);
            var folderFiles = Directory.GetFiles(Path.Combine(TestFileContext.RootPath, "folder/foo/sub/"));

            Assert.Single(folderFiles);
            TestFileContext.CreateFile("folder/foo/sub/throwTest.xml", "temp");

            var ex = Assert.Throws <IOException>(() => File.Rename(
                                                     new RenameInput()
            {
                Path        = Path.Combine(TestFileContext.RootPath, "folder/foo/sub/newTest.xml"),
                NewFileName = "throwTest.xml"
            },
                                                     new RenameOption()
            {
                RenameBehaviour = FileExistsAction.Throw
            }));

            Assert.Contains("throwTest.xml", ex.Message);

            folderFiles = Directory.GetFiles(Path.Combine(TestFileContext.RootPath, "folder/foo/sub/"));
            Assert.Equal(2, folderFiles.Length);
        }