|
| 1 | +# Suggest tests tool |
| 2 | + |
| 3 | +This chapter is about the internals of and contribution instructions for the |
| 4 | +`suggest-tests` tool. For a high-level overview of the tool, see |
| 5 | +[this section](../building/suggested.md#x-suggest). This tool is currently in a |
| 6 | +beta state and is tracked by [this](https://github.com/rust-lang/rust/issues/109933) |
| 7 | +issue on Github. Currently the number of tests it will suggest are very limited |
| 8 | +in scope, we are looking to expand this (contributions welcome!). |
| 9 | + |
| 10 | +## Internals |
| 11 | + |
| 12 | +The tool is defined in a separate crate ([`src/tools/suggest-tests`](https://github.com/rust-lang/rust/blob/master/src/tools/suggest-tests)) |
| 13 | +which outputs suggestions which are parsed by a shim in bootstrap |
| 14 | +([`src/bootstrap/suggest.rs`](https://github.com/rust-lang/rust/blob/master/src/bootstrap/suggest.rs)). |
| 15 | +The only notable thing the bootstrap shim does is (when invoked with the |
| 16 | +`--run` flag) use bootstrap's internal mechanisms to create a new `Builder` and |
| 17 | +uses it to invoke the suggested commands. The `suggest-tests` crate is where the |
| 18 | +fun happens, two kinds of suggestions are defined: "static" and "dynamic" |
| 19 | +suggestions. |
| 20 | + |
| 21 | +### Static suggestions |
| 22 | + |
| 23 | +Defined [here](https://github.com/rust-lang/rust/blob/master/src/tools/suggest-tests/src/static_suggestions.rs). |
| 24 | +Static suggestions are simple: they are just [globs](https://crates.io/crates/glob) |
| 25 | +which map to a `x` command. In `suggest-tests`, this is implemented with a |
| 26 | +simple `macro_rules` macro. |
| 27 | + |
| 28 | +### Dynamic suggestions |
| 29 | + |
| 30 | +Defined [here](https://github.com/rust-lang/rust/blob/master/src/tools/suggest-tests/src/dynamic_suggestions.rs). |
| 31 | +These are more complicated than static suggestions and are implemented as |
| 32 | +functions with the following signature: `fn(&Path) -> Vec<Suggestion>`. In |
| 33 | +other words, each suggestion takes a path to a modified file and (after running |
| 34 | +arbitrary Rust code) can return any number of suggestions, or none. Dynamic |
| 35 | +suggestions are useful for situations where fine-grained control over |
| 36 | +suggestions is needed. For example, modifications to the `compiler/xyz/` path |
| 37 | +should trigger the `x test compiler/xyz` suggestion. In the future, dynamic |
| 38 | +suggestions might even read file contents to determine if (what) tests should |
| 39 | +run. |
| 40 | + |
| 41 | +## Adding a suggestion |
| 42 | + |
| 43 | +The following steps should serve as a rough guide to add suggestions to |
| 44 | +`suggest-tests` (very welcome!): |
| 45 | + |
| 46 | +1. Determine the rules for your suggestion. Is it simple and operates only on |
| 47 | + a single path or does it match globs? Does it need fine-grained control over |
| 48 | + the resulting command or does "one size fit all"? |
| 49 | +2. Based on the previous step, decide if your suggestion should be implemented |
| 50 | + as either static or dynamic. |
| 51 | +3. Implement the suggestion. If it is dynamic then a test is highly recommended, |
| 52 | + to verify that your logic is correct and to give an example of the suggestion. |
| 53 | + See the [tests.rs](https://github.com/rust-lang/rust/blob/master/src/tools/suggest-tests/src/static_suggestions.rs) |
| 54 | + file. |
| 55 | +4. Open a PR implementing your suggestion. **(TODO: add example PR)** |
0 commit comments