Skip to content

Commit ebab1d9

Browse files
authored
Merge pull request #436 from System-IO-Abstractions/master
- Do not throw exception when calling DirectoryInfoWrapper.Parent for the root directory (#430) by @wexman - Fix MockDirectoryInfo GetFiles for UNC paths caused by bug in StringExtensions.NormalizeSlashes (#422) by @DeveloperGuo - Pass IFileSystem into FileWrapper instead of FileSystem to allow replacement file systems to be used (#432) by @kirbatious - Make sure FileNotFound exceptions contain path and proper message (#427) by @fgreinacher - Do not delete directories that start with the same path (#433) by @updateaman
2 parents 5366dcf + a2062c9 commit ebab1d9

22 files changed

+269
-105
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using NUnit.Framework;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
6+
namespace System.IO.Abstractions.TestingHelpers.Tests
7+
{
8+
[TestFixture]
9+
public class DirectoryInfoTests
10+
{
11+
[Test]
12+
public void Parent_ForRootDirectory_ShouldReturnNull()
13+
{
14+
var wrapperFilesystem = new FileSystem();
15+
16+
var current = wrapperFilesystem.Directory.GetCurrentDirectory();
17+
var root = wrapperFilesystem.DirectoryInfo.FromDirectoryName(current).Root;
18+
var rootsParent = root.Parent;
19+
Assert.IsNull(rootsParent);
20+
}
21+
}
22+
}

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,29 @@ public void MockDirectoryInfo_Exists(string path, bool expected)
5555
Assert.That(result, Is.EqualTo(expected));
5656
}
5757

58+
[Test]
59+
[WindowsOnly(WindowsSpecifics.UNCPaths)]
60+
public void MockDirectoryInfo_GetFiles_ShouldWorkWithUNCPath()
61+
{
62+
var fileName = XFS.Path(@"\\unc\folder\file.txt");
63+
var directoryName = XFS.Path(@"\\unc\folder");
64+
// Arrange
65+
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
66+
{
67+
{fileName, ""}
68+
});
69+
70+
var directoryInfo = new MockDirectoryInfo(fileSystem, directoryName);
71+
72+
// Act
73+
var files = directoryInfo.GetFiles();
74+
75+
// Assert
76+
Assert.AreEqual(fileName, files[0].FullName);
77+
}
78+
79+
80+
5881
[Test]
5982
public void MockDirectoryInfo_FullName_ShouldReturnFullNameWithoutIncludingTrailingPathDelimiter()
6083
{

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

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ public void MockDirectory_GetFiles_ShouldFilterByExtensionBasedSearchPatternWith
182182
var result = fileSystem.Directory.GetFiles(XFS.Path(@"c:\"), "*.gif", SearchOption.AllDirectories);
183183

184184
// Assert
185-
Assert.That(result, Is.EquivalentTo( expected));
185+
Assert.That(result, Is.EquivalentTo(expected));
186186
}
187187

188188
[Test]
@@ -598,6 +598,29 @@ public void MockDirectory_Delete_ShouldDeleteDirectory()
598598
Assert.IsFalse(fileSystem.Directory.Exists(XFS.Path(@"c:\bar")));
599599
}
600600

601+
[Test]
602+
public void MockDirectory_Delete_ShouldNotDeleteAllDirectories()
603+
{
604+
// Arrange
605+
var folder1Path = XFS.Path(@"D:\Test\Program");
606+
var folder1SubFolderPath = XFS.Path(@"D:\Test\Program\Subfolder");
607+
var folder2Path = XFS.Path(@"D:\Test\Program_bak");
608+
609+
var fileSystem = new MockFileSystem();
610+
611+
fileSystem.AddDirectory(folder1Path);
612+
fileSystem.AddDirectory(folder2Path);
613+
fileSystem.AddDirectory(folder1SubFolderPath);
614+
615+
// Act
616+
fileSystem.Directory.Delete(folder1Path, recursive: true);
617+
618+
// Assert
619+
Assert.IsFalse(fileSystem.Directory.Exists(folder1Path));
620+
Assert.IsFalse(fileSystem.Directory.Exists(folder1SubFolderPath));
621+
Assert.IsTrue(fileSystem.Directory.Exists(folder2Path));
622+
}
623+
601624
[Test]
602625
[WindowsOnly(WindowsSpecifics.CaseInsensitivity)]
603626
public void MockDirectory_Delete_ShouldDeleteDirectoryCaseInsensitively()
@@ -700,7 +723,7 @@ public void MockDirectory_Delete_ShouldDeleteDirectoryRecursively()
700723
public void MockDirectory_GetFileSystemEntries_Returns_Files_And_Directories()
701724
{
702725
string testPath = XFS.Path(@"c:\foo\bar.txt");
703-
string testDir = XFS.Path(@"c:\foo\bar");
726+
string testDir = XFS.Path(@"c:\foo\bar");
704727
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
705728
{
706729
{ testPath, new MockFileData("Demo text content") },
@@ -829,7 +852,7 @@ public void MockDirectory_GetFiles_ShouldFindFilesContainingTwoOrMoreDots()
829852
var actualResult = fileSystem.Directory.GetFiles(XFS.Path(@"c:\"), XFS.Path(@"foo..r\*"));
830853

831854
// Assert
832-
Assert.That(actualResult, Is.EquivalentTo(new [] { testPath }));
855+
Assert.That(actualResult, Is.EquivalentTo(new[] { testPath }));
833856
}
834857

835858
#if NET40
@@ -925,7 +948,7 @@ public void MockDirectory_GetDirectories_WithTopDirectories_ShouldOnlyReturnTopD
925948
var actualResult = fileSystem.Directory.GetDirectories(XFS.Path(@"c:\Folder\"), "*.foo");
926949

927950
// Assert
928-
Assert.That(actualResult, Is.EquivalentTo(new []{XFS.Path(@"C:\Folder\.foo"), XFS.Path(@"C:\Folder\foo.foo")}));
951+
Assert.That(actualResult, Is.EquivalentTo(new[] { XFS.Path(@"C:\Folder\.foo"), XFS.Path(@"C:\Folder\foo.foo") }));
929952
}
930953

931954
[Test]
@@ -1211,17 +1234,18 @@ public void MockDirectory_Move_ShouldMoveDirectoryWithReadOnlySubDirectory()
12111234
}
12121235

12131236
[Test]
1214-
public void MockDirectory_GetCurrentDirectory_ShouldReturnValueFromFileSystemConstructor() {
1237+
public void MockDirectory_GetCurrentDirectory_ShouldReturnValueFromFileSystemConstructor()
1238+
{
12151239
string directory = XFS.Path(@"D:\folder1\folder2");
12161240
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>(), directory);
12171241

12181242
var actual = fileSystem.Directory.GetCurrentDirectory();
12191243

12201244
Assert.AreEqual(directory, actual);
12211245
}
1222-
1246+
12231247
[Test]
1224-
public void MockDirectory_GetCurrentDirectory_ShouldReturnDefaultPathWhenNotSet()
1248+
public void MockDirectory_GetCurrentDirectory_ShouldReturnDefaultPathWhenNotSet()
12251249
{
12261250
string directory = XFS.Path(@"C:\");
12271251

@@ -1233,7 +1257,8 @@ public void MockDirectory_GetCurrentDirectory_ShouldReturnDefaultPathWhenNotSet(
12331257
}
12341258

12351259
[Test]
1236-
public void MockDirectory_SetCurrentDirectory_ShouldChangeCurrentDirectory() {
1260+
public void MockDirectory_SetCurrentDirectory_ShouldChangeCurrentDirectory()
1261+
{
12371262
string directory = XFS.Path(@"D:\folder1\folder2");
12381263
var fileSystem = new MockFileSystem();
12391264

@@ -1278,7 +1303,7 @@ public void MockDirectory_GetParent_ShouldReturnADirectoryInfoIfPathDoesNotExist
12781303
var fileSystem = new MockFileSystem();
12791304

12801305
// Act
1281-
var actualResult = fileSystem.Directory.GetParent(XFS.Path(@"c:\directory\does\not\exist"));
1306+
var actualResult = fileSystem.Directory.GetParent(XFS.Path(@"c:\directory\does\not\exist"));
12821307

12831308
// Assert
12841309
Assert.IsNotNull(actualResult);
@@ -1331,9 +1356,9 @@ public static IEnumerable<string[]> MockDirectory_GetParent_Cases
13311356
{
13321357
get
13331358
{
1334-
yield return new [] { XFS.Path(@"c:\a"), XFS.Path(@"c:\") };
1335-
yield return new [] { XFS.Path(@"c:\a\b\c\d"), XFS.Path(@"c:\a\b\c") };
1336-
yield return new [] { XFS.Path(@"c:\a\b\c\d\"), XFS.Path(@"c:\a\b\c") };
1359+
yield return new[] { XFS.Path(@"c:\a"), XFS.Path(@"c:\") };
1360+
yield return new[] { XFS.Path(@"c:\a\b\c\d"), XFS.Path(@"c:\a\b\c") };
1361+
yield return new[] { XFS.Path(@"c:\a\b\c\d\"), XFS.Path(@"c:\a\b\c") };
13371362
}
13381363
}
13391364

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
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,5 +112,13 @@ public void TrimSlashes_SlashRoot_PreserveSlashRoot()
112112
{
113113
Assert.AreEqual("/", "/".TrimSlashes());
114114
}
115+
116+
[TestCase(@"\\unc\folder\file.txt", @"\\unc\folder\file.txt")]
117+
[TestCase(@"//unc/folder/file.txt", @"\\unc\folder\file.txt")]
118+
[WindowsOnly(WindowsSpecifics.UNCPaths)]
119+
public void NormalizeSlashes_KeepsUNCPathPrefix(string path, string expectedValue)
120+
{
121+
Assert.AreEqual(expectedValue, path.NormalizeSlashes());
122+
}
115123
}
116124
}

System.IO.Abstractions.TestingHelpers.Tests/System.IO.Abstractions.TestingHelpers.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747

4848
<ItemGroup>
4949
<PackageReference Include="nunit" Version="3.11.0" />
50-
<PackageReference Include="NUnit3TestAdapter" Version="3.11.2" />
50+
<PackageReference Include="NUnit3TestAdapter" Version="3.12.0" />
5151
</ItemGroup>
5252

5353
<ItemGroup>
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+
}

0 commit comments

Comments
 (0)