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
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
<PackageVersion Include="PolyShim" Version="2.8.2" />
<PackageVersion Include="xunit" Version="2.9.2" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.2" />
<PackageVersion Include="Xunit.SkippableFact" Version="1.5.61" />
</ItemGroup>
</Project>
39 changes: 39 additions & 0 deletions PowerKit.Tests/DirectoryExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.IO;
using FluentAssertions;
using PowerKit;
Expand All @@ -8,6 +9,44 @@ namespace PowerKit.Tests;

public class DirectoryExtensionsTests
{
[Fact]
public void CheckWriteAccess_Test()
{
// Arrange
using var tempDir = TempDirectory.Create();

// Act
var result = Directory.CheckWriteAccess(tempDir.Path);

// Assert
result.Should().BeTrue();
}

[SkippableFact]
public void CheckWriteAccess_ReadOnly_Test()
{
// FileAttributes.ReadOnly removes write bits on Unix but has no effect on directories on Windows
Skip.If(OperatingSystem.IsWindows() || Environment.IsPrivilegedProcess);

// Arrange
using var tempDir = TempDirectory.Create();
var dirInfo = new DirectoryInfo(tempDir.Path);
dirInfo.Attributes |= FileAttributes.ReadOnly;

try
{
// Act
var result = Directory.CheckWriteAccess(tempDir.Path);

// Assert
result.Should().BeFalse();
}
finally
{
dirInfo.Attributes &= ~FileAttributes.ReadOnly;
}
}

[Fact]
public void Reset_Test()
{
Expand Down
53 changes: 53 additions & 0 deletions PowerKit.Tests/FileExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.IO;
using System.Threading.Tasks;
using FluentAssertions;
Expand All @@ -9,6 +10,58 @@ namespace PowerKit.Tests;

public class FileExtensionsTests
{
[Fact]
public void CheckWriteAccess_Test()
{
// Arrange
using var tempFile = TempFile.Create();

// Act
var result = File.CheckWriteAccess(tempFile.Path);

// Assert
result.Should().BeTrue();
}

[SkippableFact]
public void CheckWriteAccess_ReadOnly_Test()
{
// Privileged processes can write to read-only files on Unix
Skip.If(Environment.IsPrivilegedProcess);

// Arrange
using var tempFile = TempFile.Create();
File.SetAttributes(tempFile.Path, File.GetAttributes(tempFile.Path) | FileAttributes.ReadOnly);

try
{
// Act
var result = File.CheckWriteAccess(tempFile.Path);

// Assert
result.Should().BeFalse();
Comment thread
Tyrrrz marked this conversation as resolved.
}
finally
{
File.SetAttributes(tempFile.Path, File.GetAttributes(tempFile.Path) & ~FileAttributes.ReadOnly);
}
}

[Fact]
public void CheckWriteAccess_NonExistent_Test()
{
// Arrange
using var tempDir = TempDirectory.Create();
var path = Path.Combine(tempDir.Path, "new-file.txt");

// Act
var result = File.CheckWriteAccess(path);

// Assert
result.Should().BeTrue();
File.Exists(path).Should().BeFalse();
}

[Fact]
public void TryDelete_Test()
{
Expand Down
1 change: 1 addition & 0 deletions PowerKit.Tests/PowerKit.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio" />
<PackageReference Include="Xunit.SkippableFact" />
</ItemGroup>

</Project>
Expand Down
23 changes: 23 additions & 0 deletions PowerKit/Extensions/DirectoryExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.IO;

namespace PowerKit.Extensions;
Expand Down Expand Up @@ -36,5 +37,27 @@ public static bool TryDelete(string path, bool recursive = false)
return false;
}
}

/// <summary>
/// Checks if it's possible to write to the specified directory.
/// </summary>
public static bool CheckWriteAccess(string path)
{
var tempFilePath = Path.Combine(path, Guid.NewGuid().ToString());

try
{
using var tempFile = File.Create(tempFilePath);
return true;
}
catch (UnauthorizedAccessException)
{
return false;
}
finally
{
File.TryDelete(tempFilePath);
}
}
}
}
25 changes: 25 additions & 0 deletions PowerKit/Extensions/FileExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,31 @@ public static bool TryDelete(string path)
}
}

/// <summary>
/// Checks if it's possible to write to the specified file.
/// </summary>
public static bool CheckWriteAccess(string path)
{
var wasExisting = File.Exists(path);

try
{
using var stream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite);
Comment thread
Tyrrrz marked this conversation as resolved.
return true;
}
catch (UnauthorizedAccessException)
{
return false;
}
finally
{
if (!wasExisting)
{
File.TryDelete(path);
Comment thread
Tyrrrz marked this conversation as resolved.
}
}
}

/// <summary>
/// Creates a file at the specified path and fills it with zeroes.
/// </summary>
Expand Down