From d54d914375a6963f22929d204bb3e8fce408690a Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 2 Oct 2026 22:12:17 +0000 Subject: [PATCH 1/2] Add Directory.CreateTempSubdirectory polyfill (.NET 7.0) Co-authored-by: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com> --- PolyShim.Tests/Net70/DirectoryTests.cs | 56 ++++++++++++++++++++++++++ PolyShim/Net70/Directory.cs | 36 +++++++++++++++++ Signatures.md | 5 ++- 3 files changed, 95 insertions(+), 2 deletions(-) diff --git a/PolyShim.Tests/Net70/DirectoryTests.cs b/PolyShim.Tests/Net70/DirectoryTests.cs index c04d4e7..3267adb 100644 --- a/PolyShim.Tests/Net70/DirectoryTests.cs +++ b/PolyShim.Tests/Net70/DirectoryTests.cs @@ -36,4 +36,60 @@ public void CreateDirectory_UnixFileMode_Test() Directory.Delete(tempDirPath); } } + + [Fact] + public void CreateTempSubdirectory_Test() + { + // Act + var info = Directory.CreateTempSubdirectory(); + + try + { + // Assert + info.Should().NotBeNull(); + info.Exists.Should().BeTrue(); + info.FullName.Should() + .StartWith(Path.GetTempPath().TrimEnd(Path.DirectorySeparatorChar)); + } + finally + { + if (Directory.Exists(info.FullName)) + Directory.Delete(info.FullName); + } + } + + [Fact] + public void CreateTempSubdirectory_Prefix_Test() + { + // Arrange + const string prefix = "polyshim-"; + + // Act + var info = Directory.CreateTempSubdirectory(prefix); + + try + { + // Assert + info.Should().NotBeNull(); + info.Exists.Should().BeTrue(); + info.Name.Should().StartWith(prefix); + } + finally + { + if (Directory.Exists(info.FullName)) + Directory.Delete(info.FullName); + } + } + + [Fact] + public void CreateTempSubdirectory_PrefixWithDirectorySeparator_Test() + { + // Act & assert + Assert + .Throws(() => + Directory.CreateTempSubdirectory("foo" + Path.DirectorySeparatorChar + "bar") + ) + .ParamName.Should() + .Be("prefix"); + } } diff --git a/PolyShim/Net70/Directory.cs b/PolyShim/Net70/Directory.cs index 327f73b..e2d2f99 100644 --- a/PolyShim/Net70/Directory.cs +++ b/PolyShim/Net70/Directory.cs @@ -46,6 +46,42 @@ public static DirectoryInfo CreateDirectory(string path, UnixFileMode unixCreate return info; } + + // https://learn.microsoft.com/dotnet/api/system.io.directory.createtempsubdirectory#system-io-directory-createtempsubdirectory(system-string) + public static DirectoryInfo CreateTempSubdirectory(string? prefix = null) + { + if ( + prefix is not null + && ( + prefix.IndexOf(Path.DirectorySeparatorChar) >= 0 + || prefix.IndexOf(Path.AltDirectorySeparatorChar) >= 0 + ) + ) + { + throw new ArgumentException( + "Prefix cannot contain directory separator characters.", + nameof(prefix) + ); + } + + var tempPath = Path.GetTempPath(); + + for (var attempt = 0; attempt < 10; attempt++) + { + var path = Path.Combine(tempPath, prefix + Path.GetRandomFileName()); + + try + { + return Directory.CreateDirectory(path); + } + catch (IOException) when (Directory.Exists(path)) + { + // Collision with an existing directory, try again with a different name + } + } + + throw new IOException("Failed to create a unique temporary subdirectory."); + } } } diff --git a/Signatures.md b/Signatures.md index 954ed0c..92eafb7 100644 --- a/Signatures.md +++ b/Signatures.md @@ -1,8 +1,8 @@ # Signatures -- **Total:** 593 +- **Total:** 594 - **Types:** 119 -- **Members:** 474 +- **Members:** 475 ___ @@ -155,6 +155,7 @@ ___ - [`void Deconstruct(out object, out object?)`](https://learn.microsoft.com/dotnet/api/system.collections.dictionaryentry.deconstruct) .NET Core 2.0 - `Directory` - [`static DirectoryInfo CreateDirectory(string, UnixFileMode)`](https://learn.microsoft.com/dotnet/api/system.io.directory.createdirectory#system-io-directory-createdirectory(system-string-system-io-unixfilemode)) .NET 7.0 + - [`static DirectoryInfo CreateTempSubdirectory(string?)`](https://learn.microsoft.com/dotnet/api/system.io.directory.createtempsubdirectory#system-io-directory-createtempsubdirectory(system-string)) .NET 7.0 - `DisallowNullAttribute` - [**[class]**](https://learn.microsoft.com/dotnet/api/system.diagnostics.codeanalysis.disallownullattribute) .NET Core 3.0 - `DoesNotReturnAttribute` From 54d6115409ebc0705cd5ef3dcf64c58e26c28ef8 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 3 Oct 2026 10:55:16 +0000 Subject: [PATCH 2/2] Remove early argument check and unneeded test per review feedback Co-authored-by: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com> --- PolyShim.Tests/Net70/DirectoryTests.cs | 12 ------------ PolyShim/Net70/Directory.cs | 14 -------------- 2 files changed, 26 deletions(-) diff --git a/PolyShim.Tests/Net70/DirectoryTests.cs b/PolyShim.Tests/Net70/DirectoryTests.cs index 3267adb..3469b10 100644 --- a/PolyShim.Tests/Net70/DirectoryTests.cs +++ b/PolyShim.Tests/Net70/DirectoryTests.cs @@ -80,16 +80,4 @@ public void CreateTempSubdirectory_Prefix_Test() Directory.Delete(info.FullName); } } - - [Fact] - public void CreateTempSubdirectory_PrefixWithDirectorySeparator_Test() - { - // Act & assert - Assert - .Throws(() => - Directory.CreateTempSubdirectory("foo" + Path.DirectorySeparatorChar + "bar") - ) - .ParamName.Should() - .Be("prefix"); - } } diff --git a/PolyShim/Net70/Directory.cs b/PolyShim/Net70/Directory.cs index e2d2f99..8b5dc0c 100644 --- a/PolyShim/Net70/Directory.cs +++ b/PolyShim/Net70/Directory.cs @@ -50,20 +50,6 @@ public static DirectoryInfo CreateDirectory(string path, UnixFileMode unixCreate // https://learn.microsoft.com/dotnet/api/system.io.directory.createtempsubdirectory#system-io-directory-createtempsubdirectory(system-string) public static DirectoryInfo CreateTempSubdirectory(string? prefix = null) { - if ( - prefix is not null - && ( - prefix.IndexOf(Path.DirectorySeparatorChar) >= 0 - || prefix.IndexOf(Path.AltDirectorySeparatorChar) >= 0 - ) - ) - { - throw new ArgumentException( - "Prefix cannot contain directory separator characters.", - nameof(prefix) - ); - } - var tempPath = Path.GetTempPath(); for (var attempt = 0; attempt < 10; attempt++)