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
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ end_of_line = crlf
# Suppressions follow CODESTYLE.md "Analyzer Diagnostics and Suppressions": prefer a
# [SuppressMessage] attribute or the owning project's .editorconfig; relax a rule
# repo-wide here only when it applies to every project (never a brownfield batch).
# IDE0055: CSharpier owns formatting for every project; a second formatter would fight it.
dotnet_diagnostic.IDE0055.severity = none
dotnet_analyzer_diagnostic.severity = suggestion
csharp_indent_block_contents = true
csharp_indent_braces = false
csharp_indent_case_contents = true
Expand Down
21 changes: 17 additions & 4 deletions UtilitiesTests/DownloadAsyncTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ public async Task GetContentInfoAsync_WithValidUri_ShouldReturnSuccess()
"https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png"
);

(bool success, long size, DateTime _) = await Download.GetContentInfoAsync(uri);
(bool success, long size, DateTime _) = await Download.GetContentInfoAsync(
uri,
TestContext.Current.CancellationToken
);

_ = success.Should().BeTrue();
_ = (size > 0).Should().BeTrue();
Expand All @@ -20,7 +23,10 @@ public async Task DownloadStringAsync_WithValidUri_ShouldReturnContent()
{
Uri uri = new("https://www.google.com");

(bool success, string? content) = await Download.DownloadStringAsync(uri);
(bool success, string? content) = await Download.DownloadStringAsync(
uri,
TestContext.Current.CancellationToken
);

_ = success.Should().BeTrue();
_ = content.Should().NotBeEmpty();
Expand All @@ -37,7 +43,11 @@ public async Task DownloadFileAsync_WithValidUri_ShouldCreateFile()

try
{
bool result = await Download.DownloadFileAsync(uri, tempFile);
bool result = await Download.DownloadFileAsync(
uri,
tempFile,
TestContext.Current.CancellationToken
);

_ = result.Should().BeTrue();
_ = File.Exists(tempFile).Should().BeTrue();
Expand All @@ -57,7 +67,10 @@ public async Task DownloadAsync_WithInvalidUri_ShouldReturnFalse()
{
Uri invalidUri = new("https://thisdoesnotexist123456789.com/file.txt");

(bool success, long _, DateTime _) = await Download.GetContentInfoAsync(invalidUri);
(bool success, long _, DateTime _) = await Download.GetContentInfoAsync(
invalidUri,
TestContext.Current.CancellationToken
);

_ = success.Should().BeFalse();
}
Expand Down
20 changes: 15 additions & 5 deletions UtilitiesTests/ExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ public async Task StringExtension_CompressAsync_ShouldCompressString()
{
string original = "This is a test string for async compression.";

string compressed = await original.CompressAsync();
string compressed = await original.CompressAsync(
cancellationToken: TestContext.Current.CancellationToken
);

_ = compressed.Should().NotBeNull();
_ = compressed.Should().NotBeEmpty();
Expand All @@ -61,9 +63,13 @@ public async Task StringExtension_CompressAsync_ShouldCompressString()
public async Task StringExtension_DecompressAsync_ShouldDecompressString()
{
string original = "This is a test string for async decompression.";
string compressed = await original.CompressAsync();
string compressed = await original.CompressAsync(
cancellationToken: TestContext.Current.CancellationToken
);

string decompressed = await compressed.DecompressAsync();
string decompressed = await compressed.DecompressAsync(
TestContext.Current.CancellationToken
);

_ = decompressed.Should().Be(original);
}
Expand Down Expand Up @@ -266,8 +272,12 @@ public async Task StringExtension_CompressDecompressAsync_RoundTrip_ShouldPreser

foreach (string original in testStrings)
{
string compressed = await original.CompressAsync();
string decompressed = await compressed.DecompressAsync();
string compressed = await original.CompressAsync(
cancellationToken: TestContext.Current.CancellationToken
);
string decompressed = await compressed.DecompressAsync(
TestContext.Current.CancellationToken
);

_ = decompressed.Should().Be(original);
}
Expand Down
62 changes: 51 additions & 11 deletions UtilitiesTests/FileExAsyncTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ public async Task DeleteFileAsync_WithExistingFile_ShouldReturnTrue()

try
{
bool result = await FileEx.DeleteFileAsync(tempFile);
bool result = await FileEx.DeleteFileAsync(
tempFile,
TestContext.Current.CancellationToken
);

_ = result.Should().BeTrue();
_ = File.Exists(tempFile).Should().BeFalse();
Expand All @@ -31,7 +34,10 @@ public async Task DeleteDirectoryAsync_WithExistingDirectory_ShouldReturnTrue()

try
{
bool result = await FileEx.DeleteDirectoryAsync(tempDir);
bool result = await FileEx.DeleteDirectoryAsync(
tempDir,
TestContext.Current.CancellationToken
);

_ = result.Should().BeTrue();
_ = Directory.Exists(tempDir).Should().BeFalse();
Expand All @@ -53,11 +59,19 @@ public async Task DeleteDirectoryAsync_Recursive_ShouldDeleteAllContents()
string subDir = Path.Combine(tempDir, "subdir");
_ = Directory.CreateDirectory(subDir);
string testFile = Path.Combine(subDir, "test.txt");
await File.WriteAllTextAsync(testFile, "test content");
await File.WriteAllTextAsync(
testFile,
"test content",
TestContext.Current.CancellationToken
);

try
{
bool result = await FileEx.DeleteDirectoryAsync(tempDir, recursive: true);
bool result = await FileEx.DeleteDirectoryAsync(
tempDir,
recursive: true,
TestContext.Current.CancellationToken
);
Comment thread
ptr727 marked this conversation as resolved.

_ = result.Should().BeTrue();
_ = Directory.Exists(tempDir).Should().BeFalse();
Expand All @@ -79,7 +93,11 @@ public async Task RenameFileAsync_WithValidPaths_ShouldRenameFile()

try
{
bool result = await FileEx.RenameFileAsync(tempFile, newPath);
bool result = await FileEx.RenameFileAsync(
tempFile,
newPath,
TestContext.Current.CancellationToken
);

_ = result.Should().BeTrue();
_ = File.Exists(tempFile).Should().BeFalse();
Expand Down Expand Up @@ -107,7 +125,11 @@ public async Task RenameFolderAsync_WithValidPaths_ShouldRenameFolder()

try
{
bool result = await FileEx.RenameFolderAsync(tempDir, newPath);
bool result = await FileEx.RenameFolderAsync(
tempDir,
newPath,
TestContext.Current.CancellationToken
);

_ = result.Should().BeTrue();
_ = Directory.Exists(tempDir).Should().BeFalse();
Expand All @@ -133,9 +155,16 @@ public async Task WaitFileReadableAsync_WithReadableFile_ShouldReturnTrue()

try
{
await File.WriteAllTextAsync(tempFile, "test content");
await File.WriteAllTextAsync(
tempFile,
"test content",
TestContext.Current.CancellationToken
);

bool result = await FileEx.WaitFileReadableAsync(tempFile);
bool result = await FileEx.WaitFileReadableAsync(
tempFile,
TestContext.Current.CancellationToken
);

_ = result.Should().BeTrue();
}
Expand All @@ -156,7 +185,11 @@ public async Task CreateRandomFilledFileAsync_ShouldCreateFile()

try
{
bool result = await FileEx.CreateRandomFilledFileAsync(tempFile, fileSize);
bool result = await FileEx.CreateRandomFilledFileAsync(
tempFile,
fileSize,
TestContext.Current.CancellationToken
);

_ = result.Should().BeTrue();
_ = File.Exists(tempFile).Should().BeTrue();
Expand Down Expand Up @@ -224,11 +257,18 @@ public async Task DeleteInsideDirectoryAsync_ShouldDeleteContentsOnly()
string tempDir = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid()}");
_ = Directory.CreateDirectory(tempDir);
string testFile = Path.Combine(tempDir, "test.txt");
await File.WriteAllTextAsync(testFile, "test content");
await File.WriteAllTextAsync(
testFile,
"test content",
TestContext.Current.CancellationToken
);

try
{
bool result = await FileEx.DeleteInsideDirectoryAsync(tempDir);
bool result = await FileEx.DeleteInsideDirectoryAsync(
tempDir,
TestContext.Current.CancellationToken
);

_ = result.Should().BeTrue();
_ = Directory.Exists(tempDir).Should().BeTrue(); // Directory should still exist
Expand Down
15 changes: 12 additions & 3 deletions UtilitiesTests/HttpClientFactoryResilienceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ public async Task TransientStatus_IsRetriedThenSucceeds()
);
using HttpClient client = CreateStubbedClient(stub, FastOptions());

using HttpResponseMessage response = await client.GetAsync(new Uri("http://localhost/"));
using HttpResponseMessage response = await client.GetAsync(
new Uri("http://localhost/"),
TestContext.Current.CancellationToken
);

_ = response.StatusCode.Should().Be(HttpStatusCode.OK);
_ = stub.CallCount.Should().Be(3); // initial attempt + 2 retries
Expand All @@ -26,7 +29,10 @@ public async Task NonTransientStatus_IsNotRetried()
using StubHttpMessageHandler stub = new(Status(HttpStatusCode.NotFound));
using HttpClient client = CreateStubbedClient(stub, FastOptions());

using HttpResponseMessage response = await client.GetAsync(new Uri("http://localhost/"));
using HttpResponseMessage response = await client.GetAsync(
new Uri("http://localhost/"),
TestContext.Current.CancellationToken
);

_ = response.StatusCode.Should().Be(HttpStatusCode.NotFound);
_ = stub.CallCount.Should().Be(1); // 404 is not transient
Expand Down Expand Up @@ -123,7 +129,10 @@ public async Task HttpRequestExceptionWithTransientStatus_IsRetriedThenSucceeds(
);
using HttpClient client = CreateStubbedClient(stub, FastOptions());

using HttpResponseMessage response = await client.GetAsync(new Uri("http://localhost/"));
using HttpResponseMessage response = await client.GetAsync(
new Uri("http://localhost/"),
TestContext.Current.CancellationToken
);

_ = response.StatusCode.Should().Be(HttpStatusCode.OK);
_ = stub.CallCount.Should().Be(3); // 503 exception retried, then success
Expand Down
22 changes: 16 additions & 6 deletions UtilitiesTests/StringCompressionAsyncTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ public async Task CompressDecompressAsync()
string text = Convert.ToBase64String(buffer);

// Compress the string asynchronously
string compressed = await text.CompressAsync();
string compressed = await text.CompressAsync(
cancellationToken: TestContext.Current.CancellationToken
);

// Decompress asynchronously
string decompressed = await compressed.DecompressAsync();
string decompressed = await compressed.DecompressAsync(
TestContext.Current.CancellationToken
);

// Compare to original string
_ = decompressed.Should().Be(text);
Expand All @@ -32,8 +36,10 @@ public async Task CompressAsync_WithDifferentLevels_ShouldSucceed(CompressionLev
string text =
"This is a test string that will be compressed with different compression levels.";

string compressed = await text.CompressAsync(level);
string decompressed = await compressed.DecompressAsync();
string compressed = await text.CompressAsync(level, TestContext.Current.CancellationToken);
string decompressed = await compressed.DecompressAsync(
TestContext.Current.CancellationToken
);

_ = decompressed.Should().Be(text);
}
Expand Down Expand Up @@ -91,8 +97,12 @@ public async Task CompressAsync_LargeString_ShouldCompress()
// Create a large repetitive string (should compress well)
string largeText = new('A', 1024 * 1024); // 1MB of 'A's

string compressed = await largeText.CompressAsync();
string decompressed = await compressed.DecompressAsync();
string compressed = await largeText.CompressAsync(
cancellationToken: TestContext.Current.CancellationToken
);
string decompressed = await compressed.DecompressAsync(
TestContext.Current.CancellationToken
);

_ = decompressed.Should().Be(largeText);
_ = (compressed.Length < largeText.Length)
Expand Down