Skip to content

fix: stop sibling subtest names accumulating in snapshot paths - #4

Open
yuu-no wants to merge 1 commit into
mohawk2:masterfrom
yuu-no:fix/sibling-subtest-names
Open

yuu-no wants to merge 1 commit into
mohawk2:masterfrom
yuu-no:fix/sibling-subtest-names

Conversation

@yuu-no

@yuu-no yuu-no commented Oct 16, 2026

Copy link
Copy Markdown
Contributor

Problem

A regression from #3 (released in 0.061): the pre_subtest callback pushes each subtest name onto the parent hub's meta and never removes it. A finished sibling's name therefore leaks into the snapshot path of every later sibling, and into the parent level once the subtests have ended. This happens under both Test::More and Test2::V0.

subtest outer => sub {
  for my $name (qw(a b c)) {
    subtest $name => sub { is_deeply_snapshot $name, 'v' };
  }
};
subtest 0.06 0.061
a outer/a/v outer/a/v
b outer/b/v outer/a/b/v
c outer/c/v outer/a/b/c/v

This contradicts the POD ("any subtests names surrounding and including this one"). It also means that adding, removing or reordering one subtest changes the path of every sibling after it. Existing 0.06 snapshots in loop-generated sibling subtests fail with No snapshot filename ... found.

Fix

  • The callback now records only the name of the running child on the parent hub. It overwrites that value each time instead of appending.
  • _get_filename takes one name for each Test2::Hub::Subtest on the stack:
    • the name Test::Builder stored on that hub, if any. This is what 0.06 used, and it covers both subtest and Test::Builder->child.
    • otherwise, the name recorded on the hub below it. This covers Test2::V0's subtest.
  • Hubs that are not subtests, such as the one intercept pushes, add no name.
  • The // operator is gone, so the code builds again on the declared MIN_PERL_VERSION (5.8.3).

Known limitation: a hub that is a subclass of Test2::Hub::Subtest, created by a mechanism that neither fires pre_subtest callbacks nor sets a Test::Builder name, could still pick up a stale name. Test2::AsyncSubtest might be one; I have not checked.

Tests

t/subtest-names-testmore.t and t/subtest-names-test2.t check the generated paths in these cases:

  • nested sibling subtests
  • the parent level after its children have finished
  • the top level
  • intercept
  • Test::Builder->child, Test::More file only

Both files fail on 0.061 and pass with this change. prove -l t passes.

Note for the release

Snapshots generated with 0.061 for sibling subtests were written to the accumulated paths. Those users will need to regenerate them, or move them back to the 0.06 paths. I left Changes and $VERSION to you.

🤖 Generated with Claude Code

Since 0.061 the pre_subtest callback pushed each subtest name onto the
parent hub's meta and never removed it, so a finished sibling's name
leaked into the path of every later sibling (and into the parent level
after the subtests ended), e.g. outer/a/b/v instead of outer/b/v.

Record only the running child's name on the parent hub, and when
building the path use, for each Test2::Hub::Subtest on the stack, the
name Test::Builder stored on that hub (as 0.06 did, covering subtest()
and Test::Builder->child) or else the name recorded on the hub below
it (Test2::V0's subtest). Non-subtest hubs such as intercept's add no
name. This also drops the `//` operator, which MIN_PERL_VERSION 5.8.3
does not have.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant