public void TestBackupCopy()
        {
            string rootWorkingDir = Path.Combine(testRoot, "BackupCopyBase");

            var dirs = TestDirectory.Create(rootWorkingDir);

            string rootSourceDir = dirs.Item1;
            string rootTargetDir = dirs.Item2;

            // Create settings
            BackupSettings settings = new()
            {
                IgnoreHiddenFiles = false,
                TargetDirectory   = rootTargetDir,
                SourceDirectories = new string[] { rootSourceDir }
            };

            BackupTaskCopy task = new()
            {
                RetryEnabled         = false,
                MinFileWriteWaitTime = 0
            };

            // Add handler to main copy test for debugging output
            task.Log += Task_Log;

            /////////////////////////////////////
            // Copy files
            /////////////////////////////////////
            int filesCopied = task.Run(settings);

            // Filter source files that should have been copied
            var sourceFiles = Directory.EnumerateFiles(rootSourceDir, "*.*", SearchOption.AllDirectories);

            // Check task returned expected number of files
            Assert.AreEqual(sourceFiles.Count(), filesCopied);

            // Compare directories
            int targetCount = VerifyBackup(sourceFiles, rootTargetDir);

            // Check expected number of files were copied
            Assert.AreEqual(sourceFiles.Count(), targetCount);

            /////////////////////////////////////
            // Run copy again
            /////////////////////////////////////
            filesCopied = task.Run(settings);

            // Should be no new copies - nothing changed
            Assert.AreEqual(0, filesCopied);

            // Compare directories
            targetCount = VerifyBackup(sourceFiles, rootTargetDir);

            // Check expected number of files were copied
            Assert.AreEqual(sourceFiles.Count(), targetCount);

            /////////////////////////////////////
            // Add files, run copy again
            /////////////////////////////////////
            // Add first file to root
            string addedFile1 = Path.Combine(rootSourceDir, "added-file1.txt");

            TestFile.Create(addedFile1);

            // Add second file to a new sub dir
            DirectoryInfo addedDirInfo = Directory.CreateDirectory(Path.Combine(rootSourceDir, "added-dir"));
            string        addedFile2   = Path.Combine(addedDirInfo.FullName, "added-file2.txt");

            TestFile.Create(addedFile2);

            // Run backup again
            filesCopied = task.Run(settings);

            // Only the new files should be copied
            Assert.AreEqual(2, filesCopied);

            // Compare directories
            targetCount = VerifyBackup(sourceFiles, rootTargetDir);

            // Check expected number of files were copied
            Assert.AreEqual(sourceFiles.Count(), targetCount);

            /////////////////////////////////////
            // Modify file, run copy again
            /////////////////////////////////////
            TestFile.Modify(addedFile1);

            filesCopied = task.Run(settings);

            // Should only be modified file copied
            Assert.AreEqual(1, filesCopied);

            // Compare directories
            targetCount = VerifyBackup(sourceFiles, rootTargetDir);

            // Check expected number of files were copied
            Assert.AreEqual(sourceFiles.Count(), targetCount);

            /////////////////////////////////////
            // Delete file, run copy again
            /////////////////////////////////////
            File.Delete(addedFile1);

            filesCopied = task.Run(settings);

            // Should be nothing changed, added file remains in target
            Assert.AreEqual(0, filesCopied);

            // Compare directories
            targetCount = VerifyBackup(sourceFiles, rootTargetDir);

            // Target should have one more file than source
            Assert.AreEqual(sourceFiles.Count() + 1, targetCount);

            // Remove handler
            task.Log -= Task_Log;
        }

        [TestMethod]
Esempio n. 2
0
        public void TestBackupSync()
        {
            string rootWorkingDir = Path.Combine(testRoot, "BackupSyncBase");

            var dirs = TestDirectory.Create(rootWorkingDir);

            string rootSourceDir = dirs.Item1;
            string rootTargetDir = dirs.Item2;

            // Create settings
            BackupSettings settings = new()
            {
                IgnoreHiddenFiles = false,
                TargetDirectory   = rootTargetDir,
                SourceDirectories = new string[] { rootSourceDir }
            };

            BackupTaskSync task = new()
            {
                RetryEnabled         = false,
                MinFileWriteWaitTime = 0
            };

            // Add handler to main copy test for debugging output
            task.Log += Task_Log;

            /////////////////////////////////////
            // Copy files
            /////////////////////////////////////
            int filesCopied = task.Run(settings);

            // Filter source files that should have been copied
            var sourceFiles = Directory.EnumerateFiles(rootSourceDir, "*.*", SearchOption.AllDirectories);

            // Check task returned expected number of files
            Assert.AreEqual(sourceFiles.Count(), filesCopied);

            // Compare directories
            int targetCount = VerifyBackup(sourceFiles, rootTargetDir);

            // Check expected number of files were copied
            Assert.AreEqual(sourceFiles.Count(), targetCount);

            /////////////////////////////////////
            // Run copy again
            /////////////////////////////////////
            filesCopied = task.Run(settings);

            // Should be no new copies - nothing changed
            Assert.AreEqual(0, filesCopied);

            // Compare directories
            targetCount = VerifyBackup(sourceFiles, rootTargetDir);

            // Check expected number of files were copied
            Assert.AreEqual(sourceFiles.Count(), targetCount);

            /////////////////////////////////////
            // Add files, run copy again
            /////////////////////////////////////
            // Add first file to root
            string addedFile1 = Path.Combine(rootSourceDir, "added-file1.txt");

            TestFile.Create(addedFile1);

            // Add second file to a new sub dir
            DirectoryInfo addedDirInfo = Directory.CreateDirectory(Path.Combine(rootSourceDir, "added-dir"));
            string        addedFile2   = Path.Combine(addedDirInfo.FullName, "added-file2.txt");

            TestFile.Create(addedFile2);

            // Run backup again
            filesCopied = task.Run(settings);

            // Only the new files should be copied
            Assert.AreEqual(2, filesCopied);

            // Compare directories
            targetCount = VerifyBackup(sourceFiles, rootTargetDir);

            // Check expected number of files were copied
            Assert.AreEqual(sourceFiles.Count(), targetCount);

            /////////////////////////////////////
            // Modify file, run copy again
            /////////////////////////////////////
            TestFile.Modify(addedFile1);

            filesCopied = task.Run(settings);

            // Should only be modified file copied
            Assert.AreEqual(1, filesCopied);

            // Compare directories
            targetCount = VerifyBackup(sourceFiles, rootTargetDir);

            // Check expected number of files were copied
            Assert.AreEqual(sourceFiles.Count(), targetCount);

            /////////////////////////////////////
            // Delete file, run copy again
            /////////////////////////////////////
            File.Delete(addedFile1);

            filesCopied = task.Run(settings);

            // Should be nothing added, new file removed from target
            Assert.AreEqual(0, filesCopied);

            // Compare directories
            targetCount = VerifyBackup(sourceFiles, rootTargetDir);

            // File should also have been deleted from target
            Assert.AreEqual(sourceFiles.Count(), targetCount);

            /////////////////////////////////////
            // Delete directory, run copy again
            /////////////////////////////////////
            addedDirInfo.Delete(true);

            filesCopied = task.Run(settings);

            // Should be nothing added
            Assert.AreEqual(0, filesCopied);

            // Compare directories
            targetCount = VerifyBackup(sourceFiles, rootTargetDir);

            // Dir and contents should also have been deleted from target
            Assert.AreEqual(sourceFiles.Count(), targetCount);

            /////////////////////////////////////
            // Change hidden files, run copy again
            /////////////////////////////////////
            settings.IgnoreHiddenFiles = true;

            // Return non-hidden
            static bool sourceFilter(string f) => !File.GetAttributes(f).HasFlag(FileAttributes.Hidden) && !new DirectoryInfo(Path.GetDirectoryName(f) !).Attributes.HasFlag(FileAttributes.Hidden);

            // Refresh expected source files
            sourceFiles = Directory.EnumerateFiles(rootSourceDir, "*.*", SearchOption.AllDirectories).Where(sourceFilter);

            // Run task again
            filesCopied = task.Run(settings);

            // Should be nothing added
            Assert.AreEqual(0, filesCopied);

            // Compare directories
            targetCount = VerifyBackup(sourceFiles, rootTargetDir);

            // Dir and contents should also have been deleted from target
            Assert.AreEqual(sourceFiles.Count(), targetCount);

            // Remove handler
            task.Log -= Task_Log;
        }