Name the guarded API in ThrowIfVerifyHasBeenRun

Merged
SI
3mo ago
+32-9

The message named the calling code as "The API":

The API 'MyTests.SomeTest' must be called prior to any Verify has run.

which points at the wrong place to move into a [ModuleInitializer].

First attempt read frame 0 instead of frame 1, and CI caught that it does not hold. Whether the guarded API has a stack frame of its own is a JIT decision: on net48 in release VerifierSettings.IgnoreMembers is inlined into its caller, so frame 0 is the caller. No frame index is correct — with the original GetFrame(1) the same net48 release run names Xunit.Assert.RecordException, two frames adrift, because the test lambda is inlined too.

So the name now comes from [CallerMemberName], which the compiler fills in at each of the 111 call sites. It cannot drift with inlining, costs nothing at run time, and needs no StackTrace (which also makes it safe under trimming, where frames can be stripped).

Two consequences worth calling out:

  • The declaring type is gone from the message — CallerMemberName only supplies the member. It now reads The API 'IgnoreMembers' must be called prior…. No snapshot or doc captures this message, so nothing else moved.
  • ThrowIfVerifyHasBeenRun gains an optional parameter. Source compatible, but a binary break for anything already compiled against the no-arg signature. Every caller in this repo is rebuilt together; a third party adapter shipped as a prebuilt assembly would need a recompile. That seems acceptable for 32.0.0, but it is your call.

ThrowIfVerifyHasBeenRunTests.NamesTheApi calls a real guarded API and asserts the message names it and not the test. Verified across Debug and Release × net48 and net11.0 this time, since that axis is exactly what the first attempt missed.

Release suites: Verify.Tests net48 (1197), net11.0 (1287), net9.0 (1293); StaticSettingsTests, Verify.NUnit.Tests, Verify.MSTest.Tests, Verify.XunitV3.Tests, ApplyScrubbersTests, Verify.ExceptionParsing.Tests all green.