Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions System.IO.Abstractions.TestingHelpers.Tests/MockFileLockTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using Collections.Generic;

using NUnit.Framework;

using NUnit.Framework.Constraints;
using XFS = MockUnixSupport;
class MockFileLockTests
{
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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]
Expand All @@ -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]
Expand All @@ -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]
Expand All @@ -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]
Expand All @@ -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]
Expand All @@ -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."));
}

Expand All @@ -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]
Expand All @@ -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]
Expand All @@ -185,5 +185,7 @@ public void MockFile_Lock_FileShareDeleteDoesNotThrowDelete()

Assert.DoesNotThrow(() => filesystem.File.Delete(filepath));
}

private static IResolveConstraint IOException() => Is.TypeOf<IOException>().And.Property("HResult").EqualTo(unchecked((int) 0x80070020));
}
}
6 changes: 4 additions & 2 deletions System.IO.Abstractions.TestingHelpers/CommonExceptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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);
}
}