Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 55 additions & 3 deletions PowerKit.Tests/AssemblyExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,74 @@
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using FluentAssertions;
using PowerKit;
using PowerKit.Extensions;
using Xunit;

namespace PowerKit.Tests;

public class AssemblyExtensionsTests
{
private static readonly Assembly ThisAssembly = typeof(AssemblyExtensionsTests).Assembly;

// The embedded resource name follows the default MSBuild convention:
// <RootNamespace>.<RelativePath> with path separators replaced by dots.
private const string ResourceName = "PowerKit.Tests.TestResource.txt";

[Fact]
public void TryGetVersionString_Test()
{
// Act
var version = ThisAssembly.TryGetVersionString();

// Assert
version.Should().NotBeNullOrWhiteSpace();
}

[Fact]
public void GetManifestResourceString_Test()
{
// Act
var content = ThisAssembly.GetManifestResourceString(ResourceName);

// Assert
content.Should().Be("hello");
}
Comment thread
Tyrrrz marked this conversation as resolved.

[Fact]
public async Task GetManifestResourceStringAsync_Test()
{
// Act
var content = await ThisAssembly.GetManifestResourceStringAsync(ResourceName);

// Assert
content.Should().Be("hello");
}

[Fact]
public void ExtractManifestResource_Test()
{
// Arrange
var assembly = typeof(AssemblyExtensionsTests).Assembly;
using var tempFile = TempFile.Create();

// Act
var version = assembly.TryGetVersionString();
ThisAssembly.ExtractManifestResource(ResourceName, tempFile.Path);

// Assert
version.Should().NotBeNullOrWhiteSpace();
File.ReadAllText(tempFile.Path).Should().Be("hello");
}
Comment thread
Tyrrrz marked this conversation as resolved.

[Fact]
public async Task ExtractManifestResourceAsync_Test()
{
// Arrange
using var tempFile = TempFile.Create();

// Act
await ThisAssembly.ExtractManifestResourceAsync(ResourceName, tempFile.Path);

// Assert
File.ReadAllText(tempFile.Path).Should().Be("hello");

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

async variant here @copilot

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 2ae2487 — switched to await File.ReadAllTextAsync(tempFile.Path).

}
}
4 changes: 4 additions & 0 deletions PowerKit.Tests/PowerKit.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
<Compile Include="../PowerKit/**/*.cs" Exclude="../PowerKit/obj/**/*.cs" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="TestResource.txt" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" />
<PackageReference Include="GitHubActionsTestLogger" />
Expand Down
1 change: 1 addition & 0 deletions PowerKit.Tests/TestResource.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello
78 changes: 78 additions & 0 deletions PowerKit/Extensions/AssemblyExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
using System.IO;
using System.Reflection;
using System.Resources;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace PowerKit.Extensions;

Expand All @@ -15,5 +20,78 @@ internal static class AssemblyExtensions
assembly
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()
?.InformationalVersion ?? assembly.GetName().Version?.ToString();

/// <summary>
/// Reads the specified manifest resource as a UTF-8 string.
/// Throws <see cref="MissingManifestResourceException" /> if the resource is not found.
/// </summary>
public string GetManifestResourceString(string resourceName)
Comment thread
Tyrrrz marked this conversation as resolved.
Outdated
{
using var stream =
assembly.GetManifestResourceStream(resourceName)
?? throw new MissingManifestResourceException(
$"Failed to find resource '{resourceName}'."
);
Comment thread
Tyrrrz marked this conversation as resolved.

using var reader = new StreamReader(stream, Encoding.UTF8);

return reader.ReadToEnd();
}

/// <summary>
/// Reads the specified manifest resource as a UTF-8 string asynchronously.
/// Throws <see cref="MissingManifestResourceException" /> if the resource is not found.
/// </summary>
public async Task<string> GetManifestResourceStringAsync(
string resourceName,
CancellationToken cancellationToken = default
)
{
await using var stream =
assembly.GetManifestResourceStream(resourceName)
?? throw new MissingManifestResourceException(
$"Failed to find resource '{resourceName}'."
Comment thread
Tyrrrz marked this conversation as resolved.
);

using var reader = new StreamReader(stream, Encoding.UTF8);

return await reader.ReadToEndAsync(cancellationToken).ConfigureAwait(false);
Comment thread
Tyrrrz marked this conversation as resolved.
}

/// <summary>
/// Extracts the specified manifest resource to a file at the given path.
/// Throws <see cref="MissingManifestResourceException" /> if the resource is not found.
/// </summary>
public void ExtractManifestResource(string resourceName, string filePath)
{
using var stream =
assembly.GetManifestResourceStream(resourceName)
?? throw new MissingManifestResourceException(
$"Failed to find resource '{resourceName}'."
);

using var fileStream = File.Create(filePath);
stream.CopyTo(fileStream);
}
Comment thread
Tyrrrz marked this conversation as resolved.

/// <summary>
/// Extracts the specified manifest resource to a file at the given path asynchronously.
/// Throws <see cref="MissingManifestResourceException" /> if the resource is not found.
/// </summary>
public async Task ExtractManifestResourceAsync(
string resourceName,
string filePath,
CancellationToken cancellationToken = default
)
{
await using var stream =
assembly.GetManifestResourceStream(resourceName)
?? throw new MissingManifestResourceException(
$"Failed to find resource '{resourceName}'."
);

await using var fileStream = File.Create(filePath);
await stream.CopyToAsync(fileStream, cancellationToken).ConfigureAwait(false);
Comment thread
Tyrrrz marked this conversation as resolved.
Outdated
}
}
}