Better Dangling snapshots detection

Merged
SI
2mo ago
+2255-35

Dangling snapshot detection

DanglingSnapshots.Run() (experimental, VerifyDanglingSnapshots) previously decided
whether a .verified. file was redundant by looking at its name. Any path containing
.Net, .DotNet, .Mono., .OSX., .Windows. or .Linux. was skipped, which made
every UniqueFor* snapshot permanently invisible — no run on any framework or OS
would report SomeTests.Deleted.DotNet9_0.verified.txt. It also swallowed ordinary
names that merely start a word the same way, such as SomeTests.NetworkClient.verified.txt,
and any file under a directory whose name contained one of those tokens.

Detection is now based on which tests exist rather than on what files are called.

Snapshots of deleted tests are always reported. Each verification records the
name it produces snapshots under, up to the end of the type and method. A file whose
name starts with no recorded test belongs to no run of the project, whatever
uniqueness it carries, so it is reported regardless. The name is recorded even when
a test produces no file — an inline snapshot, a verification that threw, or one whose
targets were all excluded — so those tests still count as existing.

Multi targeted projects are resolved by manifest, not by guesswork. Each run
writes what it tracked to obj/VerifyDangling/{Configuration}/{TargetFramework}.txt.
The directory is shared across target frameworks, so a run reads back what the other
runs tracked. Once every framework in TargetFrameworks has a manifest, the union is
the complete set of snapshots the project owns, and a framework segment stops
excusing a file. Manifests are ignored if they predate the running assembly, so a
stale one cannot mask a dangling file, and they are removed by a clean. Tests behind
an #if NET48 are recorded too, so the other frameworks' runs do not report their
snapshots.

Uniqueness segments are matched whole, against the values Namer produces for
the current run, rather than searched for as substrings. A segment naming a different
OS or architecture belongs to a run on another machine and is left alone; one naming
this run's values does not excuse a file that was not produced.

UniqueForArchitecture snapshots from another machine are no longer reported as
dangling.

NUnit

NUnit's Microsoft.Testing.Platform runner prints a teardown failure but does not
count it, so a dangling snapshot was reported and the run still passed with exit
code zero. This holds for [SetUpFixture] and [TestFixture] alike, and no teardown
hook's failure is surfaced. The NUnit integration now sets the process exit code to 1
when the check fails and writes the reason after the runner's summary. Through
dotnet test the run is reported as failed.

MSTest, xUnit v3, Expecto, Fixie and TUnit already failed correctly and are unchanged.

Other

  • DanglingSnapshots.Run() now returns quietly when no verification ran, instead of
    throwing "Assembly must be set before calling this method." over whatever caused
    the run to verify nothing.
  • Documented setups added for TUnit ([After(TestSession)]), Expecto (the entry
    point) and Fixie (the end of IExecution.Run), each with a usage project.

Known limitations

  • UniqueForOSPlatform and UniqueForArchitecture cannot be settled by manifests,
    since those runs are on other machines. A segment naming an OS or architecture
    other than the current one is always left alone.
  • UniqueForAssemblyConfiguration has no enumerable value set, so it is not
    recognised as uniqueness. A snapshot only another configuration produces is
    reported.
  • UseUniqueDirectory in split mode puts .verified. in the directory name, so
    those files are outside the scan and are never reported.
  • A relative UseDirectory leaves .. in the recorded path, which does not match
    the scan's normalised path, and its snapshots are reported.
  • The scan walks bin and obj.

Build metadata

Two AssemblyMetadataAttribute entries are now emitted by Verify.props:

  • Verify.TargetFramework — the single target framework the assembly was built for.
  • Verify.DanglingDirectory — where dangling snapshot manifests are written.

AttributeReader.TryGetTargetFramework(Assembly, out string?) reads the first.