Repository navigation
Guard Assert.IsNotNull against Task return-value regressions - #12306
Conversation
Co-authored-by: Copilot App <[email protected]>
There was a problem hiding this comment.
Warning
Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.
Copilot review overview
1 open issue
What changed in this PR
Adds regression-focused MSTest unit tests to ensure statement-form Assert.IsNotNull(...) inside async methods continues to behave as a null-check (and not accidentally become a value-returning call that would trigger CS4014) for nullable Task and Task<int>.
Changes:
- Add three async unit tests covering
Assert.IsNotNulloverloads: plain, string-message, and interpolated-string-message. - Validate nullable-flow narrowing, pending-task state, and (for interpolated messages) lazy formatting behavior.
| File | Description |
|---|---|
| test/UnitTests/TestFramework.UnitTests/Assertions/AssertTests.IsNull.cs | Adds three regression tests guarding Assert.IsNotNull statement-form behavior for nullable Task/Task<T> in async methods and message overloads. |
Review effort: Lite
There was a problem hiding this comment.
🧪 Test quality review — PR #12306
Compared merge base 8bdfc29f415696738d845d56aca5d438d0a068de with head 070fe43f2052c635631a2c60d7dd7df8629b0833. Evidence applies to this head, not every intermediate commit.
Test-value evidence
| Contract | Test / decisive assertion | Counterfactual | Evidence |
|---|---|---|---|
Statement-form Assert.IsNotNull on nullable Task/Task<int> stays void, so it does not produce CS4014 or await the task. |
The three IsNotNull_WhenPendingTasks* tests. Decisive checks are the compile-time statement calls plus IsCompleted.Should().BeFalse() and, for the interpolated overload, WasToStringCalled.Should().BeFalse(). |
Add value-returning generic IsNotNull<T> overloads. |
Static prediction (consistent with the PR's reported local run). The new overloads should fail the unit-test build with CS4014. I did not run it. The PR description reports an executed result of six CS4014 errors versus 9/9 passing. I could not verify that against accessible records, so treat it as an author claim. |
No production code changed. The tests are regression coverage only. Runtime-only ablations (for example, asserting on null) are covered by the existing IsNotNull_PassNull_ShouldFail.
🧵 Parallel safety
PARALLEL-SAFETY RESULT
STATUS: PARTIAL
The specialist task was not launched in this run, so coverage is incomplete. This is not a clean result. My direct read of the three added methods found only method-local state (a local TaskCompletionSource and a local message tracker). I saw no static or shared mutable state, files, or timing dependence. No parallelization configuration changed (config_count=0).
Per-test assessment
Pseudo-mutation evidence below consists of static predictions, not executed mutation results.
| Result | Quality | Test | Notes | How to improve |
|---|---|---|---|---|
| Pass | B (80–89) | new AssertTests.IsNotNull_WhenPendingTasks_DoesNotAwaitAndLearnsNotNull |
Compile-time CS4014 guard plus a pending-state check and a completion check. | None |
| Pass | B (80–89) | new AssertTests.IsNotNull_WhenPendingTasksAndMessage_DoesNotAwaitAndLearnsNotNull |
Same guard for the string-message overload. | None |
| Pass | B (80–89) | new AssertTests.IsNotNull_WhenPendingTasksAndInterpolatedStringMessage_DoesNotAwaitAndLearnsNotNull |
Also checks that the interpolated message is not formatted when the assertion passes. | None |
- Pseudo-mutation (static): If
IsNotNullawaited or blocked on the task,IsCompleted.Should().BeFalse()would likely detect it, so that is predicted as killed (inferred). Eagerly formatting the message would likely be caught byWasToStringCalled, also predicted as killed (inferred). A null-ignoringIsNotNullis not detected by these tests (candidate survivor, unverified), butIsNotNull_PassNull_ShouldFailcovers that contract. - Inline comments: None. The one existing live thread, from Copilot, is about setup duplication. That is a rubric-only maintainability point and not a verified defect, so I did not duplicate it.
- Limitations: The production
IsNotNullbodies were not traced beyond the test contract.
<sub>This advisory review is heuristic and non-blocking.
Re-run the combined review with /review-tests, or only the parallel-safety specialist with /parallel-audit.</sub>
🤖 Automated content by GitHub Copilot. Generated by the Test Quality Review on PR (on open / sync) workflow. · copilot · auto · ~24 AIC · ⌖ ~1 AIC · ⊞ 8.1K
There was a problem hiding this comment.
🤖 Copilot expert review — workflow run
Reviewed the single changed file (AssertTests.IsNull.cs). No blocking issues.
- The tests use
.Should(), which matches this project's AwesomeAssertions style. - Using async test methods to avoid CS4014 is reasonable, and the comment explains why.
- The
IsCompletedcheck, the completion of theTaskCompletionSourceand theWasToStringCalledcheck are deterministic.RunContinuationsAsynchronouslyis a good choice. - The nullable-flow check is implicit:
task.IsCompletedandawait taskafterAssert.IsNotNullonly compile warning-free if the[NotNull]annotation applies. Warnings are presumably errors in this repo.
Non-blocking suggestions:
- The three tests are near-duplicates. They could share a small helper, but explicit per-overload tests are acceptable.
- Nothing here proves "does not await" beyond
IsCompleted == false, becauseIsNotNullis synchronous. That is fine for a regression guard.
Test-value evidence: Not executed (read-only review of untrusted PR code). These are static predictions only. The tests should fail to compile or warn if the Task/Task<T> overloads lose their null-state annotation, but I did not verify that against the base revision.

Pull request
Summary
Add three MSTest framework unit tests that preserve statement-form
Assert.IsNotNullcalls inside async methods for nullableTaskandTask<int>. Cover the plain, string-message, and interpolated-message overloads, including nullable-flow narrowing, pending-task state, subsequent completion, and lazy interpolation.This catches a generic value-returning overload during the unit-test build: an existing null-check statement would otherwise start returning
Taskand produceCS4014. No production API or behavior changes are included.Relates to #10918. This is regression coverage only; it does not implement or close the feature request.
Design and decision record
The rationale is the downstream compatibility regression and the suggestion to explore an alternative API.
The existing packed-consumer regression already protects C# 12 consumers. These tests add an earlier guard to the normal framework unit-test compilation without adding another fixture or a new API. They use the internal
TestContainerframework and the existing AwesomeAssertions conventions. Each test owns itsTaskCompletionSource; there are no sleeps, shared mutable resources, or scheduling changes. A separate feature design record is not needed for this test-only change.Demonstration
This is a nonvisual test-only change, so screenshots/video are not applicable. The meaningful before/after observation is compilation of the same statement calls:
Validation and regression proof
Validation used the repository-pinned SDK
12.0.100-alpha.1.26556.115, Release configuration, and runtimes10.0.14/11.0.0-rc.3.26522.148.Source provenance: base
8bdfc29f415696738d845d56aca5d438d0a068deplus test-patch SHA-256A46D7B34123538B065668BD72D7CF1C89F02F2B872D612FC91681B5E464B25B6. Commit070fe43f2052c635631a2c60d7dd7df8629b0833was verified to contain that identical tested patch. These are local results, not a claim that CI has run on the commit.The commands below retain the executed arguments, with the machine-local artifact root replaced by
$evidence:AssertTests.IsNotNull*, net10.0AssertTests.IsNotNull*, net11.0TRX identities/counts and the generated JUnit XML were checked. The three added identities are:
IsNotNull_WhenPendingTasks_DoesNotAwaitAndLearnsNotNullIsNotNull_WhenPendingTasksAndMessage_DoesNotAwaitAndLearnsNotNullIsNotNull_WhenPendingTasksAndInterpolatedStringMessage_DoesNotAwaitAndLearnsNotNullAdditional package-consuming checks reused the unchanged
TaskConsumerSourcefromAssertSourceCompatibilityTests.cs, with its existing C# 12 and warnings-as-errors settings. Focused package production succeeded with:All three commands exited 0. Both compared
MSTest.TestFrameworkpackages were version5.0.0-dev, with the same alignedMSTest.Analyzerspackage. Separate feeds/caches were used, the effective compile/runtime assets were checked, and restored/package/runtime-copy hashes were compared.CS4014errors; execution intentionally not attempted.CS4014errors; execution intentionally not attempted.Package SHA-256 values:
0BF443AE3FFF1A3D76611A8E82E1C05C82CB57BBF5823C63E155261DE199FC4198730AAD409C2D9D37325EDA1ABD0EF9CF7407B1D6238C0FBDD0E4B6F9A7207368ADCFE7E4D302DB04B70654211A59B705A0E2D316C310351F2E448DDA6007B3The full acceptance host was not run: the repository-wide pack bootstrap could not resolve
powershellin childcmd.exeprocesses. The existing inner consumer was compiled/executed directly instead; this is not reported as an outer acceptance-test pass. Other unit-test targets and CI were not run locally. Detailed logs, TRX, patches, and the validation helper were retained locally rather than uploaded.Test-value evidence
Executed: detected. In a task-owned isolated checkout, retain the exact test source and add generic
IsNotNull<T>overloads returningTfor the string-message and interpolated-handler forms, while keeping the originalvoidoverloads. This is an API-shape counterfactual, not a proposed implementation. API analyzers were disabled only for that comparison to permit the deliberate untracked API addition; compiler warnings remained errors.Result: exit 1 with exactly six
CS4014errors inAssertTests.IsNull.cs, at lines 102, 103, 122, 123, 143, and 144: oneTaskand oneTask<int>call in each new test. This is a compilation-contract check, not six failed runtime tests or an unrelated setup failure. The independently packed C# 12 consumers also detected the same diagnostic as described above.Remove only those temporary overloads, retaining the tests, then run:
Result: exit 0; 9 executed/passed, 0 failed/skipped. The restored production source was verified byte-for-byte against the unchanged source in this PR, and the comparison checkout was removed after preserving the evidence.