public override void Move(string sourceFileName, string destFileName) { var sourceFile = mockFileDataAccessor.GetFile(sourceFileName); mockFileDataAccessor.AddFile(destFileName, new MockFileData(sourceFile.Contents)); mockFileDataAccessor.RemoveFile(sourceFileName); }
public override void Copy(string sourceFileName, string destFileName) { if (mockFileDataAccessor.FileExists(destFileName)) { throw new IOException(string.Format("The file {0} already exists.", destFileName)); } mockFileDataAccessor.AddFile(destFileName, mockFileDataAccessor.GetFile(sourceFileName)); }
public MockFileStream(IMockFileDataAccessor mockFileDataAccessor, string path, StreamType streamType) { this.mockFileDataAccessor = mockFileDataAccessor ?? throw new ArgumentNullException(nameof(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 && streamType != StreamType.TRUNCATE) { Write(data, 0, data.Length); Seek(0, StreamType.APPEND.Equals(streamType) ? SeekOrigin.End : SeekOrigin.Begin); } } else { if (StreamType.READ.Equals(streamType)) { throw new FileNotFoundException("File not found.", path); } mockFileDataAccessor.AddFile(path, new MockFileData(new byte[] { })); } canWrite = streamType != StreamType.READ; }
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 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 override string GetTempFileName() { string fileName = mockFileDataAccessor.Path.GetRandomFileName(); string tempDir = mockFileDataAccessor.Path.GetTempPath(); string fullPath = mockFileDataAccessor.Path.Combine(tempDir, fileName); mockFileDataAccessor.AddFile(fullPath, new MockFileData(string.Empty)); return(fullPath); }
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 MockFileStream( IMockFileDataAccessor mockFileDataAccessor, string path, FileMode mode, FileAccess access = FileAccess.ReadWrite, FileOptions options = FileOptions.None) { this.mockFileDataAccessor = mockFileDataAccessor ?? throw new ArgumentNullException(nameof(mockFileDataAccessor)); this.path = path; this.options = options; if (mockFileDataAccessor.FileExists(path)) { if (mode.Equals(FileMode.CreateNew)) { throw CommonExceptions.FileAlreadyExists(path); } var fileData = mockFileDataAccessor.GetFile(path); fileData.CheckFileAccess(path, access); var existingContents = fileData.Contents; var keepExistingContents = existingContents?.Length > 0 && mode != FileMode.Truncate && mode != FileMode.Create; if (keepExistingContents) { Write(existingContents, 0, existingContents.Length); Seek(0, mode == FileMode.Append ? SeekOrigin.End : SeekOrigin.Begin); } } else { var directoryPath = mockFileDataAccessor.Path.GetDirectoryName(path); if (!string.IsNullOrEmpty(directoryPath) && !mockFileDataAccessor.Directory.Exists(directoryPath)) { throw CommonExceptions.CouldNotFindPartOfPath(path); } if (mode.Equals(FileMode.Open) || mode.Equals(FileMode.Truncate)) { throw CommonExceptions.FileNotFound(path); } mockFileDataAccessor.AddFile(path, new MockFileData(new byte[] { })); } this.access = access; }
public MockFileStream( IMockFileDataAccessor mockFileDataAccessor, string path, StreamType streamType, FileOptions options, FileMode fileMode = FileMode.Append) { this.mockFileDataAccessor = mockFileDataAccessor ?? throw new ArgumentNullException(nameof(mockFileDataAccessor)); this.path = path; this.options = options; if (mockFileDataAccessor.FileExists(path)) { if (fileMode.Equals(FileMode.CreateNew)) { throw CommonExceptions.FileAlreadyExists(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) || fileMode.Equals(FileMode.Open) || fileMode.Equals(FileMode.Truncate)) { throw CommonExceptions.FileNotFound(path); } mockFileDataAccessor.AddFile(path, new MockFileData(new byte[] { })); } canWrite = streamType != StreamType.READ; }
private void InternalFlush() { if (mockFileDataAccessor.FileExists(path)) { mockFileDataAccessor.RemoveFile(path); } /* reset back to the beginning .. */ base.Seek(0, SeekOrigin.Begin); /* .. read everything out */ var data = new byte[base.Length]; base.Read(data, 0, (int)base.Length); /* .. put it in the mock system */ mockFileDataAccessor.AddFile(path, new MockFileData(data)); }
public async Task ThenAnEmptyListIsReturnedWhenReadingTheFileFails( AccountsRepository sut, IWorkingCopy workingCopy, IMockFileDataAccessor mockFileDataAccessor, Wrapper <string> existingSettingPath) { //Arrange mockFileDataAccessor.AddFile(existingSettingPath, new MockFileData("{ invalid json }")); Mock.Get(workingCopy).Setup(w => w.SettingsPath).Returns(existingSettingPath); //Act var accounts = await sut.GetList(); // Assert accounts.Should().BeEmpty(); }
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 async Task ThenStoredAccountsAreReturned( AccountsRepository sut, IWorkingCopy workingCopy, IMockFileDataAccessor mockFileDataAccessor, Wrapper <string> existingSettingPath, List <AccountEntity> existingEntities) { //Arrange var rawEntities = JsonConvert.SerializeObject(existingEntities); mockFileDataAccessor.AddFile(existingSettingPath, new MockFileData(rawEntities)); Mock.Get(workingCopy).Setup(w => w.SettingsPath).Returns(existingSettingPath); //Act var accounts = await sut.GetList(); // Assert accounts.Count.Should().Be(existingEntities.Count); }
public override void AppendAllText(string path, string contents, Encoding encoding) { mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path"); if (encoding == null) { throw new ArgumentNullException(nameof(encoding)); } 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 DirectoryInfoBase CreateDirectory(string path, DirectorySecurity directorySecurity) { path = EnsurePathEndsWithDirectorySeparator(path); mockFileDataAccessor.AddFile(path + "__PLACEHOLDER__.dir", new MockFileData(string.Empty)); return(new MockDirectoryInfo(mockFileDataAccessor, path)); }