From 23d2ef822340b8d847f0ef9b94ca4f9761c9cd7c Mon Sep 17 00:00:00 2001 From: Florian Greinacher Date: Mon, 17 Dec 2018 21:42:46 +0100 Subject: [PATCH 1/4] Make sure FileNotFound exceptions contain path and proper messageFixes #424 --- .../MockFileMoveTests.cs | 2 +- .../MockFileReadAllLinesTests.cs | 15 +++++++ .../MockFileReadLinesTests.cs | 5 ++- .../ExceptionFactory.cs | 17 ++++++++ .../MockDirectoryInfo.cs | 2 +- .../MockFile.cs | 32 +++++++------- .../MockFileInfo.cs | 42 +++++++++---------- .../MockFileStream.cs | 2 +- 8 files changed, 76 insertions(+), 41 deletions(-) create mode 100644 System.IO.Abstractions.TestingHelpers/ExceptionFactory.cs diff --git a/System.IO.Abstractions.TestingHelpers.Tests/MockFileMoveTests.cs b/System.IO.Abstractions.TestingHelpers.Tests/MockFileMoveTests.cs index 1e839d797..fef5c3625 100644 --- a/System.IO.Abstractions.TestingHelpers.Tests/MockFileMoveTests.cs +++ b/System.IO.Abstractions.TestingHelpers.Tests/MockFileMoveTests.cs @@ -312,7 +312,7 @@ public void MockFile_Move_ShouldThrowFileNotFoundExceptionWhenSourceDoesNotExist var exception = Assert.Throws(() => fileSystem.File.Move(sourceFilePath, destFilePath)); - Assert.That(exception.Message, Is.EqualTo("The file \"" + XFS.Path("c:\\something\\demo.txt") + "\" could not be found.")); + Assert.That(exception.Message, Is.EqualTo("Could not find file '" + XFS.Path("c:\\something\\demo.txt") + "'.")); } [Test] diff --git a/System.IO.Abstractions.TestingHelpers.Tests/MockFileReadAllLinesTests.cs b/System.IO.Abstractions.TestingHelpers.Tests/MockFileReadAllLinesTests.cs index 327f11e02..74985c7d6 100644 --- a/System.IO.Abstractions.TestingHelpers.Tests/MockFileReadAllLinesTests.cs +++ b/System.IO.Abstractions.TestingHelpers.Tests/MockFileReadAllLinesTests.cs @@ -51,5 +51,20 @@ public void MockFile_ReadAllLines_ShouldReturnOriginalDataWithCustomEncoding() new [] { "Hello", "there", "Bob", "Bob!" }, result); } + + [Test] + public void MockFile_ReadAllLines_NotExistingFile_ThrowsCorrectFileNotFoundException() + { + var absentFileNameFullPath = XFS.Path(@"c:\you surely don't have such file.hope-so"); + var mockFileSystem = new MockFileSystem(); + + var act = new TestDelegate(() => + mockFileSystem.File.ReadAllText(absentFileNameFullPath) + ); + + var exception = Assert.Catch(act); + Assert.That(exception.FileName, Is.EqualTo(absentFileNameFullPath)); + Assert.That(exception.Message, Is.EqualTo("Could not find file '" + absentFileNameFullPath + "'.")); + } } } \ No newline at end of file diff --git a/System.IO.Abstractions.TestingHelpers.Tests/MockFileReadLinesTests.cs b/System.IO.Abstractions.TestingHelpers.Tests/MockFileReadLinesTests.cs index e9439b502..fdf2ef820 100644 --- a/System.IO.Abstractions.TestingHelpers.Tests/MockFileReadLinesTests.cs +++ b/System.IO.Abstractions.TestingHelpers.Tests/MockFileReadLinesTests.cs @@ -8,7 +8,8 @@ namespace System.IO.Abstractions.TestingHelpers.Tests using XFS = MockUnixSupport; - public class MockFileReadLinesTests { + public class MockFileReadLinesTests + { [Test] public void MockFile_ReadLines_ShouldReturnOriginalTextData() { @@ -48,7 +49,7 @@ public void MockFile_ReadLines_ShouldReturnOriginalDataWithCustomEncoding() // Assert CollectionAssert.AreEqual( - new [] { "Hello", "there", "Bob", "Bob!" }, + new[] { "Hello", "there", "Bob", "Bob!" }, result); } } diff --git a/System.IO.Abstractions.TestingHelpers/ExceptionFactory.cs b/System.IO.Abstractions.TestingHelpers/ExceptionFactory.cs new file mode 100644 index 000000000..0bc714a0f --- /dev/null +++ b/System.IO.Abstractions.TestingHelpers/ExceptionFactory.cs @@ -0,0 +1,17 @@ +using System.Globalization; + +namespace System.IO.Abstractions.TestingHelpers +{ + internal static class CommonExceptions + { + public static Exception FileNotFound(string path) => + new FileNotFoundException( + string.Format( + CultureInfo.InvariantCulture, + StringResources.Manager.GetString("COULD_NOT_FIND_FILE_EXCEPTION"), + path + ), + path + ); + } +} \ No newline at end of file diff --git a/System.IO.Abstractions.TestingHelpers/MockDirectoryInfo.cs b/System.IO.Abstractions.TestingHelpers/MockDirectoryInfo.cs index 0a9b651bc..a105d2217 100644 --- a/System.IO.Abstractions.TestingHelpers/MockDirectoryInfo.cs +++ b/System.IO.Abstractions.TestingHelpers/MockDirectoryInfo.cs @@ -293,7 +293,7 @@ private MockFileData GetMockFileDataForRead() private MockFileData GetMockFileDataForWrite() { return mockFileDataAccessor.GetFile(directoryPath) - ?? throw new FileNotFoundException(StringResources.Manager.GetString("COULD_NOT_FIND_FILE_EXCEPTION"), directoryPath); + ?? throw CommonExceptions.FileNotFound(directoryPath); } public override string ToString() diff --git a/System.IO.Abstractions.TestingHelpers/MockFile.cs b/System.IO.Abstractions.TestingHelpers/MockFile.cs index a2d24d91f..33dfcabbe 100644 --- a/System.IO.Abstractions.TestingHelpers/MockFile.cs +++ b/System.IO.Abstractions.TestingHelpers/MockFile.cs @@ -102,7 +102,7 @@ public override void Copy(string sourceFileName, string destFileName, bool overw if (!Exists(sourceFileName)) { - throw new FileNotFoundException(string.Format(CultureInfo.InvariantCulture, StringResources.Manager.GetString("COULD_NOT_FIND_FILE_EXCEPTION"), sourceFileName)); + throw CommonExceptions.FileNotFound(sourceFileName); } VerifyDirectoryExists(destFileName); @@ -205,7 +205,7 @@ public override FileSecurity GetAccessControl(string path) if (!mockFileDataAccessor.FileExists(path)) { - throw new FileNotFoundException(string.Format(CultureInfo.InvariantCulture, StringResources.Manager.GetString("COULD_NOT_FIND_FILE_EXCEPTION"), path)); + throw CommonExceptions.FileNotFound(path); } var fileData = mockFileDataAccessor.GetFile(path); @@ -255,7 +255,7 @@ public override FileAttributes GetAttributes(string path) { VerifyDirectoryExists(path); - throw new FileNotFoundException(string.Format(CultureInfo.InvariantCulture, "Could not find file '{0}'.", path)); + throw CommonExceptions.FileNotFound(path); } } @@ -351,7 +351,9 @@ public override void Move(string sourceFileName, string destFileName) var sourceFile = mockFileDataAccessor.GetFile(sourceFileName); if (sourceFile == null) - throw new FileNotFoundException(string.Format(CultureInfo.InvariantCulture, "The file \"{0}\" could not be found.", sourceFileName), sourceFileName); + { + throw CommonExceptions.FileNotFound(sourceFileName); + } VerifyDirectoryExists(destFileName); @@ -391,7 +393,7 @@ private Stream OpenInternal( throw new IOException(string.Format(CultureInfo.InvariantCulture, "The file '{0}' already exists.", path)); if ((mode == FileMode.Open || mode == FileMode.Truncate) && !exists) - throw new FileNotFoundException(path); + throw CommonExceptions.FileNotFound(path); if (!exists || mode == FileMode.CreateNew) return Create(path); @@ -442,7 +444,7 @@ public override byte[] ReadAllBytes(string path) if (!mockFileDataAccessor.FileExists(path)) { - throw new FileNotFoundException(string.Format(CultureInfo.InvariantCulture, StringResources.Manager.GetString("COULD_NOT_FIND_FILE_EXCEPTION"), path)); + throw CommonExceptions.FileNotFound(path); } return mockFileDataAccessor.GetFile(path).Contents; @@ -454,7 +456,7 @@ public override string[] ReadAllLines(string path) if (!mockFileDataAccessor.FileExists(path)) { - throw new FileNotFoundException(string.Format(CultureInfo.InvariantCulture, StringResources.Manager.GetString("COULD_NOT_FIND_FILE_EXCEPTION"), path)); + throw CommonExceptions.FileNotFound(path); } return mockFileDataAccessor @@ -474,7 +476,7 @@ public override string[] ReadAllLines(string path, Encoding encoding) if (!mockFileDataAccessor.FileExists(path)) { - throw new FileNotFoundException(string.Format(CultureInfo.InvariantCulture, "Can't find {0}", path)); + throw CommonExceptions.FileNotFound(path); } return encoding @@ -488,7 +490,7 @@ public override string ReadAllText(string path) if (!mockFileDataAccessor.FileExists(path)) { - throw new FileNotFoundException(string.Format(CultureInfo.InvariantCulture, "Can't find {0}", path)); + throw CommonExceptions.FileNotFound(path); } return ReadAllText(path, MockFileData.DefaultEncoding); @@ -541,12 +543,12 @@ public override void Replace(string sourceFileName, string destinationFileName, if (!mockFileDataAccessor.FileExists(sourceFileName)) { - throw new FileNotFoundException(string.Format(CultureInfo.InvariantCulture, StringResources.Manager.GetString("COULD_NOT_FIND_FILE_EXCEPTION"), sourceFileName)); + throw CommonExceptions.FileNotFound(sourceFileName); } if (!mockFileDataAccessor.FileExists(destinationFileName)) { - throw new FileNotFoundException(string.Format(CultureInfo.InvariantCulture, StringResources.Manager.GetString("COULD_NOT_FIND_FILE_EXCEPTION"), destinationFileName)); + throw CommonExceptions.FileNotFound(destinationFileName); } if (destinationBackupFileName != null) @@ -565,7 +567,7 @@ public override void SetAccessControl(string path, FileSecurity fileSecurity) if (!mockFileDataAccessor.FileExists(path)) { - throw new FileNotFoundException(string.Format(CultureInfo.InvariantCulture, "Can't find {0}", path), path); + throw CommonExceptions.FileNotFound(path); } var fileData = mockFileDataAccessor.GetFile(path); @@ -586,7 +588,7 @@ public override void SetAttributes(string path, FileAttributes fileAttributes) } else { - throw new FileNotFoundException(string.Format(CultureInfo.InvariantCulture, StringResources.Manager.GetString("COULD_NOT_FIND_FILE_EXCEPTION"), path), path); + throw CommonExceptions.FileNotFound(path); } } else @@ -676,7 +678,7 @@ public override void WriteAllBytes(string path, byte[] bytes) mockFileDataAccessor.AddFile(path, new MockFileData(bytes)); } - /// + /// /// Creates a new file, writes a collection of strings to the file, and then closes the file. /// /// The file to write to. @@ -978,7 +980,7 @@ private void VerifyDirectoryExists(string path) { throw new DirectoryNotFoundException( string.Format( - CultureInfo.InvariantCulture, + CultureInfo.InvariantCulture, StringResources.Manager.GetString("COULD_NOT_FIND_PART_OF_PATH_EXCEPTION"), path)); } diff --git a/System.IO.Abstractions.TestingHelpers/MockFileInfo.cs b/System.IO.Abstractions.TestingHelpers/MockFileInfo.cs index 257f3317c..03d7e809e 100644 --- a/System.IO.Abstractions.TestingHelpers/MockFileInfo.cs +++ b/System.IO.Abstractions.TestingHelpers/MockFileInfo.cs @@ -32,7 +32,7 @@ public override FileAttributes Attributes { get { - if (MockFileData == null) throw new FileNotFoundException("File not found", path); + if (MockFileData == null) throw CommonExceptions.FileNotFound(path); return MockFileData.Attributes; } set { MockFileData.Attributes = value; } @@ -42,12 +42,12 @@ public override DateTime CreationTime { get { - if (MockFileData == null) throw new FileNotFoundException("File not found", path); + if (MockFileData == null) throw CommonExceptions.FileNotFound(path); return MockFileData.CreationTime.DateTime; } set { - if (MockFileData == null) throw new FileNotFoundException("File not found", path); + if (MockFileData == null) throw CommonExceptions.FileNotFound(path); MockFileData.CreationTime = value; } } @@ -56,12 +56,12 @@ public override DateTime CreationTimeUtc { get { - if (MockFileData == null) throw new FileNotFoundException("File not found", path); + if (MockFileData == null) throw CommonExceptions.FileNotFound(path); return MockFileData.CreationTime.UtcDateTime; } set { - if (MockFileData == null) throw new FileNotFoundException("File not found", path); + if (MockFileData == null) throw CommonExceptions.FileNotFound(path); MockFileData.CreationTime = value.ToLocalTime(); } } @@ -90,12 +90,12 @@ public override DateTime LastAccessTime { get { - if (MockFileData == null) throw new FileNotFoundException("File not found", path); + if (MockFileData == null) throw CommonExceptions.FileNotFound(path); return MockFileData.LastAccessTime.DateTime; } set { - if (MockFileData == null) throw new FileNotFoundException("File not found", path); + if (MockFileData == null) throw CommonExceptions.FileNotFound(path); MockFileData.LastAccessTime = value; } } @@ -104,12 +104,12 @@ public override DateTime LastAccessTimeUtc { get { - if (MockFileData == null) throw new FileNotFoundException("File not found", path); + if (MockFileData == null) throw CommonExceptions.FileNotFound(path); return MockFileData.LastAccessTime.UtcDateTime; } set { - if (MockFileData == null) throw new FileNotFoundException("File not found", path); + if (MockFileData == null) throw CommonExceptions.FileNotFound(path); MockFileData.LastAccessTime = value; } } @@ -118,12 +118,12 @@ public override DateTime LastWriteTime { get { - if (MockFileData == null) throw new FileNotFoundException("File not found", path); + if (MockFileData == null) throw CommonExceptions.FileNotFound(path); return MockFileData.LastWriteTime.DateTime; } set { - if (MockFileData == null) throw new FileNotFoundException("File not found", path); + if (MockFileData == null) throw CommonExceptions.FileNotFound(path); MockFileData.LastWriteTime = value; } } @@ -132,12 +132,12 @@ public override DateTime LastWriteTimeUtc { get { - if (MockFileData == null) throw new FileNotFoundException("File not found", path); + if (MockFileData == null) throw CommonExceptions.FileNotFound(path); return MockFileData.LastWriteTime.UtcDateTime; } set { - if (MockFileData == null) throw new FileNotFoundException("File not found", path); + if (MockFileData == null) throw CommonExceptions.FileNotFound(path); MockFileData.LastWriteTime = value.ToLocalTime(); } } @@ -149,7 +149,7 @@ public override string Name public override StreamWriter AppendText() { - if (MockFileData == null) throw new FileNotFoundException("File not found", path); + if (MockFileData == null) throw CommonExceptions.FileNotFound(path); return new StreamWriter(new MockFileStream(mockFileSystem, FullName, MockFileStream.StreamType.APPEND)); } @@ -162,7 +162,7 @@ public override FileInfoBase CopyTo(string destFileName, bool overwrite) { if (!Exists) { - throw new FileNotFoundException("The file does not exist and can't be moved or copied.", FullName); + if (MockFileData == null) throw CommonExceptions.FileNotFound(FullName); } if (destFileName == FullName) { @@ -185,14 +185,14 @@ public override StreamWriter CreateText() #if NET40 public override void Decrypt() { - if (MockFileData == null) throw new FileNotFoundException("File not found", path); + if (MockFileData == null) throw CommonExceptions.FileNotFound(path); MockFileData.Attributes &= ~FileAttributes.Encrypted; } public override void Encrypt() { - if (MockFileData == null) throw new FileNotFoundException("File not found", path); + if (MockFileData == null) throw CommonExceptions.FileNotFound(path); MockFileData.Attributes |= FileAttributes.Encrypted; } @@ -236,7 +236,7 @@ public override Stream Open(FileMode mode, FileAccess access, FileShare share) public override Stream OpenRead() { - if (MockFileData == null) throw new FileNotFoundException("File not found", path); + if (MockFileData == null) throw CommonExceptions.FileNotFound(path); return new MockFileStream(mockFileSystem, path, MockFileStream.StreamType.READ); } @@ -290,12 +290,12 @@ public override bool IsReadOnly { get { - if (MockFileData == null) throw new FileNotFoundException("File not found", path); + if (MockFileData == null) throw CommonExceptions.FileNotFound(path); return (MockFileData.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly; } set { - if (MockFileData == null) throw new FileNotFoundException("File not found", path); + if (MockFileData == null) throw CommonExceptions.FileNotFound(path); if (value) MockFileData.Attributes |= FileAttributes.ReadOnly; else @@ -307,7 +307,7 @@ public override long Length { get { - if (MockFileData == null) throw new FileNotFoundException("File not found", path); + if (MockFileData == null) throw CommonExceptions.FileNotFound(path); return MockFileData.Contents.Length; } } diff --git a/System.IO.Abstractions.TestingHelpers/MockFileStream.cs b/System.IO.Abstractions.TestingHelpers/MockFileStream.cs index 7e3d2f291..a6bee6e96 100644 --- a/System.IO.Abstractions.TestingHelpers/MockFileStream.cs +++ b/System.IO.Abstractions.TestingHelpers/MockFileStream.cs @@ -56,7 +56,7 @@ public MockFileStream( { if (StreamType.READ.Equals(streamType)) { - throw new FileNotFoundException("File not found.", path); + throw CommonExceptions.FileNotFound(path); } mockFileDataAccessor.AddFile(path, new MockFileData(new byte[] { })); } From 8017e2ff5f7d30e0d9b3e2161b2965625f9537bb Mon Sep 17 00:00:00 2001 From: Florian Greinacher Date: Wed, 19 Dec 2018 21:36:13 +0100 Subject: [PATCH 2/4] Use CommonExceptions for more errors --- .../CommonExceptions.cs | 35 +++++++++++++++++++ .../ExceptionFactory.cs | 17 --------- .../MockDirectory.cs | 10 ++---- .../MockFile.cs | 8 ++--- .../MockFileSystem.cs | 6 ++-- 5 files changed, 43 insertions(+), 33 deletions(-) create mode 100644 System.IO.Abstractions.TestingHelpers/CommonExceptions.cs delete mode 100644 System.IO.Abstractions.TestingHelpers/ExceptionFactory.cs diff --git a/System.IO.Abstractions.TestingHelpers/CommonExceptions.cs b/System.IO.Abstractions.TestingHelpers/CommonExceptions.cs new file mode 100644 index 000000000..412811c7a --- /dev/null +++ b/System.IO.Abstractions.TestingHelpers/CommonExceptions.cs @@ -0,0 +1,35 @@ +using System.Globalization; + +namespace System.IO.Abstractions.TestingHelpers +{ + internal static class CommonExceptions + { + public static Exception FileNotFound(string path) => + new FileNotFoundException( + string.Format( + CultureInfo.InvariantCulture, + StringResources.Manager.GetString("COULD_NOT_FIND_FILE_EXCEPTION"), + path + ), + path + ); + + public static Exception CouldNotFindPartOfPath(string path) => + new DirectoryNotFoundException( + string.Format( + CultureInfo.InvariantCulture, + StringResources.Manager.GetString("COULD_NOT_FIND_PART_OF_PATH_EXCEPTION"), + path + ) + ); + + public static Exception AccessDenied(string path) => + new UnauthorizedAccessException( + string.Format( + CultureInfo.InvariantCulture, + StringResources.Manager.GetString("ACCESS_TO_THE_PATH_IS_DENIED"), + path + ) + ); + } +} \ No newline at end of file diff --git a/System.IO.Abstractions.TestingHelpers/ExceptionFactory.cs b/System.IO.Abstractions.TestingHelpers/ExceptionFactory.cs deleted file mode 100644 index 0bc714a0f..000000000 --- a/System.IO.Abstractions.TestingHelpers/ExceptionFactory.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System.Globalization; - -namespace System.IO.Abstractions.TestingHelpers -{ - internal static class CommonExceptions - { - public static Exception FileNotFound(string path) => - new FileNotFoundException( - string.Format( - CultureInfo.InvariantCulture, - StringResources.Manager.GetString("COULD_NOT_FIND_FILE_EXCEPTION"), - path - ), - path - ); - } -} \ No newline at end of file diff --git a/System.IO.Abstractions.TestingHelpers/MockDirectory.cs b/System.IO.Abstractions.TestingHelpers/MockDirectory.cs index c38d0ce4a..123bc8c43 100644 --- a/System.IO.Abstractions.TestingHelpers/MockDirectory.cs +++ b/System.IO.Abstractions.TestingHelpers/MockDirectory.cs @@ -117,7 +117,7 @@ public override DirectorySecurity GetAccessControl(string path) if (!mockFileDataAccessor.Directory.Exists(path)) { - throw new DirectoryNotFoundException(string.Format(CultureInfo.InvariantCulture, StringResources.Manager.GetString("COULD_NOT_FIND_PART_OF_PATH_EXCEPTION"), path)); + throw CommonExceptions.CouldNotFindPartOfPath(path); } var directoryData = (MockDirectoryData) mockFileDataAccessor.GetFile(path); @@ -203,11 +203,7 @@ private string[] GetFilesInternal( if (!Exists(path)) { - throw new DirectoryNotFoundException( - string.Format( - CultureInfo.InvariantCulture, - StringResources.Manager.GetString("COULD_NOT_FIND_PART_OF_PATH_EXCEPTION"), - path)); + throw CommonExceptions.CouldNotFindPartOfPath(path); } path = EnsureAbsolutePath(path); @@ -402,7 +398,7 @@ public override void SetAccessControl(string path, DirectorySecurity directorySe if (!mockFileDataAccessor.Directory.Exists(path)) { - throw new DirectoryNotFoundException(string.Format(CultureInfo.InvariantCulture, StringResources.Manager.GetString("COULD_NOT_FIND_PART_OF_PATH_EXCEPTION"), path)); + throw CommonExceptions.CouldNotFindPartOfPath(path); } var directoryData = (MockDirectoryData)mockFileDataAccessor.GetFile(path); diff --git a/System.IO.Abstractions.TestingHelpers/MockFile.cs b/System.IO.Abstractions.TestingHelpers/MockFile.cs index 33dfcabbe..4affecf85 100644 --- a/System.IO.Abstractions.TestingHelpers/MockFile.cs +++ b/System.IO.Abstractions.TestingHelpers/MockFile.cs @@ -939,7 +939,7 @@ public override void WriteAllText(string path, string contents, Encoding encodin if (mockFileDataAccessor.Directory.Exists(path)) { - throw new UnauthorizedAccessException(string.Format(CultureInfo.InvariantCulture, StringResources.Manager.GetString("ACCESS_TO_THE_PATH_IS_DENIED"), path)); + throw CommonExceptions.AccessDenied(path); } VerifyDirectoryExists(path); @@ -978,11 +978,7 @@ private void VerifyDirectoryExists(string path) if (!mockFileDataAccessor.Directory.Exists(dir)) { - throw new DirectoryNotFoundException( - string.Format( - CultureInfo.InvariantCulture, - StringResources.Manager.GetString("COULD_NOT_FIND_PART_OF_PATH_EXCEPTION"), - path)); + throw CommonExceptions.CouldNotFindPartOfPath(path); } } } diff --git a/System.IO.Abstractions.TestingHelpers/MockFileSystem.cs b/System.IO.Abstractions.TestingHelpers/MockFileSystem.cs index cd3d15f64..44939a8c5 100644 --- a/System.IO.Abstractions.TestingHelpers/MockFileSystem.cs +++ b/System.IO.Abstractions.TestingHelpers/MockFileSystem.cs @@ -127,7 +127,7 @@ public void AddFile(string path, MockFileData mockFile) if (isReadOnly || isHidden) { - throw new UnauthorizedAccessException(string.Format(CultureInfo.InvariantCulture, StringResources.Manager.GetString("ACCESS_TO_THE_PATH_IS_DENIED"), path)); + throw CommonExceptions.AccessDenied(path); } } @@ -151,7 +151,7 @@ public void AddDirectory(string path) { if (FileExists(fixedPath) && (GetFile(fixedPath).Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly) - throw new UnauthorizedAccessException(string.Format(CultureInfo.InvariantCulture, StringResources.Manager.GetString("ACCESS_TO_THE_PATH_IS_DENIED"), fixedPath)); + throw CommonExceptions.AccessDenied(fixedPath); var lastIndex = 0; var isUnc = @@ -247,7 +247,7 @@ public void RemoveFile(string path) { if (FileExists(path) && (GetFile(path).Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly) { - throw new UnauthorizedAccessException(string.Format(CultureInfo.InvariantCulture, StringResources.Manager.GetString("ACCESS_TO_THE_PATH_IS_DENIED"), path)); + throw CommonExceptions.AccessDenied(path); } files.Remove(path); From 6719a59e69fa2a69c4e2f2105d87da6054cad1d4 Mon Sep 17 00:00:00 2001 From: Florian Greinacher Date: Sun, 23 Dec 2018 14:41:24 +0100 Subject: [PATCH 3/4] Move more logic to CommonExceptions --- .../CommonExceptions.cs | 16 +++++++++++++--- .../MockDirectory.cs | 2 +- .../MockFile.cs | 16 ++++++++-------- .../PathVerifier.cs | 6 +++--- 4 files changed, 25 insertions(+), 15 deletions(-) diff --git a/System.IO.Abstractions.TestingHelpers/CommonExceptions.cs b/System.IO.Abstractions.TestingHelpers/CommonExceptions.cs index 412811c7a..56f779d82 100644 --- a/System.IO.Abstractions.TestingHelpers/CommonExceptions.cs +++ b/System.IO.Abstractions.TestingHelpers/CommonExceptions.cs @@ -4,7 +4,7 @@ namespace System.IO.Abstractions.TestingHelpers { internal static class CommonExceptions { - public static Exception FileNotFound(string path) => + public static FileNotFoundException FileNotFound(string path) => new FileNotFoundException( string.Format( CultureInfo.InvariantCulture, @@ -14,7 +14,7 @@ public static Exception FileNotFound(string path) => path ); - public static Exception CouldNotFindPartOfPath(string path) => + public static DirectoryNotFoundException CouldNotFindPartOfPath(string path) => new DirectoryNotFoundException( string.Format( CultureInfo.InvariantCulture, @@ -23,7 +23,7 @@ public static Exception CouldNotFindPartOfPath(string path) => ) ); - public static Exception AccessDenied(string path) => + public static UnauthorizedAccessException AccessDenied(string path) => new UnauthorizedAccessException( string.Format( CultureInfo.InvariantCulture, @@ -31,5 +31,15 @@ public static Exception AccessDenied(string path) => path ) ); + public static ArgumentNullException FilenameCannotBeNull(string paramName) => + new ArgumentNullException( + paramName, + StringResources.Manager.GetString("FILENAME_CANNOT_BE_NULL") + ); + + public static ArgumentException IllegalCharactersInPath(string paramName = null) => + paramName != null + ? new ArgumentException(StringResources.Manager.GetString("ILLEGAL_CHARACTERS_IN_PATH_EXCEPTION"), paramName) + : new ArgumentException(StringResources.Manager.GetString("ILLEGAL_CHARACTERS_IN_PATH_EXCEPTION")); } } \ No newline at end of file diff --git a/System.IO.Abstractions.TestingHelpers/MockDirectory.cs b/System.IO.Abstractions.TestingHelpers/MockDirectory.cs index 123bc8c43..bf370ce78 100644 --- a/System.IO.Abstractions.TestingHelpers/MockDirectory.cs +++ b/System.IO.Abstractions.TestingHelpers/MockDirectory.cs @@ -536,7 +536,7 @@ private void CheckSearchPattern(string searchPattern) var invalidPathChars = Path.GetInvalidPathChars(); if (searchPattern.IndexOfAny(invalidPathChars) > -1) { - throw new ArgumentException(StringResources.Manager.GetString("ILLEGAL_CHARACTERS_IN_PATH_EXCEPTION"), "searchPattern"); + throw CommonExceptions.IllegalCharactersInPath(nameof(searchPattern)); } } } diff --git a/System.IO.Abstractions.TestingHelpers/MockFile.cs b/System.IO.Abstractions.TestingHelpers/MockFile.cs index 4affecf85..b9fb822c6 100644 --- a/System.IO.Abstractions.TestingHelpers/MockFile.cs +++ b/System.IO.Abstractions.TestingHelpers/MockFile.cs @@ -89,16 +89,16 @@ public override void Copy(string sourceFileName, string destFileName, bool overw { if (sourceFileName == null) { - throw new ArgumentNullException(nameof(sourceFileName), StringResources.Manager.GetString("FILENAME_CANNOT_BE_NULL")); + throw CommonExceptions.FilenameCannotBeNull(nameof(sourceFileName)); } if (destFileName == null) { - throw new ArgumentNullException(nameof(destFileName), StringResources.Manager.GetString("FILENAME_CANNOT_BE_NULL")); + throw CommonExceptions.FilenameCannotBeNull(nameof(destFileName)); } - mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(sourceFileName, "sourceFileName"); - mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(destFileName, "destFileName"); + mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(sourceFileName, nameof(sourceFileName)); + mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(destFileName, nameof(destFileName)); if (!Exists(sourceFileName)) { @@ -324,16 +324,16 @@ public override void Move(string sourceFileName, string destFileName) { if (sourceFileName == null) { - throw new ArgumentNullException(nameof(sourceFileName), StringResources.Manager.GetString("FILENAME_CANNOT_BE_NULL")); + throw CommonExceptions.FilenameCannotBeNull(nameof(sourceFileName)); } if (destFileName == null) { - throw new ArgumentNullException(nameof(destFileName), StringResources.Manager.GetString("FILENAME_CANNOT_BE_NULL")); + throw CommonExceptions.FilenameCannotBeNull(nameof(destFileName)); } - mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(sourceFileName, "sourceFileName"); - mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(destFileName, "destFileName"); + mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(sourceFileName, nameof(sourceFileName)); + mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(destFileName, nameof(destFileName)); if (mockFileDataAccessor.GetFile(destFileName) != null) { diff --git a/System.IO.Abstractions.TestingHelpers/PathVerifier.cs b/System.IO.Abstractions.TestingHelpers/PathVerifier.cs index a1d11cfdd..aaccb8f91 100644 --- a/System.IO.Abstractions.TestingHelpers/PathVerifier.cs +++ b/System.IO.Abstractions.TestingHelpers/PathVerifier.cs @@ -38,14 +38,14 @@ public void IsLegalAbsoluteOrRelative(string path, string paramName) if (ExtractFileName(path).IndexOfAny(_mockFileDataAccessor.Path.GetInvalidFileNameChars()) > -1) { - throw new ArgumentException(StringResources.Manager.GetString("ILLEGAL_CHARACTERS_IN_PATH_EXCEPTION")); + throw CommonExceptions.IllegalCharactersInPath(); } var filePath = ExtractFilePath(path); if (HasIllegalCharacters(filePath, checkAdditional: false)) { - throw new ArgumentException(StringResources.Manager.GetString("ILLEGAL_CHARACTERS_IN_PATH_EXCEPTION")); + throw CommonExceptions.IllegalCharactersInPath(); } } @@ -96,7 +96,7 @@ public void CheckInvalidPathChars(string path, bool checkAdditional = false) if (HasIllegalCharacters(path, checkAdditional)) { - throw new ArgumentException(StringResources.Manager.GetString("ILLEGAL_CHARACTERS_IN_PATH_EXCEPTION")); + throw CommonExceptions.IllegalCharactersInPath(); } } } From 6087eb8b61e37f24302e1a74605014dd342107fb Mon Sep 17 00:00:00 2001 From: Florian Greinacher Date: Tue, 1 Jan 2019 14:59:00 +0100 Subject: [PATCH 4/4] add some more common exceptions --- .../CommonExceptions.cs | 15 ++++++++++++++- System.IO.Abstractions.TestingHelpers/MockFile.cs | 2 +- .../MockFileSystem.cs | 2 +- System.IO.Abstractions.TestingHelpers/MockPath.cs | 4 ++-- .../PathVerifier.cs | 7 ++++--- 5 files changed, 22 insertions(+), 8 deletions(-) diff --git a/System.IO.Abstractions.TestingHelpers/CommonExceptions.cs b/System.IO.Abstractions.TestingHelpers/CommonExceptions.cs index 56f779d82..66bdc287b 100644 --- a/System.IO.Abstractions.TestingHelpers/CommonExceptions.cs +++ b/System.IO.Abstractions.TestingHelpers/CommonExceptions.cs @@ -31,6 +31,16 @@ public static UnauthorizedAccessException AccessDenied(string path) => path ) ); + + public static Exception InvalidUseOfVolumeSeparator() => + new NotSupportedException(StringResources.Manager.GetString("THE_PATH_IS_NOT_OF_A_LEGAL_FORM")); + + public static Exception PathIsNotOfALegalForm(string paramName) => + new ArgumentException( + StringResources.Manager.GetString("THE_PATH_IS_NOT_OF_A_LEGAL_FORM"), + paramName + ); + public static ArgumentNullException FilenameCannotBeNull(string paramName) => new ArgumentNullException( paramName, @@ -40,6 +50,9 @@ public static ArgumentNullException FilenameCannotBeNull(string paramName) => public static ArgumentException IllegalCharactersInPath(string paramName = null) => paramName != null ? new ArgumentException(StringResources.Manager.GetString("ILLEGAL_CHARACTERS_IN_PATH_EXCEPTION"), paramName) - : new ArgumentException(StringResources.Manager.GetString("ILLEGAL_CHARACTERS_IN_PATH_EXCEPTION")); + : new ArgumentException(StringResources.Manager.GetString("ILLEGAL_CHARACTERS_IN_PATH_EXCEPTION")); + + public static Exception InvalidUncPath(string paramName) => + new ArgumentException(@"The UNC path should be of the form \\server\share.", paramName); } } \ No newline at end of file diff --git a/System.IO.Abstractions.TestingHelpers/MockFile.cs b/System.IO.Abstractions.TestingHelpers/MockFile.cs index b9fb822c6..1ca3ac9bf 100644 --- a/System.IO.Abstractions.TestingHelpers/MockFile.cs +++ b/System.IO.Abstractions.TestingHelpers/MockFile.cs @@ -233,7 +233,7 @@ public override FileAttributes GetAttributes(string path) { if (path != null && path.Length == 0) { - throw new ArgumentException(StringResources.Manager.GetString("THE_PATH_IS_NOT_OF_A_LEGAL_FORM"), "path"); + throw CommonExceptions.PathIsNotOfALegalForm(nameof(path)); } mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path"); diff --git a/System.IO.Abstractions.TestingHelpers/MockFileSystem.cs b/System.IO.Abstractions.TestingHelpers/MockFileSystem.cs index 44939a8c5..d4bde1869 100644 --- a/System.IO.Abstractions.TestingHelpers/MockFileSystem.cs +++ b/System.IO.Abstractions.TestingHelpers/MockFileSystem.cs @@ -164,7 +164,7 @@ public void AddDirectory(string path) lastIndex = StringOperations.IndexOf(fixedPath, separator, 2); if (lastIndex < 0) - throw new ArgumentException(@"The UNC path should be of the form \\server\share.", "path"); + 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/System.IO.Abstractions.TestingHelpers/MockPath.cs b/System.IO.Abstractions.TestingHelpers/MockPath.cs index a1929b457..921f650e4 100644 --- a/System.IO.Abstractions.TestingHelpers/MockPath.cs +++ b/System.IO.Abstractions.TestingHelpers/MockPath.cs @@ -26,7 +26,7 @@ public override string GetFullPath(string path) if (path.Length == 0) { - throw new ArgumentException(StringResources.Manager.GetString("THE_PATH_IS_NOT_OF_A_LEGAL_FORM"), "path"); + throw CommonExceptions.PathIsNotOfALegalForm(nameof(path)); } path = path.Replace(AltDirectorySeparatorChar, DirectorySeparatorChar); @@ -53,7 +53,7 @@ public override string GetFullPath(string path) pathSegments = GetSegments(path); if (pathSegments.Length < 2) { - throw new ArgumentException(@"The UNC path should be of the form \\server\share.", "path"); + throw CommonExceptions.InvalidUncPath(nameof(path)); } } else if (mockFileDataAccessor.StringOperations.Equals(@"\", root) || diff --git a/System.IO.Abstractions.TestingHelpers/PathVerifier.cs b/System.IO.Abstractions.TestingHelpers/PathVerifier.cs index aaccb8f91..c32680276 100644 --- a/System.IO.Abstractions.TestingHelpers/PathVerifier.cs +++ b/System.IO.Abstractions.TestingHelpers/PathVerifier.cs @@ -25,15 +25,16 @@ public void IsLegalAbsoluteOrRelative(string path, string paramName) { throw new ArgumentException("Empty file name is not legal.", paramName); } - + if (path.Trim() == string.Empty) { - throw new ArgumentException(StringResources.Manager.GetString("THE_PATH_IS_NOT_OF_A_LEGAL_FORM"), paramName); + throw CommonExceptions.PathIsNotOfALegalForm(paramName); } if (XFS.IsWindowsPlatform() && !IsValidUseOfVolumeSeparatorChar(path)) { - throw new NotSupportedException(StringResources.Manager.GetString("THE_PATH_IS_NOT_OF_A_LEGAL_FORM")); + + throw CommonExceptions.InvalidUseOfVolumeSeparator(); } if (ExtractFileName(path).IndexOfAny(_mockFileDataAccessor.Path.GetInvalidFileNameChars()) > -1)