Skip to content

Commit 29b4dba

Browse files
IrisClassonIris Classon
andauthored
Ensure temp directory in MockFileSystem exists
Fixes #588 Co-authored-by: Iris Classon <iris@greenbyte.com>
1 parent 5abf6a5 commit 29b4dba

4 files changed

Lines changed: 75 additions & 5 deletions

File tree

System.IO.Abstractions.TestingHelpers.Tests/MockFileSystemTests.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,8 @@ public void MockFileSystem_AllNodes_ShouldReturnAllNodes()
292292
XFS.Path(@"c:\something\demo.txt"),
293293
XFS.Path(@"c:\something\other.gif"),
294294
XFS.Path(@"d:\foobar"),
295-
XFS.Path(@"d:\foo\bar")
295+
XFS.Path(@"d:\foo\bar"),
296+
XFS.Path(@"C:\temp")
296297
};
297298

298299
var result = fileSystem.AllNodes;
@@ -340,6 +341,18 @@ public void MockFileSystem_DefaultState_CurrentDirectoryExists(string currentDir
340341
Assert.IsTrue(actualCurrentDirectory.Exists);
341342
}
342343

344+
[Test]
345+
public void MockFileSystem_DefaultState_DefaultTempDirectoryExists()
346+
{
347+
var tempDirectory = XFS.Path(@"C:\temp");
348+
349+
var mockFileSystem = new MockFileSystem();
350+
var mockFileSystemOverload = new MockFileSystem(null, string.Empty);
351+
352+
Assert.IsTrue(mockFileSystem.Directory.Exists(tempDirectory));
353+
Assert.IsTrue(mockFileSystemOverload.Directory.Exists(tempDirectory));
354+
}
355+
343356
[Test]
344357
public void MockFileSystem_FileSystemWatcher_PathShouldBeAssignable()
345358
{

System.IO.Abstractions.TestingHelpers.Tests/MockPathTests.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,49 @@ public void GetTempPath_Called_ReturnsStringLengthGreaterThanZero()
372372
Assert.IsTrue(result.Length > 0);
373373
}
374374

375+
[Test]
376+
[TestCase(null)]
377+
[TestCase("")]
378+
[TestCase(@"C:\temp")]
379+
public void GetTempPath_Called_ReturnsStringLengthGreaterThanZero(string tempDirectory) {
380+
//Arrange
381+
var mockPath = new MockPath(new MockFileSystem(), string.IsNullOrEmpty(tempDirectory)? tempDirectory: XFS.Path(tempDirectory));
382+
383+
//Act
384+
var result = mockPath.GetTempPath();
385+
386+
//Assert
387+
Assert.IsTrue(result.Length > 0);
388+
}
389+
390+
[Test]
391+
public void GetTempPath_Called_WithNonNullVirtualTempDirectory_ReturnsVirtualTempDirectory() {
392+
//Arrange
393+
var tempDirectory = XFS.Path(@"C:\temp");
394+
395+
var mockPath = new MockPath(new MockFileSystem(), tempDirectory);
396+
397+
//Act
398+
var result = mockPath.GetTempPath();
399+
400+
//Assert
401+
Assert.AreEqual(tempDirectory,result);
402+
}
403+
404+
[Test]
405+
[TestCase(null)]
406+
[TestCase("")]
407+
public void GetTempPath_Called_WithNullOrEmptyVirtualTempDirectory_ReturnsFallbackTempDirectory(string tempDirectory) {
408+
//Arrange
409+
var mockPath = new MockPath(new MockFileSystem(), tempDirectory);
410+
411+
//Act
412+
var result = mockPath.GetTempPath();
413+
414+
//Assert
415+
Assert.IsTrue(result.Length > 0);
416+
}
417+
375418
[Test]
376419
public void HasExtension_PathSentIn_DeterminesExtension()
377420
{

System.IO.Abstractions.TestingHelpers/MockFileSystem.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace System.IO.Abstractions.TestingHelpers
1111
public class MockFileSystem : IFileSystem, IMockFileDataAccessor
1212
{
1313
private const string DEFAULT_CURRENT_DIRECTORY = @"C:\";
14+
private const string TEMP_DIRECTORY = @"C:\temp";
1415

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

28+
var defaultTempDirectory = XFS.Path(TEMP_DIRECTORY);
29+
2730
StringOperations = new StringOperations(XFS.IsUnixPlatform());
2831
pathVerifier = new PathVerifier(this);
2932
this.files = new Dictionary<string, MockFileData>(StringOperations.Comparer);
3033

31-
Path = new MockPath(this);
34+
Path = new MockPath(this,defaultTempDirectory);
3235
File = new MockFile(this);
3336
Directory = new MockDirectory(this, currentDirectory);
3437
FileInfo = new MockFileInfoFactory(this);
@@ -49,6 +52,11 @@ public MockFileSystem(IDictionary<string, MockFileData> files, string currentDir
4952
{
5053
AddDirectory(currentDirectory);
5154
}
55+
56+
if (!FileExists(defaultTempDirectory))
57+
{
58+
AddDirectory(defaultTempDirectory);
59+
}
5260
}
5361

5462
public StringOperations StringOperations { get; }

System.IO.Abstractions.TestingHelpers/MockPath.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@ namespace System.IO.Abstractions.TestingHelpers
1111
public class MockPath : PathWrapper
1212
{
1313
private readonly IMockFileDataAccessor mockFileDataAccessor;
14+
private readonly string defaultTempDirectory;
1415

15-
public MockPath(IMockFileDataAccessor mockFileDataAccessor) : base(mockFileDataAccessor?.FileSystem)
16+
public MockPath(IMockFileDataAccessor mockFileDataAccessor) : this(mockFileDataAccessor,string.Empty) { }
17+
18+
public MockPath(IMockFileDataAccessor mockFileDataAccessor,string defaultTempDirectory) : base(mockFileDataAccessor?.FileSystem)
1619
{
1720
this.mockFileDataAccessor = mockFileDataAccessor ?? throw new ArgumentNullException(nameof(mockFileDataAccessor));
21+
this.defaultTempDirectory = !string.IsNullOrEmpty(defaultTempDirectory) ? defaultTempDirectory : base.GetTempPath();
1822
}
1923

2024
public override string GetFullPath(string path)
@@ -128,13 +132,15 @@ private string[] GetSegments(params string[] paths)
128132
public override string GetTempFileName()
129133
{
130134
string fileName = mockFileDataAccessor.Path.GetRandomFileName();
131-
string tempDir = mockFileDataAccessor.Path.GetTempPath();
135+
string tempDir = this.GetTempPath();
132136

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

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

137141
return fullPath;
138142
}
143+
144+
public override string GetTempPath() => defaultTempDirectory;
139145
}
140-
}
146+
}

0 commit comments

Comments
 (0)