Merge pull request #51 from BinaryAlley/feature/add-multiplatform-testing-on-github-actions

Adds multiplatform testing on GitHub Actions
This commit is contained in:
BinaryAlley authored and GitHub committed 2024-11-07 10:00:53 +02:00
commit b4ed06461d
7 files changed
+95 -49

No files matched your search

+5 -1
View File
@@ -8,7 +8,11 @@ on:
jobs:
test:
runs-on: ubuntu-latest
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macOS-latest]
max-parallel: 1
environment: test
steps:
- uses: actions/checkout@v2
@@ -2,8 +2,6 @@
using Lumina.Domain.Common.Enums.FileSystem;
using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.Strategies.Platform;
using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.ValueObjects;
using Mono.Unix;
using Mono.Unix.Native;
using System;
using System.IO;
using System.Runtime.Versioning;
@@ -40,7 +38,7 @@ internal class FileSystemPermissionsService : IFileSystemPermissionsService // T
{
PlatformType platformType = _platformContextManager.GetCurrentContext().Platform;
if (platformType == PlatformType.Unix)
return CanAccessPathLinux(path.Path, accessMode);
return CanAccessPathUnix(path.Path, accessMode);
else if (platformType == PlatformType.Windows)
#pragma warning disable CA1416 // Validate platform compatibility
return CanAccessPathWindows(path.Path, accessMode, isFile);
@@ -50,51 +48,99 @@ internal class FileSystemPermissionsService : IFileSystemPermissionsService // T
}
/// <summary>
/// Checks if a the current user has access permissions on Linux for <paramref name="path"/>, with <paramref name="accessMode"/>.
/// Checks if a the current user has access permissions on Unix for <paramref name="path"/>, with <paramref name="accessMode"/>.
/// </summary>
/// <param name="path">The path for which to check the access.</param>
/// <param name="accessMode">The access mode in which to check that path access.</param>
/// <returns><see langword="true"/> if the current user has rights for the specified path and acccess mode, <see langword="false"/> otherwise.</returns>
/// <exception cref="ArgumentOutOfRangeException">Thrown when trying to check access for a mode that is not supported</exception>
private static bool CanAccessPathLinux(string path, FileAccessMode accessMode)
/// <exception cref="ArgumentOutOfRangeException">Thrown when trying to check access for a mode that is not supported.</exception>
private static bool CanAccessPathUnix(string path, FileAccessMode accessMode)
{
AccessModes modes;
UnixFileSystemInfo fileInfo = new UnixFileInfo(path);
switch (accessMode)
try
{
case FileAccessMode.ReadProperties:
// we would typically need execute permissions on a directory, to list its contents or view file properties
if (fileInfo.FileType == FileTypes.Directory)
return fileInfo.CanAccess(AccessModes.X_OK);
// for files, just verifying existence might be sufficient for reading properties
return fileInfo.Exists;
case FileAccessMode.ReadContents:
return fileInfo.CanAccess(AccessModes.R_OK);
case FileAccessMode.Write:
modes = AccessModes.W_OK;
break;
case FileAccessMode.Execute:
modes = AccessModes.X_OK;
break;
case FileAccessMode.ListDirectory:
modes = AccessModes.F_OK; // check existence, not an exact match but the closest
break;
case FileAccessMode.Delete:
if (fileInfo.FileType == FileTypes.Directory)
return fileInfo.CanAccess(AccessModes.W_OK | AccessModes.X_OK); // to delete a directory, we need write and execute permissions on the directory itself
else
{
// to delete a file, we need write permission on the parent directory
string? parentDirectoryPath = Path.GetDirectoryName(path);
if (string.IsNullOrEmpty(parentDirectoryPath))
return false; // if path is a root directory or has no parent, deletion is not possible
UnixDirectoryInfo parentDirectoryInfo = new(parentDirectoryPath);
return parentDirectoryInfo.CanAccess(AccessModes.W_OK);
}
default:
throw new ArgumentOutOfRangeException(nameof(accessMode), "Unknown FileAccessMode");
switch (accessMode)
{
case FileAccessMode.ReadProperties:
// for properties, we only need to check if the path exists
return File.Exists(path) || Directory.Exists(path);
case FileAccessMode.ReadContents:
// attempt to open the file for reading to verify read access
if (File.Exists(path))
{
FileStream fileStream = File.OpenRead(path);
fileStream.Dispose();
return true;
}
return false;
case FileAccessMode.Write:
// attempt to open the file for writing or verify directory exists
if (File.Exists(path))
{
FileStream fileStream = File.OpenWrite(path);
fileStream.Dispose();
return true;
}
return Directory.Exists(path);
case FileAccessMode.Execute:
// check Unix execute permission bit
FileInfo fileInfo = new(path);
return (fileInfo.UnixFileMode & UnixFileMode.UserExecute) != 0;
case FileAccessMode.ListDirectory:
// attempt to list directory contents to verify access
if (Directory.Exists(path))
{
string[] _ = Directory.GetFiles(path);
return true;
}
return false;
case FileAccessMode.Delete:
if (File.Exists(path))
{
// for files, check if parent directory is writable
string? directoryPath = Path.GetDirectoryName(path);
return directoryPath != null && HasWriteAccessToDirectory(directoryPath);
}
// for directories, check if the directory itself is writable
return Directory.Exists(path) && HasWriteAccessToDirectory(path);
default:
throw new ArgumentOutOfRangeException(nameof(accessMode), "Unknown FileAccessMode");
}
}
catch (UnauthorizedAccessException)
{
return false; // access denied by the operating system
}
catch (IOException)
{
return false; // IO operation failed, indicating no access
}
}
/// <summary>
/// Verifies write access to a directory by attempting to create and delete a temporary file.
/// </summary>
/// <param name="path">The directory path to check for write access.</param>
/// <returns><see langword="true"/> if write access is granted, <see langword="false"/> otherwise.</returns>
private static bool HasWriteAccessToDirectory(string path)
{
try
{
// create a unique temporary filename
string temporaryFilePath = Path.Combine(path, string.Format(".test_{0}", Guid.NewGuid()));
// attempt to create and immediately delete a temporary file
FileStream testStream = File.Create(temporaryFilePath);
testStream.Dispose();
File.Delete(temporaryFilePath);
return true;
}
catch (UnauthorizedAccessException)
{
return false;
}
catch (IOException)
{
return false;
}
return new UnixDirectoryInfo(path).CanAccess(modes);
}
/// <summary>
@@ -34,7 +34,7 @@ public class PlatformContextManager : IPlatformContextManager
{
// set a default context if none is set
if (_currentPlatformContext is null)
if (_operatingSystemInfo.IsOSPlatform(OSPlatform.Linux))
if (_operatingSystemInfo.IsOSPlatform(OSPlatform.Linux) || _operatingSystemInfo.IsOSPlatform(OSPlatform.OSX))
SetCurrentPlatform(PlatformType.Unix);
else
SetCurrentPlatform(PlatformType.Windows);
-1
View File
@@ -18,7 +18,6 @@
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.5" />
<PackageReference Include="System.IO.Abstractions" Version="21.0.29" />
<PackageReference Include="System.IO.FileSystem.AccessControl" Version="5.0.0" />
<PackageReference Include="Mono.Posix.NETStandard" Version="5.20.1-preview" />
</ItemGroup>
</Project>
-1
View File
@@ -67,7 +67,6 @@ public class Program
.ReadFrom.Services(services)
.Enrich.FromLogContext();
});
string? aa = builder.Configuration.GetValue<string>("LOG_PATH");
WebApplication app = builder.Build();
app.UseSerilogRequestLogging();
@@ -6,7 +6,7 @@
<BaseOutputPath>$(SolutionDir)..\bin\web\</BaseOutputPath>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<UserSecretsId>6413036f-92d7-45d8-a8B1-205200089047</UserSecretsId>
<UserSecretsId>10a94da9-18ed-4c70-a9da-b461e08958c4</UserSecretsId>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<ContainerRepository>lumina-web</ContainerRepository>
<DockerfileTag>lumina-web</DockerfileTag>
@@ -35,7 +35,5 @@
<!--disable warnings generated by Mediator source generator-->
<PropertyGroup>
<NoWarn>$(NoWarn);TS6385</NoWarn>
<UserSecretsId>10a94da9-18ed-4c70-a9da-b461e08958c4</UserSecretsId>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>
</Project>
@@ -35,7 +35,7 @@ public class FileSystemBrowserViewComponent : ViewComponent
{
return View(new
{
Path = RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? @"/" : @"C:\Users\",
Path = RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? @"/" : @"C:\Users\",
ServerBasePath = $"{_serverConfigurationModel.BaseAddress}:{_serverConfigurationModel.Port}/api/v{_serverConfigurationModel.ApiVersion}/",
ClientBasePath = "http://localhost:5012/", // TODO: take from appsettings or environment
ViewMode = FileSystemViewMode.List,