public MockFileStream(IMockFileDataAccessor mockFileDataAccessor, string path, bool forAppend = false) { if (mockFileDataAccessor == null) { throw new ArgumentNullException("mockFileDataAccessor"); } this.mockFileDataAccessor = mockFileDataAccessor; this.path = path; if (mockFileDataAccessor.FileExists(path)) { /* only way to make an expandable MemoryStream that starts with a particular content */ var data = mockFileDataAccessor.GetFile(path).Contents; if (data != null && data.Length > 0) { Write(data, 0, data.Length); Seek(0, forAppend ? SeekOrigin.End : SeekOrigin.Begin); } } else { mockFileDataAccessor.AddFile(path, new MockFileData(new byte[] { })); } }
public MockFileStream(IMockFileDataAccessor mockFileDataAccessor, string path) { this.mockFileDataAccessor = mockFileDataAccessor; this.path = path; if (mockFileDataAccessor.FileExists(path)) { /* only way to make an expandable MemoryStream that starts with a particular content */ var data = mockFileDataAccessor.GetFile(path).Contents; base.Write(data, 0, data.Length); base.Seek(0, SeekOrigin.Begin); } }
private void InternalFlush() { if (mockFileDataAccessor.FileExists(path)) { var mockFileData = mockFileDataAccessor.GetFile(path); /* reset back to the beginning .. */ Seek(0, SeekOrigin.Begin); /* .. read everything out */ var data = new byte[Length]; Read(data, 0, (int)Length); /* .. put it in the mock system */ mockFileData.Contents = data; } }
public MockFileStream(IMockFileDataAccessor mockFileDataAccessor, string path, bool forAppend = false) { this.mockFileDataAccessor = mockFileDataAccessor; this.path = path; if (mockFileDataAccessor.FileExists(path)) { /* only way to make an expandable MemoryStream that starts with a particular content */ var data = mockFileDataAccessor.GetFile(path).Contents; if (data != null && data.Length > 0) { base.Write(data, 0, data.Length); base.Seek(0, forAppend ? SeekOrigin.End : SeekOrigin.Begin); } } }
public MockFileStream( IMockFileDataAccessor mockFileDataAccessor, string path, StreamType streamType, FileOptions options) { this.mockFileDataAccessor = mockFileDataAccessor ?? throw new ArgumentNullException(nameof(mockFileDataAccessor)); this.path = path; this.options = options; if (mockFileDataAccessor.FileExists(path)) { var fileData = mockFileDataAccessor.GetFile(path); fileData.CheckFileAccess(path, streamType != StreamType.READ ? FileAccess.Write : FileAccess.Read); /* only way to make an expandable MemoryStream that starts with a particular content */ var data = fileData.Contents; if (data != null && data.Length > 0 && streamType != StreamType.TRUNCATE) { Write(data, 0, data.Length); Seek(0, StreamType.APPEND.Equals(streamType) ? SeekOrigin.End : SeekOrigin.Begin); } } else { if (!mockFileDataAccessor.Directory.Exists(mockFileDataAccessor.Path.GetDirectoryName(path))) { throw CommonExceptions.CouldNotFindPartOfPath(path); } if (StreamType.READ.Equals(streamType)) { throw CommonExceptions.FileNotFound(path); } mockFileDataAccessor.AddFile(path, new MockFileData(new byte[] { })); } canWrite = streamType != StreamType.READ; }
public override void AppendAllText(string path, string contents, Encoding encoding) { if (encoding == null) { throw new ArgumentNullException(nameof(encoding)); } mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path"); if (!mockFileDataAccessor.FileExists(path)) { VerifyDirectoryExists(path); mockFileDataAccessor.AddFile(path, new MockFileData(contents, encoding)); } else { var file = mockFileDataAccessor.GetFile(path); file.CheckFileAccess(path, FileAccess.Write); var bytesToAppend = encoding.GetBytes(contents); file.Contents = file.Contents.Concat(bytesToAppend).ToArray(); } }
public override void AppendAllText(string path, string contents, Encoding encoding) { if (encoding == null) { throw new ArgumentNullException(nameof(encoding)); } mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path"); if (!mockFileDataAccessor.FileExists(path)) { var dir = mockFileDataAccessor.Path.GetDirectoryName(path); if (!mockFileDataAccessor.Directory.Exists(dir)) { throw new DirectoryNotFoundException(string.Format(CultureInfo.InvariantCulture, StringResources.Manager.GetString("COULD_NOT_FIND_PART_OF_PATH_EXCEPTION"), path)); } mockFileDataAccessor.AddFile(path, new MockFileData(contents, encoding)); } else { var file = mockFileDataAccessor.GetFile(path); var bytesToAppend = encoding.GetBytes(contents); file.Contents = file.Contents.Concat(bytesToAppend).ToArray(); } }
public void MockFile_WriteAllLinesGeneric_ShouldWriteTheCorrectContent(IMockFileDataAccessor fileSystem, Action action, string expectedContent) { // Arrange // is done in the test case source // Act action(); // Assert var actualContent = fileSystem.GetFile(TestDataForWriteAllLines.Path).TextContents; Assert.That(actualContent, Is.EqualTo(expectedContent)); }
private MockFileData GetMockFileDataForRead() { return(mockFileDataAccessor.GetFile(directoryPath) ?? MockFileData.NullObject); }
public override void AppendAllText(string path, string contents, Encoding encoding) { ValidateParameter(path, "path"); if (encoding == null) { throw new ArgumentNullException("encoding"); } if (!mockFileDataAccessor.FileExists(path)) { var dir = mockFileDataAccessor.Path.GetDirectoryName(path); if (!mockFileDataAccessor.Directory.Exists(dir)) { throw new DirectoryNotFoundException(string.Format(CultureInfo.InvariantCulture, "Could not find a part of the path '{0}'.", path)); } mockFileDataAccessor.AddFile(path, new MockFileData(contents, encoding)); } else { var file = mockFileDataAccessor.GetFile(path); var bytesToAppend = encoding.GetBytes(contents); file.Contents = file.Contents.Concat(bytesToAppend).ToArray(); } }
public override void AppendAllText(string path, string contents) { if (!mockFileDataAccessor.FileExists(path)) { var dir = mockFileDataAccessor.Path.GetDirectoryName(path); if (!mockFileDataAccessor.Directory.Exists(dir)) { throw new DirectoryNotFoundException(String.Format(CultureInfo.InvariantCulture, "Could not find a part of the path '{0}'.", path)); } mockFileDataAccessor.AddFile(path, new MockFileData(contents)); } else { mockFileDataAccessor .GetFile(path) .TextContents += contents; } }
public override void AppendAllText(string path, string contents) { mockFileDataAccessor .GetFile(path) .TextContents += contents; }