diff --git a/System.IO.Abstractions.TestingHelpers.Tests/MockFileSystemTests.cs b/System.IO.Abstractions.TestingHelpers.Tests/MockFileSystemTests.cs index fec774718..75575da70 100644 --- a/System.IO.Abstractions.TestingHelpers.Tests/MockFileSystemTests.cs +++ b/System.IO.Abstractions.TestingHelpers.Tests/MockFileSystemTests.cs @@ -334,13 +334,22 @@ 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(() => + new MockFileSystem(null, "non-rooted") + ); + Assert.AreEqual("currentDirectory", ae.ParamName); + } + [Test] public void MockFileSystem_DefaultState_DefaultTempDirectoryExists() { @@ -348,7 +357,7 @@ public void MockFileSystem_DefaultState_DefaultTempDirectoryExists() var mockFileSystem = new MockFileSystem(); var mockFileSystemOverload = new MockFileSystem(null, string.Empty); - + Assert.IsTrue(mockFileSystem.Directory.Exists(tempDirectory)); Assert.IsTrue(mockFileSystemOverload.Directory.Exists(tempDirectory)); } diff --git a/System.IO.Abstractions.TestingHelpers/MockFileSystem.cs b/System.IO.Abstractions.TestingHelpers/MockFileSystem.cs index d87331bcf..d70b6a7cd 100644 --- a/System.IO.Abstractions.TestingHelpers/MockFileSystem.cs +++ b/System.IO.Abstractions.TestingHelpers/MockFileSystem.cs @@ -24,6 +24,10 @@ public MockFileSystem(IDictionary 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); @@ -31,7 +35,7 @@ public MockFileSystem(IDictionary files, string currentDir pathVerifier = new PathVerifier(this); this.files = new Dictionary(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); @@ -53,7 +57,7 @@ public MockFileSystem(IDictionary files, string currentDir AddDirectory(currentDirectory); } - if (!FileExists(defaultTempDirectory)) + if (!FileExists(defaultTempDirectory)) { AddDirectory(defaultTempDirectory); } @@ -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); @@ -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 = @@ -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]; diff --git a/System.IO.Abstractions.TestingHelpers/MockUnixSupport.cs b/System.IO.Abstractions.TestingHelpers/MockUnixSupport.cs index d96acd332..9753a4a79 100644 --- a/System.IO.Abstractions.TestingHelpers/MockUnixSupport.cs +++ b/System.IO.Abstractions.TestingHelpers/MockUnixSupport.cs @@ -6,7 +6,7 @@ public static class MockUnixSupport { private static readonly Regex pathTransform = new Regex(@"^[a-zA-Z]:(?.*)$"); - public static string Path(string path) => IsUnixPlatform() + public static string Path(string path) => path != null && IsUnixPlatform() ? pathTransform.Replace(path, "${path}").Replace(@"\", "/") : path;