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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather not use the host's temp path as the default. It makes the mock depend on the machine it runs on: on Windows every new MockFileSystem() gets something like C:\Users\<name>\AppData\Local\Temp, on Linux it follows TMPDIR, and if TEMP is on another drive the mock quietly gets a second drive. The same test then sees a different file tree on each machine, and snapshots pick up user names.

The actual bug is only the Unix default (/temp/ instead of /tmp/), so could we fix just that and keep the constant?

var defaultTempDirectory = !string.IsNullOrEmpty(options.TemporaryDirectory)
    ? options.TemporaryDirectory
    : XFS.IsUnixPlatform() ? "/tmp/" : TEMP_DIRECTORY;

For your scoped-filesystem case, new MockFileSystemOptions { TemporaryDirectory = Path.GetTempPath() } gives you the real path explicitly.

? options.TemporaryDirectory
: System.IO.Path.GetTempPath();
Comment thread
vbreuss marked this conversation as resolved.
Resolved

StringOperations = new StringOperations(XFS.IsUnixPlatform());
Comment thread
vbreuss marked this conversation as resolved.
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; }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two things about the new option:

  • CurrentDirectory throws when it isn't rooted (MockFileSystem.cs, a few lines above line 60), but TemporaryDirectory accepts a relative path. Could you validate it the same way?
  • The real GetTempPath() always ends with a directory separator, and callers rely on that when they concatenate. If someone passes C:\custom\temp, the mock returns it without one. Please append the separator when it's missing.

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests had to switch off the temp directory because the host's temp path now shows up deeper in enumerations of C:\. Users' test suites would break in the same way, which is what makes the new default a breaking change. With a fixed default these changes shouldn't be needed any more. Could you revert them, together with the ones in MockFileInfoTests.cs and MockFileSystemTests.cs (lines 399 and 513)?


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
Loading