Skip to content

Commit baecc07

Browse files
authored
feat: add options to constructor of MockFileSystem (#1013)
Add a new class `MockFileSystemOptions` and corresponding overloads in the constructor of `MockFileSystem`. Default values don't change the current behaviour, but when setting `CreateDefaultTempDir` to `false`, the temp directory is not created. Fixes: - #983
1 parent 2fcccbb commit baecc07

File tree

4 files changed

+78
-2
lines changed

4 files changed

+78
-2
lines changed

src/TestableIO.System.IO.Abstractions.TestingHelpers/MockFileSystem.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,21 @@ public MockFileSystem() : this(null) { }
2525

2626
/// <inheritdoc />
2727
public MockFileSystem(IDictionary<string, MockFileData> files, string currentDirectory = "")
28+
: this(files, new MockFileSystemOptions
29+
{
30+
CurrentDirectory = currentDirectory,
31+
CreateDefaultTempDir = true
32+
}) { }
33+
34+
/// <inheritdoc />
35+
public MockFileSystem(MockFileSystemOptions options)
36+
: this(null, options) { }
37+
38+
/// <inheritdoc />
39+
public MockFileSystem(IDictionary<string, MockFileData> files, MockFileSystemOptions options)
2840
{
41+
options ??= new MockFileSystemOptions();
42+
var currentDirectory = options.CurrentDirectory;
2943
if (string.IsNullOrEmpty(currentDirectory))
3044
{
3145
currentDirectory = XFS.Path(DEFAULT_CURRENT_DIRECTORY);
@@ -63,7 +77,7 @@ public MockFileSystem(IDictionary<string, MockFileData> files, string currentDir
6377
AddDirectory(currentDirectory);
6478
}
6579

66-
if (!FileExists(defaultTempDirectory))
80+
if (options.CreateDefaultTempDir && !FileExists(defaultTempDirectory))
6781
{
6882
AddDirectory(defaultTempDirectory);
6983
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace System.IO.Abstractions.TestingHelpers
2+
{
3+
/// <summary>
4+
/// Constructor options for <see cref="MockFileSystem" />
5+
/// </summary>
6+
public class MockFileSystemOptions
7+
{
8+
/// <summary>
9+
/// The <see cref="Directory.GetCurrentDirectory()" /> with which the <see cref="MockFileSystem" /> is initialized.
10+
/// </summary>
11+
public string CurrentDirectory { get; init; } = "";
12+
13+
/// <summary>
14+
/// Flag indicating, if a temporary directory should be created.
15+
/// </summary>
16+
public bool CreateDefaultTempDir { get; init; } = true;
17+
}
18+
}

src/TestableIO.System.IO.Abstractions.TestingHelpers/TestableIO.System.IO.Abstractions.TestingHelpers.csproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@
1515
<PrivateAssets>all</PrivateAssets>
1616
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
1717
</PackageReference>
18-
</ItemGroup>
18+
<PackageReference Include="IsExternalInit" Version="1.0.3">
19+
<PrivateAssets>all</PrivateAssets>
20+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
21+
</PackageReference>
22+
</ItemGroup>
1923
<ItemGroup>
2024
<None Include="..\..\images\icon_256x256.png" Pack="true" PackagePath="\" />
2125
</ItemGroup>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using NUnit.Framework;
2+
using XFS = System.IO.Abstractions.TestingHelpers.MockUnixSupport;
3+
4+
namespace System.IO.Abstractions.TestingHelpers.Tests
5+
{
6+
[TestFixture]
7+
public class MockFileSystemOptionTests
8+
{
9+
[Test]
10+
[TestCase(true)]
11+
[TestCase(false)]
12+
public void CreateDefaultTempDir_ShouldBeConsidered(bool createTempDir)
13+
{
14+
var fileSystem = new MockFileSystem(new MockFileSystemOptions
15+
{
16+
CreateDefaultTempDir = createTempDir
17+
});
18+
19+
var result = fileSystem.Directory.Exists(fileSystem.Path.GetTempPath());
20+
21+
Assert.AreEqual(createTempDir, result);
22+
}
23+
24+
[Test]
25+
[TestCase(@"C:\path")]
26+
[TestCase(@"C:\foo\bar")]
27+
public void CurrentDirectory_ShouldBeConsidered(string currentDirectory)
28+
{
29+
currentDirectory = XFS.Path(currentDirectory);
30+
var fileSystem = new MockFileSystem(new MockFileSystemOptions
31+
{
32+
CurrentDirectory = currentDirectory
33+
});
34+
35+
var result = fileSystem.Directory.GetCurrentDirectory();
36+
37+
Assert.AreEqual(currentDirectory, result);
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)