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
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,8 @@ public void MockFileSystem_AllNodes_ShouldReturnAllNodes()
XFS.Path(@"c:\something\demo.txt"),
XFS.Path(@"c:\something\other.gif"),
XFS.Path(@"d:\foobar"),
XFS.Path(@"d:\foo\bar")
XFS.Path(@"d:\foo\bar"),
XFS.Path(@"C:\temp")
};

var result = fileSystem.AllNodes;
Expand Down Expand Up @@ -340,6 +341,18 @@ public void MockFileSystem_DefaultState_CurrentDirectoryExists(string currentDir
Assert.IsTrue(actualCurrentDirectory.Exists);
}

[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));
}

[Test]
public void MockFileSystem_FileSystemWatcher_PathShouldBeAssignable()
{
Expand Down
43 changes: 43 additions & 0 deletions System.IO.Abstractions.TestingHelpers.Tests/MockPathTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,49 @@ public void GetTempPath_Called_ReturnsStringLengthGreaterThanZero()
Assert.IsTrue(result.Length > 0);
}

[Test]
[TestCase(null)]
[TestCase("")]
[TestCase(@"C:\temp")]
public void GetTempPath_Called_ReturnsStringLengthGreaterThanZero(string tempDirectory) {
//Arrange
var mockPath = new MockPath(new MockFileSystem(), string.IsNullOrEmpty(tempDirectory)? tempDirectory: XFS.Path(tempDirectory));

//Act
var result = mockPath.GetTempPath();

//Assert
Assert.IsTrue(result.Length > 0);
}

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

var mockPath = new MockPath(new MockFileSystem(), tempDirectory);

//Act
var result = mockPath.GetTempPath();

//Assert
Assert.AreEqual(tempDirectory,result);
}

[Test]
[TestCase(null)]
[TestCase("")]
public void GetTempPath_Called_WithNullOrEmptyVirtualTempDirectory_ReturnsFallbackTempDirectory(string tempDirectory) {
//Arrange
var mockPath = new MockPath(new MockFileSystem(), tempDirectory);

//Act
var result = mockPath.GetTempPath();

//Assert
Assert.IsTrue(result.Length > 0);
}

[Test]
public void HasExtension_PathSentIn_DeterminesExtension()
{
Expand Down
10 changes: 9 additions & 1 deletion System.IO.Abstractions.TestingHelpers/MockFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace System.IO.Abstractions.TestingHelpers
public class MockFileSystem : IFileSystem, IMockFileDataAccessor
{
private const string DEFAULT_CURRENT_DIRECTORY = @"C:\";
private const string TEMP_DIRECTORY = @"C:\temp";

private readonly IDictionary<string, MockFileData> files;
private readonly PathVerifier pathVerifier;
Expand All @@ -24,11 +25,13 @@ public MockFileSystem(IDictionary<string, MockFileData> files, string currentDir
currentDirectory = XFS.Path(DEFAULT_CURRENT_DIRECTORY);
}

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);
Path = new MockPath(this,defaultTempDirectory);
File = new MockFile(this);
Directory = new MockDirectory(this, currentDirectory);
FileInfo = new MockFileInfoFactory(this);
Expand All @@ -49,6 +52,11 @@ public MockFileSystem(IDictionary<string, MockFileData> files, string currentDir
{
AddDirectory(currentDirectory);
}

if (!FileExists(defaultTempDirectory))
{
AddDirectory(defaultTempDirectory);
}
}

public StringOperations StringOperations { get; }
Expand Down
12 changes: 9 additions & 3 deletions System.IO.Abstractions.TestingHelpers/MockPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@ namespace System.IO.Abstractions.TestingHelpers
public class MockPath : PathWrapper
{
private readonly IMockFileDataAccessor mockFileDataAccessor;
private readonly string defaultTempDirectory;

public MockPath(IMockFileDataAccessor mockFileDataAccessor) : base(mockFileDataAccessor?.FileSystem)
public MockPath(IMockFileDataAccessor mockFileDataAccessor) : this(mockFileDataAccessor,string.Empty) { }
Comment thread
IrisClasson marked this conversation as resolved.
Resolved

public MockPath(IMockFileDataAccessor mockFileDataAccessor,string defaultTempDirectory) : base(mockFileDataAccessor?.FileSystem)
{
this.mockFileDataAccessor = mockFileDataAccessor ?? throw new ArgumentNullException(nameof(mockFileDataAccessor));
this.defaultTempDirectory = !string.IsNullOrEmpty(defaultTempDirectory) ? defaultTempDirectory : base.GetTempPath();
}

public override string GetFullPath(string path)
Expand Down Expand Up @@ -128,13 +132,15 @@ private string[] GetSegments(params string[] paths)
public override string GetTempFileName()
{
string fileName = mockFileDataAccessor.Path.GetRandomFileName();
string tempDir = mockFileDataAccessor.Path.GetTempPath();
string tempDir = this.GetTempPath();

string fullPath = mockFileDataAccessor.Path.Combine(tempDir, fileName);

mockFileDataAccessor.AddFile(fullPath, new MockFileData(string.Empty));

return fullPath;
}

public override string GetTempPath() => defaultTempDirectory;
}
}
}