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
15 changes: 15 additions & 0 deletions PowerKit.Tests/Extensions/DirectoryExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,21 @@ public void Copy_UnixFileMode_Test()
.Be(File.GetUnixFileMode(sourceFilePath));
}

[Fact]
public void CreateForFile_Test()
{
// Arrange
using var tempDir = TempDirectory.Create();
var filePath = Path.Combine(tempDir.Path, "sub", "nested", "file.txt");

// Act
Directory.CreateForFile(filePath);

// Assert
Directory.Exists(Path.Combine(tempDir.Path, "sub", "nested")).Should().BeTrue();
Comment thread
Tyrrrz marked this conversation as resolved.
File.Exists(filePath).Should().BeFalse();
}

[Fact]
public void Reset_Test()
{
Expand Down
11 changes: 11 additions & 0 deletions PowerKit/Extensions/DirectoryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,17 @@ var sourceFilePath in Directory.GetFiles(
}
}

/// <summary>
/// Creates the directory for the specified file path, including all intermediate directories.
/// Does nothing if the directory already exists or if the path has no directory component.
/// </summary>
public static void CreateForFile(string filePath)
{
var dirPath = Path.GetDirectoryName(filePath);
if (!string.IsNullOrEmpty(dirPath))
Directory.CreateDirectory(dirPath);
}

/// <summary>
/// Deletes the directory and all its contents, then recreates it as an empty directory.
/// </summary>
Expand Down