Skip to content

Fix CommandGuesser mislabelling suites from incidental path substrings - #1251

Merged
sferik merged 1 commit into
mainfrom
command-guesser-path-segment-matching
Aug 7, 2026
Merged

sferik merged 1 commit into
mainfrom
command-guesser-path-segment-matching

Conversation

@sferik

@sferik sferik commented Aug 7, 2026 •

Copy link
Copy Markdown
Collaborator

CommandGuesser matches its framework patterns against "#{$PROGRAM_NAME} #{ARGV.join(' ')}", and those patterns were bare substrings, so a test/ anywhere in that string decided the suite name. A Ruby installed under a latest/bin directory (the layout mise creates alongside the versioned one) puts test/ into the path of every binary run through it, and because test/ is checked ahead of spec/, RSpec and Cucumber suites alike came out as Unit Tests.

Under merging the command name is the resultset key, so this is wrong data rather than a cosmetic label. A mislabelled suite is filed under the wrong key, letting two genuinely different suites merge into each other rather than failing loudly.

The false positive also fires from ARGV, so paths inside the project are affected too. rspec spec/greatest/foo_spec.rb and cucumber features/contest/foo.feature both returned Unit Tests. Sanitising only $PROGRAM_NAME, as the issue proposes, would leave this half standing. rspec features works today only by accident. It returns RSpec because the unanchored spec pattern matches the letters inside the word rspec and is checked ahead of features. Tightening the patterns without compensating would flip that case to Cucumber Features.

The patterns now require the keyword to begin a path segment or an argument, so latest/, contest/, and greatest/ no longer read as test/. This applies to $PROGRAM_NAME and ARGV alike, which covers both halves of the bug.

An rspec or cucumber binary names the framework regardless of what surrounds it on the command line, which keeps rspec features reporting as RSpec. An RSpec suite whose examples live in features/ is still an RSpec suite, and after anchoring the executable name is the only thing that says so. Generic runners are deliberately absent from that table, so ruby test/integration/foo_test.rb and rake's test loader still fall through to the path patterns that draw the unit, functional, and integration distinction.

That executable comes from $PROGRAM_NAME, which defaults.rb now records separately as CommandGuesser.original_program_name, because recovering it from the flattened command by taking the first token would truncate a program path containing a space and hand /opt/My Ruby/bin/rspec features to the features pattern. Assigning the command clears the recorded name so the two cannot describe different runs, and callers that set only the command still get the first-token derivation.

Reported by @andriytyurnikov in #1249.

@sferik
sferik requested a balanced review from Copilot August 7, 2026 11:14

This comment was marked as resolved.

This comment was marked as resolved.

@andriytyurnikov

Copy link
Copy Markdown
Contributor

Verified on the machine that reproduces it, at 61055d0. The 3 examples in spec/formatter/html_formatter_spec.rb and the 6 cucumber scenarios named in the issue all pass without the PATH workaround. Full suite: 1227 examples / 0 failures / 2 pending, 102 scenarios / 658 steps.

Issue repro, plus your ARGV half and the cases the anchoring could have broken:

                                                 main                #1251
/opt/rubies/latest/bin/rspec                   => Unit Tests          RSpec
/usr/local/contest/bin/cucumber                => Unit Tests          Cucumber Features
/home/dev/projects/greatest/bin/rspec features => Unit Tests          RSpec
rspec spec/greatest/foo_spec.rb                => Unit Tests          RSpec
cucumber features/contest/foo.feature          => Unit Tests          Cucumber Features
rspec features                                 => RSpec               RSpec
ruby test/unit/foo_test.rb                     => Unit Tests          Unit Tests
ruby test/functional/foo_test.rb               => Functional Tests    Functional Tests
ruby test/integration/foo_test.rb              => Integration Tests   Integration Tests

You were right not to take the $0 route — rspec spec/greatest/foo_spec.rb is the half I missed.

One real gap. The anchors are forward-slash-only, so Windows backslash arguments lose matches main caught:

C:\Ruby\bin\ruby.exe spec\foo_spec.rb    main => RSpec    #1251 => Unknown Test Framework

spec followed by \ fails SEGMENT_END. Adding \\ to both classes restores it without reintroducing the bug:

SEGMENT_START = %r{(?<![^\s/\\])}
SEGMENT_END   = %r{(?=[\s/\\]|\z)}

With that, the Windows case reads RSpec again, latest/, contest/ and greatest/ are still rejected, suite stays green, rubocop clean. CI can't catch this: windows-latest invokes with forward slashes, and the executable table short-circuits before the patterns run. (rspec.bat spec\foo_spec.rb is fine — File.basename splits on \ where File::ALT_SEPARATOR is set. It only looks broken if you test on Unix.)

Second, minor — the only other place this reads worse than main:

SimpleCov::CommandGuesser.original_run_command = "/opt/My Ruby/bin/rspec features"
# main => RSpec    #1251 => Cucumber Features

The first-token fallback yields /opt/My, the executable check misses, and features wins. defaults.rb sets both, so this is third-party callers only. Narrow enough to leave, but it is the case original_program_name exists for.

Unrelated: CHANGELOG.md conflicts with main — #1252 added a Bugfixes heading to the same Unreleased block.

ruby 4.0.6 +PRISM [arm64-darwin25], mise, ~/.local/share/mise/installs/ruby/latest/bin/rspec.

@sferik

sferik commented Aug 7, 2026

Copy link
Copy Markdown
Collaborator Author

@copilot resolve the merge conflicts in this pull request

Copilot AI commented Aug 7, 2026

Copy link
Copy Markdown

@copilot resolve the merge conflicts in this pull request

Resolved by merging origin/main into this branch and fixing the CHANGELOG.md conflict in commit 986d62c.

@sferik
sferik force-pushed the command-guesser-path-segment-matching branch 2 times, most recently from b88215e to 07c6f36 Compare August 7, 2026 19:36
@sferik
sferik requested a balanced review from Copilot August 7, 2026 19:54

This comment was marked as resolved.

The framework patterns were bare substrings matched against
"#{$PROGRAM_NAME} #{ARGV.join(' ')}", so a "test/" anywhere in that
string decided the suite name. A Ruby installed under a "latest/bin"
directory (the layout mise creates) put "test/" in the path of every
binary run through it, and because "test/" is checked before "spec/",
RSpec and Cucumber suites alike came out as "Unit Tests". Arguments were
affected too, so "rspec spec/greatest/foo_spec.rb" was mislabelled the
same way. Under merging the command name is the resultset key, so a
mislabelled suite was filed under the wrong key rather than failing
loudly.

The patterns now match only at a path segment boundary, where a boundary
is the start of the string, whitespace, a forward slash, or a backslash.
Counting only forward slashes would drop matches the bare substrings used
to make on a Windows argument like "spec\foo_spec.rb", and no CI job
catches that: windows-latest invokes with forward slashes, and a
recognized executable short-circuits before the patterns run.

Segment matching alone would have regressed "rspec features", an RSpec
suite whose examples live in features/, which passed before only because
the unanchored "spec" pattern matched the letters inside "rspec". So the
invoked executable is now consulted first. Generic runners are
deliberately absent from that table, letting "ruby test/integration/
foo_test.rb" and rake's test loader still reach the path patterns that
draw the unit, functional, and integration distinction.

The executable is read from $PROGRAM_NAME, recorded separately as
CommandGuesser.original_program_name. Recovering it from the flattened
command by taking the first token would truncate a program path that
contains a space, so "/opt/My Ruby/bin/rspec features" would lose the
executable and be claimed by the features pattern. Assigning the
flattened command clears the recorded name so the two cannot describe
different runs, and callers that set only the command still get the
first-token derivation.

Reported with an exemplary diagnosis by @andriytyurnikov, who also
verified the fix on an affected machine and found the Windows gap.

Resolves #1249.
@sferik
sferik force-pushed the command-guesser-path-segment-matching branch from 07c6f36 to 344664e Compare August 7, 2026 20:28
@sferik
sferik merged commit a2c9569 into main Aug 7, 2026
20 checks passed
@sferik
sferik deleted the command-guesser-path-segment-matching branch August 12, 2026 19:51
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.

4 participants