Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ public void MockFile_Move_ShouldThrowFileNotFoundExceptionWhenSourceDoesNotExist

var exception = Assert.Throws<FileNotFoundException>(() => 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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<FileNotFoundException>(act);
Assert.That(exception.FileName, Is.EqualTo(absentFileNameFullPath));
Assert.That(exception.Message, Is.EqualTo("Could not find file '" + absentFileNameFullPath + "'."));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -48,7 +49,7 @@ public void MockFile_ReadLines_ShouldReturnOriginalDataWithCustomEncoding()

// Assert
CollectionAssert.AreEqual(
new [] { "Hello", "there", "Bob", "Bob!" },
new[] { "Hello", "there", "Bob", "Bob!" },
result);
}
}
Expand Down
58 changes: 58 additions & 0 deletions System.IO.Abstractions.TestingHelpers/CommonExceptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System.Globalization;

namespace System.IO.Abstractions.TestingHelpers
{
internal static class CommonExceptions
{
public static FileNotFoundException FileNotFound(string path) =>
new FileNotFoundException(
string.Format(
CultureInfo.InvariantCulture,
StringResources.Manager.GetString("COULD_NOT_FIND_FILE_EXCEPTION"),
path
),
path
);

public static DirectoryNotFoundException CouldNotFindPartOfPath(string path) =>
new DirectoryNotFoundException(
string.Format(
CultureInfo.InvariantCulture,
StringResources.Manager.GetString("COULD_NOT_FIND_PART_OF_PATH_EXCEPTION"),
path
)
);

public static UnauthorizedAccessException AccessDenied(string path) =>
new UnauthorizedAccessException(
string.Format(
CultureInfo.InvariantCulture,
StringResources.Manager.GetString("ACCESS_TO_THE_PATH_IS_DENIED"),
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,
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"));

public static Exception InvalidUncPath(string paramName) =>
new ArgumentException(@"The UNC path should be of the form \\server\share.", paramName);
}
}
12 changes: 4 additions & 8 deletions System.IO.Abstractions.TestingHelpers/MockDirectory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -540,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));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion System.IO.Abstractions.TestingHelpers/MockDirectoryInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
56 changes: 27 additions & 29 deletions System.IO.Abstractions.TestingHelpers/MockFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,20 @@ 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))
{
throw new FileNotFoundException(string.Format(CultureInfo.InvariantCulture, StringResources.Manager.GetString("COULD_NOT_FIND_FILE_EXCEPTION"), sourceFileName));
throw CommonExceptions.FileNotFound(sourceFileName);
}

VerifyDirectoryExists(destFileName);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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");
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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)
{
Expand All @@ -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);

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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)
Expand All @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -676,7 +678,7 @@ public override void WriteAllBytes(string path, byte[] bytes)
mockFileDataAccessor.AddFile(path, new MockFileData(bytes));
}

/// <summary>
/// <summary>
/// Creates a new file, writes a collection of strings to the file, and then closes the file.
/// </summary>
/// <param name="path">The file to write to.</param>
Expand Down Expand Up @@ -937,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);
Expand Down Expand Up @@ -976,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);
}
}
}
Expand Down
Loading