public void MockFileSystem_AddFile_ShouldRepaceExistingFile() { const string path = @"c:\some\file.txt"; const string existingContent = "Existing content"; var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData> { { path, new MockFileData(existingContent) } }); Assert.That(fileSystem.GetFile(path).TextContents, Is.EqualTo(existingContent)); const string newContent = "New content"; fileSystem.AddFile(path, new MockFileData(newContent)); Assert.That(fileSystem.GetFile(path).TextContents, Is.EqualTo(newContent)); }
public void Mockfile_Create_OverwritesExistingFile() { string path = XFS.Path(@"c:\some\file.txt"); var fileSystem = new MockFileSystem(); var mockFile = new MockFile(fileSystem); fileSystem.Directory.CreateDirectory(Path.GetDirectoryName(path)); // Create a file using (var stream = mockFile.Create(path)) { var contents = new UTF8Encoding(false).GetBytes("Test 1"); stream.Write(contents, 0, contents.Length); } // Create new file that should overwrite existing file var expectedContents = new UTF8Encoding(false).GetBytes("Test 2"); using (var stream = mockFile.Create(path)) { stream.Write(expectedContents, 0, expectedContents.Length); } var actualContents = fileSystem.GetFile(path).Contents; Assert.That(actualContents, Is.EqualTo(expectedContents)); }
public void MockFileStream_Flush_WritesByteToFile() { // Arrange var filepath = XFS.Path(@"c:\something\foo.txt"); var filesystem = new MockFileSystem(new Dictionary<string, MockFileData>()); var cut = new MockFileStream(filesystem, filepath); // Act cut.WriteByte(255); cut.Flush(); // Assert CollectionAssert.AreEqual(new byte[]{255}, filesystem.GetFile(filepath).Contents); }
public void MockFile_WriteAllText_ShouldOverriteAnExistingFile() { // http://msdn.microsoft.com/en-us/library/ms143375.aspx // Arrange string path = XFS.Path(@"c:\something\demo.txt"); var fileSystem = new MockFileSystem(); // Act fileSystem.File.WriteAllText(path, "foo"); fileSystem.File.WriteAllText(path, "bar"); // Assert Assert.AreEqual("bar", fileSystem.GetFile(path).TextContents); }
public void MockFile_WriteAllText_ShouldWriteTextFileToMemoryFileSystem() { // Arrange string path = XFS.Path(@"c:\something\demo.txt"); string fileContent = "Hello there!"; var fileSystem = new MockFileSystem(); // Act fileSystem.File.WriteAllText(path, fileContent); // Assert Assert.AreEqual( fileContent, fileSystem.GetFile(path).TextContents); }
public void MockFile_Copy_ShouldCreateFileAtNewDestination() { string sourceFileName = XFS.Path(@"c:\source\demo.txt"); var sourceContents = new MockFileData("Source content"); string destFileName = XFS.Path(@"c:\source\demo_copy.txt"); var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData> { {sourceFileName, sourceContents} }); fileSystem.File.Copy(sourceFileName, destFileName, false); var copyResult = fileSystem.GetFile(destFileName); Assert.AreEqual(copyResult.Contents, sourceContents.Contents); }
public void MockFile_WriteAllBytes_ShouldWriteDataToMemoryFileSystem() { // Arrange string path = XFS.Path(@"c:\something\demo.txt"); var fileSystem = new MockFileSystem(); var fileContent = new byte[] { 1, 2, 3, 4 }; // Act fileSystem.File.WriteAllBytes(path, fileContent); // Assert Assert.AreEqual( fileContent, fileSystem.GetFile(path).Contents); }
public void MockFileSystem_GetFile_ShouldReturnNullWhenFileIsNotRegistered() { // Arrange var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData> { { @"c:\something\demo.txt", new MockFileData("Demo\r\ntext\ncontent\rvalue") }, { @"c:\something\other.gif", new MockFileData(new byte[] { 0x21, 0x58, 0x3f, 0xa9 }) } }); // Act var result = fileSystem.GetFile(@"c:\something\else.txt"); // Assert Assert.IsNull(result); }
public void MockFile_Copy_ShouldOverwriteFileWhenOverwriteFlagIsTrue() { string sourceFileName = XFS.Path(@"c:\source\demo.txt"); var sourceContents = new MockFileData("Source content"); string destFileName = XFS.Path(@"c:\destination\demo.txt"); var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData> { {sourceFileName, sourceContents}, {destFileName, new MockFileData("Destination content")} }); fileSystem.File.Copy(sourceFileName, destFileName, true); var copyResult = fileSystem.GetFile(destFileName); Assert.AreEqual(copyResult.Contents, sourceContents.Contents); }
public void MockFileSystem_GetFile_ShouldReturnFileRegisteredInConstructorWhenPathsDifferByCase() { // Arrange var file1 = new MockFileData("Demo\r\ntext\ncontent\rvalue"); var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData> { { @"c:\something\demo.txt", file1 }, { @"c:\something\other.gif", new MockFileData(new byte[] { 0x21, 0x58, 0x3f, 0xa9 }) } }); // Act var result = fileSystem.GetFile(@"c:\SomeThing\DEMO.txt"); // Assert Assert.AreEqual(file1, result); }
public void MockFile_WriteAllText_ShouldOverriteAnExistingFile() { // http://msdn.microsoft.com/en-us/library/ms143375.aspx // Arrange string path = XFS.Path(@"c:\something\demo.txt"); var fileSystem = new MockFileSystem(); fileSystem.AddDirectory(XFS.Path(@"c:\something")); // Act fileSystem.File.WriteAllText(path, "foo"); fileSystem.File.WriteAllText(path, "bar"); // Assert Assert.AreEqual("bar", fileSystem.GetFile(path).TextContents); }
public void ResetSaveFile_newFileCreated() { var mockFileSystem = new MockFileSystem(); var mockInputFile = new MockFileData("test1\test2\test3"); mockFileSystem.AddFile(@"C:\temp\in.txt", mockInputFile); var sut = new WeatherAPI(mockFileSystem, null); sut.ResetSaveFile(@"C:\temp\in.txt"); MockFileData mockOutputFile = mockFileSystem.GetFile(@"C:\temp\in.txt"); string[] outputLines = mockOutputFile.TextContents.SplitLines(); Assert.That(outputLines.Count, Is.EqualTo(0)); }
public void MockFile_WriteAllBytes_ShouldWriteDataToMemoryFileSystem() { // Arrange string path = XFS.Path(@"c:\something\demo.txt"); var fileSystem = new MockFileSystem(); var fileContent = new byte[] { 1, 2, 3, 4 }; fileSystem.AddDirectory(@"c:\something"); // Act fileSystem.File.WriteAllBytes(path, fileContent); // Assert Assert.Equal( fileContent, fileSystem.GetFile(path).Contents); }
public void MockFile_OpenWrite_ShouldCreateNewFiles() { string filePath = XFS.Path(@"c:\something\demo.txt"); string fileContent = "this is some content"; var fileSystem = new MockFileSystem(); fileSystem.AddDirectory(XFS.Path(@"c:\something")); var bytes = new UTF8Encoding(true).GetBytes(fileContent); var stream = fileSystem.File.OpenWrite(filePath); stream.Write(bytes, 0, bytes.Length); stream.Dispose(); Assert.That(fileSystem.FileExists(filePath), Is.True); Assert.That(fileSystem.GetFile(filePath).TextContents, Is.EqualTo(fileContent)); }
public void MockFile_WriteAllText_Encoding_ShouldWriteTextFileToMemoryFileSystem(KeyValuePair <Encoding, byte[]> encodingsWithContents) { // Arrange const string FILE_CONTENT = "Hello there! Dzięki."; string path = XFS.Path(@"c:\something\demo.txt"); byte[] expectedBytes = encodingsWithContents.Value; Encoding encoding = encodingsWithContents.Key; MockFileSystem fileSystem = new MockFileSystem(); // Act fileSystem.File.WriteAllText(path, FILE_CONTENT, encoding); // Assert byte[] actualBytes = fileSystem.GetFile(path).Contents; Assert.Equal(expectedBytes, actualBytes); }
public void MockFile_Move_SameSourceAndTargetIsANoOp() { string sourceFilePath = XFS.Path(@"c:\something\demo.txt"); string sourceFileContent = "this is some content"; var fileSystem = new MockFileSystem(new Dictionary <string, MockFileData> { { sourceFilePath, new MockFileData(sourceFileContent) }, { XFS.Path(@"c:\somethingelse\dummy.txt"), new MockFileData(new byte[] { 0 }) } }); string destFilePath = XFS.Path(@"c:\somethingelse\demo.txt"); fileSystem.File.Move(sourceFilePath, destFilePath); Assert.That(fileSystem.FileExists(destFilePath), Is.True); Assert.That(fileSystem.GetFile(destFilePath).TextContents, Is.EqualTo(sourceFileContent)); }
public void MockFile_Move_ShouldMoveFileWithinMemoryFileSystem() { const string sourceFilePath = @"c:\something\demo.txt"; const string sourceFileContent = "this is some content"; var fileSystem = new MockFileSystem(new Dictionary <string, MockFileData> { { sourceFilePath, new MockFileData(sourceFileContent) } }); const string destFilePath = @"c:\somethingelse\demo1.txt"; fileSystem.File.Move(sourceFilePath, destFilePath); Assert.That(fileSystem.FileExists(destFilePath), Is.True); Assert.That(fileSystem.GetFile(destFilePath).TextContents, Is.EqualTo(sourceFileContent)); Assert.That(fileSystem.FileExists(sourceFilePath), Is.False); }
public void MockFileStream_Flush_WritesByteToFile() { // Arrange var filepath = XFS.Path(@"C:\something\foo.txt"); var fileSystem = new MockFileSystem(new Dictionary <string, MockFileData>()); fileSystem.AddDirectory(XFS.Path(@"C:\something")); var cut = new MockFileStream(fileSystem, filepath, FileMode.Create); // Act cut.WriteByte(255); cut.Flush(); // Assert CollectionAssert.AreEqual(new byte[] { 255 }, fileSystem.GetFile(filepath).Contents); }
public async Task ShouldWriteToDiskAsync() { // Arrange var fileSystemMock = new MockFileSystem(); var options = new StrykerOptions(basePath: @"C:/Users/JohnDoe/Project/TestFolder", fileSystem: fileSystemMock); var sut = new DiskBaselineProvider(options, fileSystemMock); // Act await sut.Save(JsonReport.Build(options, JsonReportTestHelper.CreateProjectWith().ToReadOnlyInputComponent()), "version"); // Assert var path = FilePathUtils.NormalizePathSeparators(@"C:/Users/JohnDoe/Project/TestFolder/StrykerOutput/Baselines/version/stryker-report.json"); MockFileData file = fileSystemMock.GetFile(path); file.ShouldNotBeNull(); }
public void MockFile_WriteAllText_ShouldWriteTextFileToMemoryFileSystem() { // Arrange string path = XFS.Path(@"c:\something\demo.txt"); string fileContent = "Hello there!"; var fileSystem = new MockFileSystem(); fileSystem.AddDirectory(XFS.Path(@"c:\something")); // Act fileSystem.File.WriteAllText(path, fileContent); // Assert Assert.AreEqual( fileContent, fileSystem.GetFile(path).TextContents); }
public void Mockfile_Create_CanWriteToNewStream() { string fullPath = XFS.Path(@"c:\something\demo.txt"); var fileSystem = new MockFileSystem(); var data = new UTF8Encoding(false).GetBytes("Test string"); var sut = new MockFile(fileSystem); using (var stream = sut.Create(fullPath)) { stream.Write(data, 0, data.Length); } var mockFileData = fileSystem.GetFile(fullPath); var fileData = mockFileData.Contents; Assert.That(fileData, Is.EqualTo(data)); }
public void MockFile_Copy_ShouldOverwriteFileWhenOverwriteFlagIsTrue() { string sourceFileName = XFS.Path(@"c:\source\demo.txt"); var sourceContents = new MockFileData("Source content"); string destFileName = XFS.Path(@"c:\destination\demo.txt"); var fileSystem = new MockFileSystem(new Dictionary <string, MockFileData> { { sourceFileName, sourceContents }, { destFileName, new MockFileData("Destination content") } }); fileSystem.File.Copy(sourceFileName, destFileName, true); var copyResult = fileSystem.GetFile(destFileName); Assert.AreEqual(copyResult.Contents, sourceContents.Contents); }
public void OutputProcessedOrderCsvData() { const string inputDir = @"c:\root\in"; const string inputFileName = "myfile.csv"; var inputFilePath = Path.Combine(inputDir, inputFileName); const string outputDir = @"c:\root\out"; const string outputFileName = "myfileout.csv"; var outputFilePath = Path.Combine(outputDir, outputFileName); var csvLines = new StringBuilder(); csvLines.AppendLine("OrderNumber,CustomerNumber,Description,Quantity"); csvLines.AppendLine("42, 100001, Shirt, II"); csvLines.AppendLine("43, 200002, Shorts, I"); csvLines.AppendLine("@ This is a comment"); csvLines.AppendLine(""); csvLines.Append("44, 300003, Cap, V"); var mockInputFile = new MockFileData(csvLines.ToString()); var mockFileSystem = new MockFileSystem(); mockFileSystem.AddFile(inputFilePath, mockInputFile); mockFileSystem.AddDirectory(outputDir); var sut = new CsvFileProcessor(inputFilePath, outputFilePath, mockFileSystem); sut.Process(); Assert.True(mockFileSystem.FileExists(outputFilePath)); var processedFile = mockFileSystem.GetFile(outputFilePath); var lines = processedFile.TextContents.SplitLines(); Assert.Equal("OrderNumber,Customer,Amount", lines[0]); Assert.Equal("42,100001,2", lines[1]); Assert.Equal("43,200002,1", lines[2]); Assert.Equal("44,300003,5", lines[3]); }
public void Mockfile_Create_CanWriteToNewStream() { const string fullPath = @"c:\something\demo.txt"; var fileSystem = new MockFileSystem(); var data = new UTF8Encoding(false).GetBytes("Test string"); var sut = new MockFile(fileSystem); using (var stream = sut.Create(fullPath)) { stream.Write(data, 0, data.Length); } var mockFileData = fileSystem.GetFile(fullPath); var fileData = mockFileData.Contents; Assert.That(fileData, Is.EqualTo(data)); }
public void OnlyTracesAboveEnableLevelAreLogged() { var mockFileSystem = new MockFileSystem(); var logger = new TextFileLogger(LogLevel.Information, _filePath, mockFileSystem); logger.Log(LogLevel.Debug, _logMessage); logger.Log(LogLevel.Critical, _logMessage); var logFile = mockFileSystem.GetFile(_filePath); var fileContents = logFile.TextContents.ToLower(); Assert.IsTrue( fileContents.Contains($"{LogLevel.Critical.ToString().ToLower()}: {_logMessage}")); Assert.IsFalse( fileContents.Contains($"{LogLevel.Debug.ToString().ToLower()}: {_logMessage}")); }
public void MockFile_Move_ShouldMoveFileWithinMemoryFileSystem() { string sourceFilePath = XFS.Path(@"c:\something\demo.txt"); string sourceFileContent = "this is some content"; var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData> { {sourceFilePath, new MockFileData(sourceFileContent)}, {XFS.Path(@"c:\somethingelse\dummy.txt"), new MockFileData(new byte[] {0})} }); string destFilePath = XFS.Path(@"c:\somethingelse\demo1.txt"); fileSystem.File.Move(sourceFilePath, destFilePath); Assert.That(fileSystem.FileExists(destFilePath), Is.True); Assert.That(fileSystem.GetFile(destFilePath).TextContents, Is.EqualTo(sourceFileContent)); Assert.That(fileSystem.FileExists(sourceFilePath), Is.False); }
public void ShouldAddGitIgnore() { var fileSystemMock = new MockFileSystem(); var basePath = Directory.GetCurrentDirectory(); var target = new LoggingInitializer(); var inputs = new StrykerInputs(); inputs.BasePathInput.SuppliedInput = basePath; target.SetupLogOptions(inputs, fileSystemMock); var gitIgnoreFile = fileSystemMock.AllFiles.FirstOrDefault(x => x.EndsWith(Path.Combine("StrykerOutput", ".gitignore"))); gitIgnoreFile.ShouldNotBeNull(); var fileContents = fileSystemMock.GetFile(gitIgnoreFile).Contents; Encoding.Default.GetString(fileContents).ShouldBe("*"); }
public void MockFile_AppendText_CreatesNewFileForAppendToNonExistingFile() { string filepath = XFS.Path(@"c:\something\doesnt\exist.txt"); var filesystem = new MockFileSystem(new Dictionary <string, MockFileData>()); filesystem.AddDirectory(XFS.Path(@"c:\something\doesnt")); var stream = filesystem.File.AppendText(filepath); stream.Write("New too!"); stream.Flush(); stream.Dispose(); var file = filesystem.GetFile(filepath); Assert.That(file.TextContents, Is.EqualTo("New too!")); Assert.That(filesystem.FileExists(filepath)); }
public void MockFile_AppendText_AppendTextToanExistingFile() { string filepath = XFS.Path(@"c:\something\does\exist.txt"); var filesystem = new MockFileSystem(new Dictionary <string, MockFileData> { { filepath, new MockFileData("I'm here. ") } }); var stream = filesystem.File.AppendText(filepath); stream.Write("Me too!"); stream.Flush(); stream.Close(); var file = filesystem.GetFile(filepath); Assert.That(file.TextContents, Is.EqualTo("I'm here. Me too!")); }
public FileInfoBase AddLogFile(string name, string contents, DateTimeOffset lastWriteTime) { var path = System.IO.Path.Combine(Constants.LogFilesPath, Constants.ApplicationLogFilesDirectory, name); _defaultFileSystem.AddFile(path, new MockFileData(contents ?? string.Empty) { LastWriteTime = lastWriteTime }); // System.IO.Abstractions.TestHelpers does not provide an implementation for .Open(..) so we have to provide one if (contents != null) { _fileMock.Setup(f => f.Open(It.Is <string>(s => s.EndsWith(path)), FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) .Returns(() => new MemoryStream(_defaultFileSystem.GetFile(path).Contents)); } return(FileSystemHelpers.FileInfoFromFileName(path)); }
public void MockFile_Move_ShouldMoveFileWithinMemoryFileSystem() { string sourceFilePath = XFS.Path(@"c:\something\demo.txt"); string sourceFileContent = "this is some content"; MockFileSystem fileSystem = new MockFileSystem(new Dictionary <string, MockFileData> { { sourceFilePath, new MockFileData(sourceFileContent) }, { XFS.Path(@"c:\somethingelse\dummy.txt"), new MockFileData(new byte[] { 0 }) } }); string destFilePath = XFS.Path(@"c:\somethingelse\demo1.txt"); fileSystem.File.Move(sourceFilePath, destFilePath); Assert.True(fileSystem.FileExists(destFilePath)); Assert.Equal(fileSystem.GetFile(destFilePath).TextContents, sourceFileContent); Assert.False(fileSystem.FileExists(sourceFilePath)); }
public void WriteFileChunkTest_WorksFine() { // arrange var filePath = @"C:\temp\in.txt"; var fileData = new MockFileData("test data"); var fileBytesData = Encoding.ASCII.GetBytes(fileData.TextContents); mockFileSystem.AddFile(filePath, null); // act target.WriteFileChunk(filePath, fileBytesData); var mockOutputFile = mockFileSystem.GetFile(filePath); var actual = mockOutputFile.TextContents; // assert Assert.Equal(fileData.TextContents, actual); }
public void MockFile_WriteAllText_ShouldNotThrowAnArgumentNullExceptionIfTheContentIsNull() { // Arrange var fileSystem = new MockFileSystem(); string directoryPath = XFS.Path(@"c:\something"); string filePath = XFS.Path(@"c:\something\demo.txt"); fileSystem.AddDirectory(directoryPath); // Act fileSystem.File.WriteAllText(filePath, null); // Assert // no exception should be thrown, also the documentation says so var data = fileSystem.GetFile(filePath); Assert.That(data.Contents, Is.Empty); }
public void MockFile_AppendAllText_ShouldPersistNewTextWithDifferentEncoding() { // Arrange const string Path = @"c:\something\demo.txt"; var fileSystem = new MockFileSystem(new Dictionary <string, MockFileData> { { Path, new MockFileData("AA", Encoding.UTF32) } }); var file = new MockFile(fileSystem); // Act file.AppendAllText(Path, "BB", Encoding.UTF8); // Assert fileSystem.GetFile(Path) .Contents.ShouldBeEquivalentTo(new byte[] { 255, 254, 0, 0, 65, 0, 0, 0, 65, 0, 0, 0, 66, 66 }, options => options.WithStrictOrdering()); }
public void MockFile_AppendAllText_ShouldPersistNewTextWithDifferentEncoding() { // Arrange const string PATH = @"c:\something\demo.txt"; MockFileSystem fileSystem = new MockFileSystem(new Dictionary <string, MockFileData> { { PATH, new MockFileData("AA", Encoding.UTF32) } }); MockFile file = new MockFile(fileSystem); // Act file.AppendAllText(PATH, "BB", Encoding.UTF8); // Assert Assert.Equal( new byte[] { 255, 254, 0, 0, 65, 0, 0, 0, 65, 0, 0, 0, 66, 66 }, fileSystem.GetFile(PATH).Contents); }
public void CreateNewEntry_OnlyIncludesTagsInFrontMatter_WhenOnlyTagsAreProvided() { var fileSystem = new MockFileSystem(); const string rootDirectory = "J:\\Current"; var ioFactory = new JournalReaderWriterFactory(fileSystem, rootDirectory); var systemProcess = A.Fake <ISystemProcess>(); var markdownFiles = A.Fake <IMarkdownFiles>(); var journal = Journal.Open(ioFactory, markdownFiles, systemProcess); journal.CreateNewEntry(new LocalDate(2019, 7, 19), new List <string> { "horse", "Dog", "panda" }.ToArray(), null); const string tags = "tags:\r\n - Dog\r\n - horse\r\n - panda"; var entryText = fileSystem.GetFile("J:\\Current\\2019\\07 July\\2019.07.19.md").TextContents; entryText.Should().Contain(tags); entryText.Should().NotContain("readme:"); }
public void MockFile_AppendAllText_ShouldPersistNewTextWithDifferentEncoding() { // Arrange const string Path = @"c:\something\demo.txt"; var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData> { {Path, new MockFileData("AA", Encoding.UTF32)} }); var file = new MockFile(fileSystem); // Act file.AppendAllText(Path, "BB", Encoding.UTF8); // Assert CollectionAssert.AreEqual( new byte[] {255, 254, 0, 0, 65, 0, 0, 0, 65, 0, 0, 0, 66, 66}, fileSystem.GetFile(Path).Contents); }
public async Task MockFile_AppendAllTextAsync_ShouldPersistNewTextWithDifferentEncoding() { // Arrange const string Path = @"c:\something\demo.txt"; var fileSystem = new MockFileSystem(new Dictionary <string, MockFileData> { { Path, new MockFileData("AA", Encoding.UTF32) } }); var file = new MockFile(fileSystem); // Act await file.AppendAllTextAsync(Path, "BB", Encoding.UTF8); // Assert CollectionAssert.AreEqual( new byte[] { 255, 254, 0, 0, 65, 0, 0, 0, 65, 0, 0, 0, 66, 66 }, fileSystem.GetFile(Path).Contents); }
public void MockFile_OpenWrite_ShouldOverwriteExistingFiles() { string filePath = XFS.Path(@"c:\something\demo.txt"); string startFileContent = "this is some content"; string endFileContent = "this is some other content"; var fileSystem = new MockFileSystem(new Dictionary <string, MockFileData> { { filePath, new MockFileData(startFileContent) } }); var bytes = new UTF8Encoding(true).GetBytes(endFileContent); var stream = fileSystem.File.OpenWrite(filePath); stream.Write(bytes, 0, bytes.Length); stream.Close(); Assert.That(fileSystem.FileExists(filePath), Is.True); Assert.That(fileSystem.GetFile(filePath).TextContents, Is.EqualTo(endFileContent)); }
public void When_ValidFile_Then_CreateReaderWriter() { //Set up fileManager with mock file system var fileSystem = new MockFileSystem(); var fileManager = new FileManager(fileSystem); //Create file on filesystem fileSystem.File.Create("/file.txt").Close(); //Create path for file var file = new Path("file.txt", isDirectory: false); //Initialize file fileManager.InitPath("/", file); //Create writer var writer = fileManager.FileWriter(file); //Assert writer is not null Assert.NotNull(writer); //Write to file writer.WriteLine("Hello"); writer.Close(); //Assert content was written Assert.Contains("Hello", fileSystem.GetFile("/file.txt").TextContents); //Create reader var reader = fileManager.FileReader(file); //Assert reader is not null Assert.NotNull(reader); //Read from file var read = reader.ReadLine(); reader.Close(); //Assert read content Assert.Equal("Hello", read); }
public void OutputProcessedPolicyXmlData() { // Create mock file system to test program const string inputDir = @"c:\root\in"; const string inputFileName = "myFile.csv"; string inputFilePath = Path.Combine(inputDir, inputFileName); const string outputDir = @"c:\root\out"; const string outputFileName = "myFileOut.xml"; string outputFilePath = Path.Combine(outputDir, outputFileName); // Create dummy test data to test program produces correct values StringBuilder csvLines = new StringBuilder(); csvLines.AppendLine("policy_number,policy_start_date,premiums,membership,discretionary_bonus,uplift_percentage"); csvLines.AppendLine("A100001,01/06/1986,10000,Y,1000,40"); csvLines.AppendLine("A100002,01/01/1990,12500,N,1350,37.5"); csvLines.AppendLine("A100003,31/12/1989,15250,N,1600,42"); csvLines.AppendLine("B100001,01/01/1995,12000,Y,2000,41"); csvLines.AppendLine("B100002,01/01/1970,18000,N,3000,43"); csvLines.AppendLine("B100003,20/07/1969,20000,Y,4000,45"); csvLines.AppendLine("C100001,01/01/1992,13000,N,1000,42"); csvLines.AppendLine("C100002,31/12/1989,15000,Y,2000,44"); csvLines.Append("C100003,01/01/1990,17000,Y,3000,46"); MockFileData mockInputFile = new MockFileData(csvLines.ToString()); MockFileSystem mockFileSystem = new MockFileSystem(); mockFileSystem.AddFile(inputFilePath, mockInputFile); mockFileSystem.AddDirectory(outputDir); CsvFileProcessor csvFileProcessor = new CsvFileProcessor(inputFilePath, outputFilePath, mockFileSystem); csvFileProcessor.Process(); Assert.True(mockFileSystem.FileExists(outputFilePath)); MockFileData processedFile = mockFileSystem.GetFile(outputFilePath); Approvals.Verify(processedFile.TextContents); }
public async Task MockFile_WriteAllTextAsync_Encoding_ShouldWriteTextFileToMemoryFileSystem(KeyValuePair <Encoding, byte[]> encodingsWithContents) { // Arrange const string FileContent = "Hello there! Dzięki."; string path = XFS.Path(@"c:\something\demo.txt"); byte[] expectedBytes = encodingsWithContents.Value; Encoding encoding = encodingsWithContents.Key; var fileSystem = new MockFileSystem(); fileSystem.AddDirectory(XFS.Path(@"c:\something")); // Act await fileSystem.File.WriteAllTextAsync(path, FileContent, encoding); // Assert var actualBytes = fileSystem.GetFile(path).Contents; Assert.AreEqual(expectedBytes, actualBytes); }
public void MockFile_Open_OpensExistingFileOnOpenOrCreate() { string filepath = XFS.Path(@"c:\something\does\exist.txt"); var filesystem = new MockFileSystem(new Dictionary <string, MockFileData> { { filepath, new MockFileData("I'm here") } }); var stream = filesystem.File.Open(filepath, FileMode.OpenOrCreate); var file = filesystem.GetFile(filepath); Assert.Equal(0, stream.Position); Assert.Equal(file.Contents.Length, stream.Length); byte[] data; using (var br = new BinaryReader(stream)) data = br.ReadBytes((int)stream.Length); file.Contents.ShouldBeEquivalentTo(data, options => options.WithStrictOrdering()); }
public void MockFile_Open_OpensExistingFileOnAppend() { string filepath = XFS.Path(@"c:\something\does\exist.txt"); var filesystem = new MockFileSystem(new Dictionary<string, MockFileData> { { filepath, new MockFileData("I'm here") } }); var stream = filesystem.File.Open(filepath, FileMode.Append); var file = filesystem.GetFile(filepath); Assert.That(stream.Position, Is.EqualTo(file.Contents.Length)); Assert.That(stream.Length, Is.EqualTo(file.Contents.Length)); stream.Seek(0, SeekOrigin.Begin); byte[] data; using (var br = new BinaryReader(stream)) data = br.ReadBytes((int)stream.Length); CollectionAssert.AreEqual(file.Contents, data); }
public void MockFile_WriteAllText_Encoding_ShouldWriteTextFileToMemoryFileSystem(Encoding encoding) { // Arrange const string path = @"c:\something\demo.txt"; const string fileContent = "Hello there! Dzięki."; var fileSystem = new MockFileSystem(); // Act fileSystem.File.WriteAllText(path, fileContent, encoding); // Assert Assert.AreEqual( encoding.GetString(encoding.GetBytes(fileContent)), fileSystem.GetFile(path).TextContents); }
public void MockFile_WriteAllTextMultipleLines_ShouldWriteTextFileToMemoryFileSystem() { // Arrange string path = XFS.Path(@"c:\something\demo.txt"); var fileContent = new List<string> {"Hello there!", "Second line!"}; var expected = "Hello there!" + Environment.NewLine + "Second line!" + Environment.NewLine; var fileSystem = new MockFileSystem(); fileSystem.AddDirectory(@"c:\something"); // Act fileSystem.File.WriteAllLines(path, fileContent); // Assert Assert.AreEqual( expected, fileSystem.GetFile(path).TextContents); }
public void MockFile_WriteAllText_Encoding_ShouldWriteTextFileToMemoryFileSystem(KeyValuePair<Encoding, byte[]> encodingsWithContents) { // Arrange const string FileContent = "Hello there! Dzięki."; string path = XFS.Path(@"c:\something\demo.txt"); byte[] expectedBytes = encodingsWithContents.Value; Encoding encoding = encodingsWithContents.Key; var fileSystem = new MockFileSystem(); fileSystem.AddDirectory(@"c:\something"); // Act fileSystem.File.WriteAllText(path, FileContent, encoding); // Assert var actualBytes = fileSystem.GetFile(path).Contents; Assert.AreEqual(expectedBytes, actualBytes); }
public void MockFile_AppendText_AppendTextToanExistingFile() { string filepath = XFS.Path(@"c:\something\does\exist.txt"); var filesystem = new MockFileSystem(new Dictionary<string, MockFileData> { { filepath, new MockFileData("I'm here. ") } }); var stream = filesystem.File.AppendText(filepath); stream.Write("Me too!"); stream.Flush(); stream.Close(); var file = filesystem.GetFile(filepath); Assert.That(file.TextContents, Is.EqualTo("I'm here. Me too!")); }
public void MockFile_AppendText_CreatesNewFileForAppendToNonExistingFile() { string filepath = XFS.Path(@"c:\something\doesnt\exist.txt"); var filesystem = new MockFileSystem(new Dictionary<string, MockFileData>()); var stream = filesystem.File.AppendText(filepath); stream.Write("New too!"); stream.Flush(); stream.Close(); var file = filesystem.GetFile(filepath); Assert.That(file.TextContents, Is.EqualTo("New too!")); Assert.That(filesystem.FileExists(filepath)); }
public void MockFile_AppendAllText_ShouldCreateIfNotExistWithBom() { // Arrange var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>()); const string path = @"c:\something\demo3.txt"; fileSystem.AddDirectory(@"c:\something\"); // Act fileSystem.File.AppendAllText(path, "AA", Encoding.UTF32); // Assert CollectionAssert.AreEqual( new byte[] {255, 254, 0, 0, 65, 0, 0, 0, 65, 0, 0, 0}, fileSystem.GetFile(path).Contents); }
public void MockFile_WriteAllText_ShouldWriteTextFileToMemoryFileSystem() { // Arrange const string path = @"c:\something\demo.txt"; const string fileContent = "Hello there!"; var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>()); // Act fileSystem.File.WriteAllText(path, fileContent); // Assert Assert.AreEqual( fileContent, fileSystem.GetFile(path).TextContents); }
public void MockFile_WriteAllBytes_ShouldWriteDataToMemoryFileSystem() { // Arrange const string path = @"c:\something\demo.txt"; var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>()); var fileContent = new byte[] { 1, 2, 3, 4 }; // Act fileSystem.File.WriteAllBytes(path, fileContent); // Assert Assert.AreEqual( fileContent, fileSystem.GetFile(path).Contents); }
public void MockFile_OpenWrite_ShouldOverwriteExistingFiles() { const string filePath = @"c:\something\demo.txt"; const string startFileContent = "this is some content"; const string endFileContent = "this is some other content"; var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData> { {filePath, new MockFileData(startFileContent)} }); var bytes = new UTF8Encoding(true).GetBytes(endFileContent); var stream = fileSystem.File.OpenWrite(filePath); stream.Write(bytes, 0, bytes.Length); stream.Close(); Assert.That(fileSystem.FileExists(filePath), Is.True); Assert.That(fileSystem.GetFile(filePath).TextContents, Is.EqualTo(endFileContent)); }
public void MockFile_OpenWrite_ShouldCreateNewFiles() { const string filePath = @"c:\something\demo.txt"; const string fileContent = "this is some content"; var fileSystem = new MockFileSystem(); var bytes = new UTF8Encoding(true).GetBytes(fileContent); var stream = fileSystem.File.OpenWrite(filePath); stream.Write(bytes, 0, bytes.Length); stream.Close(); Assert.That(fileSystem.FileExists(filePath), Is.True); Assert.That(fileSystem.GetFile(filePath).TextContents, Is.EqualTo(fileContent)); }
public void MockFile_Open_OverwritesExistingFileOnCreate() { const string filepath = @"c:\something\doesnt\exist.txt"; var filesystem = new MockFileSystem(new Dictionary<string, MockFileData> { { filepath, new MockFileData("I'm here") } }); var stream = filesystem.File.Open(filepath, FileMode.Create); var file = filesystem.GetFile(filepath); Assert.That(stream.Position, Is.EqualTo(0)); Assert.That(stream.Length, Is.EqualTo(0)); Assert.That(file.Contents.Length, Is.EqualTo(0)); }