Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions System.IO.Abstractions.TestingHelpers.Tests/MockFileSystemTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -334,21 +334,30 @@ public void MockFileSystem_GetFiles_ThrowsArgumentExceptionForInvalidCharacters(
[TestCase(@"C:\somepath")]
public void MockFileSystem_DefaultState_CurrentDirectoryExists(string currentDirectory)
{
var fs = new MockFileSystem(null, currentDirectory);
var fs = new MockFileSystem(null, XFS.Path(currentDirectory));

var actualCurrentDirectory = fs.DirectoryInfo.FromDirectoryName(".");

Assert.IsTrue(actualCurrentDirectory.Exists);
}

[Test]
public void MockFileSystem_Constructor_ThrowsForNonRootedCurrentDirectory()
{
var ae = Assert.Throws<ArgumentException>(() =>
new MockFileSystem(null, "non-rooted")
);
Assert.AreEqual("currentDirectory", ae.ParamName);
}

[Test]
public void MockFileSystem_DefaultState_DefaultTempDirectoryExists()
{
var tempDirectory = XFS.Path(@"C:\temp");

var mockFileSystem = new MockFileSystem();
var mockFileSystemOverload = new MockFileSystem(null, string.Empty);

Assert.IsTrue(mockFileSystem.Directory.Exists(tempDirectory));
Assert.IsTrue(mockFileSystemOverload.Directory.Exists(tempDirectory));
}
Expand Down
14 changes: 9 additions & 5 deletions System.IO.Abstractions.TestingHelpers/MockFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,18 @@ public MockFileSystem(IDictionary<string, MockFileData> files, string currentDir
{
currentDirectory = XFS.Path(DEFAULT_CURRENT_DIRECTORY);
}
else if (!System.IO.Path.IsPathRooted(currentDirectory))
{
throw new ArgumentException("Current directory needs to be rooted.", nameof(currentDirectory));
}

var defaultTempDirectory = XFS.Path(TEMP_DIRECTORY);

StringOperations = new StringOperations(XFS.IsUnixPlatform());
pathVerifier = new PathVerifier(this);
this.files = new Dictionary<string, MockFileData>(StringOperations.Comparer);

Path = new MockPath(this,defaultTempDirectory);
Path = new MockPath(this, defaultTempDirectory);
File = new MockFile(this);
Directory = new MockDirectory(this, currentDirectory);
FileInfo = new MockFileInfoFactory(this);
Expand All @@ -53,7 +57,7 @@ public MockFileSystem(IDictionary<string, MockFileData> files, string currentDir
AddDirectory(currentDirectory);
}

if (!FileExists(defaultTempDirectory))
if (!FileExists(defaultTempDirectory))
{
AddDirectory(defaultTempDirectory);
}
Expand All @@ -77,7 +81,7 @@ private string FixPath(string path, bool checkCaps = false)
{
throw new ArgumentNullException(nameof(path), StringResources.Manager.GetString("VALUE_CANNOT_BE_NULL"));
}

var pathSeparatorFixed = path.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
var fullPath = Path.GetFullPath(pathSeparatorFixed);

Expand Down Expand Up @@ -159,7 +163,7 @@ public void AddDirectory(string path)
{
if (FileExists(fixedPath) &&
(GetFile(fixedPath).Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
throw CommonExceptions.AccessDenied(fixedPath);
throw CommonExceptions.AccessDenied(fixedPath);

var lastIndex = 0;
var isUnc =
Expand Down Expand Up @@ -238,7 +242,7 @@ public void MoveDirectory(string sourcePath, string destPath)
.Where(p => StringOperations.StartsWith(p, sourcePath))
.ToList();

foreach(var path in affectedPaths)
foreach (var path in affectedPaths)
{
var newPath = StringOperations.Replace(path, sourcePath, destPath);
files[newPath] = files[path];
Expand Down
2 changes: 1 addition & 1 deletion System.IO.Abstractions.TestingHelpers/MockUnixSupport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public static class MockUnixSupport
{
private static readonly Regex pathTransform = new Regex(@"^[a-zA-Z]:(?<path>.*)$");

public static string Path(string path) => IsUnixPlatform()
public static string Path(string path) => path != null && IsUnixPlatform()
? pathTransform.Replace(path, "${path}").Replace(@"\", "/")
: path;

Expand Down