DOC: add line marker and test run configuration provider for doctests
This PR adds line markers and run configurations for doctests. The isDoctest parameter is a bit of a hack, but I wasn't sure how to solve it more elegantly. I would like to change the kind of the passed target e.g. to Unknown, so that no other flag like --lib or --bin is passed to cargo test, since it must contain only --doc.
Tests work fine, but in the IDE it behaves in a strange way. If I click on the test gutter of a doctest, it creates and runs the test correctly. But then if I add a normal #[test] function to the same file and execute that, from that moment on the gutters on the doctests go crazy and behave like if they were the main function or something like that, they offer me to profile them etc. Any idea what might be causing that? :) Maybe the test locator URL?
Fixes: https://github.com/intellij-rust/intellij-rust/issues/9098
changelog: You can now create test run configurations from doctests.
I found a problem:
/// ```
/// use rs_test::foo;
/// eprintln!("11111");
/// foo();
/// ```
pub fn foo() {}
#[test]
fn test3333() {
println!("2222")
}
If you run test3333 and then tries to run the doctest, test3333 will run anyway. It happens whether you run the test using a line marker or Ctrl+Shift+F10.
I'm not very familiar with this subsystem, so I think we @mchernyavsky help
Yup, that's the situation that I described in the PR description. I have no idea what causes it though.
This is amazing! It is very needed. When will it be shipped? <3
@mchernyavsky Can you take a look please? :)
I have missed this functionality for quite a while. I'm looking forward for this to be merged and delivered! Many thank @Kobzol for implementing this!
@Kobzol Hi! I have fixed the issue you described in the description. And I also fixed line marker icons - now they show doc-tests statuses (passed/failed) as well as for regular tests.
Awesome! Thank you very much.
Some tests have been failed
Sorry, I didn't notice. That's interesting, it looks like here:
rust("lib.rs", """
/*caret*//// ```
/// 1;
/// ```
/// ```
/// 2;
/// ```
pub fn doctest() {}
""")
The test is named doctest (line 1), which is correct, but it also looks like the test location redirects to line 1, which for some reason in the test is the line /// 1;. It looks like the test location counts lines from 0 and not from 1.
For cargo the line is ok, for the test icon the line is ok, but for the test it's offset by one. @mchernyavsky I suppose that this was caused by https://github.com/intellij-rust/intellij-rust/pull/9105/commits/3585fa8035721a842d6635dba762ad3f860e3ca3. What does that commit solve? When I revert it, it looks like everything works fine, and the tests pass. The doctest test icons also seem to look correct.
@Kobzol
When I revert it, it looks like everything works fine, and the tests pass. The doctest test icons also seem to look correct.
Hmm, for me doctest icons are not updating without https://github.com/intellij-rust/intellij-rust/commit/3585fa8035721a842d6635dba762ad3f860e3ca3:

You can check it during debugging, we use different urls
- when reading the test results:
https://github.com/Kobzol/intellij-rust/blob/d19258df95ffdb5c6f474b88ed4ac75e4c7497c5/src/main/kotlin/org/rust/ide/lineMarkers/CargoTestRunLineMarkerContributor.kt#L76
- and when writing the test results:
https://github.com/JetBrains/intellij-community/blob/6155f366c3b6b1aa86aac39d64a4a606f4f33385/platform/smRunner/src/com/intellij/execution/testframework/sm/runner/ui/SMTestRunnerResultsForm.java#L822
I have found another issue - for the following code:
/// ```
/// eprintln!("doc-test");
/// assert!(false);
/// ```
#[test]
fn test() {
println!("test");
assert!(false);
}
the IDE shows the linemarker for the doc-test, but cargo can't find the test (probably because of #[test] attribute).
Aha, I thought that the problem was with the location of the icons, which is correct even without that commit, but the problem is with their updating. And indeed without the commit they don't update correctly.
So what should I modify to make the tests pass? :thinking:
@Kobzol
So what should I modify to make the tests pass? 🤔
Looks like the code snippet has an extra line at the beginning, so the old answer for the test is not correct
Hmm. I massaged the tests to make them pass, but I couldn't get it to work when the caret was on the very first line.
Thanks!
@intellij-rust/technical-writers, seems like it's time to write the documentation!
@intellij-rust/technical-writers, seems like it's time to write the documentation!
Done! Link to docs: https://plugins.jetbrains.com/plugin/8182-rust/docs/rust-doctests.html