Skip to content
Open
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 @@ -14,7 +14,6 @@ namespace System.IO.Abstractions.TestingHelpers;
public class MockFileSystem : FileSystemBase, IMockFileDataAccessor
{
private const string DEFAULT_CURRENT_DIRECTORY = @"C:\";
private const string TEMP_DIRECTORY = @"C:\temp\";

private readonly IDictionary<string, FileSystemEntry> files;
private readonly IDictionary<string, MockDriveData> drives;
Expand Down Expand Up @@ -58,7 +57,9 @@ public MockFileSystem(IDictionary<string, MockFileData> files, MockFileSystemOpt
throw new ArgumentException("Current directory needs to be rooted.", nameof(currentDirectory));
}

var defaultTempDirectory = XFS.Path(TEMP_DIRECTORY);
var defaultTempDirectory = !string.IsNullOrEmpty(options.TemporaryDirectory)
? options.TemporaryDirectory
: System.IO.Path.GetTempPath();
Comment thread
vbreuss marked this conversation as resolved.

StringOperations = new StringOperations(XFS.IsUnixPlatform());
Comment thread
vbreuss marked this conversation as resolved.
pathVerifier = new PathVerifier(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,10 @@ public class MockFileSystemOptions
/// Flag indicating, if a temporary directory should be created.
/// </summary>
public bool CreateDefaultTempDir { get; init; } = true;

/// <summary>
/// The temporary directory used by the <see cref="MockFileSystem" />.
/// Defaults to <see cref="System.IO.Path.GetTempPath()" /> when <see langword="null" /> or empty.
/// </summary>
public string TemporaryDirectory { get; init; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,7 @@ namespace System.IO.Abstractions.TestingHelpers
public MockFileSystemOptions() { }
public bool CreateDefaultTempDir { get; init; }
public string CurrentDirectory { get; init; }
public string TemporaryDirectory { get; init; }
}
[System.Serializable]
public class MockFileSystemWatcherFactory : System.IO.Abstractions.IFileSystemEntity, System.IO.Abstractions.IFileSystemWatcherFactory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@ namespace System.IO.Abstractions.TestingHelpers
public MockFileSystemOptions() { }
public bool CreateDefaultTempDir { get; init; }
public string CurrentDirectory { get; init; }
public string TemporaryDirectory { get; init; }
}
[System.Serializable]
public class MockFileSystemWatcherFactory : System.IO.Abstractions.IFileSystemEntity, System.IO.Abstractions.IFileSystemWatcherFactory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,7 @@ namespace System.IO.Abstractions.TestingHelpers
public MockFileSystemOptions() { }
public bool CreateDefaultTempDir { get; init; }
public string CurrentDirectory { get; init; }
public string TemporaryDirectory { get; init; }
}
[System.Serializable]
public class MockFileSystemWatcherFactory : System.IO.Abstractions.IFileSystemEntity, System.IO.Abstractions.IFileSystemWatcherFactory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,7 @@ namespace System.IO.Abstractions.TestingHelpers
public MockFileSystemOptions() { }
public bool CreateDefaultTempDir { get; init; }
public string CurrentDirectory { get; init; }
public string TemporaryDirectory { get; init; }
}
[System.Serializable]
public class MockFileSystemWatcherFactory : System.IO.Abstractions.IFileSystemEntity, System.IO.Abstractions.IFileSystemWatcherFactory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,7 @@ namespace System.IO.Abstractions.TestingHelpers
public MockFileSystemOptions() { }
public bool CreateDefaultTempDir { get; init; }
public string CurrentDirectory { get; init; }
public string TemporaryDirectory { get; init; }
}
[System.Serializable]
public class MockFileSystemWatcherFactory : System.IO.Abstractions.IFileSystemEntity, System.IO.Abstractions.IFileSystemWatcherFactory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@ namespace System.IO.Abstractions.TestingHelpers
public MockFileSystemOptions() { }
public bool CreateDefaultTempDir { get; init; }
public string CurrentDirectory { get; init; }
public string TemporaryDirectory { get; init; }
}
[System.Serializable]
public class MockFileSystemWatcherFactory : System.IO.Abstractions.IFileSystemEntity, System.IO.Abstractions.IFileSystemWatcherFactory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@ namespace System.IO.Abstractions.TestingHelpers
public MockFileSystemOptions() { }
public bool CreateDefaultTempDir { get; init; }
public string CurrentDirectory { get; init; }
public string TemporaryDirectory { get; init; }
}
[System.Serializable]
public class MockFileSystemWatcherFactory : System.IO.Abstractions.IFileSystemEntity, System.IO.Abstractions.IFileSystemWatcherFactory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public async Task MockDirectoryInfo_EnumerateFileSystemInfos_ShouldReturnDirecto
{ XFS.Path(@"c:\temp\folder\file.txt"), new MockFileData("") },
{ XFS.Path(@"c:\temp\folder\folder"), new MockDirectoryData() },
{ XFS.Path(@"c:\temp\folder\older"), new MockDirectoryData() }
});
}, new MockFileSystemOptions { CreateDefaultTempDir = false });

var directoryInfo = new MockDirectoryInfo(fileSystem, XFS.Path(@"c:\"));
var result = directoryInfo.EnumerateFileSystemInfos("*", SearchOption.AllDirectories).ToArray();
Expand All @@ -236,7 +236,7 @@ public async Task MockDirectoryInfo_EnumerateFileSystemInfos_ShouldReturnDirecto
{ XFS.Path(@"c:\temp\folder\file.txt"), new MockFileData("") },
{ XFS.Path(@"c:\temp\folder\folder"), new MockDirectoryData() },
{ XFS.Path(@"c:\temp\folder\older"), new MockDirectoryData() }
});
}, new MockFileSystemOptions { CreateDefaultTempDir = false });

var directoryInfo = new MockDirectoryInfo(fileSystem, XFS.Path(@"c:\"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -907,7 +907,7 @@ public async Task MockFileInfo_Exists_ShouldUpdateCachedDataOnRefresh()
public async Task MockFileInfo_Create_ShouldUpdateCachedDataAndReturnTrueForExists()
{
IFileSystem fileSystem = new MockFileSystem();
var path = XFS.Path(@"c:\temp\file1.txt");
var path = fileSystem.Path.Combine(fileSystem.Path.GetTempPath(), "file1.txt");
IFileInfo fileInfo = fileSystem.FileInfo.New(path);

// Act
Expand All @@ -922,7 +922,7 @@ public async Task MockFileInfo_Create_ShouldUpdateCachedDataAndReturnTrueForExis
public async Task MockFileInfo_CreateText_ShouldUpdateCachedDataAndReturnTrueForExists()
{
IFileSystem fileSystem = new MockFileSystem();
var path = XFS.Path(@"c:\temp\file1.txt");
var path = fileSystem.Path.Combine(fileSystem.Path.GetTempPath(), "file1.txt");
IFileInfo fileInfo = fileSystem.FileInfo.New(path);

// Act
Expand All @@ -936,7 +936,7 @@ public async Task MockFileInfo_CreateText_ShouldUpdateCachedDataAndReturnTrueFor
public async Task MockFileInfo_Delete_ShouldUpdateCachedDataAndReturnFalseForExists()
{
var fileSystem = new MockFileSystem();
var path = XFS.Path(@"c:\temp\file1.txt");
var path = fileSystem.Path.Combine(fileSystem.Path.GetTempPath(), "file1.txt");
IFileInfo fileInfo = fileSystem.FileInfo.New(path);

// Act
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ public async Task MockFileSystem_AllNodes_ShouldReturnAllNodes()
XFS.Path(@"c:\something\other.gif"),
XFS.Path(@"d:\foobar"),
XFS.Path(@"d:\foo\bar"),
XFS.Path(@"C:\temp")
fileSystem.Path.GetTempPath().TrimEnd(fileSystem.Path.DirectorySeparatorChar)
};

var result = fileSystem.AllNodes;
Expand Down Expand Up @@ -510,7 +510,7 @@ public async Task MockFileSystem_Constructor_ShouldNotDuplicateDrives()
[Test]
public async Task MockFileSystem_DefaultState_DefaultTempDirectoryExists()
{
var tempDirectory = XFS.Path(@"C:\temp");
var tempDirectory = System.IO.Path.GetTempPath();

var mockFileSystem = new MockFileSystem();
var mockFileSystemOverload = new MockFileSystem(null, string.Empty);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,34 @@ public async Task GetTempPath_Called_ReturnsStringLengthGreaterThanZero(string t
await That(result.Length > 0).IsTrue();
}

[Test]
public async Task GetTempPath_Default_MatchesRealOsTempPath()
{
//Arrange
var mockPath = new MockFileSystem().Path;

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

//Assert
await That(result).IsEqualTo(System.IO.Path.GetTempPath());
}

[Test]
public async Task GetTempPath_WithTemporaryDirectoryOption_ReturnsAndCreatesIt()
{
//Arrange
var temporaryDirectory = XFS.Path(@"C:\custom\temp\");
var fileSystem = new MockFileSystem(new MockFileSystemOptions { TemporaryDirectory = temporaryDirectory });

//Act
var result = fileSystem.Path.GetTempPath();

//Assert
await That(result).IsEqualTo(temporaryDirectory);
await That(fileSystem.Directory.Exists(temporaryDirectory)).IsTrue();
}

[Test]
public async Task GetTempPath_Called_WithNonNullVirtualTempDirectory_ReturnsVirtualTempDirectory()
{
Expand Down