Skip to content

Commit b84a1e1

Browse files
WeiermannWeiermann
authored andcommitted
Merge remote-tracking branch 'original/master'
2 parents 1375888 + d8be8f2 commit b84a1e1

20 files changed

Lines changed: 264 additions & 104 deletions

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: 58 additions & 13 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]
@@ -1141,7 +1164,7 @@ public void Move_DirectoryExistsWithDifferentCase_DirectorySuccessfullyMoved()
11411164
}
11421165

11431166
[TestCaseSource("GetPathsForMoving")]
1144-
public void MockDirectory_Move_ShouldMove(string sourceDirName, string destDirName, string filePathOne, string filePathTwo)
1167+
public void MockDirectory_Move_ShouldMoveDirectories(string sourceDirName, string destDirName, string filePathOne, string filePathTwo)
11451168
{
11461169
// Arrange
11471170
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
@@ -1159,6 +1182,26 @@ public void MockDirectory_Move_ShouldMove(string sourceDirName, string destDirNa
11591182
Assert.IsTrue(fileSystem.File.Exists(XFS.Path(destDirName + filePathTwo)));
11601183
}
11611184

1185+
[Test]
1186+
public void MockDirectory_Move_ShouldMoveFiles()
1187+
{
1188+
string sourceFilePath = XFS.Path(@"c:\demo.txt");
1189+
string sourceFileContent = "this is some content";
1190+
1191+
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
1192+
{
1193+
{ sourceFilePath, new MockFileData(sourceFileContent) }
1194+
});
1195+
1196+
string destFilePath = XFS.Path(@"c:\demo1.txt");
1197+
1198+
fileSystem.Directory.Move(sourceFilePath, destFilePath);
1199+
1200+
Assert.That(fileSystem.FileExists(destFilePath), Is.True);
1201+
Assert.That(fileSystem.FileExists(sourceFilePath), Is.False);
1202+
Assert.That(fileSystem.GetFile(destFilePath).TextContents, Is.EqualTo(sourceFileContent));
1203+
}
1204+
11621205
[Test]
11631206
public void MockDirectory_Move_ShouldMoveDirectoryAtrributes()
11641207
{
@@ -1211,17 +1254,18 @@ public void MockDirectory_Move_ShouldMoveDirectoryWithReadOnlySubDirectory()
12111254
}
12121255

12131256
[Test]
1214-
public void MockDirectory_GetCurrentDirectory_ShouldReturnValueFromFileSystemConstructor() {
1257+
public void MockDirectory_GetCurrentDirectory_ShouldReturnValueFromFileSystemConstructor()
1258+
{
12151259
string directory = XFS.Path(@"D:\folder1\folder2");
12161260
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>(), directory);
12171261

12181262
var actual = fileSystem.Directory.GetCurrentDirectory();
12191263

12201264
Assert.AreEqual(directory, actual);
12211265
}
1222-
1266+
12231267
[Test]
1224-
public void MockDirectory_GetCurrentDirectory_ShouldReturnDefaultPathWhenNotSet()
1268+
public void MockDirectory_GetCurrentDirectory_ShouldReturnDefaultPathWhenNotSet()
12251269
{
12261270
string directory = XFS.Path(@"C:\");
12271271

@@ -1233,7 +1277,8 @@ public void MockDirectory_GetCurrentDirectory_ShouldReturnDefaultPathWhenNotSet(
12331277
}
12341278

12351279
[Test]
1236-
public void MockDirectory_SetCurrentDirectory_ShouldChangeCurrentDirectory() {
1280+
public void MockDirectory_SetCurrentDirectory_ShouldChangeCurrentDirectory()
1281+
{
12371282
string directory = XFS.Path(@"D:\folder1\folder2");
12381283
var fileSystem = new MockFileSystem();
12391284

@@ -1278,7 +1323,7 @@ public void MockDirectory_GetParent_ShouldReturnADirectoryInfoIfPathDoesNotExist
12781323
var fileSystem = new MockFileSystem();
12791324

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

12831328
// Assert
12841329
Assert.IsNotNull(actualResult);
@@ -1331,9 +1376,9 @@ public static IEnumerable<string[]> MockDirectory_GetParent_Cases
13311376
{
13321377
get
13331378
{
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") };
1379+
yield return new[] { XFS.Path(@"c:\a"), XFS.Path(@"c:\") };
1380+
yield return new[] { XFS.Path(@"c:\a\b\c\d"), XFS.Path(@"c:\a\b\c") };
1381+
yield return new[] { XFS.Path(@"c:\a\b\c\d\"), XFS.Path(@"c:\a\b\c") };
13371382
}
13381383
}
13391384

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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@
4141
</ItemGroup>
4242

4343
<ItemGroup Condition="'$(TargetFramework)' != 'net40'">
44-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
44+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.0.1" />
4545
<PackageReference Include="Moq" Version="4.10.1" />
4646
</ItemGroup>
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.13.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)