public void SoftDeleteWhenNoFile() { _file.SoftDelete(); AssertFile.Exists(false, _file); AssertFile.Exists(false, _softDeleteFile); AssertFile.Exists(true, _backup); }
public void FileIndexCounterIncrementsWhenFileExists() { const string newDirName = "IndexIncLogDir"; if (Directory.Exists(newDirName)) { Directory.Delete(newDirName, true); } var fileLogger = new FileLogger("testLogger", newDirName); var loggerDate = new DateTime(2001, 1, 1, 0, 0, 0); var fixedTimeProvider = new FixedTimeProvider(loggerDate); fileLogger.TimeProvider = fixedTimeProvider; fileLogger.Open(); fileLogger.Dispose(); fileLogger = new FileLogger("testLogger", newDirName); fileLogger.TimeProvider = fixedTimeProvider; fileLogger.Open(); fileLogger.Dispose(); var expectedFilename = string.Format("{0}-1.log.txt", loggerDate.ToString("yyyy-MM-dd HH-mm")); var expectedFilePath = Path.Combine(newDirName, expectedFilename); AssertFile.Exists(expectedFilePath); }
public void WhenSizeIsNotEqual_ThenAssertFalse() { var file1 = BaseDir.CreateFile("SizeEquals-Test1.txt", "test1"); var file2 = BaseDir.CreateFile("SizeEquals-Test2.txt", "test12"); Assert.Throws <AssertionException>(() => AssertFile.SizeEquals(file1, file2)); }
public void SoftDeleteWhenOnlySoftFile() { _softDeleteFile.VoidCreate(); _file.SoftDelete(); AssertFile.Exists(false, _file); AssertFile.Exists(true, _softDeleteFile); }
public void WhenSizeIsEqual_ThenAssertTrue() { var file1 = BaseDir.CreateFile("SizeEquals-Test1.txt"); var file2 = BaseDir.CreateFile("SizeEquals-Test2.txt"); Assert.DoesNotThrow(() => AssertFile.SizeEquals(file1, file2)); }
public void DeleteFileName(bool deleteBackups) { if (this.CreateRepository() is IDataRepository dataRepository) { var file = CreateTestFile(dataRepository.Settings); var softDelete = file.SoftDeleteFile(); file.CreateFileOnDisk(); softDelete.CreateFileOnDisk(); if (dataRepository.Settings.BackupSettings != null) { BackupFile.CreateFor(file, dataRepository.Settings.BackupSettings).CreateFileOnDisk(); } AssertFile.Exists(true, file); AssertFile.Exists(true, softDelete); dataRepository.Delete(file.Name, deleteBackups); AssertFile.Exists(false, file); AssertFile.Exists(false, softDelete); if (dataRepository.Settings.BackupSettings != null) { AssertFile.Exists(!deleteBackups, BackupFile.CreateFor(file, dataRepository.Settings.BackupSettings)); } } }
public void RenameGeneric(bool hasBackup, bool hasSoft) { var repository = this.CreateRepository(); var file = CreateTestFile(repository.Settings, nameof(DummySerializable)); file.CreateFileOnDisk(); if (hasBackup && repository.Settings.BackupSettings != null) { BackupFile.CreateFor(file, repository.Settings.BackupSettings).CreateFileOnDisk(); } if (hasSoft) { file.SoftDeleteFile().CreateFileOnDisk(); } repository.Rename <DummySerializable>("NewName", false); AssertFile.Exists(false, file); AssertFile.Exists(true, file.WithNewName("NewName", repository.Settings)); if (hasBackup && repository.Settings.BackupSettings != null) { AssertFile.Exists(false, BackupFile.CreateFor(file, repository.Settings.BackupSettings)); AssertFile.Exists(true, BackupFile.CreateFor(file.WithNewName("NewName", repository.Settings), repository.Settings.BackupSettings)); } if (hasSoft) { AssertFile.Exists(false, file.SoftDeleteFile()); AssertFile.Exists(true, file.WithNewName("NewName", repository.Settings).SoftDeleteFile()); } }
public static void TryRestoreWhenHasSoftDeleteAndBackup() { var dummy = new DummySerializable(1); var file = CreateFile(); var backup = CreateBackupFile(); var softDelete = file.SoftDeleteFile(); backup.Save(dummy); dummy.Value++; softDelete.Save(dummy); AssertFile.Exists(false, file); AssertFile.Exists(true, softDelete); AssertFile.Exists(true, backup); var backuper = Backuper.Create(new BackupSettings(Directory.FullName, BackupSettings.DefaultExtension, BackupSettings.DefaultTimeStampFormat, 2, 3)); Assert.IsTrue(backuper.CanRestore(file)); Assert.IsTrue(backuper.TryRestore(file)); Assert.AreEqual(dummy, file.Read <DummySerializable>()); AssertFile.Exists(true, file); AssertFile.Exists(false, softDelete); AssertFile.Exists(true, backup); }
public void RenameType(bool hasBackup, bool hasSoft) { if (!IsBackingUp && hasBackup) { Assert.Inconclusive("Not a relevant test for this config"); return; // due to inheritance } _dummyFile.VoidCreate(); if (hasBackup) { _dummyBackup.VoidCreate(); } if (hasSoft) { _dummySoftDelete.VoidCreate(); } Repository.Rename <DummySerializable>(NewName, false); AssertFile.Exists(true, _dummyNewName); if (hasBackup) { AssertFile.Exists(true, _dummyBackupNewName); } if (hasSoft) { AssertFile.Exists(true, _dummySoftDeleteNewName); } }
public void SoftDeleteWhenOnlySoftFile() { this.softDeleteFile.CreatePlaceHolder(); this.file.SoftDelete(); AssertFile.Exists(false, this.file); AssertFile.Exists(true, this.softDeleteFile); }
public void SaveTwice(TestCase testCase) { var dummy = new DummySerializable(1); var repository = this.CreateRepository(); var file = testCase.File <DummySerializable>(repository); var backupFile = testCase.BackupFile <DummySerializable>(repository); AssertFile.Exists(false, file); testCase.Save(repository, file, dummy); AssertFile.Exists(true, file); AssertFile.Exists(false, file.SoftDeleteFile()); AssertFile.Exists(false, file.TempFile(repository.Settings)); if (repository.Settings.BackupSettings != null) { AssertFile.Exists(false, backupFile !); } var read = this.Read <DummySerializable>(file); Assert.AreEqual(dummy.Value, read.Value); Assert.AreNotSame(dummy, read); testCase.Save(repository, file, dummy); AssertFile.Exists(true, file); AssertFile.Exists(false, file.SoftDeleteFile()); AssertFile.Exists(false, file.TempFile(repository.Settings)); if (repository.Settings.BackupSettings != null) { AssertFile.Exists(true, backupFile !); } read = this.Read <DummySerializable>(file); Assert.AreEqual(dummy.Value, read.Value); Assert.AreNotSame(dummy, read); }
public void RenameFileName(bool hasBackup, bool hasSoft) { this.NamedFiles.File.CreatePlaceHolder(); if (hasBackup && this.IsBackingUp) { this.NamedFiles.Backup.CreatePlaceHolder(); } if (hasSoft) { this.NamedFiles.SoftDelete.CreatePlaceHolder(); } this.Repository.Rename(this.NamedFiles.File, "NewName", false); AssertFile.Exists(true, this.NamedFiles.WithNewName); if (hasBackup && this.IsBackingUp) { AssertFile.Exists(false, this.NamedFiles.Backup); AssertFile.Exists(true, this.NamedFiles.BackupNewName); } if (hasSoft) { AssertFile.Exists(false, this.NamedFiles.SoftDelete); AssertFile.Exists(true, this.NamedFiles.SoftDeleteNewName); } }
public async Task SaveTypeAsyncNull() { await this.Repository.SaveAsync(this.dummy).ConfigureAwait(false); AssertFile.Exists(true, this.TypeFiles.File); if (this.IsBackingUp) { AssertFile.Exists(false, this.TypeFiles.Backup); } if ((this.Settings as IDataRepositorySettings)?.SaveNullDeletesFile == true) { await this.Repository.SaveAsync <DummySerializable>(this.TypeFiles.File, null).ConfigureAwait(false); AssertFile.Exists(false, this.TypeFiles.File); if (this.IsBackingUp) { AssertFile.Exists(true, this.TypeFiles.Backup); } } else { Assert.ThrowsAsync <ArgumentNullException>(() => this.Repository.SaveAsync <DummySerializable>(null)); } }
public void RenameType(bool hasBackup, bool hasSoft) { this.TypeFiles.File.CreatePlaceHolder(); if (hasBackup && this.IsBackingUp) { this.TypeFiles.Backup.CreatePlaceHolder(); } if (hasSoft) { this.TypeFiles.SoftDelete.CreatePlaceHolder(); } this.Repository.Rename <DummySerializable>("NewName", false); AssertFile.Exists(true, this.TypeFiles.WithNewName); if (hasBackup && this.IsBackingUp) { AssertFile.Exists(false, this.TypeFiles.Backup); AssertFile.Exists(true, this.TypeFiles.BackupNewName); } if (hasSoft) { AssertFile.Exists(false, this.TypeFiles.SoftDelete); AssertFile.Exists(true, this.TypeFiles.SoftDeleteNewName); } }
public void SaveLongListThenShortListFile() { var list = new List <DummySerializable> { this.dummy, new DummySerializable(2) }; this.Repository.Save(this.NamedFiles.File, list); AssertFile.Exists(true, this.NamedFiles.File); if (this.IsBackingUp) { AssertFile.Exists(false, this.NamedFiles.Backup); } var read = this.Read <List <DummySerializable> >(this.NamedFiles.File); CollectionAssert.AreEqual(list, read); Assert.AreNotSame(this.dummy, read); list.RemoveAt(1); this.Repository.Save(this.NamedFiles.File, list); AssertFile.Exists(true, this.NamedFiles.File); read = this.Read <List <DummySerializable> >(this.NamedFiles.File); CollectionAssert.AreEqual(list, read); Assert.AreNotSame(this.dummy, read); }
public async Task SaveFileNameAsync() { await this.Repository.SaveAsync(this.NamedFiles.File, this.dummy).ConfigureAwait(false); AssertFile.Exists(true, this.NamedFiles.File); if (this.IsBackingUp) { AssertFile.Exists(false, this.NamedFiles.Backup); } AssertFile.Exists(false, this.NamedFiles.SoftDelete); await this.Repository.SaveAsync(this.NamedFiles.File, this.dummy).ConfigureAwait(false); AssertFile.Exists(true, this.NamedFiles.File); AssertFile.Exists(false, this.NamedFiles.SoftDelete); if (this.IsBackingUp) { AssertFile.Exists(true, this.NamedFiles.Backup); } var read = this.Read <DummySerializable>(this.NamedFiles.File); Assert.AreEqual(this.dummy.Value, read.Value); Assert.AreNotSame(this.dummy, read); }
public void DeleteName(bool deleteBakups) { var dataRepository = this.Repository as IDataRepository; if (dataRepository == null) { return; } this.NamedFiles.File.CreatePlaceHolder(); this.NamedFiles.SoftDelete.CreatePlaceHolder(); if (this.IsBackingUp) { this.NamedFiles.Backup.CreatePlaceHolder(); AssertFile.Exists(true, this.NamedFiles.Backup); } AssertFile.Exists(true, this.NamedFiles.File); AssertFile.Exists(true, this.NamedFiles.SoftDelete); dataRepository.Delete(this.NamedFiles.File, deleteBakups); AssertFile.Exists(false, this.NamedFiles.File); AssertFile.Exists(false, this.NamedFiles.SoftDelete); if (this.IsBackingUp) { AssertFile.Exists(!deleteBakups, this.NamedFiles.Backup); } }
public void SoftDeleteWhenNoFile() { this.file.SoftDelete(); AssertFile.Exists(false, this.file); AssertFile.Exists(false, this.softDeleteFile); AssertFile.Exists(true, this.backup); }
public void WhenReplaceMultipleLines_ThenDeleteLines() { const string text = "Line1\n" + "\n" + "Line3\r\n" + "Line4\n" + "Line5\r\n" + "Line6"; const string expected = "Line1\n" + "New Line2\n" + "Line3\r\n" + "\n" + "\r\n" + "New Line6"; var sut = FileBuilder.InFileSystem.WithText(text).WithPath(GetAbsolutePath("ReplaceLines2.txt")).Build(); var result = sut.ReplaceLines(new Dictionary <int, string> { { 2, "New Line2" }, { 4, "" }, { 5, null }, { 6, "New Line6\n" } }, GetAbsolutePath("ReplaceLine2-Output.txt")); AssertFile.ContentEquals(result, expected); }
public override void Specify() { var configuredRunspace = arrange(() => PowershallRunspaceFactory.GetPowershellRunspace()); it("can set hosts when hosts file is empty", () => { var address = "192.168.0.12"; var hostName = "someserver.net"; var hostsFile = ReadWriteScenario.GetFileWithContents(@"", Encoding.UTF8); run_set_host_command(configuredRunspace, hostName, address, hostsFile); Encoding ignored; var result = ReadWriteScenario.ReadFileContents(hostsFile, out ignored); expect(() => result == "192.168.0.12\t\tsomeserver.net\r\n"); }); it("can set hosts when host file is nonempty", () => { var address = "192.168.0.12"; var hostName = "someserver.net"; var hostsFile = ReadWriteScenario.GetFileWithContents(@" #line 1 meh 127.0.0.1 someotherserver.net ", Encoding.UTF8); run_set_host_command(configuredRunspace, hostName, address, hostsFile); AssertFile.MatchesIgnoringNewlines(hostsFile, @" #line 1 meh 192.168.0.12" + "\t\t" + @"someserver.net 127.0.0.1 someotherserver.net "); }); it("can set multiple host files at once", () => { var hostsFile = ReadWriteScenario.GetFileWithContents(@"", Encoding.UTF8); using (var command = configuredRunspace.CreatePipeline( string.Format("@(@('someserver.net', '192.168.0.12'), @('someOtherServer.net', '192.168.0.1')) | set-HostsFileEntry -f \"{0}\"", hostsFile))) { var results = command.Invoke(); expect(() => results.Count() == 0); } Encoding ignored; var result = ReadWriteScenario.ReadFileContents(hostsFile, out ignored); expect(() => result == "192.168.0.12\t\tsomeserver.net\r\n192.168.0.1\t\tsomeOtherServer.net\r\n"); }); }
public void SaveFileName() { AssertFile.Exists(false, this.NamedFiles.File); this.Repository.Save(this.NamedFiles.File, this.dummy); AssertFile.Exists(true, this.NamedFiles.File); AssertFile.Exists(false, this.NamedFiles.SoftDelete); if (this.IsBackingUp) { AssertFile.Exists(false, this.NamedFiles.Backup); } this.dummy.Value++; this.Repository.Save(this.NamedFiles.File, this.dummy); AssertFile.Exists(true, this.NamedFiles.File); AssertFile.Exists(false, this.NamedFiles.SoftDelete); if (this.IsBackingUp) { AssertFile.Exists(true, this.NamedFiles.Backup); } var read = this.Read <DummySerializable>(this.NamedFiles.File); Assert.AreEqual(this.dummy.Value, read.Value); Assert.AreNotSame(this.dummy, read); }
public void WhenNoFileExistsOnDisk_ThenSaveToDisk() { var sut = AssemblyEmbeddedResource.CreateFromAssemblyContaining <AssemblyEmbeddedResourceTests>(TestFileNames.ExistingEmbeddedFile); var fileInfo = sut.Save(GetWorkingPath(sut.FileName)); AssertFile.ContentEquals(fileInfo, "Embedded Resource 1"); }
public void WhenFileDoesNotExist_ThenDoNothing() { var sut = new FileInfo(Path.Combine(WorkingDir, "DoesntExist")); sut.DeleteIfExists(); AssertFile.NotExists(sut); }
public void SavesSettingsFile() { var file = this.Directory.CreateFileInfoInDirectory(nameof(DataRepositorySettings) + ".cfg"); AssertFile.Exists(false, file); _ = this.CreateRepository(); AssertFile.Exists(true, file); }
public void WhenFileIsEmpty_ThenDeleteNothing() { var sut = FileBuilder.InFileSystem.WithPath(GetAbsolutePath("DeleteLastLine1.txt")).Build(); var result = sut.DeleteLastLine(GetAbsolutePath("DeleteLastLine1-Output.txt")); AssertFile.IsEmpty(result); }
public void WhenLineDoesNotExist_ThenDeleteNothing() { var sut = FileBuilder.InFileSystem.WithText("Line 1").WithPath(GetAbsolutePath("DeleteLine5.txt")).Build(); var result = sut.DeleteLine(2, GetAbsolutePath("DeleteLine5-Output.txt")); AssertFile.AreSame(result, sut); }
public void WhenFileHasOneLine_ThenDeleteLine() { var sut = FileBuilder.InFileSystem.WithText("Line 1").WithPath(GetAbsolutePath("DeleteLastLine2.txt")).Build(); var result = sut.DeleteLastLine(GetAbsolutePath("DeleteLastLine2-Output.txt")); AssertFile.IsEmpty(result); }
public void WhenFileHasNoExtension_ThenDoNothing() { var sut = FileBuilder.InFileSystem.WithPath(Path.Combine(WorkingDir, "Test")).Build(); sut.RemoveExtension(); AssertFile.Exists(Path.Combine(WorkingDir, "Test")); }
public void IsUnchanged() => AssertFile.Contains( "api-netcoreapp2_1.txt", ApiGenerator .GeneratePublicApi( typeof(Targets).Assembly, typeof(Targets).Assembly.GetExportedTypes().Where(type => !type.Namespace.Contains("Internal")).ToArray()) .Replace(Environment.NewLine, "\r\n"));
public void WhenFileIsEmpty_ThenReplaceNothing() { var sut = FileBuilder.InFileSystem.WithPath(GetAbsolutePath("ReplaceLine1.txt")).Build(); var result = sut.ReplaceLine(1, "New Line 1", GetAbsolutePath("ReplaceLine1-Output.txt")); AssertFile.IsEmpty(result); }