//-----------------------------------------------------------------------
//
// Copyright (c) lanedirt. All rights reserved.
// Licensed under the AGPLv3 license. See LICENSE.md file in the project root for full license information.
//
//-----------------------------------------------------------------------
namespace AliasVault.UnitTests.Common;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
///
/// Utility for reading strings from project embedded resources used in tests.
///
public static class ResourceReaderUtility
{
///
/// Reads string from embedded resource.
///
/// Name of the embedded resource.
/// Contents of embedded resource as string.
/// Thrown when resource is not found with that name.
public static async Task 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();
}
///
/// Reads byte array from embedded resource.
///
/// Name of the embedded resource.
/// Contents of embedded resource as byte array.
/// Thrown when resource is not found with that name.
public static async Task 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();
}
///
/// Get all embedded resource names in current assembly.
///
/// Array of resource names.
public static string[] GetEmbeddedResourceNames()
{
return Assembly.GetExecutingAssembly().GetManifestResourceNames();
}
}