From e7150944153c498d1e2bd0561686fdf614de78db Mon Sep 17 00:00:00 2001 From: Florian Greinacher Date: Sat, 12 Sep 2020 00:20:53 +0200 Subject: [PATCH] chore: add some missing curlies --- .../MockDirectoryInfo.cs | 1 + .../MockFile.cs | 9 +++- .../MockFileData.cs | 2 + .../MockFileInfo.cs | 46 +++++++++++++++---- .../MockFileSystem.cs | 5 +- .../DirectoryInfoBase.cs | 2 + .../FileSystemWatcherBase.cs | 2 +- 7 files changed, 55 insertions(+), 12 deletions(-) diff --git a/src/System.IO.Abstractions.TestingHelpers/MockDirectoryInfo.cs b/src/System.IO.Abstractions.TestingHelpers/MockDirectoryInfo.cs index 5a864eb55..772e45851 100644 --- a/src/System.IO.Abstractions.TestingHelpers/MockDirectoryInfo.cs +++ b/src/System.IO.Abstractions.TestingHelpers/MockDirectoryInfo.cs @@ -41,6 +41,7 @@ public override void Delete() public override void Refresh() { + // Nothing to do here. Mock file system is always up-to-date. } public override FileAttributes Attributes diff --git a/src/System.IO.Abstractions.TestingHelpers/MockFile.cs b/src/System.IO.Abstractions.TestingHelpers/MockFile.cs index c519439ba..f6918fe1b 100644 --- a/src/System.IO.Abstractions.TestingHelpers/MockFile.cs +++ b/src/System.IO.Abstractions.TestingHelpers/MockFile.cs @@ -393,13 +393,19 @@ private Stream OpenInternal( 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) { @@ -424,8 +430,7 @@ public override StreamReader OpenText(string path) { mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path"); - return new StreamReader( - OpenRead(path)); + return new StreamReader(OpenRead(path)); } public override Stream OpenWrite(string path) => OpenWriteInternal(path, FileOptions.None); diff --git a/src/System.IO.Abstractions.TestingHelpers/MockFileData.cs b/src/System.IO.Abstractions.TestingHelpers/MockFileData.cs index 7aae029ab..0ac2d35ec 100644 --- a/src/System.IO.Abstractions.TestingHelpers/MockFileData.cs +++ b/src/System.IO.Abstractions.TestingHelpers/MockFileData.cs @@ -177,7 +177,9 @@ public FileSecurity AccessControl internal void CheckFileAccess(string path, FileAccess access) { if (!AllowedFileShare.HasFlag((FileShare)access)) + { throw CommonExceptions.ProcessCannotAccessFileInUse(path); + } } } } diff --git a/src/System.IO.Abstractions.TestingHelpers/MockFileInfo.cs b/src/System.IO.Abstractions.TestingHelpers/MockFileInfo.cs index 0b44c6cf8..c9a02abb7 100644 --- a/src/System.IO.Abstractions.TestingHelpers/MockFileInfo.cs +++ b/src/System.IO.Abstractions.TestingHelpers/MockFileInfo.cs @@ -29,28 +29,45 @@ public override void Delete() public override void Refresh() { + // Nothing to do here. Mock file system is always up-to-date. } public override FileAttributes Attributes { get { - if (MockFileData == null) throw CommonExceptions.FileNotFound(path); + if (MockFileData == null) + { + throw CommonExceptions.FileNotFound(path); + } return MockFileData.Attributes; } - set { MockFileData.Attributes = value; } + set + { + if (MockFileData == null) + { + throw CommonExceptions.FileNotFound(path); + } + MockFileData.Attributes = value; + } } public override DateTime CreationTime { get { - if (MockFileData == null) throw CommonExceptions.FileNotFound(path); + if (MockFileData == null) + { + throw CommonExceptions.FileNotFound(path); + } return MockFileData.CreationTime.DateTime; } set { - if (MockFileData == null) throw CommonExceptions.FileNotFound(path); + if (MockFileData == null) + { + throw CommonExceptions.FileNotFound(path); + } MockFileData.CreationTime = value; } } @@ -238,7 +255,7 @@ public override Stream Open(FileMode mode, FileAccess access, FileShare share) public override StreamReader OpenText() => mockFileSystem.File.OpenText(path); - public override Stream OpenWrite() => mockFileSystem.File.OpenWrite(path); + public override Stream OpenWrite() => mockFileSystem.File.OpenWrite(path); public override IFileInfo Replace(string destinationFileName, string destinationBackupFileName) { @@ -278,16 +295,26 @@ public override bool IsReadOnly { get { - if (MockFileData == null) throw CommonExceptions.FileNotFound(path); + if (MockFileData == null) + { + throw CommonExceptions.FileNotFound(path); + } return (MockFileData.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly; } set { - if (MockFileData == null) throw CommonExceptions.FileNotFound(path); + if (MockFileData == null) + { + throw CommonExceptions.FileNotFound(path); + } if (value) + { MockFileData.Attributes |= FileAttributes.ReadOnly; + } else + { MockFileData.Attributes &= ~FileAttributes.ReadOnly; + } } } @@ -295,7 +322,10 @@ public override long Length { get { - if (MockFileData == null || MockFileData.IsDirectory) throw CommonExceptions.FileNotFound(path); + if (MockFileData == null || MockFileData.IsDirectory) + { + throw CommonExceptions.FileNotFound(path); + } return MockFileData.Contents.Length; } } diff --git a/src/System.IO.Abstractions.TestingHelpers/MockFileSystem.cs b/src/System.IO.Abstractions.TestingHelpers/MockFileSystem.cs index b233267dc..5aaf48f65 100644 --- a/src/System.IO.Abstractions.TestingHelpers/MockFileSystem.cs +++ b/src/System.IO.Abstractions.TestingHelpers/MockFileSystem.cs @@ -163,8 +163,9 @@ public void AddDirectory(string path) { if (FileExists(fixedPath) && (GetFile(fixedPath).Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly) + { throw CommonExceptions.AccessDenied(fixedPath); - + } var lastIndex = 0; var isUnc = StringOperations.StartsWith(fixedPath, @"\\") || @@ -176,7 +177,9 @@ public void AddDirectory(string path) lastIndex = StringOperations.IndexOf(fixedPath, separator, 2); if (lastIndex < 0) + { throw CommonExceptions.InvalidUncPath(nameof(path)); + } /* * Although CreateDirectory(@"\\server\share\") is not going to work in real code, we allow it here for the purposes of setting up test doubles. diff --git a/src/System.IO.Abstractions/DirectoryInfoBase.cs b/src/System.IO.Abstractions/DirectoryInfoBase.cs index 3ffeb4766..ce67a6e74 100644 --- a/src/System.IO.Abstractions/DirectoryInfoBase.cs +++ b/src/System.IO.Abstractions/DirectoryInfoBase.cs @@ -100,7 +100,9 @@ internal DirectoryInfoBase() { } public static implicit operator DirectoryInfoBase(DirectoryInfo directoryInfo) { if (directoryInfo == null) + { return null; + } return new DirectoryInfoWrapper(new FileSystem(), directoryInfo); } } diff --git a/src/System.IO.Abstractions/FileSystemWatcherBase.cs b/src/System.IO.Abstractions/FileSystemWatcherBase.cs index b79182664..e66271420 100644 --- a/src/System.IO.Abstractions/FileSystemWatcherBase.cs +++ b/src/System.IO.Abstractions/FileSystemWatcherBase.cs @@ -67,7 +67,7 @@ public static implicit operator FileSystemWatcherBase(FileSystemWatcher watcher) { if (watcher == null) { - throw new ArgumentNullException(nameof(watcher)); + return null; } return new FileSystemWatcherWrapper(watcher);