private Stream OpenInternal( string path, FileMode mode, FileAccess access, FileShare share, FileOptions options) { mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path"); bool exists = mockFileDataAccessor.FileExists(path); if (mode == FileMode.CreateNew && exists) { throw new IOException(string.Format(CultureInfo.InvariantCulture, "The file '{0}' already exists.", path)); } if ((mode == FileMode.Open || mode == FileMode.Truncate) && !exists) { throw CommonExceptions.FileNotFound(path); } if (!exists || mode == FileMode.CreateNew) { return(Create(path)); } if (mode == FileMode.Create || mode == FileMode.Truncate) { Delete(path); return(Create(path)); } var mockFileData = mockFileDataAccessor.GetFile(path); mockFileData.CheckFileAccess(path, access); var length = mockFileData.Contents.Length; MockFileStream.StreamType streamType = MockFileStream.StreamType.WRITE; if (access == FileAccess.Read) { streamType = MockFileStream.StreamType.READ; } else if (mode == FileMode.Append) { streamType = MockFileStream.StreamType.APPEND; } return(new MockFileStream(mockFileDataAccessor, path, streamType, options)); }
public void MockFileStream_Constructor_Insufficient_FileShare_Throws_Exception(FileShare allowedFileShare, MockFileStream.StreamType streamType) { var filePath = @"C:\locked.txt"; var fileSystem = new MockFileSystem(new Dictionary <string, MockFileData> { { filePath, new MockFileData("cannot access") { AllowedFileShare = allowedFileShare } } }); Assert.Throws <IOException>(() => new MockFileStream(fileSystem, filePath, streamType)); }