Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
114 changes: 97 additions & 17 deletions Plugin.BaseTypeExtensions.Tests/AssemblyExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class AssemblyExtensionsTests

private static string CreateTempDirectory()
{
var path = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N"));
var path = Path.Join(Path.GetTempPath(), Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(path);
return path;
}
Expand Down Expand Up @@ -62,7 +62,7 @@ public void MoveManifestResourceToDirectory_GeneratesRandomFilename()
public void MoveManifestResourceToDirectory_CreatesDirectoryIfNotExists()
{
var parentDirectory = CreateTempDirectory();
var targetDirectory = Path.Combine(parentDirectory, "subdir", "nested");
var targetDirectory = Path.Join(parentDirectory, "subdir", "nested");
try
{
var assembly = typeof(AssemblyExtensionsTests).Assembly;
Expand All @@ -89,7 +89,7 @@ public void MoveManifestResourceToDirectory_HandlesFileAlreadyExistsBehavior(Ass
var directory = CreateTempDirectory();
try
{
var targetFile = Path.Combine(directory, "resource.txt");
var targetFile = Path.Join(directory, "resource.txt");
File.WriteAllText(targetFile, "existing");

var assembly = typeof(AssemblyExtensionsTests).Assembly;
Expand Down Expand Up @@ -129,7 +129,7 @@ public void MoveManifestResourceToDirectory_FailThrows()
var directory = CreateTempDirectory();
try
{
var targetFile = Path.Combine(directory, "resource.txt");
var targetFile = Path.Join(directory, "resource.txt");
File.WriteAllText(targetFile, "existing");

var assembly = typeof(AssemblyExtensionsTests).Assembly;
Expand All @@ -154,9 +154,9 @@ public void MoveManifestResourceToDirectory_RenameHandlesMultipleConflicts()
try
{
// Create multiple existing files
File.WriteAllText(Path.Combine(directory, "resource.txt"), "existing");
File.WriteAllText(Path.Combine(directory, "resource_1.txt"), "existing_1");
File.WriteAllText(Path.Combine(directory, "resource_2.txt"), "existing_2");
File.WriteAllText(Path.Join(directory, "resource.txt"), "existing");
File.WriteAllText(Path.Join(directory, "resource_1.txt"), "existing_1");
File.WriteAllText(Path.Join(directory, "resource_2.txt"), "existing_2");

var assembly = typeof(AssemblyExtensionsTests).Assembly;
var result = assembly.MoveManifestResourceToDirectory(
Expand All @@ -180,7 +180,7 @@ public void MoveManifestResourceToDirectory_RenameWithExtension()
var directory = CreateTempDirectory();
try
{
var targetFile = Path.Combine(directory, "document.docx");
var targetFile = Path.Join(directory, "document.docx");
File.WriteAllText(targetFile, "existing");

var assembly = typeof(AssemblyExtensionsTests).Assembly;
Expand All @@ -205,7 +205,7 @@ public void MoveManifestResourceToDirectory_RenameWithoutExtension()
var directory = CreateTempDirectory();
try
{
var targetFile = Path.Combine(directory, "document");
var targetFile = Path.Join(directory, "document");
File.WriteAllText(targetFile, "existing");

var assembly = typeof(AssemblyExtensionsTests).Assembly;
Expand Down Expand Up @@ -272,7 +272,7 @@ public async Task MoveManifestResourceToDirectoryAsync_GeneratesRandomFilename()
public async Task MoveManifestResourceToDirectoryAsync_CreatesDirectoryIfNotExists()
{
var parentDirectory = CreateTempDirectory();
var targetDirectory = Path.Combine(parentDirectory, "async_subdir", "nested");
var targetDirectory = Path.Join(parentDirectory, "async_subdir", "nested");
try
{
var assembly = typeof(AssemblyExtensionsTests).Assembly;
Expand All @@ -299,7 +299,7 @@ public async Task MoveManifestResourceToDirectoryAsync_HandlesFileAlreadyExistsB
var directory = CreateTempDirectory();
try
{
var targetFile = Path.Combine(directory, "async_resource.txt");
var targetFile = Path.Join(directory, "async_resource.txt");
File.WriteAllText(targetFile, "existing async");

var assembly = typeof(AssemblyExtensionsTests).Assembly;
Expand Down Expand Up @@ -339,7 +339,7 @@ public async Task MoveManifestResourceToDirectoryAsync_FailThrows()
var directory = CreateTempDirectory();
try
{
var targetFile = Path.Combine(directory, "async_resource.txt");
var targetFile = Path.Join(directory, "async_resource.txt");
File.WriteAllText(targetFile, "existing async");

var assembly = typeof(AssemblyExtensionsTests).Assembly;
Expand Down Expand Up @@ -406,7 +406,7 @@ public async Task MoveManifestResourceToDirectoryAsync_CancellationCleanupPartia
catch (OperationCanceledException)
{
// Expected - verify partial file cleanup
var expectedFile = Path.Combine(directory, "cancel_test.txt");
var expectedFile = Path.Join(directory, "cancel_test.txt");
File.Exists(expectedFile).Should().BeFalse("Partial file should be cleaned up on cancellation");
}
}
Expand Down Expand Up @@ -479,7 +479,7 @@ public void MoveManifestResourceToCache_WithAllFileAlreadyExistsBehaviors()
var assembly = typeof(AssemblyExtensionsTests).Assembly;

// Test Skip behavior
var existingFile = Path.Combine(tempCacheDirectory, "test_skip.txt");
var existingFile = Path.Join(tempCacheDirectory, "test_skip.txt");
File.WriteAllText(existingFile, "existing content");

var result = assembly.MoveManifestResourceToCache(
Expand Down Expand Up @@ -575,7 +575,7 @@ public async Task MoveManifestResourceToCacheAsync_WithAllFileAlreadyExistsBehav
var assembly = typeof(AssemblyExtensionsTests).Assembly;

// Test Skip behavior
var existingFile = Path.Combine(tempCacheDirectory, "test_async_skip.txt");
var existingFile = Path.Join(tempCacheDirectory, "test_async_skip.txt");
File.WriteAllText(existingFile, "existing async content");

var result = await assembly.MoveManifestResourceToCacheAsync(
Expand Down Expand Up @@ -829,6 +829,86 @@ public async Task MoveManifestResourceToCacheAsync_ThrowsOnEmptyResourceName()
await action.Should().ThrowAsync<ArgumentException>().WithParameterName("manifestResourceName");
}

[Theory]
[InlineData("../escape.txt")]
[InlineData("sub/file.txt")]
public void MoveManifestResourceToDirectory_ThrowsOnFilenameWithDirectoryPart(string filename)
{
var directory = CreateTempDirectory();
try
{
var assembly = typeof(AssemblyExtensionsTests).Assembly;
Action action = () => assembly.MoveManifestResourceToDirectory(
"Plugin.BaseTypeExtensions.Tests.Resources.FirstResource.txt", directory, filename);
action.Should().Throw<ArgumentException>().WithParameterName("filename");
}
finally
{
Directory.Delete(directory, recursive: true);
}
}

[Fact]
public void MoveManifestResourceToDirectory_ThrowsOnRootedFilename_AndWritesNothing()
{
var directory = CreateTempDirectory();
var elsewhere = CreateTempDirectory();
try
{
var rootedFilename = Path.Join(elsewhere, "escape.txt");
var assembly = typeof(AssemblyExtensionsTests).Assembly;
Action action = () => assembly.MoveManifestResourceToDirectory(
"Plugin.BaseTypeExtensions.Tests.Resources.FirstResource.txt", directory, rootedFilename);
action.Should().Throw<ArgumentException>().WithParameterName("filename");
File.Exists(rootedFilename).Should().BeFalse();
}
finally
{
Directory.Delete(directory, recursive: true);
Directory.Delete(elsewhere, recursive: true);
}
}

[Theory]
[InlineData("../escape.txt")]
[InlineData("sub/file.txt")]
public async Task MoveManifestResourceToDirectoryAsync_ThrowsOnFilenameWithDirectoryPart(string filename)
{
var directory = CreateTempDirectory();
try
{
var assembly = typeof(AssemblyExtensionsTests).Assembly;
Func<Task> action = () => assembly.MoveManifestResourceToDirectoryAsync(
"Plugin.BaseTypeExtensions.Tests.Resources.FirstResource.txt", directory, filename);
await action.Should().ThrowAsync<ArgumentException>().WithParameterName("filename");
}
finally
{
Directory.Delete(directory, recursive: true);
}
}

[Fact]
public async Task MoveManifestResourceToDirectoryAsync_ThrowsOnRootedFilename_AndWritesNothing()
{
var directory = CreateTempDirectory();
var elsewhere = CreateTempDirectory();
try
{
var rootedFilename = Path.Join(elsewhere, "escape.txt");
var assembly = typeof(AssemblyExtensionsTests).Assembly;
Func<Task> action = () => assembly.MoveManifestResourceToDirectoryAsync(
"Plugin.BaseTypeExtensions.Tests.Resources.FirstResource.txt", directory, rootedFilename);
await action.Should().ThrowAsync<ArgumentException>().WithParameterName("filename");
File.Exists(rootedFilename).Should().BeFalse();
}
finally
{
Directory.Delete(directory, recursive: true);
Directory.Delete(elsewhere, recursive: true);
}
}

#endregion

#region Edge Case Tests
Expand All @@ -844,7 +924,7 @@ public void MoveManifestResourceToDirectory_RenameCounterLimit()
var assembly = typeof(AssemblyExtensionsTests).Assembly;

// Create a file that would cause the rename logic to increment
File.WriteAllText(Path.Combine(directory, "test.txt"), "existing");
File.WriteAllText(Path.Join(directory, "test.txt"), "existing");

// This should work normally - just testing the edge case exists
var result = assembly.MoveManifestResourceToDirectory(
Expand Down Expand Up @@ -951,7 +1031,7 @@ public void FileAlreadyExistsBehavior_DefaultValue_IsFail()
var directory = CreateTempDirectory();
try
{
var targetFile = Path.Combine(directory, "default_test.txt");
var targetFile = Path.Join(directory, "default_test.txt");
File.WriteAllText(targetFile, "existing");

var assembly = typeof(AssemblyExtensionsTests).Assembly;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ public class CancellationTokenExtensionsTests
public void WithTimeout_TimeSpan_CreatesCombinedToken()
{
// Arrange
var originalToken = new CancellationTokenSource().Token;
using var cts = new CancellationTokenSource();
var originalToken = cts.Token;
var timeout = TimeSpan.FromMilliseconds(100);

// Act
Expand All @@ -21,7 +22,8 @@ public void WithTimeout_TimeSpan_CreatesCombinedToken()
public void WithTimeout_Milliseconds_CreatesCombinedToken()
{
// Arrange
var originalToken = new CancellationTokenSource().Token;
using var cts = new CancellationTokenSource();
var originalToken = cts.Token;
var timeoutMs = 100;

// Act
Expand Down Expand Up @@ -76,7 +78,7 @@ public void WithTimeout_ZeroTimeSpan_ReturnsAlreadyCanceledToken()
public void WithTimeout_AlreadyCanceledToken_ReturnsSameToken()
{
// Arrange
var cts = new CancellationTokenSource();
using var cts = new CancellationTokenSource();
cts.Cancel();
var canceledToken = cts.Token;

Expand All @@ -92,7 +94,7 @@ public void WithTimeout_AlreadyCanceledToken_ReturnsSameToken()
public void AsTask_ReturnsTaskThatCompletesOnCancellation()
{
// Arrange
var cts = new CancellationTokenSource();
using var cts = new CancellationTokenSource();
var token = cts.Token;

// Act
Expand Down Expand Up @@ -120,7 +122,7 @@ public void AsTask_NeverCompletesForNonCanceledToken()
public void AsTask_AlreadyCanceledToken_ReturnsCompletedTask()
{
// Arrange
var cts = new CancellationTokenSource();
using var cts = new CancellationTokenSource();
cts.Cancel();
var token = cts.Token;

Expand All @@ -136,7 +138,7 @@ public void AsTask_AlreadyCanceledToken_ReturnsCompletedTask()
public void ThrowIfCanceledAfter_ThrowsWhenCanceled()
{
// Arrange
var cts = new CancellationTokenSource();
using var cts = new CancellationTokenSource();
var token = cts.Token;
cts.Cancel();

Expand Down Expand Up @@ -174,7 +176,7 @@ public void ThrowIfCanceledAfter_ZeroTimeout_ThrowsOperationCanceledException()
public void ThrowIfCanceledAfter_TimeoutExpires_ThrowsOperationCanceledException()
{
// Arrange
var cts = new CancellationTokenSource();
using var cts = new CancellationTokenSource();
var token = cts.Token;

// Act & Assert
Expand All @@ -187,7 +189,7 @@ public void ThrowIfCanceledAfter_TimeoutExpires_ThrowsOperationCanceledException
public void WaitHandle_ReturnsTrueWhenCanceled()
{
// Arrange
var cts = new CancellationTokenSource();
using var cts = new CancellationTokenSource();
var token = cts.Token;
cts.Cancel();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public async Task TryAddAsync_WithCancellation_ThrowsOperationCanceledException(
{
// Arrange
var dictionary = new ConcurrentDictionary<string, int>();
var cts = new CancellationTokenSource();
using var cts = new CancellationTokenSource();
cts.Cancel();

// Act
Expand All @@ -111,7 +111,7 @@ public async Task TryAddAsync_WithCancellationDuringRetries_ThrowsOperationCance
// Arrange
var dictionary = new ConcurrentDictionary<string, int>();
dictionary.TryAdd("test", 1); // Ensure first attempt fails
var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(50));
using var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(50));

// Act
Func<Task> act = async () => await dictionary.TryAddAsync("test", 42, retryCount: 10, delayBetweenRetries: TimeSpan.FromMilliseconds(100), cancellationToken: cts.Token);
Expand All @@ -124,10 +124,10 @@ public async Task TryAddAsync_WithCancellationDuringRetries_ThrowsOperationCance
public async Task TryAddAsync_WithNullDictionary_ThrowsArgumentNullException()
{
// Arrange
ConcurrentDictionary<string, int>? dictionary = null;
ConcurrentDictionary<string, int> dictionary = null!;

// Act
Func<Task> act = async () => await dictionary!.TryAddAsync("test", 42);
Func<Task> act = async () => await dictionary.TryAddAsync("test", 42);

// Assert
await act.Should().ThrowAsync<ArgumentNullException>()
Expand Down Expand Up @@ -265,7 +265,7 @@ public async Task TryRemoveAsync_WithCancellation_ThrowsOperationCanceledExcepti
{
// Arrange
var dictionary = new ConcurrentDictionary<string, int>();
var cts = new CancellationTokenSource();
using var cts = new CancellationTokenSource();
cts.Cancel();

// Act
Expand All @@ -279,10 +279,10 @@ public async Task TryRemoveAsync_WithCancellation_ThrowsOperationCanceledExcepti
public async Task TryRemoveAsync_WithNullDictionary_ThrowsArgumentNullException()
{
// Arrange
ConcurrentDictionary<string, int>? dictionary = null;
ConcurrentDictionary<string, int> dictionary = null!;

// Act
Func<Task> act = async () => await dictionary!.TryRemoveAsync("test");
Func<Task> act = async () => await dictionary.TryRemoveAsync("test");

// Assert
await act.Should().ThrowAsync<ArgumentNullException>()
Expand Down Expand Up @@ -422,7 +422,7 @@ public async Task TryUpdateAsync_WithCancellation_ThrowsOperationCanceledExcepti
{
// Arrange
var dictionary = new ConcurrentDictionary<string, int>();
var cts = new CancellationTokenSource();
using var cts = new CancellationTokenSource();
cts.Cancel();

// Act
Expand All @@ -436,10 +436,10 @@ public async Task TryUpdateAsync_WithCancellation_ThrowsOperationCanceledExcepti
public async Task TryUpdateAsync_WithNullDictionary_ThrowsArgumentNullException()
{
// Arrange
ConcurrentDictionary<string, int>? dictionary = null;
ConcurrentDictionary<string, int> dictionary = null!;

// Act
Func<Task> act = async () => await dictionary!.TryUpdateAsync("test", newValue: 42, comparisonValue: 1);
Func<Task> act = async () => await dictionary.TryUpdateAsync("test", newValue: 42, comparisonValue: 1);

// Assert
await act.Should().ThrowAsync<ArgumentNullException>()
Expand Down Expand Up @@ -562,7 +562,7 @@ public async Task TryGetValueAsync_WithCancellation_ThrowsOperationCanceledExcep
{
// Arrange
var dictionary = new ConcurrentDictionary<string, int>();
var cts = new CancellationTokenSource();
using var cts = new CancellationTokenSource();
cts.Cancel();

// Act
Expand All @@ -576,10 +576,10 @@ public async Task TryGetValueAsync_WithCancellation_ThrowsOperationCanceledExcep
public async Task TryGetValueAsync_WithNullDictionary_ThrowsArgumentNullException()
{
// Arrange
ConcurrentDictionary<string, int>? dictionary = null;
ConcurrentDictionary<string, int> dictionary = null!;

// Act
Func<Task> act = async () => await dictionary!.TryGetValueAsync("test");
Func<Task> act = async () => await dictionary.TryGetValueAsync("test");

// Assert
await act.Should().ThrowAsync<ArgumentNullException>()
Expand Down
Loading
Loading