Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions PolyShim.Tests/Net70/DirectoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,48 @@ 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);
}
}
Comment thread
Tyrrrz marked this conversation as resolved.
}
22 changes: 22 additions & 0 deletions PolyShim/Net70/Directory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,28 @@ 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)
{
var tempPath = Path.GetTempPath();

for (var attempt = 0; attempt < 10; attempt++)
{
var path = Path.Combine(tempPath, prefix + Path.GetRandomFileName());
Comment thread
Tyrrrz marked this conversation as resolved.

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.");
}
}
}

Expand Down
5 changes: 3 additions & 2 deletions Signatures.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Signatures

- **Total:** 600
- **Total:** 601
- **Types:** 119
- **Members:** 481
- **Members:** 482

___

Expand Down Expand Up @@ -155,6 +155,7 @@ ___
- [`void Deconstruct(out object, out object?)`](https://learn.microsoft.com/dotnet/api/system.collections.dictionaryentry.deconstruct) <sup><sub>.NET Core 2.0</sub></sup>
- `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)) <sup><sub>.NET 7.0</sub></sup>
- [`static DirectoryInfo CreateTempSubdirectory(string?)`](https://learn.microsoft.com/dotnet/api/system.io.directory.createtempsubdirectory#system-io-directory-createtempsubdirectory(system-string)) <sup><sub>.NET 7.0</sub></sup>
- `DisallowNullAttribute`
- [**[class]**](https://learn.microsoft.com/dotnet/api/system.diagnostics.codeanalysis.disallownullattribute) <sup><sub>.NET Core 3.0</sub></sup>
- `DoesNotReturnAttribute`
Expand Down
Loading