rust-lang / rust-lang/rust

Change behavior of `--exact` doc-tests

Open
#147,653 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-doctests A-libtest C-discussion T-testing-devex
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Problem

I have this code:

#[test]
fn foo() {}

#[test]
fn foo_more() {}

/// ```
/// let foo = 10;
/// ```
fn doc_foo() {}

/// ```
/// let foo_more = 10;
/// ```
fn doc_foo_more() {}

Command cargo test foo runs both (non-doc) tests. Command cargo test foo -- --exact runs 1 test named foo.

Command cargo test --doc doc_foo runs both doc tests. Command cargo test --doc doc_foo -- --exact doesn't run any tests, but I expect it to run 1 doc test named doc_foo.

cargo test -- --list shows this 4 tests:

foo: test
foo_more: test

src\lib.rs - doc_foo (line 7): test
src\lib.rs - doc_foo_more (line 12): test

I am able to run specific doc test named doc_foo using cargo test --doc 'doc_foo\ (line\ 7)' thanks to this issue on rust-lang.org.

This solution is not ideal imo because of the need for escape sequences and line number. I think its very hard to use this in complex cases such as doctests in structs or inline modules, basically places with namespace nesting.

This issue originates from my issue in rust-analyzer but the more I think about it, the more I feel like this problem is not about rust-analyzer. It's too tedious to run specific doc test even manually in a shell.

Proposed Solution

Change behavior of --exact flag to match only the name of the test in doc tests by default as it does in non-doc tests.

  • This solution still allows to run specific test if you modify test name to match 'test_name\ (line\ 4)'
  • Currently existing scripts will remain unchanged
Notes

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the documented cargo test --doc doc_foo and --exact commands, comparing their output with cargo test -- --list. Trace the doctest name filtering entry point and add coverage showing that --exact matches doc_foo without requiring the escaped line suffix, while preserving the existing full-name behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
cli, testing-qa
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.