Skip to content
22 changes: 22 additions & 0 deletions System.IO.Abstractions.TestingHelpers.Tests/DirectoryInfoTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Text;

namespace System.IO.Abstractions.TestingHelpers.Tests
{
[TestFixture]
public class DirectoryInfoTests
{
[Test]
public void Parent_ForRootDirectory_ShouldReturnNull()
{
var wrapperFilesystem = new FileSystem();

var current = wrapperFilesystem.Directory.GetCurrentDirectory();
var root = wrapperFilesystem.DirectoryInfo.FromDirectoryName(current).Root;
var rootsParent = root.Parent;
Assert.IsNull(rootsParent);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,29 @@ public void MockDirectoryInfo_Exists(string path, bool expected)
Assert.That(result, Is.EqualTo(expected));
}

[Test]
[WindowsOnly(WindowsSpecifics.UNCPaths)]
public void MockDirectoryInfo_GetFiles_ShouldWorkWithUNCPath()
{
var fileName = XFS.Path(@"\\unc\folder\file.txt");
var directoryName = XFS.Path(@"\\unc\folder");
// Arrange
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{fileName, ""}
});

var directoryInfo = new MockDirectoryInfo(fileSystem, directoryName);

// Act
var files = directoryInfo.GetFiles();

// Assert
Assert.AreEqual(fileName, files[0].FullName);
}



[Test]
public void MockDirectoryInfo_FullName_ShouldReturnFullNameWithoutIncludingTrailingPathDelimiter()
{
Expand Down
49 changes: 37 additions & 12 deletions System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public void MockDirectory_GetFiles_ShouldFilterByExtensionBasedSearchPatternWith
var result = fileSystem.Directory.GetFiles(XFS.Path(@"c:\"), "*.gif", SearchOption.AllDirectories);

// Assert
Assert.That(result, Is.EquivalentTo( expected));
Assert.That(result, Is.EquivalentTo(expected));
}

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

[Test]
public void MockDirectory_Delete_ShouldNotDeleteAllDirectories()
{
// Arrange
var folder1Path = XFS.Path(@"D:\Test\Program");
var folder1SubFolderPath = XFS.Path(@"D:\Test\Program\Subfolder");
var folder2Path = XFS.Path(@"D:\Test\Program_bak");

var fileSystem = new MockFileSystem();

fileSystem.AddDirectory(folder1Path);
fileSystem.AddDirectory(folder2Path);
fileSystem.AddDirectory(folder1SubFolderPath);

// Act
fileSystem.Directory.Delete(folder1Path, recursive: true);

// Assert
Assert.IsFalse(fileSystem.Directory.Exists(folder1Path));
Assert.IsFalse(fileSystem.Directory.Exists(folder1SubFolderPath));
Assert.IsTrue(fileSystem.Directory.Exists(folder2Path));
}

[Test]
[WindowsOnly(WindowsSpecifics.CaseInsensitivity)]
public void MockDirectory_Delete_ShouldDeleteDirectoryCaseInsensitively()
Expand Down Expand Up @@ -700,7 +723,7 @@ public void MockDirectory_Delete_ShouldDeleteDirectoryRecursively()
public void MockDirectory_GetFileSystemEntries_Returns_Files_And_Directories()
{
string testPath = XFS.Path(@"c:\foo\bar.txt");
string testDir = XFS.Path(@"c:\foo\bar");
string testDir = XFS.Path(@"c:\foo\bar");
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ testPath, new MockFileData("Demo text content") },
Expand Down Expand Up @@ -829,7 +852,7 @@ public void MockDirectory_GetFiles_ShouldFindFilesContainingTwoOrMoreDots()
var actualResult = fileSystem.Directory.GetFiles(XFS.Path(@"c:\"), XFS.Path(@"foo..r\*"));

// Assert
Assert.That(actualResult, Is.EquivalentTo(new [] { testPath }));
Assert.That(actualResult, Is.EquivalentTo(new[] { testPath }));
}

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

// Assert
Assert.That(actualResult, Is.EquivalentTo(new []{XFS.Path(@"C:\Folder\.foo"), XFS.Path(@"C:\Folder\foo.foo")}));
Assert.That(actualResult, Is.EquivalentTo(new[] { XFS.Path(@"C:\Folder\.foo"), XFS.Path(@"C:\Folder\foo.foo") }));
}

[Test]
Expand Down Expand Up @@ -1211,17 +1234,18 @@ public void MockDirectory_Move_ShouldMoveDirectoryWithReadOnlySubDirectory()
}

[Test]
public void MockDirectory_GetCurrentDirectory_ShouldReturnValueFromFileSystemConstructor() {
public void MockDirectory_GetCurrentDirectory_ShouldReturnValueFromFileSystemConstructor()
{
string directory = XFS.Path(@"D:\folder1\folder2");
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>(), directory);

var actual = fileSystem.Directory.GetCurrentDirectory();

Assert.AreEqual(directory, actual);
}

[Test]
public void MockDirectory_GetCurrentDirectory_ShouldReturnDefaultPathWhenNotSet()
public void MockDirectory_GetCurrentDirectory_ShouldReturnDefaultPathWhenNotSet()
{
string directory = XFS.Path(@"C:\");

Expand All @@ -1233,7 +1257,8 @@ public void MockDirectory_GetCurrentDirectory_ShouldReturnDefaultPathWhenNotSet(
}

[Test]
public void MockDirectory_SetCurrentDirectory_ShouldChangeCurrentDirectory() {
public void MockDirectory_SetCurrentDirectory_ShouldChangeCurrentDirectory()
{
string directory = XFS.Path(@"D:\folder1\folder2");
var fileSystem = new MockFileSystem();

Expand Down Expand Up @@ -1278,7 +1303,7 @@ public void MockDirectory_GetParent_ShouldReturnADirectoryInfoIfPathDoesNotExist
var fileSystem = new MockFileSystem();

// Act
var actualResult = fileSystem.Directory.GetParent(XFS.Path(@"c:\directory\does\not\exist"));
var actualResult = fileSystem.Directory.GetParent(XFS.Path(@"c:\directory\does\not\exist"));

// Assert
Assert.IsNotNull(actualResult);
Expand Down Expand Up @@ -1331,9 +1356,9 @@ public static IEnumerable<string[]> MockDirectory_GetParent_Cases
{
get
{
yield return new [] { XFS.Path(@"c:\a"), XFS.Path(@"c:\") };
yield return new [] { XFS.Path(@"c:\a\b\c\d"), XFS.Path(@"c:\a\b\c") };
yield return new [] { XFS.Path(@"c:\a\b\c\d\"), XFS.Path(@"c:\a\b\c") };
yield return new[] { XFS.Path(@"c:\a"), XFS.Path(@"c:\") };
yield return new[] { XFS.Path(@"c:\a\b\c\d"), XFS.Path(@"c:\a\b\c") };
yield return new[] { XFS.Path(@"c:\a\b\c\d\"), XFS.Path(@"c:\a\b\c") };
}
}

Expand Down
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
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,13 @@ public void TrimSlashes_SlashRoot_PreserveSlashRoot()
{
Assert.AreEqual("/", "/".TrimSlashes());
}

[TestCase(@"\\unc\folder\file.txt", @"\\unc\folder\file.txt")]
[TestCase(@"//unc/folder/file.txt", @"\\unc\folder\file.txt")]
[WindowsOnly(WindowsSpecifics.UNCPaths)]
public void NormalizeSlashes_KeepsUNCPathPrefix(string path, string expectedValue)
{
Assert.AreEqual(expectedValue, path.NormalizeSlashes());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@

<ItemGroup>
<PackageReference Include="nunit" Version="3.11.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.11.2" />
<PackageReference Include="NUnit3TestAdapter" Version="3.12.0" />
</ItemGroup>

<ItemGroup>
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);
}
}
Loading