Skip to content

Commit

Permalink
feat: added support for GetUnixFileMode and SetUnixFileMode (#1160)
Browse files Browse the repository at this point in the history
* only overload with string path is added, overload with SafeFileHandle fileHandle is not added
  • Loading branch information
tomwis authored Nov 7, 2024
1 parent 3df422f commit 15320f7
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 3 deletions.
20 changes: 18 additions & 2 deletions src/TestableIO.System.IO.Abstractions.TestingHelpers/MockFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,15 @@ public override DateTime GetLastWriteTimeUtc(SafeFileHandle fileHandle)
/// <inheritdoc />
public override UnixFileMode GetUnixFileMode(string path)
{
throw CommonExceptions.NotImplemented();
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path");

if (!mockFileDataAccessor.FileExists(path))
{
throw CommonExceptions.FileNotFound(path);
}

var mockFileData = mockFileDataAccessor.GetFile(path);
return mockFileData.UnixMode;
}
#endif

Expand Down Expand Up @@ -954,7 +962,15 @@ public override void SetLastWriteTimeUtc(SafeFileHandle fileHandle, DateTime las
/// <inheritdoc />
public override void SetUnixFileMode(string path, UnixFileMode mode)
{
throw CommonExceptions.NotImplemented();
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path");

if (!mockFileDataAccessor.FileExists(path))
{
throw CommonExceptions.FileNotFound(path);
}

var mockFileData = mockFileDataAccessor.GetFile(path);
mockFileData.UnixMode = mode;
}
#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,16 @@ public FileSecurity AccessControl
/// Gets or sets the File sharing mode for this file, this allows you to lock a file for reading or writing.
/// </summary>
public FileShare AllowedFileShare { get; set; } = FileShare.ReadWrite | FileShare.Delete;

#if FEATURE_UNIX_FILE_MODE
/// <summary>
/// Gets or sets the Unix file mode (permissions) for this file.
/// This allows you to configure the read, write and execute access for user, group and other.
/// </summary>
public UnixFileMode UnixMode { get; set; } = UnixFileMode.UserRead | UnixFileMode.GroupRead |
UnixFileMode.OtherRead | UnixFileMode.UserWrite;
#endif

/// <summary>
/// Checks whether the file is accessible for this type of FileAccess.
/// MockFileData can be configured to have FileShare.None, which indicates it is locked by a 'different process'.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#if FEATURE_UNIX_FILE_MODE
using System.Runtime.Versioning;

namespace System.IO.Abstractions.TestingHelpers.Tests
{
using Collections.Generic;

using NUnit.Framework;

using XFS = MockUnixSupport;

[UnsupportedOSPlatform("windows")]
[UnixOnly("This feature is not supported on Windows.")]
public class MockFileGetUnixFileModeTests
{
[Test]
public void MockFile_GetUnixFileMode_ShouldReturnDefaultAccessMode()
{
// Arrange
var expected = UnixFileMode.UserRead |
UnixFileMode.GroupRead |
UnixFileMode.OtherRead |
UnixFileMode.UserWrite;

var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ XFS.Path(@"C:\something\some.txt"), new MockFileData("Demo text content") }
});

// Act
var result = fileSystem.File.GetUnixFileMode(XFS.Path(@"C:\something\some.txt"));

// Assert
Assert.That(result, Is.EqualTo(expected));
}

[Test]
public void MockFile_GetUnixFileMode_ShouldReturnSpecifiedAccessMode([Values] UnixFileMode unixFileMode)
{
// Arrange
var mockFileData = new MockFileData("Demo text content")
{
UnixMode = unixFileMode
};

var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ XFS.Path(@"C:\something\some.txt"), mockFileData }
});

// Act
var result = fileSystem.File.GetUnixFileMode(XFS.Path(@"C:\something\some.txt"));

// Assert
Assert.That(result, Is.EqualTo(unixFileMode));
}
}
}
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#if FEATURE_UNIX_FILE_MODE
using System.Runtime.Versioning;

namespace System.IO.Abstractions.TestingHelpers.Tests
{
using Collections.Generic;

using NUnit.Framework;

using XFS = MockUnixSupport;

[UnsupportedOSPlatform("windows")]
[UnixOnly("This feature is not supported on Windows.")]
public class MockFileSetUnixFileModeTests
{
[Test]
public void MockFile_SetUnixFileMode_ShouldSetSpecifiedAccessMode([Values] UnixFileMode unixFileMode)
{
// Arrange
var mockFileData = new MockFileData("Demo text content");
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ XFS.Path(@"C:\something\some.txt"), mockFileData }
});

// Act
fileSystem.File.SetUnixFileMode(XFS.Path(@"C:\something\some.txt"), unixFileMode);

// Assert
Assert.That(mockFileData.UnixMode, Is.EqualTo(unixFileMode));
}

[TestCase(UnixFileMode.UserRead | UnixFileMode.GroupRead | UnixFileMode.OtherRead)]
[TestCase(UnixFileMode.UserRead | UnixFileMode.UserWrite | UnixFileMode.UserExecute)]
[TestCase(UnixFileMode.UserExecute | UnixFileMode.OtherWrite | UnixFileMode.GroupRead)]
public void MockFile_SetUnixFileMode_ShouldSetSpecifiedAccessModeFlags(UnixFileMode unixFileMode)
{
// Arrange
var mockFileData = new MockFileData("Demo text content");
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ XFS.Path(@"C:\something\some.txt"), mockFileData }
});

// Act
fileSystem.File.SetUnixFileMode(XFS.Path(@"C:\something\some.txt"), unixFileMode);

// Assert
Assert.That(mockFileData.UnixMode, Is.EqualTo(unixFileMode));
}
}
}
#endif
2 changes: 1 addition & 1 deletion version.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://raw.githubusercontent.com/AArnott/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json",
"version": "21.0",
"version": "21.1",
"assemblyVersion": {
"precision": "major"
},
Expand Down

0 comments on commit 15320f7

Please sign in to comment.