Skip to content

Commit 180249f

Browse files
VTJDaileytathamoddie
authored andcommitted
Support creating UNC paths
Create failing test to highlight problem creating UNC paths. Create two other tests for other UNC related commands that should throw an exception. Bug fix, plus corrected tests.
1 parent 85e2996 commit 180249f

2 files changed

Lines changed: 55 additions & 0 deletions

File tree

TestHelpers.Tests/MockDirectoryTests.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,42 @@ public void MockDirectory_CreateDirectory_ShouldReturnDirectoryInfoBase()
410410
Assert.IsNotNull(result);
411411
}
412412

413+
[Test]
414+
public void MockDirectory_CreateDirectory_ShouldWorkWithUNCPath()
415+
{
416+
// Arrange
417+
var fileSystem = new MockFileSystem();
418+
419+
// Act
420+
fileSystem.Directory.CreateDirectory(XFS.Path(@"\\server\share\path\to\create", () => false));
421+
422+
// Assert
423+
Assert.IsTrue(fileSystem.Directory.Exists(XFS.Path(@"\\server\share\path\to\create\", () => false)));
424+
}
425+
426+
[Test]
427+
public void MockDirectory_CreateDirectory_ShouldFailIfTryingToCreateUNCPathOnlyServer()
428+
{
429+
// Arrange
430+
var fileSystem = new MockFileSystem();
431+
432+
var ex = Assert.Throws<ArgumentException>(() => fileSystem.Directory.CreateDirectory(XFS.Path(@"\\server", () => false)));
433+
Assert.That(ex.Message, Is.EqualTo("The UNC path should be of the form \\\\server\\share.\r\nParameter name: path"));
434+
}
435+
436+
[Test]
437+
public void MockDirectory_CreateDirectory_ShouldSucceedIfTryingToCreateUNCPathShare()
438+
{
439+
// Arrange
440+
var fileSystem = new MockFileSystem();
441+
442+
// Act
443+
fileSystem.Directory.CreateDirectory(XFS.Path(@"\\server\share", () => false));
444+
445+
// Assert
446+
Assert.IsTrue(fileSystem.Directory.Exists(XFS.Path(@"\\server\share\", () => false)));
447+
}
448+
413449
[Test]
414450
public void MockDirectory_Delete_ShouldDeleteDirectory()
415451
{

TestingHelpers/MockFileSystem.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,25 @@ public void AddDirectory(string path)
105105
throw new UnauthorizedAccessException(string.Format(CultureInfo.InvariantCulture, "Access to the path '{0}' is denied.", path));
106106

107107
var lastIndex = 0;
108+
109+
bool isUnc =
110+
path.StartsWith(@"\\", StringComparison.OrdinalIgnoreCase) ||
111+
path.StartsWith(@"//", StringComparison.OrdinalIgnoreCase);
112+
113+
if (isUnc)
114+
{
115+
116+
//First, confirm they aren't trying to create '\\server\'
117+
lastIndex = path.IndexOf(separator, 2);
118+
if (lastIndex < 0)
119+
throw new ArgumentException(@"The UNC path should be of the form \\server\share.", "path");
120+
121+
/*
122+
* Although CreateDirectory(@"\\server\share\") is not going to work in real code, we allow it here for the purposes of setting up test doubles.
123+
* See PR https://github.com/tathamoddie/System.IO.Abstractions/pull/90 for conversation
124+
*/
125+
}
126+
108127
while ((lastIndex = path.IndexOf(separator, lastIndex + 1)) > -1)
109128
{
110129
var segment = path.Substring(0, lastIndex + 1);

0 commit comments

Comments
 (0)