diff --git a/global.json b/global.json index edbc8a2e4..860b52da4 100644 --- a/global.json +++ b/global.json @@ -1,6 +1,6 @@ { "sdk": { - "version": "7.0.202", + "version": "7.0.203", "rollForward": "latestMinor" } } diff --git a/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockDirectory.cs b/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockDirectory.cs index 490030718..a7d476955 100644 --- a/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockDirectory.cs +++ b/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockDirectory.cs @@ -110,7 +110,17 @@ public override IFileSystemInfo CreateSymbolicLink(string path, string pathToTar /// public override IDirectoryInfo CreateTempSubdirectory(string prefix = null) { - throw CommonExceptions.NotImplemented(); + prefix ??= ""; + string potentialTempDirectory; + + // Perform directory name generation in a loop, just in case the randomly generated name already exists. + do + { + var randomDir = $"{prefix}{Path.GetRandomFileName()}"; + potentialTempDirectory = Path.Combine(Path.GetTempPath(), randomDir); + } while (Exists(potentialTempDirectory)); + + return CreateDirectoryInternal(potentialTempDirectory); } #endif diff --git a/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs b/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs index 543553fef..6992b0a8d 100644 --- a/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs +++ b/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockDirectoryTests.cs @@ -813,6 +813,37 @@ public void MockDirectory_CreateDirectory_ShouldSucceedIfTryingToCreateUNCPathSh Assert.IsTrue(fileSystem.Directory.Exists(@"\\server\share\")); } +#if FEATURE_CREATE_TEMP_SUBDIRECTORY + [Test] + public void MockDirectory_CreateTempSubdirectory_ShouldCreateSubdirectoryInTempDirectory() + { + // Arrange + var fileSystem = new MockFileSystem(); + + // Act + var result = fileSystem.Directory.CreateTempSubdirectory(); + + // Assert + Assert.IsTrue(fileSystem.Directory.Exists(result.FullName)); + Assert.IsTrue(result.FullName.Contains(Path.GetTempPath())); + } + + [Test] + public void MockDirectory_CreateTempSubdirectoryWithPrefix_ShouldCreateDirectoryWithGivenPrefixInTempDirectory() + { + // Arrange + var fileSystem = new MockFileSystem(); + + // Act + var result = fileSystem.Directory.CreateTempSubdirectory("foo-"); + + // Assert + Assert.IsTrue(fileSystem.Directory.Exists(result.FullName)); + Assert.IsTrue(Path.GetFileName(result.FullName).StartsWith("foo-")); + Assert.IsTrue(result.FullName.Contains(Path.GetTempPath())); + } +#endif + [Test] public void MockDirectory_Delete_ShouldDeleteDirectory() {