Centralizes and standardizes test fixtures.

This commit is contained in:
BinaryAlley committed 2026-08-15 02:58:17 -07:00
1 parent dfc513da4f
commit b648e2f054
294 files changed
+3807 -4437

No files matched your search

@@ -0,0 +1,59 @@
#region ========================================================================= USING =====================================================================================
using FastEndpoints;
using Lumina.Domain.Common.Primitives;
using Lumina.Presentation.Api.Core.Endpoints.Common;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Presentation.Api.Fixtures.Core.Endpoints.Common;
/// <summary>
/// Fixture class for the <see cref="BaseEndpoint"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
[DontRegister]
public class BaseEndpointFixture : BaseEndpoint<object, object>
{
/// <summary>
/// Returns a standardized error response using the <see cref="ProblemDetails"/> format.
/// </summary>
/// <param name="errors">A list of errors to include in the response.</param>
/// <returns>An <see cref="IResult"/> with the problem details containing the specified errors.</returns>
public IResult TestProblem(List<Error> errors)
{
return Problem(errors);
}
/// <summary>
/// Returns a standardized validation error response using the <see cref="ValidationProblemDetails"/> format.
/// </summary>
/// <param name="errors">A list of validation errors to include in the response.</param>
/// <returns>An <see cref="IResult"/> with the validation problem details containing the specified errors.</returns>
public IResult TestValidationProblem(List<Error> errors)
{
return ValidationProblem(errors);
}
/// <summary>
/// Configures the API endpoint.
/// </summary>
public override void Configure()
{
}
/// <summary>
/// Handles the API request.
/// </summary>
/// <param name="request">The request containing the file system path for which to get the root.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
public override Task<object> ExecuteAsync(object request, CancellationToken cancellationToken)
{
return Task.FromResult(new object());
}
}
@@ -0,0 +1,135 @@
#region ========================================================================= USING =====================================================================================
using Bogus;
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
#endregion
namespace Lumina.Presentation.Api.Fixtures.Core.Endpoints.FileSystemManagement;
/// <summary>
/// Fixture class for creating and managing a file system structure for integration testing purposes.
/// </summary>
[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>
public FileSystemStructureFixture()
{
_rootPath = Path.Combine(AppContext.BaseDirectory, "TestFileSystemStructure_" + Guid.NewGuid());
}
/// <summary>
/// Creates a file system structure with three nested directories, each containing two text files.
/// Makes the second nested directory and the first file in each directory hidden.
/// </summary>
/// <returns>The path to the root directory of the created file system structure.</returns>
public string CreateFileSystemStructure()
{
// create the root directory
Directory.CreateDirectory(_rootPath);
string currentPath = _rootPath;
for (int i = 1; i <= 3; i++)
{
// create a nested subdirectory
currentPath = Path.Combine(currentPath, $"NestedDirectory_{i}");
Directory.CreateDirectory(currentPath);
// create two text files in the current directory
for (int j = 1; j <= 2; j++)
{
string filePath = Path.Combine(currentPath, $"TestFile_{j}.txt");
File.WriteAllText(filePath, _faker.Lorem.Paragraph());
// make the first file in each directory hidden
if (j == 1)
SetHidden(filePath);
}
// make the second nested directory hidden
if (i == 2)
currentPath = SetHidden(currentPath);
}
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 = 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())
File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.Hidden);
else if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS())
{
// On Unix-like systems, rename the file/directory to start with a dot
string directory = Path.GetDirectoryName(path)!;
string name = Path.GetFileName(path);
string newPath = Path.Combine(directory, "." + name);
if (Directory.Exists(path))
Directory.Move(path, newPath);
else if (File.Exists(path))
File.Move(path, newPath);
path = newPath;
}
return path;
}
/// <summary>
/// Deletes the file system structure created for testing purposes.
/// </summary>
public void CleanupFileSystemStructure()
{
if (Directory.Exists(_rootPath))
Directory.Delete(_rootPath, recursive: true);
}
}
@@ -0,0 +1,49 @@
#region ========================================================================= USING =====================================================================================
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats.Jpeg;
using SixLabors.ImageSharp.PixelFormats;
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
#endregion
namespace Lumina.Presentation.Api.Fixtures.Core.Endpoints.FileSystemManagement;
/// <summary>
/// Fixture class for creating and managing a test image for integration testing purposes.
/// </summary>
[ExcludeFromCodeCoverage]
public class TestImageFixture : IDisposable
{
/// <summary>
/// Gets the path of the created test image.
/// </summary>
public string ImagePath { get; }
/// <summary>
/// Initializes a new instance of the <see cref="TestImageFixture"/> class.
/// </summary>
public TestImageFixture()
{
ImagePath = Path.Combine(Path.GetTempPath(), $"TestImage_{Guid.NewGuid()}.jpg");
CreateTestImage();
}
/// <summary>
/// Creates a test image file.
/// </summary>
private void CreateTestImage()
{
using (Image image = new Image<Rgba32>(100, 100))
image.Save(ImagePath, new JpegEncoder());
}
/// <summary>
/// Disposes the fixture, ensuring the test image is deleted.
/// </summary>
public void Dispose()
{
if (File.Exists(ImagePath))
File.Delete(ImagePath);
}
}
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>enable</Nullable>
<BaseOutputPath>$(SolutionDir)..\bin\api\</BaseOutputPath>
<IsPackable>false</IsPackable>
<IsTestProject>false</IsTestProject>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AutoFixture" Version="5.0.0-preview0011" />
<PackageReference Include="AutoFixture.AutoNSubstitute" Version="5.0.0-preview0011" />
<PackageReference Include="AutoFixture.Xunit2" Version="5.0.0-preview0011" />
<PackageReference Include="Bogus" Version="35.6.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\src\Lumina.Presentation.Api\Lumina.Presentation.Api.csproj" />
</ItemGroup>
</Project>
@@ -0,0 +1,21 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Lumina.Presentation.Api.Fixtures")]
[assembly: AssemblyProduct("Lumina.Presentation.Api.Fixtures")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("A1B2C3D4-0005-4A5B-9C0D-555555555555")]
[assembly: InternalsVisibleTo("Lumina.Presentation.Api.UnitTests")]
[assembly: InternalsVisibleTo("Lumina.Presentation.Api.IntegrationTests")]
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")]