Add separate ResourceReaderUtility to E2E project because of namespace(#542)

This commit is contained in:
Leendert de Borst
2025-03-31 12:10:18 +02:00
committed by Leendert de Borst
parent 9bec5a3ae5
commit 1dca845731
3 changed files with 68 additions and 2 deletions

View File

@@ -53,7 +53,6 @@
<ProjectReference Include="..\..\Databases\AliasServerDb\AliasServerDb.csproj" />
<ProjectReference Include="..\..\Utilities\AliasVault.TotpGenerator\AliasVault.TotpGenerator.csproj" />
<ProjectReference Include="..\AliasVault.IntegrationTests\AliasVault.IntegrationTests.csproj" />
<ProjectReference Include="..\AliasVault.UnitTests\AliasVault.UnitTests.csproj" />
<ProjectReference Include="..\Server\AliasVault.E2ETests.Client.Server\AliasVault.E2ETests.Client.Server.csproj" />
</ItemGroup>

View File

@@ -0,0 +1,68 @@
//-----------------------------------------------------------------------
// <copyright file="ResourceReaderUtility.cs" company="lanedirt">
// Copyright (c) lanedirt. All rights reserved.
// Licensed under the MIT license. See LICENSE.md file in the project root for full license information.
// </copyright>
//-----------------------------------------------------------------------
namespace AliasVault.E2ETests.Common;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
/// <summary>
/// Utility for reading strings from project embedded resources used in tests.
/// </summary>
public static class ResourceReaderUtility
{
/// <summary>
/// Reads string from embedded resource.
/// </summary>
/// <param name="resourceName">Name of the embedded resource.</param>
/// <returns>Contents of embedded resource as string.</returns>
/// <exception cref="InvalidOperationException">Thrown when resource is not found with that name.</exception>
public static async Task<string> ReadEmbeddedResourceStringAsync(string resourceName)
{
var assembly = Assembly.GetExecutingAssembly();
using var stream = assembly.GetManifestResourceStream(resourceName);
if (stream == null)
{
throw new InvalidOperationException($"Resource {resourceName} not found in {assembly.FullName}");
}
using var reader = new StreamReader(stream);
return await reader.ReadToEndAsync();
}
/// <summary>
/// Reads byte array from embedded resource.
/// </summary>
/// <param name="resourceName">Name of the embedded resource.</param>
/// <returns>Contents of embedded resource as byte array.</returns>
/// <exception cref="InvalidOperationException">Thrown when resource is not found with that name.</exception>
public static async Task<byte[]> ReadEmbeddedResourceBytesAsync(string resourceName)
{
var assembly = Assembly.GetExecutingAssembly();
using var stream = assembly.GetManifestResourceStream(resourceName);
if (stream == null)
{
throw new InvalidOperationException($"Resource {resourceName} not found in {assembly.FullName}");
}
using var memoryStream = new MemoryStream();
await stream.CopyToAsync(memoryStream);
return memoryStream.ToArray();
}
/// <summary>
/// Get all embedded resource names in current assembly.
/// </summary>
/// <returns>Array of resource names.</returns>
public static string[] GetEmbeddedResourceNames()
{
return Assembly.GetExecutingAssembly().GetManifestResourceNames();
}
}

View File

@@ -9,6 +9,5 @@
global using System.Threading.Tasks;
global using AliasVault.E2ETests.Infrastructure;
global using AliasVault.E2ETests.Common;
global using AliasVault.UnitTests.Common;
global using NUnit.Framework;
global using Microsoft.Playwright;