From 65d5cb2acff294faba49c4910d37116787896dd9 Mon Sep 17 00:00:00 2001 From: Mike McFarland Date: Tue, 2 Apr 2019 14:55:55 +0100 Subject: [PATCH 1/2] Added HResult value for ERROR_SHARING_VIOLATION to File in use exception --- System.IO.Abstractions.TestingHelpers/CommonExceptions.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/System.IO.Abstractions.TestingHelpers/CommonExceptions.cs b/System.IO.Abstractions.TestingHelpers/CommonExceptions.cs index c42acc894..b6e6ab9db 100644 --- a/System.IO.Abstractions.TestingHelpers/CommonExceptions.cs +++ b/System.IO.Abstractions.TestingHelpers/CommonExceptions.cs @@ -4,6 +4,8 @@ namespace System.IO.Abstractions.TestingHelpers { internal static class CommonExceptions { + private const int _fileLockHResult = unchecked((int) 0x80070020); + public static FileNotFoundException FileNotFound(string path) => new FileNotFoundException( string.Format( @@ -57,7 +59,7 @@ public static Exception InvalidUncPath(string paramName) => public static IOException ProcessCannotAccessFileInUse(string paramName = null) => paramName != null - ? new IOException(string.Format(StringResources.Manager.GetString("PROCESS_CANNOT_ACCESS_FILE_IN_USE_WITH_FILENAME"), paramName)) - : new IOException(StringResources.Manager.GetString("PROCESS_CANNOT_ACCESS_FILE_IN_USE")); + ? new IOException(string.Format(StringResources.Manager.GetString("PROCESS_CANNOT_ACCESS_FILE_IN_USE_WITH_FILENAME"), paramName), _fileLockHResult) + : new IOException(StringResources.Manager.GetString("PROCESS_CANNOT_ACCESS_FILE_IN_USE"), _fileLockHResult); } } \ No newline at end of file From 44beeb8e147d0eb4455212b549fdffd0743e15ec Mon Sep 17 00:00:00 2001 From: Mike McFarland Date: Wed, 3 Apr 2019 10:51:40 +0100 Subject: [PATCH 2/2] Added HResult property check for MockFileLockTests --- .../MockFileLockTests.cs | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/System.IO.Abstractions.TestingHelpers.Tests/MockFileLockTests.cs b/System.IO.Abstractions.TestingHelpers.Tests/MockFileLockTests.cs index 4ba64b9b3..bc0a3d293 100644 --- a/System.IO.Abstractions.TestingHelpers.Tests/MockFileLockTests.cs +++ b/System.IO.Abstractions.TestingHelpers.Tests/MockFileLockTests.cs @@ -3,7 +3,7 @@ using Collections.Generic; using NUnit.Framework; - + using NUnit.Framework.Constraints; using XFS = MockUnixSupport; class MockFileLockTests { @@ -16,7 +16,7 @@ public void MockFile_Lock_FileShareNoneThrows() { filepath, new MockFileData("I'm here") { AllowedFileShare = FileShare.None }} }); - Assert.Throws(typeof(IOException), () => filesystem.File.Open(filepath, FileMode.Open, FileAccess.Read, FileShare.Read)); + Assert.Throws(IOException(), () => filesystem.File.Open(filepath, FileMode.Open, FileAccess.Read, FileShare.Read)); } [Test] public void MockFile_Lock_FileShareReadDoesNotThrowOnRead() @@ -38,7 +38,7 @@ public void MockFile_Lock_FileShareReadThrowsOnWrite() { filepath, new MockFileData("I'm here") { AllowedFileShare = FileShare.Read }} }); - Assert.Throws(typeof(IOException), () => filesystem.File.Open(filepath, FileMode.Open, FileAccess.Write, FileShare.Read)); + Assert.Throws(IOException(), () => filesystem.File.Open(filepath, FileMode.Open, FileAccess.Write, FileShare.Read)); } [Test] public void MockFile_Lock_FileShareWriteThrowsOnRead() @@ -49,7 +49,7 @@ public void MockFile_Lock_FileShareWriteThrowsOnRead() { filepath, new MockFileData("I'm here") { AllowedFileShare = FileShare.Write }} }); - Assert.Throws(typeof(IOException), () => filesystem.File.Open(filepath, FileMode.Open, FileAccess.Read, FileShare.Read)); + Assert.Throws(IOException(), () => filesystem.File.Open(filepath, FileMode.Open, FileAccess.Read, FileShare.Read)); } [Test] public void MockFile_Lock_FileShareWriteDoesNotThrowOnWrite() @@ -73,7 +73,7 @@ public void MockFile_Lock_FileShareNoneThrowsOnOpenRead() { filepath, new MockFileData("I'm here") { AllowedFileShare = FileShare.None }} }); - var exception = Assert.Throws(typeof(IOException), () => filesystem.File.OpenRead(filepath)); + var exception = Assert.Throws(IOException(), () => filesystem.File.OpenRead(filepath)); Assert.That(exception.Message, Is.EqualTo($"The process cannot access the file '{filepath}' because it is being used by another process.")); } [Test] @@ -85,7 +85,7 @@ public void MockFile_Lock_FileShareNoneThrowsOnWriteAllLines() { filepath, new MockFileData("I'm here") { AllowedFileShare = FileShare.None }} }); - var exception = Assert.Throws(typeof(IOException), () => filesystem.File.WriteAllLines(filepath, new string[] { "hello", "world" })); + var exception = Assert.Throws(IOException(), () => filesystem.File.WriteAllLines(filepath, new string[] { "hello", "world" })); Assert.That(exception.Message, Is.EqualTo($"The process cannot access the file '{filepath}' because it is being used by another process.")); } [Test] @@ -97,7 +97,7 @@ public void MockFile_Lock_FileShareNoneThrowsOnReadAllLines() { filepath, new MockFileData("I'm here") { AllowedFileShare = FileShare.None }} }); - var exception = Assert.Throws(typeof(IOException), () => filesystem.File.ReadAllLines(filepath)); + var exception = Assert.Throws(IOException(), () => filesystem.File.ReadAllLines(filepath)); Assert.That(exception.Message, Is.EqualTo($"The process cannot access the file '{filepath}' because it is being used by another process.")); } [Test] @@ -109,7 +109,7 @@ public void MockFile_Lock_FileShareNoneThrowsOnReadAllText() { filepath, new MockFileData("I'm here") { AllowedFileShare = FileShare.None }} }); - var exception = Assert.Throws(typeof(IOException), () => filesystem.File.ReadAllText(filepath)); + var exception = Assert.Throws(IOException(), () => filesystem.File.ReadAllText(filepath)); Assert.That(exception.Message, Is.EqualTo($"The process cannot access the file '{filepath}' because it is being used by another process.")); } [Test] @@ -121,7 +121,7 @@ public void MockFile_Lock_FileShareNoneThrowsOnReadAllBytes() { filepath, new MockFileData("I'm here") { AllowedFileShare = FileShare.None }} }); - var exception = Assert.Throws(typeof(IOException), () => filesystem.File.ReadAllBytes(filepath)); + var exception = Assert.Throws(IOException(), () => filesystem.File.ReadAllBytes(filepath)); Assert.That(exception.Message, Is.EqualTo($"The process cannot access the file '{filepath}' because it is being used by another process.")); } [Test] @@ -133,7 +133,7 @@ public void MockFile_Lock_FileShareNoneThrowsOnAppendLines() { filepath, new MockFileData("I'm here") { AllowedFileShare = FileShare.None }} }); - var exception = Assert.Throws(typeof(IOException), () => filesystem.File.AppendAllLines(filepath, new string[] { "hello", "world" })); + var exception = Assert.Throws(IOException(), () => filesystem.File.AppendAllLines(filepath, new string[] { "hello", "world" })); Assert.That(exception.Message, Is.EqualTo($"The process cannot access the file '{filepath}' because it is being used by another process.")); } @@ -147,7 +147,7 @@ public void MockFile_Lock_FileShareNoneThrowsFileMove() { filepath, new MockFileData("I'm here") { AllowedFileShare = FileShare.None }} }); - var exception = Assert.Throws(typeof(IOException), () => filesystem.File.Move(filepath, target)); + var exception = Assert.Throws(IOException(), () => filesystem.File.Move(filepath, target)); Assert.That(exception.Message, Is.EqualTo("The process cannot access the file because it is being used by another process.")); } [Test] @@ -171,7 +171,7 @@ public void MockFile_Lock_FileShareNoneThrowsDelete() { filepath, new MockFileData("I'm here") { AllowedFileShare = FileShare.None }} }); - var exception = Assert.Throws(typeof(IOException), () => filesystem.File.Delete(filepath)); + var exception = Assert.Throws(IOException(), () => filesystem.File.Delete(filepath)); Assert.That(exception.Message, Is.EqualTo($"The process cannot access the file '{filepath}' because it is being used by another process.")); } [Test] @@ -185,5 +185,7 @@ public void MockFile_Lock_FileShareDeleteDoesNotThrowDelete() Assert.DoesNotThrow(() => filesystem.File.Delete(filepath)); } + + private static IResolveConstraint IOException() => Is.TypeOf().And.Property("HResult").EqualTo(unchecked((int) 0x80070020)); } }