Пример #1
0
        public void itShouldDeleteOnlyCopiedFilesOnUndo()
        {
            var fileSysCommand = new MockFileSystemCommand();
            var sourceDir      = @"c:\sourceDir";
            var sourceDirFile1 = @"c:\sourceDir\file1.txt";
            var sourceDirFile2 = @"c:\sourceDir\file2.txt";

            var targetDir              = @"c:\targetDir";
            var targetDirFile1         = @"c:\targetDir\file2.txt";
            var dirCopyContentsCommand = new DirectoryCopyContentsCommand(sourceDir, targetDir, fileSysCommand);
            var fakeFileSystem         = new FakeFileSystem(fileSysCommand);

            fakeFileSystem.AddDirectory(sourceDir);
            fakeFileSystem.AddFiles(sourceDirFile1);
            fakeFileSystem.AddFiles(sourceDirFile2);

            fakeFileSystem.AddDirectory(targetDir);
            fakeFileSystem.AddFiles(targetDirFile1);

            dirCopyContentsCommand.Do();

            Assert.IsFalse(dirCopyContentsCommand.DidCommandSucceed);
            Assert.IsTrue(fakeFileSystem.FileExists(targetDirFile1));

            dirCopyContentsCommand.Undo();

            Assert.IsFalse(fakeFileSystem.FileExists(@"c:\targetDir\file1.txt"));
            Assert.IsTrue(fakeFileSystem.FileExists(targetDirFile1));
        }
        public void itShouldRevertNameOnUndoButOnlyForThoseThatWereRenamed()
        {
            string inputFile1 = @"C:\someDir\someFile1.txt";
            string inputFile2 = @"C:\someDir\someFile2.txt";
            string inputFile3 = @"C:\someDir\someFile3.txt";

            string pattern        = "preText-[n]-postText.txt";
            var    fileSysCommand = new MockFileSystemCommand();
            var    fakeFileSystem = new FakeFileSystem(fileSysCommand);

            fakeFileSystem.AddFiles(inputFile1, inputFile2, inputFile3);
            var fileRenameCommand = new MultiFileRenameWithPatternCommand(pattern, fileSysCommand, inputFile1, inputFile2, inputFile3);
            var outputFile1       = @"C:\someDir\preText-someFile1-postText.txt";
            var outputFile2       = @"C:\someDir\preText-someFile2-postText.txt";
            var outputFile3       = @"C:\someDir\preText-someFile3-postText.txt";

            fakeFileSystem.AddFiles(outputFile3);

            fileRenameCommand.Do();

            Assert.IsFalse(fileRenameCommand.DidCommandSucceed);

            fileRenameCommand.Undo();

            Assert.IsFalse(fakeFileSystem.FileExists(outputFile1));
            Assert.IsFalse(fakeFileSystem.FileExists(outputFile2));
            Assert.IsTrue(fakeFileSystem.FileExists(outputFile3));
        }
        public void itShouldRenameUsingDateTimeToken()
        {
            string inputFile1 = @"C:\someDir\someFile1.txt";
            string inputFile2 = @"C:\someDir\someFile2.txt";

            string pattern        = "preText-[n]-[d:MMdd]-postText.txt";
            var    fileSysCommand = new MockFileSystemCommand();
            var    fakeFileSystem = new FakeFileSystem(fileSysCommand);

            fakeFileSystem.AddFiles(inputFile1, inputFile2);
            var renameFileCommand = new MultiFileRenameWithPatternCommand(pattern, fileSysCommand, inputFile1, inputFile2);

            renameFileCommand.Do();

            var computedReplacement = renameFileCommand.DateTimeReference.ToString("MMdd");
            var targetFile1         = $@"C:\someDir\preText-someFile1-{computedReplacement}-postText.txt";
            var targetFile2         = $@"C:\someDir\preText-someFile2-{computedReplacement}-postText.txt";

            Assert.IsTrue(renameFileCommand.DidCommandSucceed);
            Assert.AreEqual(renameFileCommand.RenamedFiles[inputFile1], targetFile1);
            Assert.AreEqual(renameFileCommand.RenamedFiles[inputFile2], targetFile2);

            Assert.IsTrue(fakeFileSystem.FileExists(targetFile1));
            Assert.IsTrue(fakeFileSystem.FileExists(targetFile2));
        }
        public void itShouldRenameUsingFilenameToken()
        {
            string inputFile1 = @"C:\someDir\someFile1.txt";
            string inputFile2 = @"C:\someDir\someFile2.txt";

            string pattern        = "preText-[n]-postText.txt";
            var    fileSysCommand = new MockFileSystemCommand();
            var    fakeFileSystem = new FakeFileSystem(fileSysCommand);

            fakeFileSystem.AddFiles(inputFile1, inputFile2);
            var renameFileCommand = new MultiFileRenameWithPatternCommand(pattern, fileSysCommand, inputFile1, inputFile2);

            renameFileCommand.Do();

            var outputFile1 = @"C:\someDir\preText-someFile1-postText.txt";
            var outputFile2 = @"C:\someDir\preText-someFile2-postText.txt";

            Assert.IsTrue(renameFileCommand.DidCommandSucceed);

            Assert.AreEqual(renameFileCommand.RenamedFiles[inputFile1], outputFile1);
            Assert.AreEqual(renameFileCommand.RenamedFiles[inputFile2], outputFile2);

            Assert.IsTrue(fakeFileSystem.FileExists(outputFile1));
            Assert.IsTrue(fakeFileSystem.FileExists(outputFile2));
        }
        public void itShouldFailIfOneOfTheTargetFilesAlreadyExists()
        {
            var fileSysCommand = new MockFileSystemCommand();
            var fakeFileSystem = new FakeFileSystem(fileSysCommand);

            var    inputFiles        = new string[] { @"C:\someDir\someFile.txt" };
            string pattern           = "preText-[n]-postText.txt";
            var    fileRenameCommand = new MultiFileRenameWithPatternCommand(pattern, fileSysCommand, inputFiles);
            var    outputFile        = @"C:\someDir\preText-someFile-postText.txt";

            fakeFileSystem.AddFiles(inputFiles);
            fakeFileSystem.AddFiles(outputFile);

            fileRenameCommand.Do();

            Assert.IsFalse(fileRenameCommand.DidCommandSucceed);
        }
Пример #6
0
        public void itShouldFailIfAnyOfTheTargetFilesAlreadyExist()
        {
            var fileSysCommand         = new MockFileSystemCommand();
            var sourceDir              = @"c:\sourceDir";
            var sourceDirFile1         = @"c:\sourceDir\file1.txt";
            var targetDir              = @"c:\targetDir";
            var targetDirFile1         = @"c:\targetDir\file1.txt";
            var dirCopyContentsCommand = new DirectoryCopyContentsCommand(sourceDir, targetDir, fileSysCommand);
            var fakeFileSystem         = new FakeFileSystem(fileSysCommand);

            fakeFileSystem.AddDirectory(sourceDir);
            fakeFileSystem.AddFiles(sourceDirFile1);
            fakeFileSystem.AddDirectory(targetDir);
            fakeFileSystem.AddFiles(targetDirFile1);

            dirCopyContentsCommand.Do();

            Assert.IsFalse(dirCopyContentsCommand.DidCommandSucceed);
        }
        public void itMustDeleteDirectoryContentsOnly()
        {
            var fileSysCommand                  = new MockFileSystemCommand();
            var dirToDeleteContents             = @"c:\maindir\subdir";
            var dirDeleteDirContentsOnlyCommand = new DirectoryDeleteContentsOnlyCommandInternal(dirToDeleteContents, @"c:\dummybackupdir", fileSysCommand);
            var fakeFileSystem                  = new FakeFileSystem(fileSysCommand);

            fakeFileSystem.AddDirectory(dirToDeleteContents);
            var file1 = dirToDeleteContents + @"\dummyfile1.txt";
            var file2 = dirToDeleteContents + @"\dummyfile2.txt";

            fakeFileSystem.AddFiles(file1);
            fakeFileSystem.AddFiles(file2);

            dirDeleteDirContentsOnlyCommand.Do();

            Assert.IsTrue(dirDeleteDirContentsOnlyCommand.DidCommandSucceed);
            Assert.IsTrue(fakeFileSystem.DirectoryExists(dirToDeleteContents));
            Assert.IsFalse(fakeFileSystem.FileExists(file1));
            Assert.IsFalse(fakeFileSystem.FileExists(file2));
        }
        public void itShouldFailIfTokenOpeningBracketsAreMismatched()
        {
            string inputFile      = @"C:\someDir\someFile.txt";
            string pattern        = "preText-[[n]-postText.txt";
            var    fileSysCommand = new MockFileSystemCommand();
            var    fakeFileSystem = new FakeFileSystem(fileSysCommand);

            fakeFileSystem.AddFiles(inputFile);
            var fileRenameCommand = new MultiFileRenameWithPatternCommand(pattern, fileSysCommand, inputFile);

            fileRenameCommand.Do();

            Assert.IsFalse(fileRenameCommand.DidCommandSucceed);
        }
        public void itShouldFailIfReplacementTokenIsUnrecognized()
        {
            string inputFile      = @"C:\someDir\someFile.txt";
            string pattern        = "preText-[X:abcd]-postText.txt";
            var    fileSysCommand = new MockFileSystemCommand();
            var    fakeFileSystem = new FakeFileSystem(fileSysCommand);

            fakeFileSystem.AddFiles(inputFile);
            var fileRenameCommand = new MultiFileRenameWithPatternCommand(pattern, fileSysCommand, inputFile);

            fileRenameCommand.Do();

            Assert.IsFalse(fileRenameCommand.DidCommandSucceed);
        }
        public void itShouldRenameUsingMultipleFilenameTokens()
        {
            string inputFile1     = @"C:\someDir\someFile.txt";
            string pattern        = "preText-[n]-postText-[n].txt";
            var    fileSysCommand = new MockFileSystemCommand();
            var    fakeFileSystem = new FakeFileSystem(fileSysCommand);

            fakeFileSystem.AddFiles(inputFile1);
            var renameFileCommand = new MultiFileRenameWithPatternCommand(pattern, fileSysCommand, inputFile1);

            renameFileCommand.Do();

            var targetFile = @"C:\someDir\preText-someFile-postText-someFile.txt";

            Assert.AreEqual(renameFileCommand.RenamedFiles[inputFile1], targetFile);
            Assert.IsTrue(fakeFileSystem.FileExists(targetFile));
        }
        public void itShouldRenameUsingFileExtensionTokenEvenIfSourceHasNoExtension()
        {
            string inputFile      = @"C:\someDir\someFileNoExt";
            string pattern        = "renamed-[n][e]";
            var    fileSysCommand = new MockFileSystemCommand();
            var    fakeFileSystem = new FakeFileSystem(fileSysCommand);

            fakeFileSystem.AddFiles(inputFile);
            var renameFileCommand = new MultiFileRenameWithPatternCommand(pattern, fileSysCommand, inputFile);

            renameFileCommand.Do();

            Assert.IsTrue(renameFileCommand.DidCommandSucceed);
            var outputFile = $@"C:\someDir\renamed-someFileNoExt";

            Assert.AreEqual(renameFileCommand.RenamedFiles[inputFile], outputFile);
            Assert.IsTrue(fakeFileSystem.FileExists(outputFile));
        }
        public void itShouldSucceedWhenThereAreNoReplacementTokens()
        {
            string inputFile      = @"C:\someDir\someFile.txt";
            string pattern        = "newFileName.txt";
            var    fileSysCommand = new MockFileSystemCommand();
            var    fakeFileSystem = new FakeFileSystem(fileSysCommand);

            fakeFileSystem.AddFiles(inputFile);
            var fileRenameCommand = new MultiFileRenameWithPatternCommand(pattern, fileSysCommand, inputFile);

            fileRenameCommand.Do();

            Assert.IsTrue(fileRenameCommand.DidCommandSucceed);
            var outputFile = @"C:\someDir\newFileName.txt";

            Assert.AreEqual(fileRenameCommand.RenamedFiles[inputFile], outputFile);
            Assert.IsTrue(fakeFileSystem.FileExists(outputFile));
        }
        public void itShouldRenameUsingFilenameAndDateTimeToken()
        {
            string inputFile      = @"C:\someDir\someFile.txt";
            string pattern        = "preText-[n]-postText-[d:MMdd].txt";
            var    fileSysCommand = new MockFileSystemCommand();
            var    fakeFileSystem = new FakeFileSystem(fileSysCommand);

            fakeFileSystem.AddFiles(inputFile);
            var renameFileCommand = new MultiFileRenameWithPatternCommand(pattern, fileSysCommand, inputFile);

            renameFileCommand.Do();

            Assert.IsTrue(renameFileCommand.DidCommandSucceed);
            var computedReplacement = renameFileCommand.DateTimeReference.ToString("MMdd");
            var outputFile          = $@"C:\someDir\preText-someFile-postText-{computedReplacement}.txt";

            Assert.AreEqual(renameFileCommand.RenamedFiles[inputFile], outputFile);
            Assert.IsTrue(fakeFileSystem.FileExists(outputFile));
        }
        public void itShouldRevertNameOnUndo()
        {
            string inputFile      = @"C:\someDir\someFile.txt";
            string pattern        = "preText-[n]-postText.txt";
            var    fileSysCommand = new MockFileSystemCommand();
            var    fakeFileSystem = new FakeFileSystem(fileSysCommand);

            fakeFileSystem.AddFiles(inputFile);
            var fileRenameCommand = new MultiFileRenameWithPatternCommand(pattern, fileSysCommand, inputFile);

            fileRenameCommand.Do();

            Assert.IsTrue(fileRenameCommand.DidCommandSucceed);
            var outputFile = @"C:\someDir\preText-someFile-postText.txt";

            Assert.AreEqual(fileRenameCommand.RenamedFiles[inputFile], outputFile);
            Assert.IsTrue(fakeFileSystem.FileExists(outputFile));
            Assert.IsFalse(fakeFileSystem.FileExists(inputFile));

            fileRenameCommand.Undo();

            Assert.IsFalse(fakeFileSystem.FileExists(outputFile));
            Assert.IsTrue(fakeFileSystem.FileExists(inputFile));
        }