Skip to content

Commit 14e634e

Browse files
authored
Make sure FileNotFound exceptions contain path and proper message (#427)
Fixes #424
1 parent 5f0c988 commit 14e634e

File tree

12 files changed

+144
-75
lines changed

12 files changed

+144
-75
lines changed

System.IO.Abstractions.TestingHelpers.Tests/MockFileMoveTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ public void MockFile_Move_ShouldThrowFileNotFoundExceptionWhenSourceDoesNotExist
312312

313313
var exception = Assert.Throws<FileNotFoundException>(() => fileSystem.File.Move(sourceFilePath, destFilePath));
314314

315-
Assert.That(exception.Message, Is.EqualTo("The file \"" + XFS.Path("c:\\something\\demo.txt") + "\" could not be found."));
315+
Assert.That(exception.Message, Is.EqualTo("Could not find file '" + XFS.Path("c:\\something\\demo.txt") + "'."));
316316
}
317317

318318
[Test]

System.IO.Abstractions.TestingHelpers.Tests/MockFileReadAllLinesTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,20 @@ public void MockFile_ReadAllLines_ShouldReturnOriginalDataWithCustomEncoding()
5151
new [] { "Hello", "there", "Bob", "Bob!" },
5252
result);
5353
}
54+
55+
[Test]
56+
public void MockFile_ReadAllLines_NotExistingFile_ThrowsCorrectFileNotFoundException()
57+
{
58+
var absentFileNameFullPath = XFS.Path(@"c:\you surely don't have such file.hope-so");
59+
var mockFileSystem = new MockFileSystem();
60+
61+
var act = new TestDelegate(() =>
62+
mockFileSystem.File.ReadAllText(absentFileNameFullPath)
63+
);
64+
65+
var exception = Assert.Catch<FileNotFoundException>(act);
66+
Assert.That(exception.FileName, Is.EqualTo(absentFileNameFullPath));
67+
Assert.That(exception.Message, Is.EqualTo("Could not find file '" + absentFileNameFullPath + "'."));
68+
}
5469
}
5570
}

System.IO.Abstractions.TestingHelpers.Tests/MockFileReadLinesTests.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ namespace System.IO.Abstractions.TestingHelpers.Tests
88

99
using XFS = MockUnixSupport;
1010

11-
public class MockFileReadLinesTests {
11+
public class MockFileReadLinesTests
12+
{
1213
[Test]
1314
public void MockFile_ReadLines_ShouldReturnOriginalTextData()
1415
{
@@ -48,7 +49,7 @@ public void MockFile_ReadLines_ShouldReturnOriginalDataWithCustomEncoding()
4849

4950
// Assert
5051
CollectionAssert.AreEqual(
51-
new [] { "Hello", "there", "Bob", "Bob!" },
52+
new[] { "Hello", "there", "Bob", "Bob!" },
5253
result);
5354
}
5455
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using System.Globalization;
2+
3+
namespace System.IO.Abstractions.TestingHelpers
4+
{
5+
internal static class CommonExceptions
6+
{
7+
public static FileNotFoundException FileNotFound(string path) =>
8+
new FileNotFoundException(
9+
string.Format(
10+
CultureInfo.InvariantCulture,
11+
StringResources.Manager.GetString("COULD_NOT_FIND_FILE_EXCEPTION"),
12+
path
13+
),
14+
path
15+
);
16+
17+
public static DirectoryNotFoundException CouldNotFindPartOfPath(string path) =>
18+
new DirectoryNotFoundException(
19+
string.Format(
20+
CultureInfo.InvariantCulture,
21+
StringResources.Manager.GetString("COULD_NOT_FIND_PART_OF_PATH_EXCEPTION"),
22+
path
23+
)
24+
);
25+
26+
public static UnauthorizedAccessException AccessDenied(string path) =>
27+
new UnauthorizedAccessException(
28+
string.Format(
29+
CultureInfo.InvariantCulture,
30+
StringResources.Manager.GetString("ACCESS_TO_THE_PATH_IS_DENIED"),
31+
path
32+
)
33+
);
34+
35+
public static Exception InvalidUseOfVolumeSeparator() =>
36+
new NotSupportedException(StringResources.Manager.GetString("THE_PATH_IS_NOT_OF_A_LEGAL_FORM"));
37+
38+
public static Exception PathIsNotOfALegalForm(string paramName) =>
39+
new ArgumentException(
40+
StringResources.Manager.GetString("THE_PATH_IS_NOT_OF_A_LEGAL_FORM"),
41+
paramName
42+
);
43+
44+
public static ArgumentNullException FilenameCannotBeNull(string paramName) =>
45+
new ArgumentNullException(
46+
paramName,
47+
StringResources.Manager.GetString("FILENAME_CANNOT_BE_NULL")
48+
);
49+
50+
public static ArgumentException IllegalCharactersInPath(string paramName = null) =>
51+
paramName != null
52+
? new ArgumentException(StringResources.Manager.GetString("ILLEGAL_CHARACTERS_IN_PATH_EXCEPTION"), paramName)
53+
: new ArgumentException(StringResources.Manager.GetString("ILLEGAL_CHARACTERS_IN_PATH_EXCEPTION"));
54+
55+
public static Exception InvalidUncPath(string paramName) =>
56+
new ArgumentException(@"The UNC path should be of the form \\server\share.", paramName);
57+
}
58+
}

System.IO.Abstractions.TestingHelpers/MockDirectory.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public override DirectorySecurity GetAccessControl(string path)
117117

118118
if (!mockFileDataAccessor.Directory.Exists(path))
119119
{
120-
throw new DirectoryNotFoundException(string.Format(CultureInfo.InvariantCulture, StringResources.Manager.GetString("COULD_NOT_FIND_PART_OF_PATH_EXCEPTION"), path));
120+
throw CommonExceptions.CouldNotFindPartOfPath(path);
121121
}
122122

123123
var directoryData = (MockDirectoryData) mockFileDataAccessor.GetFile(path);
@@ -203,11 +203,7 @@ private string[] GetFilesInternal(
203203

204204
if (!Exists(path))
205205
{
206-
throw new DirectoryNotFoundException(
207-
string.Format(
208-
CultureInfo.InvariantCulture,
209-
StringResources.Manager.GetString("COULD_NOT_FIND_PART_OF_PATH_EXCEPTION"),
210-
path));
206+
throw CommonExceptions.CouldNotFindPartOfPath(path);
211207
}
212208

213209
path = EnsureAbsolutePath(path);
@@ -402,7 +398,7 @@ public override void SetAccessControl(string path, DirectorySecurity directorySe
402398

403399
if (!mockFileDataAccessor.Directory.Exists(path))
404400
{
405-
throw new DirectoryNotFoundException(string.Format(CultureInfo.InvariantCulture, StringResources.Manager.GetString("COULD_NOT_FIND_PART_OF_PATH_EXCEPTION"), path));
401+
throw CommonExceptions.CouldNotFindPartOfPath(path);
406402
}
407403

408404
var directoryData = (MockDirectoryData)mockFileDataAccessor.GetFile(path);
@@ -540,7 +536,7 @@ private void CheckSearchPattern(string searchPattern)
540536
var invalidPathChars = Path.GetInvalidPathChars();
541537
if (searchPattern.IndexOfAny(invalidPathChars) > -1)
542538
{
543-
throw new ArgumentException(StringResources.Manager.GetString("ILLEGAL_CHARACTERS_IN_PATH_EXCEPTION"), "searchPattern");
539+
throw CommonExceptions.IllegalCharactersInPath(nameof(searchPattern));
544540
}
545541
}
546542
}

System.IO.Abstractions.TestingHelpers/MockDirectoryInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ private MockFileData GetMockFileDataForRead()
293293
private MockFileData GetMockFileDataForWrite()
294294
{
295295
return mockFileDataAccessor.GetFile(directoryPath)
296-
?? throw new FileNotFoundException(StringResources.Manager.GetString("COULD_NOT_FIND_FILE_EXCEPTION"), directoryPath);
296+
?? throw CommonExceptions.FileNotFound(directoryPath);
297297
}
298298

299299
public override string ToString()

System.IO.Abstractions.TestingHelpers/MockFile.cs

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -89,20 +89,20 @@ public override void Copy(string sourceFileName, string destFileName, bool overw
8989
{
9090
if (sourceFileName == null)
9191
{
92-
throw new ArgumentNullException(nameof(sourceFileName), StringResources.Manager.GetString("FILENAME_CANNOT_BE_NULL"));
92+
throw CommonExceptions.FilenameCannotBeNull(nameof(sourceFileName));
9393
}
9494

9595
if (destFileName == null)
9696
{
97-
throw new ArgumentNullException(nameof(destFileName), StringResources.Manager.GetString("FILENAME_CANNOT_BE_NULL"));
97+
throw CommonExceptions.FilenameCannotBeNull(nameof(destFileName));
9898
}
9999

100-
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(sourceFileName, "sourceFileName");
101-
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(destFileName, "destFileName");
100+
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(sourceFileName, nameof(sourceFileName));
101+
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(destFileName, nameof(destFileName));
102102

103103
if (!Exists(sourceFileName))
104104
{
105-
throw new FileNotFoundException(string.Format(CultureInfo.InvariantCulture, StringResources.Manager.GetString("COULD_NOT_FIND_FILE_EXCEPTION"), sourceFileName));
105+
throw CommonExceptions.FileNotFound(sourceFileName);
106106
}
107107

108108
VerifyDirectoryExists(destFileName);
@@ -205,7 +205,7 @@ public override FileSecurity GetAccessControl(string path)
205205

206206
if (!mockFileDataAccessor.FileExists(path))
207207
{
208-
throw new FileNotFoundException(string.Format(CultureInfo.InvariantCulture, StringResources.Manager.GetString("COULD_NOT_FIND_FILE_EXCEPTION"), path));
208+
throw CommonExceptions.FileNotFound(path);
209209
}
210210

211211
var fileData = mockFileDataAccessor.GetFile(path);
@@ -233,7 +233,7 @@ public override FileAttributes GetAttributes(string path)
233233
{
234234
if (path != null && path.Length == 0)
235235
{
236-
throw new ArgumentException(StringResources.Manager.GetString("THE_PATH_IS_NOT_OF_A_LEGAL_FORM"), "path");
236+
throw CommonExceptions.PathIsNotOfALegalForm(nameof(path));
237237
}
238238

239239
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path");
@@ -255,7 +255,7 @@ public override FileAttributes GetAttributes(string path)
255255
{
256256
VerifyDirectoryExists(path);
257257

258-
throw new FileNotFoundException(string.Format(CultureInfo.InvariantCulture, "Could not find file '{0}'.", path));
258+
throw CommonExceptions.FileNotFound(path);
259259
}
260260
}
261261

@@ -324,16 +324,16 @@ public override void Move(string sourceFileName, string destFileName)
324324
{
325325
if (sourceFileName == null)
326326
{
327-
throw new ArgumentNullException(nameof(sourceFileName), StringResources.Manager.GetString("FILENAME_CANNOT_BE_NULL"));
327+
throw CommonExceptions.FilenameCannotBeNull(nameof(sourceFileName));
328328
}
329329

330330
if (destFileName == null)
331331
{
332-
throw new ArgumentNullException(nameof(destFileName), StringResources.Manager.GetString("FILENAME_CANNOT_BE_NULL"));
332+
throw CommonExceptions.FilenameCannotBeNull(nameof(destFileName));
333333
}
334334

335-
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(sourceFileName, "sourceFileName");
336-
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(destFileName, "destFileName");
335+
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(sourceFileName, nameof(sourceFileName));
336+
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(destFileName, nameof(destFileName));
337337

338338
if (mockFileDataAccessor.GetFile(destFileName) != null)
339339
{
@@ -351,7 +351,9 @@ public override void Move(string sourceFileName, string destFileName)
351351
var sourceFile = mockFileDataAccessor.GetFile(sourceFileName);
352352

353353
if (sourceFile == null)
354-
throw new FileNotFoundException(string.Format(CultureInfo.InvariantCulture, "The file \"{0}\" could not be found.", sourceFileName), sourceFileName);
354+
{
355+
throw CommonExceptions.FileNotFound(sourceFileName);
356+
}
355357

356358
VerifyDirectoryExists(destFileName);
357359

@@ -391,7 +393,7 @@ private Stream OpenInternal(
391393
throw new IOException(string.Format(CultureInfo.InvariantCulture, "The file '{0}' already exists.", path));
392394

393395
if ((mode == FileMode.Open || mode == FileMode.Truncate) && !exists)
394-
throw new FileNotFoundException(path);
396+
throw CommonExceptions.FileNotFound(path);
395397

396398
if (!exists || mode == FileMode.CreateNew)
397399
return Create(path);
@@ -442,7 +444,7 @@ public override byte[] ReadAllBytes(string path)
442444

443445
if (!mockFileDataAccessor.FileExists(path))
444446
{
445-
throw new FileNotFoundException(string.Format(CultureInfo.InvariantCulture, StringResources.Manager.GetString("COULD_NOT_FIND_FILE_EXCEPTION"), path));
447+
throw CommonExceptions.FileNotFound(path);
446448
}
447449

448450
return mockFileDataAccessor.GetFile(path).Contents;
@@ -454,7 +456,7 @@ public override string[] ReadAllLines(string path)
454456

455457
if (!mockFileDataAccessor.FileExists(path))
456458
{
457-
throw new FileNotFoundException(string.Format(CultureInfo.InvariantCulture, StringResources.Manager.GetString("COULD_NOT_FIND_FILE_EXCEPTION"), path));
459+
throw CommonExceptions.FileNotFound(path);
458460
}
459461

460462
return mockFileDataAccessor
@@ -474,7 +476,7 @@ public override string[] ReadAllLines(string path, Encoding encoding)
474476

475477
if (!mockFileDataAccessor.FileExists(path))
476478
{
477-
throw new FileNotFoundException(string.Format(CultureInfo.InvariantCulture, "Can't find {0}", path));
479+
throw CommonExceptions.FileNotFound(path);
478480
}
479481

480482
return encoding
@@ -488,7 +490,7 @@ public override string ReadAllText(string path)
488490

489491
if (!mockFileDataAccessor.FileExists(path))
490492
{
491-
throw new FileNotFoundException(string.Format(CultureInfo.InvariantCulture, "Can't find {0}", path));
493+
throw CommonExceptions.FileNotFound(path);
492494
}
493495

494496
return ReadAllText(path, MockFileData.DefaultEncoding);
@@ -541,12 +543,12 @@ public override void Replace(string sourceFileName, string destinationFileName,
541543

542544
if (!mockFileDataAccessor.FileExists(sourceFileName))
543545
{
544-
throw new FileNotFoundException(string.Format(CultureInfo.InvariantCulture, StringResources.Manager.GetString("COULD_NOT_FIND_FILE_EXCEPTION"), sourceFileName));
546+
throw CommonExceptions.FileNotFound(sourceFileName);
545547
}
546548

547549
if (!mockFileDataAccessor.FileExists(destinationFileName))
548550
{
549-
throw new FileNotFoundException(string.Format(CultureInfo.InvariantCulture, StringResources.Manager.GetString("COULD_NOT_FIND_FILE_EXCEPTION"), destinationFileName));
551+
throw CommonExceptions.FileNotFound(destinationFileName);
550552
}
551553

552554
if (destinationBackupFileName != null)
@@ -565,7 +567,7 @@ public override void SetAccessControl(string path, FileSecurity fileSecurity)
565567

566568
if (!mockFileDataAccessor.FileExists(path))
567569
{
568-
throw new FileNotFoundException(string.Format(CultureInfo.InvariantCulture, "Can't find {0}", path), path);
570+
throw CommonExceptions.FileNotFound(path);
569571
}
570572

571573
var fileData = mockFileDataAccessor.GetFile(path);
@@ -586,7 +588,7 @@ public override void SetAttributes(string path, FileAttributes fileAttributes)
586588
}
587589
else
588590
{
589-
throw new FileNotFoundException(string.Format(CultureInfo.InvariantCulture, StringResources.Manager.GetString("COULD_NOT_FIND_FILE_EXCEPTION"), path), path);
591+
throw CommonExceptions.FileNotFound(path);
590592
}
591593
}
592594
else
@@ -676,7 +678,7 @@ public override void WriteAllBytes(string path, byte[] bytes)
676678
mockFileDataAccessor.AddFile(path, new MockFileData(bytes));
677679
}
678680

679-
/// <summary>
681+
/// <summary>
680682
/// Creates a new file, writes a collection of strings to the file, and then closes the file.
681683
/// </summary>
682684
/// <param name="path">The file to write to.</param>
@@ -937,7 +939,7 @@ public override void WriteAllText(string path, string contents, Encoding encodin
937939

938940
if (mockFileDataAccessor.Directory.Exists(path))
939941
{
940-
throw new UnauthorizedAccessException(string.Format(CultureInfo.InvariantCulture, StringResources.Manager.GetString("ACCESS_TO_THE_PATH_IS_DENIED"), path));
942+
throw CommonExceptions.AccessDenied(path);
941943
}
942944

943945
VerifyDirectoryExists(path);
@@ -976,11 +978,7 @@ private void VerifyDirectoryExists(string path)
976978

977979
if (!mockFileDataAccessor.Directory.Exists(dir))
978980
{
979-
throw new DirectoryNotFoundException(
980-
string.Format(
981-
CultureInfo.InvariantCulture,
982-
StringResources.Manager.GetString("COULD_NOT_FIND_PART_OF_PATH_EXCEPTION"),
983-
path));
981+
throw CommonExceptions.CouldNotFindPartOfPath(path);
984982
}
985983
}
986984
}

0 commit comments

Comments
 (0)