diff --git a/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockFileSystem.cs b/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockFileSystem.cs index 0925d4ed1..971e8b584 100644 --- a/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockFileSystem.cs +++ b/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockFileSystem.cs @@ -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 files; private readonly IDictionary drives; @@ -58,7 +57,9 @@ public MockFileSystem(IDictionary 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(); StringOperations = new StringOperations(XFS.IsUnixPlatform()); pathVerifier = new PathVerifier(this); diff --git a/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockFileSystemOptions.cs b/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockFileSystemOptions.cs index f6f19dbf3..64b851d2e 100644 --- a/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockFileSystemOptions.cs +++ b/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockFileSystemOptions.cs @@ -14,4 +14,10 @@ public class MockFileSystemOptions /// Flag indicating, if a temporary directory should be created. /// public bool CreateDefaultTempDir { get; init; } = true; + + /// + /// The temporary directory used by the . + /// Defaults to when or empty. + /// + public string TemporaryDirectory { get; init; } } \ No newline at end of file diff --git a/tests/TestableIO.System.IO.Abstractions.Api.Tests/Expected/TestableIO.System.IO.Abstractions.TestingHelpers_net10.0.txt b/tests/TestableIO.System.IO.Abstractions.Api.Tests/Expected/TestableIO.System.IO.Abstractions.TestingHelpers_net10.0.txt index 9e26f088d..1a6969e3d 100644 --- a/tests/TestableIO.System.IO.Abstractions.Api.Tests/Expected/TestableIO.System.IO.Abstractions.TestingHelpers_net10.0.txt +++ b/tests/TestableIO.System.IO.Abstractions.Api.Tests/Expected/TestableIO.System.IO.Abstractions.TestingHelpers_net10.0.txt @@ -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 diff --git a/tests/TestableIO.System.IO.Abstractions.Api.Tests/Expected/TestableIO.System.IO.Abstractions.TestingHelpers_net472.txt b/tests/TestableIO.System.IO.Abstractions.Api.Tests/Expected/TestableIO.System.IO.Abstractions.TestingHelpers_net472.txt index e64a25b2e..ebf947270 100644 --- a/tests/TestableIO.System.IO.Abstractions.Api.Tests/Expected/TestableIO.System.IO.Abstractions.TestingHelpers_net472.txt +++ b/tests/TestableIO.System.IO.Abstractions.Api.Tests/Expected/TestableIO.System.IO.Abstractions.TestingHelpers_net472.txt @@ -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 diff --git a/tests/TestableIO.System.IO.Abstractions.Api.Tests/Expected/TestableIO.System.IO.Abstractions.TestingHelpers_net6.0.txt b/tests/TestableIO.System.IO.Abstractions.Api.Tests/Expected/TestableIO.System.IO.Abstractions.TestingHelpers_net6.0.txt index b02c1735a..3bf8d36fe 100644 --- a/tests/TestableIO.System.IO.Abstractions.Api.Tests/Expected/TestableIO.System.IO.Abstractions.TestingHelpers_net6.0.txt +++ b/tests/TestableIO.System.IO.Abstractions.Api.Tests/Expected/TestableIO.System.IO.Abstractions.TestingHelpers_net6.0.txt @@ -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 diff --git a/tests/TestableIO.System.IO.Abstractions.Api.Tests/Expected/TestableIO.System.IO.Abstractions.TestingHelpers_net8.0.txt b/tests/TestableIO.System.IO.Abstractions.Api.Tests/Expected/TestableIO.System.IO.Abstractions.TestingHelpers_net8.0.txt index a5a8f2770..ac31d092f 100644 --- a/tests/TestableIO.System.IO.Abstractions.Api.Tests/Expected/TestableIO.System.IO.Abstractions.TestingHelpers_net8.0.txt +++ b/tests/TestableIO.System.IO.Abstractions.Api.Tests/Expected/TestableIO.System.IO.Abstractions.TestingHelpers_net8.0.txt @@ -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 diff --git a/tests/TestableIO.System.IO.Abstractions.Api.Tests/Expected/TestableIO.System.IO.Abstractions.TestingHelpers_net9.0.txt b/tests/TestableIO.System.IO.Abstractions.Api.Tests/Expected/TestableIO.System.IO.Abstractions.TestingHelpers_net9.0.txt index 00e02741d..b2cd7b886 100644 --- a/tests/TestableIO.System.IO.Abstractions.Api.Tests/Expected/TestableIO.System.IO.Abstractions.TestingHelpers_net9.0.txt +++ b/tests/TestableIO.System.IO.Abstractions.Api.Tests/Expected/TestableIO.System.IO.Abstractions.TestingHelpers_net9.0.txt @@ -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 diff --git a/tests/TestableIO.System.IO.Abstractions.Api.Tests/Expected/TestableIO.System.IO.Abstractions.TestingHelpers_netstandard2.0.txt b/tests/TestableIO.System.IO.Abstractions.Api.Tests/Expected/TestableIO.System.IO.Abstractions.TestingHelpers_netstandard2.0.txt index 1dc0ed02e..8d6cece7b 100644 --- a/tests/TestableIO.System.IO.Abstractions.Api.Tests/Expected/TestableIO.System.IO.Abstractions.TestingHelpers_netstandard2.0.txt +++ b/tests/TestableIO.System.IO.Abstractions.Api.Tests/Expected/TestableIO.System.IO.Abstractions.TestingHelpers_netstandard2.0.txt @@ -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 diff --git a/tests/TestableIO.System.IO.Abstractions.Api.Tests/Expected/TestableIO.System.IO.Abstractions.TestingHelpers_netstandard2.1.txt b/tests/TestableIO.System.IO.Abstractions.Api.Tests/Expected/TestableIO.System.IO.Abstractions.TestingHelpers_netstandard2.1.txt index cba08bc11..0a86625e3 100644 --- a/tests/TestableIO.System.IO.Abstractions.Api.Tests/Expected/TestableIO.System.IO.Abstractions.TestingHelpers_netstandard2.1.txt +++ b/tests/TestableIO.System.IO.Abstractions.Api.Tests/Expected/TestableIO.System.IO.Abstractions.TestingHelpers_netstandard2.1.txt @@ -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 diff --git a/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryInfoTests.cs b/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryInfoTests.cs index bed4d6702..015352e7f 100644 --- a/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryInfoTests.cs +++ b/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryInfoTests.cs @@ -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(); @@ -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:\")); diff --git a/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockFileInfoTests.cs b/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockFileInfoTests.cs index 55a7b1f01..bf0492476 100644 --- a/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockFileInfoTests.cs +++ b/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockFileInfoTests.cs @@ -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 @@ -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 @@ -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 diff --git a/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockFileSystemTests.cs b/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockFileSystemTests.cs index 7252317cd..f2564ba42 100644 --- a/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockFileSystemTests.cs +++ b/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockFileSystemTests.cs @@ -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; @@ -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); diff --git a/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockPathTests.cs b/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockPathTests.cs index e614de495..be5f0f7f6 100644 --- a/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockPathTests.cs +++ b/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockPathTests.cs @@ -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() {