Skip to content
Open
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
23 changes: 18 additions & 5 deletions tests/Elzik.Breef.Api.Tests.Functional/Breefs/BreefTestsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public abstract class BreefTestsBase
protected virtual int HostPort { get; set; } = 8080;
protected abstract HttpClient Client { get; }
protected virtual bool SkipTestsIf { get; }
protected virtual string SkipTestsReason { get; } = "Test was skipped but no reason was given.";
protected virtual string SkipTestsReason { get; private set; } = "Test was skipped but no reason was given.";

private readonly WallabagOptions _wallabagOptions;

Expand All @@ -38,13 +38,26 @@ protected BreefTestsBase()
ApiKey = breefApiOptions.ApiKey;
}

[SkippableFact]
public async Task EndToEndHappyPath()
[SkippableTheory]
[InlineData("http://example.com")]
[InlineData("https://www.reddit.com/r/dotnet/")]
[InlineData("https://www.reddit.com/r/selfhosted/comments/1ojndg6/advice_should_i_buy_a_new_router_or_build_one/")]
public async Task EndToEndHappyPath(string url)
{
Skip.If(SkipTestsIf, SkipTestsReason);
var attemptingRedditTestInGithubWorklow =
url.StartsWith("https://www.reddit.com") &&
Environment.GetEnvironmentVariable("GITHUB_ACTIONS") == "true";
var skipTest = SkipTestsIf || attemptingRedditTestInGithubWorklow;
if(attemptingRedditTestInGithubWorklow)
{
SkipTestsReason = "Skipped because requests to reddit.com from GitHub workflows are " +
"always blocked meaning this test case always fails. This must be run locally instead.";
}

Skip.If(skipTest, SkipTestsReason);
Comment on lines +47 to +57
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🔴 Critical

Race condition: mutating shared property in parameterized test.

Modifying SkipTestsReason (line 53) within a theory test creates a race condition because xUnit runs theory tests in parallel by default. Multiple test instances can overwrite each other's skip reasons, leading to incorrect or misleading skip messages.

Recommended solution: Use a local variable for the skip reason instead:

         [SkippableTheory]
         [InlineData("http://example.com")]
         [InlineData("https://www.reddit.com/r/dotnet/")]
         [InlineData("https://www.reddit.com/r/selfhosted/comments/1ojndg6/advice_should_i_buy_a_new_router_or_build_one/")]
         public async Task EndToEndHappyPath(string url)
         {
             var attemptingRedditTestInGithubWorklow = 
                 url.StartsWith("https://www.reddit.com") && 
                 Environment.GetEnvironmentVariable("GITHUB_ACTIONS") == "true";
             var skipTest = SkipTestsIf || attemptingRedditTestInGithubWorklow;
+            var skipReason = SkipTestsReason;
             if(attemptingRedditTestInGithubWorklow)
             {
-                SkipTestsReason = "Skipped because requests to reddit.com from GitHub workflows are " +
+                skipReason = "Skipped because requests to reddit.com from GitHub workflows are " +
                     "always blocked meaning this test case always fails. This must be run locally instead.";
             }
             
-            Skip.If(skipTest, SkipTestsReason);
+            Skip.If(skipTest, skipReason);

Minor: Use case-insensitive URL comparison.

The url.StartsWith("https://www.reddit.com") check (line 48) is case-sensitive. For robustness, use case-insensitive comparison for the URL scheme and host:

-            var attemptingRedditTestInGithubWorklow = 
-                url.StartsWith("https://www.reddit.com") && 
-                Environment.GetEnvironmentVariable("GITHUB_ACTIONS") == "true";
+            var attemptingRedditTestInGithubWorklow = 
+                url.StartsWith("https://www.reddit.com", StringComparison.OrdinalIgnoreCase) && 
+                Environment.GetEnvironmentVariable("GITHUB_ACTIONS") == "true";
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
var attemptingRedditTestInGithubWorklow =
url.StartsWith("https://www.reddit.com") &&
Environment.GetEnvironmentVariable("GITHUB_ACTIONS") == "true";
var skipTest = SkipTestsIf || attemptingRedditTestInGithubWorklow;
if(attemptingRedditTestInGithubWorklow)
{
SkipTestsReason = "Skipped because requests to reddit.com from GitHub workflows are " +
"always blocked meaning this test case always fails. This must be run locally instead.";
}
Skip.If(skipTest, SkipTestsReason);
var attemptingRedditTestInGithubWorklow =
url.StartsWith("https://www.reddit.com") &&
Environment.GetEnvironmentVariable("GITHUB_ACTIONS") == "true";
var skipTest = SkipTestsIf || attemptingRedditTestInGithubWorklow;
var skipReason = SkipTestsReason;
if(attemptingRedditTestInGithubWorklow)
{
skipReason = "Skipped because requests to reddit.com from GitHub workflows are " +
"always blocked meaning this test case always fails. This must be run locally instead.";
}
Skip.If(skipTest, skipReason);
Suggested change
var attemptingRedditTestInGithubWorklow =
url.StartsWith("https://www.reddit.com") &&
Environment.GetEnvironmentVariable("GITHUB_ACTIONS") == "true";
var skipTest = SkipTestsIf || attemptingRedditTestInGithubWorklow;
if(attemptingRedditTestInGithubWorklow)
{
SkipTestsReason = "Skipped because requests to reddit.com from GitHub workflows are " +
"always blocked meaning this test case always fails. This must be run locally instead.";
}
Skip.If(skipTest, SkipTestsReason);
var attemptingRedditTestInGithubWorklow =
url.StartsWith("https://www.reddit.com", StringComparison.OrdinalIgnoreCase) &&
Environment.GetEnvironmentVariable("GITHUB_ACTIONS") == "true";
var skipTest = SkipTestsIf || attemptingRedditTestInGithubWorklow;
if(attemptingRedditTestInGithubWorklow)
{
SkipTestsReason = "Skipped because requests to reddit.com from GitHub workflows are " +
"always blocked meaning this test case always fails. This must be run locally instead.";
}
Skip.If(skipTest, SkipTestsReason);
🤖 Prompt for AI Agents
In tests/Elzik.Breef.Api.Tests.Functional/Breefs/BreefTestsBase.cs around lines
47 to 57, avoid mutating the shared property SkipTestsReason inside the
parameterized/theory test to prevent race conditions: create a local variable
(e.g., localSkipReason) and set that when computing whether to skip, then pass
that local variable into Skip.If; also make the reddit URL check
case-insensitive (e.g., use StartsWith with an OrdinalIgnoreCase comparison or
parse the URL and compare the scheme/host case-insensitively) so the detection
is robust.


// Arrange
var breef = new { Url = "http://example.com" };
var breef = new { Url = url };

// Act
var response = await Client.PostAsJsonAsync($"{BaseUrl}/breefs", breef);
Expand Down
Loading