intellij-rust icon indicating copy to clipboard operation
intellij-rust copied to clipboard

DOC: add line marker and test run configuration provider for doctests

Open Kobzol opened this issue 3 years ago • 2 comments

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.

Kobzol avatar Jul 22 '22 20:07 Kobzol

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

vlad20012 avatar Jul 27 '22 13:07 vlad20012

Yup, that's the situation that I described in the PR description. I have no idea what causes it though.

Kobzol avatar Jul 27 '22 13:07 Kobzol

This is amazing! It is very needed. When will it be shipped? <3

wdanilo avatar Sep 30 '22 17:09 wdanilo

@mchernyavsky Can you take a look please? :)

Kobzol avatar Sep 30 '22 17:09 Kobzol

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!

mwu-tow avatar Sep 30 '22 19:09 mwu-tow

@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.

mchernyavsky avatar Oct 03 '22 01:10 mchernyavsky

Awesome! Thank you very much.

Kobzol avatar Oct 03 '22 07:10 Kobzol

Some tests have been failed

vlad20012 avatar Oct 03 '22 08:10 vlad20012

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 avatar Oct 03 '22 09:10 Kobzol

@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:

2022-10-04 17-18-57 2022-10-04 17_20_06

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).

mchernyavsky avatar Oct 04 '22 15:10 mchernyavsky

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 avatar Oct 04 '22 17:10 Kobzol

@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

Screen Shot 2022-10-05 at 18 10 38

mchernyavsky avatar Oct 05 '22 16:10 mchernyavsky

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.

Kobzol avatar Oct 23 '22 08:10 Kobzol

Thanks!

Kobzol avatar Oct 25 '22 15:10 Kobzol

Build succeeded:

bors[bot] avatar Oct 25 '22 20:10 bors[bot]

@intellij-rust/technical-writers, seems like it's time to write the documentation!

intellij-rust-bot avatar Nov 07 '22 11:11 intellij-rust-bot

@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

KseniaStar avatar Dec 21 '22 15:12 KseniaStar