From fdbc86dd0ddfb0c5b2eec3d68df35da5c540f293 Mon Sep 17 00:00:00 2001 From: Iris Classon Date: Mon, 4 May 2020 16:39:31 +0200 Subject: [PATCH 1/6] #588 Set default temp directory for virtual file system --- .../MockFileSystemTests.cs | 3 ++- .../MockFileSystem.cs | 10 +++++++++- System.IO.Abstractions.TestingHelpers/MockPath.cs | 15 ++++++++++++--- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/System.IO.Abstractions.TestingHelpers.Tests/MockFileSystemTests.cs b/System.IO.Abstractions.TestingHelpers.Tests/MockFileSystemTests.cs index 3e5ef7c7e..6a7375219 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; diff --git a/System.IO.Abstractions.TestingHelpers/MockFileSystem.cs b/System.IO.Abstractions.TestingHelpers/MockFileSystem.cs index a9cd3b49d..8d615978a 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 DEFAULT_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(DEFAULT_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..61b759eda 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 = defaultTempDirectory; } 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,10 @@ public override string GetTempFileName() return fullPath; } + + public override string GetTempPath() + { + return !string.IsNullOrEmpty(defaultTempDirectory) ? defaultTempDirectory : base.GetTempPath(); + } } -} +} \ No newline at end of file From 36e6cfdf41d6a4ee0083919ad5e51386ebfd93e5 Mon Sep 17 00:00:00 2001 From: Iris Classon Date: Mon, 4 May 2020 16:43:47 +0200 Subject: [PATCH 2/6] #588 Add test for default temp directory creation in constructor --- .../MockFileSystemTests.cs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/System.IO.Abstractions.TestingHelpers.Tests/MockFileSystemTests.cs b/System.IO.Abstractions.TestingHelpers.Tests/MockFileSystemTests.cs index 6a7375219..e45b6ce36 100644 --- a/System.IO.Abstractions.TestingHelpers.Tests/MockFileSystemTests.cs +++ b/System.IO.Abstractions.TestingHelpers.Tests/MockFileSystemTests.cs @@ -341,6 +341,18 @@ public void MockFileSystem_DefaultState_CurrentDirectoryExists(string currentDir Assert.IsTrue(actualCurrentDirectory.Exists); } + [Test] + public void MockFileSystem_DefaultState_DefaultTempDirectoryExists() + { + var tempDirectory = @"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() { From 7a9e37e276def5501faef01ebf1b7ad9322731d8 Mon Sep 17 00:00:00 2001 From: Iris Classon Date: Mon, 4 May 2020 16:57:26 +0200 Subject: [PATCH 3/6] #588 Add MockPath test for GetTempPath --- .../MockPathTests.cs | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/System.IO.Abstractions.TestingHelpers.Tests/MockPathTests.cs b/System.IO.Abstractions.TestingHelpers.Tests/MockPathTests.cs index d66fb281b..ff727391a 100644 --- a/System.IO.Abstractions.TestingHelpers.Tests/MockPathTests.cs +++ b/System.IO.Abstractions.TestingHelpers.Tests/MockPathTests.cs @@ -372,6 +372,48 @@ 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(),tempDirectory); + + //Act + var result = mockPath.GetTempPath(); + + //Assert + Assert.IsTrue(result.Length > 0); + } + + [Test] + public void GetTempPath_Called_WithNonNullVirtualTempDirectory_ReturnsVirtualTempDirectory() { + //Arrange + var tempDirectory = @"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() { From f9ee32f2d77be4cd2ee9f59fb10b9a0039edd1d0 Mon Sep 17 00:00:00 2001 From: Iris Classon Date: Mon, 4 May 2020 17:26:30 +0200 Subject: [PATCH 4/6] #588 Wrap path with XFS.Path() --- .../MockFileSystemTests.cs | 2 +- System.IO.Abstractions.TestingHelpers.Tests/MockPathTests.cs | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/System.IO.Abstractions.TestingHelpers.Tests/MockFileSystemTests.cs b/System.IO.Abstractions.TestingHelpers.Tests/MockFileSystemTests.cs index e45b6ce36..fec774718 100644 --- a/System.IO.Abstractions.TestingHelpers.Tests/MockFileSystemTests.cs +++ b/System.IO.Abstractions.TestingHelpers.Tests/MockFileSystemTests.cs @@ -344,7 +344,7 @@ public void MockFileSystem_DefaultState_CurrentDirectoryExists(string currentDir [Test] public void MockFileSystem_DefaultState_DefaultTempDirectoryExists() { - var tempDirectory = @"C:\temp"; + var tempDirectory = XFS.Path(@"C:\temp"); var mockFileSystem = new MockFileSystem(); var mockFileSystemOverload = new MockFileSystem(null, string.Empty); diff --git a/System.IO.Abstractions.TestingHelpers.Tests/MockPathTests.cs b/System.IO.Abstractions.TestingHelpers.Tests/MockPathTests.cs index ff727391a..3f55ddc6f 100644 --- a/System.IO.Abstractions.TestingHelpers.Tests/MockPathTests.cs +++ b/System.IO.Abstractions.TestingHelpers.Tests/MockPathTests.cs @@ -378,7 +378,7 @@ public void GetTempPath_Called_ReturnsStringLengthGreaterThanZero() [TestCase(@"C:\temp")] public void GetTempPath_Called_ReturnsStringLengthGreaterThanZero(string tempDirectory) { //Arrange - var mockPath = new MockPath(new MockFileSystem(),tempDirectory); + var mockPath = new MockPath(new MockFileSystem(), XFS.Path(tempDirectory)); //Act var result = mockPath.GetTempPath(); @@ -390,7 +390,8 @@ public void GetTempPath_Called_ReturnsStringLengthGreaterThanZero(string tempDir [Test] public void GetTempPath_Called_WithNonNullVirtualTempDirectory_ReturnsVirtualTempDirectory() { //Arrange - var tempDirectory = @"C:\temp"; + var tempDirectory = XFS.Path(@"C:\temp"); + var mockPath = new MockPath(new MockFileSystem(), tempDirectory); //Act From 47f93ed48d10c2d42ce6998e4df117f55ed7571e Mon Sep 17 00:00:00 2001 From: Iris Classon Date: Mon, 4 May 2020 17:45:04 +0200 Subject: [PATCH 5/6] #588 Only wrap non null or empty paths --- System.IO.Abstractions.TestingHelpers.Tests/MockPathTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/System.IO.Abstractions.TestingHelpers.Tests/MockPathTests.cs b/System.IO.Abstractions.TestingHelpers.Tests/MockPathTests.cs index 3f55ddc6f..7d3c7f041 100644 --- a/System.IO.Abstractions.TestingHelpers.Tests/MockPathTests.cs +++ b/System.IO.Abstractions.TestingHelpers.Tests/MockPathTests.cs @@ -378,7 +378,7 @@ public void GetTempPath_Called_ReturnsStringLengthGreaterThanZero() [TestCase(@"C:\temp")] public void GetTempPath_Called_ReturnsStringLengthGreaterThanZero(string tempDirectory) { //Arrange - var mockPath = new MockPath(new MockFileSystem(), XFS.Path(tempDirectory)); + var mockPath = new MockPath(new MockFileSystem(), string.IsNullOrEmpty(tempDirectory)? tempDirectory: XFS.Path(tempDirectory)); //Act var result = mockPath.GetTempPath(); From 13a73402d42a9cbea6fcb32d23da0e50373cd33b Mon Sep 17 00:00:00 2001 From: Iris Classon Date: Wed, 6 May 2020 21:49:37 +0200 Subject: [PATCH 6/6] #588 Renamed temp dir constant, moved temp path logic to the constructor --- System.IO.Abstractions.TestingHelpers/MockFileSystem.cs | 4 ++-- System.IO.Abstractions.TestingHelpers/MockPath.cs | 7 ++----- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/System.IO.Abstractions.TestingHelpers/MockFileSystem.cs b/System.IO.Abstractions.TestingHelpers/MockFileSystem.cs index 8d615978a..d87331bcf 100644 --- a/System.IO.Abstractions.TestingHelpers/MockFileSystem.cs +++ b/System.IO.Abstractions.TestingHelpers/MockFileSystem.cs @@ -11,7 +11,7 @@ namespace System.IO.Abstractions.TestingHelpers public class MockFileSystem : IFileSystem, IMockFileDataAccessor { private const string DEFAULT_CURRENT_DIRECTORY = @"C:\"; - private const string DEFAULT_TEMP_DIRECTORY = @"C:\temp"; + private const string TEMP_DIRECTORY = @"C:\temp"; private readonly IDictionary files; private readonly PathVerifier pathVerifier; @@ -25,7 +25,7 @@ public MockFileSystem(IDictionary files, string currentDir currentDirectory = XFS.Path(DEFAULT_CURRENT_DIRECTORY); } - var defaultTempDirectory = XFS.Path(DEFAULT_TEMP_DIRECTORY); + var defaultTempDirectory = XFS.Path(TEMP_DIRECTORY); StringOperations = new StringOperations(XFS.IsUnixPlatform()); pathVerifier = new PathVerifier(this); diff --git a/System.IO.Abstractions.TestingHelpers/MockPath.cs b/System.IO.Abstractions.TestingHelpers/MockPath.cs index 61b759eda..bc37472f4 100644 --- a/System.IO.Abstractions.TestingHelpers/MockPath.cs +++ b/System.IO.Abstractions.TestingHelpers/MockPath.cs @@ -18,7 +18,7 @@ public MockPath(IMockFileDataAccessor mockFileDataAccessor) : this(mockFileDataA public MockPath(IMockFileDataAccessor mockFileDataAccessor,string defaultTempDirectory) : base(mockFileDataAccessor?.FileSystem) { this.mockFileDataAccessor = mockFileDataAccessor ?? throw new ArgumentNullException(nameof(mockFileDataAccessor)); - this.defaultTempDirectory = defaultTempDirectory; + this.defaultTempDirectory = !string.IsNullOrEmpty(defaultTempDirectory) ? defaultTempDirectory : base.GetTempPath(); } public override string GetFullPath(string path) @@ -141,9 +141,6 @@ public override string GetTempFileName() return fullPath; } - public override string GetTempPath() - { - return !string.IsNullOrEmpty(defaultTempDirectory) ? defaultTempDirectory : base.GetTempPath(); - } + public override string GetTempPath() => defaultTempDirectory; } } \ No newline at end of file