Skip to content

Commit a14979e

Browse files
authored
Reject non-rooted current directory (#607)
Fixes #592
1 parent 9d6da3a commit a14979e

3 files changed

Lines changed: 21 additions & 8 deletions

File tree

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,21 +334,30 @@ public void MockFileSystem_GetFiles_ThrowsArgumentExceptionForInvalidCharacters(
334334
[TestCase(@"C:\somepath")]
335335
public void MockFileSystem_DefaultState_CurrentDirectoryExists(string currentDirectory)
336336
{
337-
var fs = new MockFileSystem(null, currentDirectory);
337+
var fs = new MockFileSystem(null, XFS.Path(currentDirectory));
338338

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

341341
Assert.IsTrue(actualCurrentDirectory.Exists);
342342
}
343343

344+
[Test]
345+
public void MockFileSystem_Constructor_ThrowsForNonRootedCurrentDirectory()
346+
{
347+
var ae = Assert.Throws<ArgumentException>(() =>
348+
new MockFileSystem(null, "non-rooted")
349+
);
350+
Assert.AreEqual("currentDirectory", ae.ParamName);
351+
}
352+
344353
[Test]
345354
public void MockFileSystem_DefaultState_DefaultTempDirectoryExists()
346355
{
347356
var tempDirectory = XFS.Path(@"C:\temp");
348357

349358
var mockFileSystem = new MockFileSystem();
350359
var mockFileSystemOverload = new MockFileSystem(null, string.Empty);
351-
360+
352361
Assert.IsTrue(mockFileSystem.Directory.Exists(tempDirectory));
353362
Assert.IsTrue(mockFileSystemOverload.Directory.Exists(tempDirectory));
354363
}

System.IO.Abstractions.TestingHelpers/MockFileSystem.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,18 @@ public MockFileSystem(IDictionary<string, MockFileData> files, string currentDir
2424
{
2525
currentDirectory = XFS.Path(DEFAULT_CURRENT_DIRECTORY);
2626
}
27+
else if (!System.IO.Path.IsPathRooted(currentDirectory))
28+
{
29+
throw new ArgumentException("Current directory needs to be rooted.", nameof(currentDirectory));
30+
}
2731

2832
var defaultTempDirectory = XFS.Path(TEMP_DIRECTORY);
2933

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

34-
Path = new MockPath(this,defaultTempDirectory);
38+
Path = new MockPath(this, defaultTempDirectory);
3539
File = new MockFile(this);
3640
Directory = new MockDirectory(this, currentDirectory);
3741
FileInfo = new MockFileInfoFactory(this);
@@ -53,7 +57,7 @@ public MockFileSystem(IDictionary<string, MockFileData> files, string currentDir
5357
AddDirectory(currentDirectory);
5458
}
5559

56-
if (!FileExists(defaultTempDirectory))
60+
if (!FileExists(defaultTempDirectory))
5761
{
5862
AddDirectory(defaultTempDirectory);
5963
}
@@ -77,7 +81,7 @@ private string FixPath(string path, bool checkCaps = false)
7781
{
7882
throw new ArgumentNullException(nameof(path), StringResources.Manager.GetString("VALUE_CANNOT_BE_NULL"));
7983
}
80-
84+
8185
var pathSeparatorFixed = path.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
8286
var fullPath = Path.GetFullPath(pathSeparatorFixed);
8387

@@ -159,7 +163,7 @@ public void AddDirectory(string path)
159163
{
160164
if (FileExists(fixedPath) &&
161165
(GetFile(fixedPath).Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
162-
throw CommonExceptions.AccessDenied(fixedPath);
166+
throw CommonExceptions.AccessDenied(fixedPath);
163167

164168
var lastIndex = 0;
165169
var isUnc =
@@ -238,7 +242,7 @@ public void MoveDirectory(string sourcePath, string destPath)
238242
.Where(p => StringOperations.StartsWith(p, sourcePath))
239243
.ToList();
240244

241-
foreach(var path in affectedPaths)
245+
foreach (var path in affectedPaths)
242246
{
243247
var newPath = StringOperations.Replace(path, sourcePath, destPath);
244248
files[newPath] = files[path];

System.IO.Abstractions.TestingHelpers/MockUnixSupport.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public static class MockUnixSupport
66
{
77
private static readonly Regex pathTransform = new Regex(@"^[a-zA-Z]:(?<path>.*)$");
88

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

0 commit comments

Comments
 (0)