diff --git a/System.IO.Abstractions.TestingHelpers.Tests/MockFileSystemTests.cs b/System.IO.Abstractions.TestingHelpers.Tests/MockFileSystemTests.cs index 3e5ef7c7e..fec774718 100644 --- a/System.IO.Abstractions.TestingHelpers.Tests/MockFileSystemTests.cs +++ b/System.IO.Abstractions.TestingHelpers.Tests/MockFileSystemTests.cs @@ -292,7 +292,8 @@ public void MockFileSystem_AllNodes_ShouldReturnAllNodes() XFS.Path(@"c:\something\demo.txt"), XFS.Path(@"c:\something\other.gif"), XFS.Path(@"d:\foobar"), - XFS.Path(@"d:\foo\bar") + XFS.Path(@"d:\foo\bar"), + XFS.Path(@"C:\temp") }; var result = fileSystem.AllNodes; @@ -340,6 +341,18 @@ public void MockFileSystem_DefaultState_CurrentDirectoryExists(string currentDir Assert.IsTrue(actualCurrentDirectory.Exists); } + [Test] + public void MockFileSystem_DefaultState_DefaultTempDirectoryExists() + { + var tempDirectory = XFS.Path(@"C:\temp"); + + var mockFileSystem = new MockFileSystem(); + var mockFileSystemOverload = new MockFileSystem(null, string.Empty); + + Assert.IsTrue(mockFileSystem.Directory.Exists(tempDirectory)); + Assert.IsTrue(mockFileSystemOverload.Directory.Exists(tempDirectory)); + } + [Test] public void MockFileSystem_FileSystemWatcher_PathShouldBeAssignable() { diff --git a/System.IO.Abstractions.TestingHelpers.Tests/MockPathTests.cs b/System.IO.Abstractions.TestingHelpers.Tests/MockPathTests.cs index d66fb281b..7d3c7f041 100644 --- a/System.IO.Abstractions.TestingHelpers.Tests/MockPathTests.cs +++ b/System.IO.Abstractions.TestingHelpers.Tests/MockPathTests.cs @@ -372,6 +372,49 @@ public void GetTempPath_Called_ReturnsStringLengthGreaterThanZero() Assert.IsTrue(result.Length > 0); } + [Test] + [TestCase(null)] + [TestCase("")] + [TestCase(@"C:\temp")] + public void GetTempPath_Called_ReturnsStringLengthGreaterThanZero(string tempDirectory) { + //Arrange + var mockPath = new MockPath(new MockFileSystem(), string.IsNullOrEmpty(tempDirectory)? tempDirectory: XFS.Path(tempDirectory)); + + //Act + var result = mockPath.GetTempPath(); + + //Assert + Assert.IsTrue(result.Length > 0); + } + + [Test] + public void GetTempPath_Called_WithNonNullVirtualTempDirectory_ReturnsVirtualTempDirectory() { + //Arrange + var tempDirectory = XFS.Path(@"C:\temp"); + + var mockPath = new MockPath(new MockFileSystem(), tempDirectory); + + //Act + var result = mockPath.GetTempPath(); + + //Assert + Assert.AreEqual(tempDirectory,result); + } + + [Test] + [TestCase(null)] + [TestCase("")] + public void GetTempPath_Called_WithNullOrEmptyVirtualTempDirectory_ReturnsFallbackTempDirectory(string tempDirectory) { + //Arrange + var mockPath = new MockPath(new MockFileSystem(), tempDirectory); + + //Act + var result = mockPath.GetTempPath(); + + //Assert + Assert.IsTrue(result.Length > 0); + } + [Test] public void HasExtension_PathSentIn_DeterminesExtension() { diff --git a/System.IO.Abstractions.TestingHelpers/MockFileSystem.cs b/System.IO.Abstractions.TestingHelpers/MockFileSystem.cs index a9cd3b49d..d87331bcf 100644 --- a/System.IO.Abstractions.TestingHelpers/MockFileSystem.cs +++ b/System.IO.Abstractions.TestingHelpers/MockFileSystem.cs @@ -11,6 +11,7 @@ namespace System.IO.Abstractions.TestingHelpers public class MockFileSystem : IFileSystem, IMockFileDataAccessor { private const string DEFAULT_CURRENT_DIRECTORY = @"C:\"; + private const string TEMP_DIRECTORY = @"C:\temp"; private readonly IDictionary files; private readonly PathVerifier pathVerifier; @@ -24,11 +25,13 @@ public MockFileSystem(IDictionary files, string currentDir currentDirectory = XFS.Path(DEFAULT_CURRENT_DIRECTORY); } + var defaultTempDirectory = XFS.Path(TEMP_DIRECTORY); + StringOperations = new StringOperations(XFS.IsUnixPlatform()); pathVerifier = new PathVerifier(this); this.files = new Dictionary(StringOperations.Comparer); - Path = new MockPath(this); + Path = new MockPath(this,defaultTempDirectory); File = new MockFile(this); Directory = new MockDirectory(this, currentDirectory); FileInfo = new MockFileInfoFactory(this); @@ -49,6 +52,11 @@ public MockFileSystem(IDictionary files, string currentDir { AddDirectory(currentDirectory); } + + if (!FileExists(defaultTempDirectory)) + { + AddDirectory(defaultTempDirectory); + } } public StringOperations StringOperations { get; } diff --git a/System.IO.Abstractions.TestingHelpers/MockPath.cs b/System.IO.Abstractions.TestingHelpers/MockPath.cs index 921f650e4..bc37472f4 100644 --- a/System.IO.Abstractions.TestingHelpers/MockPath.cs +++ b/System.IO.Abstractions.TestingHelpers/MockPath.cs @@ -11,10 +11,14 @@ namespace System.IO.Abstractions.TestingHelpers public class MockPath : PathWrapper { private readonly IMockFileDataAccessor mockFileDataAccessor; + private readonly string defaultTempDirectory; - public MockPath(IMockFileDataAccessor mockFileDataAccessor) : base(mockFileDataAccessor?.FileSystem) + public MockPath(IMockFileDataAccessor mockFileDataAccessor) : this(mockFileDataAccessor,string.Empty) { } + + public MockPath(IMockFileDataAccessor mockFileDataAccessor,string defaultTempDirectory) : base(mockFileDataAccessor?.FileSystem) { this.mockFileDataAccessor = mockFileDataAccessor ?? throw new ArgumentNullException(nameof(mockFileDataAccessor)); + this.defaultTempDirectory = !string.IsNullOrEmpty(defaultTempDirectory) ? defaultTempDirectory : base.GetTempPath(); } public override string GetFullPath(string path) @@ -128,7 +132,7 @@ private string[] GetSegments(params string[] paths) public override string GetTempFileName() { string fileName = mockFileDataAccessor.Path.GetRandomFileName(); - string tempDir = mockFileDataAccessor.Path.GetTempPath(); + string tempDir = this.GetTempPath(); string fullPath = mockFileDataAccessor.Path.Combine(tempDir, fileName); @@ -136,5 +140,7 @@ public override string GetTempFileName() return fullPath; } + + public override string GetTempPath() => defaultTempDirectory; } -} +} \ No newline at end of file