Skip to content

Run all tests in the module #154

@matklad

Description

@matklad

We have a feature to invoke the right cargo test command for the test at caret:

image

(In VS Code, it is triggered by the Rust Run action)

Currently, it works only for #[test] functions, but it would be useful to run test modules as well.

Here's how the feature works currently:

runnables function discovers #[test] functions:
https://github.com/rust-analyzer/rust-analyzer/blob/0043d7e9c7f27af95cbf299d5b92ce28ba9945ab/crates/ra_editor/src/lib.rs#L119-L140

The function is exposed on Analysis struct (the API between language server and "compiler") here:
https://github.com/rust-analyzer/rust-analyzer/blob/0043d7e9c7f27af95cbf299d5b92ce28ba9945ab/crates/ra_analysis/src/lib.rs#L245-L248

in main_loop/handlers we add information about current Cargo target/package to it and handle, as JSON, to the editor:

https://github.com/rust-analyzer/rust-analyzer/blob/0043d7e9c7f27af95cbf299d5b92ce28ba9945ab/crates/ra_lsp_server/src/main_loop/handlers.rs#L255-L268

To support testing modules, we need to add fully-qualified module name to the command line. The steps to do so would be

  • move the runnable function from ra_editor to ra_analysis: ra_editor knows nothing about module
  • in the runnable function, detect not only functinons, but mods with test functions as well
  • make a function to compute a foo::bar::baz path to a module. See the implementation of crate_root function, which contains "traverse parent modules" logic.
  • add module path to a Runnable struct
  • adjust the handle_runnables function to construct a command line, using the full path

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions