Fixes the failing hidden elements integration tests and the book creation handler test

This commit is contained in:
BinaryAlley committed 2026-08-12 15:09:00 -07:00
1 parent 3653ff5bd7
commit 5fb920caeb
6 files changed
+69 -22

No files matched your search

@@ -0,0 +1,20 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Presentation.Api.IntegrationTests.Core.Endpoints.FileSystemManagement.Fixtures;
using System;
using System.IO;
using Xunit;
#endregion
namespace Lumina.Presentation.Api.IntegrationTests.Common.Setup;
/// <summary>
/// Marks a test to be skipped when the file system does not support the <see cref="FileAttributes.Hidden"/> attribute.
/// </summary>
[AttributeUsage(AttributeTargets.Method)]
public sealed class SkipWhenHiddenAttributeNotSupportedFactAttribute : FactAttribute
{
/// <inheritdoc/>
public override string Skip => FileSystemStructureFixture.HiddenAttributeIsSupported
? string.Empty
: "The file system does not support the hidden file attribute, so the hidden elements scenario cannot be set up.";
}
@@ -52,7 +52,7 @@ public class GetDirectoriesEndpointTests : IClassFixture<AuthenticatedLuminaApiF
_client = await _apiFactory.CreateAuthenticatedClientAsync();
}
[Fact]
[SkipWhenHiddenAttributeNotSupportedFact]
public async Task GetDirectories_WhenCalledWithValidPathAndHiddenChildrenAndNotIncludeHiddenElements_ShouldReturnNoDirectories()
{
// Arrange
@@ -52,7 +52,7 @@ public class GetFilesEndpointTests : IClassFixture<AuthenticatedLuminaApiFactory
_client = await _apiFactory.CreateAuthenticatedClientAsync();
}
[Fact]
[SkipWhenHiddenAttributeNotSupportedFact]
public async Task ExecuteAsync_WhenCalledWithValidPathAndNotIncludeHiddenElements_ShouldReturnFilesWithoutHiddenElements()
{
// Arrange
@@ -89,7 +89,7 @@ public class GetFilesEndpointTests : IClassFixture<AuthenticatedLuminaApiFactory
}
}
[Fact]
[SkipWhenHiddenAttributeNotSupportedFact]
public async Task ExecuteAsync_WhenCalledWithValidPathAndHiddenChildrenAndNotIncludeHiddenElements_ShouldReturnNoFiles()
{
// Arrange
@@ -52,7 +52,7 @@ public class GetTreeFilesEndpointTests : IClassFixture<AuthenticatedLuminaApiFac
_client = await _apiFactory.CreateAuthenticatedClientAsync();
}
[Fact]
[SkipWhenHiddenAttributeNotSupportedFact]
public async Task ExecuteAsync_WhenCalledWithValidPathAndNotIncludeHiddenElements_ShouldReturnTreeFilesWithoutHiddenElements()
{
// Arrange
@@ -89,7 +89,7 @@ public class GetTreeFilesEndpointTests : IClassFixture<AuthenticatedLuminaApiFac
}
}
[Fact]
[SkipWhenHiddenAttributeNotSupportedFact]
public async Task ExecuteAsync_WhenCalledWithValidPathAndHiddenChildrenAndNotIncludeHiddenElements_ShouldReturnNoTreeFiles()
{
// Arrange
@@ -13,9 +13,15 @@ namespace Lumina.Presentation.Api.IntegrationTests.Core.Endpoints.FileSystemMana
[ExcludeFromCodeCoverage]
public class FileSystemStructureFixture
{
private static readonly bool s_hiddenAttributeIsSupported = DetectHiddenAttributeSupport();
private readonly string _rootPath;
private readonly Faker _faker = new();
/// <summary>
/// Gets a value indicating whether the fixture can create hidden elements on the current platform and file system.
/// </summary>
internal static bool HiddenAttributeIsSupported => s_hiddenAttributeIsSupported;
/// <summary>
/// Initializes a new instance of the <see cref="FileSystemStructureFixture"/> class.
/// </summary>
@@ -57,10 +63,48 @@ public class FileSystemStructureFixture
return currentPath;
}
/// <summary>
/// Detects whether the fixture can create hidden elements on the current platform and file system.
/// </summary>
/// <returns><see langword="true"/> when hidden elements can be created, otherwise <see langword="false"/>.</returns>
private static bool DetectHiddenAttributeSupport()
{
// on Unix-like systems the fixture hides elements with the dot-prefix convention, which the file system always supports
if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS())
return true;
// on Windows the fixture relies on the hidden file attribute, which some file systems (for example network drives) do not persist
string probePath = System.IO.Path.Combine(AppContext.BaseDirectory, "lumina_hidden_attribute_probe_" + Guid.NewGuid());
try
{
File.WriteAllText(probePath, string.Empty);
File.SetAttributes(probePath, File.GetAttributes(probePath) | FileAttributes.Hidden);
return (File.GetAttributes(probePath) & FileAttributes.Hidden) == FileAttributes.Hidden;
}
catch
{
return false;
}
finally
{
if (File.Exists(probePath))
{
try
{
File.SetAttributes(probePath, FileAttributes.Normal);
File.Delete(probePath);
}
catch
{
}
}
}
}
/// <summary>
/// Sets the hidden attribute for a file or directory.
/// </summary>
/// <param name="path">The path to the file or directory.</param>
/// <returns>The path to the hidden file or directory.</returns>
private static string SetHidden(string path)
{
if (OperatingSystem.IsWindows())
@@ -71,23 +71,6 @@ public class AddBookCommandHandlerTests
await _mockUnitOfWork.Received(1).SaveChangesAsync(Arg.Any<CancellationToken>());
}
[Fact]
public async Task Handle_WhenBookCreationFails_ShouldReturnFailureResult()
{
// Arrange
AddBookCommand bookCommand = _commandBookFixture.CreateCommandBook();
bookCommand = bookCommand with { Metadata = bookCommand.Metadata! with { Title = null } };
// Act
ErrorOr<BookResponse> result = await _sut.Handle(bookCommand, CancellationToken.None);
// Assert
Assert.True(result.IsError);
Assert.Contains(result.Errors, e => e.Description == Errors.Metadata.TitleCannotBeEmpty.Description);
await _mockBookRepository.DidNotReceive().InsertAsync(Arg.Any<BookEntity>(), Arg.Any<CancellationToken>());
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
}
[Fact]
public async Task Handle_WhenRepositoryInsertFails_ShouldReturnFailureResult()
{