private void AttemptBackup(FileInfo f) { var version = new GenerationalFileBackupInfo(f); if (versions.Contains(version)) return; if(version.CurrentGeneration > maxBackups) { f.Delete(); return; } if (version.NextGeneration > maxBackups) { return; } FileInfo destination = version.NextFile; if (destination.Exists) { destination.Delete(); } versions.Add(version); File.Copy(f.FullName, destination.FullName); }
public void if_the_file_is_versioned_the_next_file_has_the_next_gen_appended_to_filename() { const string ROOT_FILE = "original.file.txt.1"; var version = new GenerationalFileBackupInfo(new FileInfo(ROOT_FILE)); Assert.AreEqual("original.file.txt.1", version.CurrentFile.Name); Assert.AreEqual("original.file.txt.002", version.NextFile.Name); }
public void if_the_file_is_versioned_the_currentGen_is_version_and_nextGen_is_incremented_by_1() { const string ROOT_FILE = "original.file.txt.1"; var version = new GenerationalFileBackupInfo(new FileInfo(ROOT_FILE)); Assert.AreEqual(1, version.CurrentGeneration); Assert.AreEqual(2, version.NextGeneration); }
public void if_the_file_is_not_versioned_the_currentGen_is_0_and_the_next_gen_is_1() { const string ROOT_FILE = "original.file.txt"; var version = new GenerationalFileBackupInfo(new FileInfo(ROOT_FILE)); Assert.AreEqual(0, version.CurrentGeneration); Assert.AreEqual(1, version.NextGeneration); }
public void if_at_max_backups_only_keep_newest() { FileInfo file = CreateFile(); var backup = new RollingFileBackup(file, 5); IEnumerable<IFileBackupInfo> versions = new GenerationalFileBackupInfo[0]; for (int i = 0; i < 10; i++) { versions = backup.Create(); } List<FileInfo> files = GetFiles(); //original + 5 backups Assert.AreEqual(6, files.Count); Assert.AreEqual(5, versions.Count()); }