Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add api to run specific test #2

Merged
merged 6 commits into from
Jan 8, 2016
Merged

Conversation

nlamirault
Copy link
Contributor

With this file :

#[test]
fn it_works() {
}

#[test]
fn it_works_another() {

}

#[test]
fn just_a_test() {

}

When pointer is in it_works_another, with M-x cargo-process-current-test, cargo is called to run specific test :

-*- mode: cargo-process; default-directory: "~/Perso/cargo.el/var/hello_world/src/" -*-
Cargo-Process started at Mon Nov  2 12:24:19

cargo test it_works_another
     Running /home/nlamirault/Perso/cargo.el/var/hello_world/target/debug/hello_world-9f660b2810b4c89b

running 1 test
test it_works_another ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured

   Doc-tests hello_world

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured

There is also another method to run all test from current file, but actually it doesn't work :

-*- mode: cargo-process; default-directory: "~/Perso/cargo.el/var/hello_world/src/" -*-
Cargo-Process started at Mon Nov  2 12:19:12

cargo test it_works|it_works_another|just_a_test
/usr/bin/bash: it_works_another: command not found
/usr/bin/bash: just_a_test: command not found
An unknown error occurred

To learn more, run the command again with --verbose.

Another command which fails (only 2 tests are executed) :

cargo test it_works it_works_another just_a_test
Running target/debug/hello_world-9f660b2810b4c89b

running 2 tests
test it_works ... ok
test it_works_another ... ok

test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured

   Doc-tests hello_world

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured

I don't really know if it is possible to launch all tests from one file with Cargo.

Signed-off-by: Nicolas Lamirault nicolas.lamirault@gmail.com

Signed-off-by: Nicolas Lamirault <nicolas.lamirault@gmail.com>
@kwrooijen
Copy link
Owner

Hey, thanks for the PR. I'll take a look at it once I'm home.

Cargo: Run the tests."
(interactive)
(let ((name (cargo-process--get-current-test)))
(cargo-process--start "Test" (s-concat "cargo test " name))))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use concat instead of s-concat? Would be nice to not be dependent on 's library.

Signed-off-by: Nicolas Lamirault <nicolas.lamirault@gmail.com>
@kwrooijen
Copy link
Owner

A few things:

cargo test it_works|it_works_another|just_a_test

is failing because there are no spaces between the pipes, but this will still not work.

Taken from cargo help test:
--test NAME Test only the specified integration test

So let's say your file name is lib.rs, testing only that file should be
cargo test --test lib

Maybe you could try that instead?

(unless (and
(search-backward-regexp
(s-concat "^[[:space:]]*fn[[:space:]]*") nil t)
(save-excursion (go-end-of-defun) (< start (point))))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(go-end-of-defun) is undefined, where did this come from?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A Mistake. It must be rust-end-of-defun. Defined in rust-mode [1].

[1] : https://github.com/rust-lang/rust-mode/blob/master/rust-mode.el

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fine, then we should add rust-mode as a dependency (which isn't the case right now)

@nlamirault
Copy link
Contributor Author

Great ! I will try that

@kwrooijen
Copy link
Owner

These are some really cool additions btw, maybe give them keybindings? e.g.
"C-c C-c C-f" For the current function
"C-c C-c C-i" For the current file

Or something else.

(save-excursion (go-end-of-defun) (< start (point))))
(error "Unable to find test"))
(save-excursion
(search-forward "fn ")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not search-forward-regexp like the previous one? If you're going to use that regex again you should create a defconst to hold that value (instead of repeating it)

Signed-off-by: Nicolas Lamirault <nicolas.lamirault@gmail.com>
Signed-off-by: Nicolas Lamirault <nicolas.lamirault@gmail.com>
@marcbowes
Copy link

Any chance this can be merged? I'd really appreciate this functionality.

(As an aside, please crib from rspec-mode :-) That mode has been very useful for ruby development.)

@kwrooijen
Copy link
Owner

There are still a few things that need to be fixed, removal of the 's lib, stray comments, usage of the cargo test --test lib for file specific tests. I'm a bit busy right now but I might be able to look into these things later.

@marcbowes
Copy link

Thanks!

I downloaded a copy of your changes and played around with it. One thing I ran into is some projects use macros to define their tests, e.g. Cargo uses test! to get some consistent naming.

I think it'd be great if we could add arbitrary args to cargo test beyond just the automatic "run the current / all in this file" that this PR adds. For example, I added this:

;;;###autoload
(defun cargo-process-test-args (extra)
  "Run the Cargo test command with arguments.
Cargo: Run the tests with args."
  (interactive "sargs: ")
  (cargo-process--start "Test" (s-concat "cargo test " extra)))

@kwrooijen
Copy link
Owner

That snippet looks fine (except should use concat in favor of s-concat). Since this PR still needs a bit of fixing you could create a new PR, maybe add a shortcut key for it. Adding a custom test command would be nice since there are quite a few flags you can give.

    --lib                        Test only this package's library
    --bin NAME                   Test only the specified binary
    --example NAME               Test only the specified example
    --test NAME                  Test only the specified integration test target
    --bench NAME                 Test only the specified benchmark target
    --no-run                     Compile, but don't run tests
    -p SPEC, --package SPEC ...  Package to run tests for
    -j N, --jobs N               The number of jobs to run in parallel
    --release                    Build artifacts in release mode, with optimizations
    --features FEATURES          Space-separated list of features to also build
    --no-default-features        Do not build the `default` feature
    --target TRIPLE              Build for the target triple
    --manifest-path PATH         Path to the manifest to build tests for
    -v, --verbose                Use verbose output
    -q, --quiet                  No output printed to stdout
    --color WHEN                 Coloring: auto, always, never
    --no-fail-fast               Run all tests regardless of failure

Possibly adding auto completion for these flags in the future would be a great addition. But for now, if it benefits you, feel free to create a PR with your change. Or I can quickly add it myself since it's not too much work.

@marcbowes
Copy link

I'm in no hurry since I was able to make these changes locally to unblock myself.

The one thing that bothers me about this strategy is having to do it for each of the cargo commands. For example, cargo run also has a bunch of options and you might want to run examples in your IDE.

The one way to deal with this is to play keep-up. You can make or accept pull requests for changes that allow argument plumbing per cargo command. This falls flat in the face of custom cargo commands which may be public (https://github.com/kbknapp/cargo-extras) or private.

Thus, I think a better route would be to allow the top-level function (defun cargo-process--start (name command) accept optional args and then allow for each of the wrapper functions (e.g. cargo test/run) pass the optional args through either interactively or via a prefix arg.

Does that make sense?

@marcbowes
Copy link

(For my own education; I just hacked my snippet on top of this PR. I don't really know elisp.)

except should use concat in favor of s-concat

I did a bit of reading and it looks like these functions are identical? Is it just defined for convenience/consistency in magnars/s.el or is there some other benefit to that definition?

@kwrooijen
Copy link
Owner

Thus, I think a better route would be to allow the top-level function (defun cargo-process--start (name command) accept optional args and then allow for each of the wrapper functions (e.g. cargo test/run) pass the optional args through either interactively or via a prefix arg.

This could be done, we can simply add a &rest argument and concatenate the command. Would be a clean solution I think (if that's what you meant)

except should use concat in favor of s-concat

I did a bit of reading and it looks like these functions are identical? Is it just defined for convenience/consistency in magnars/s.el or is there some other benefit to that definition?

The reason is that they're identical. If we add s-concat we'll have to add 's as a dependency. This isn't a bad thing per se, but I'd like avoid unnecessary dependencies.

@marcbowes
Copy link

👍

Signed-off-by: Nicolas Lamirault <nicolas.lamirault@gmail.com>
Like that:

M-x cargo-process-current-test

-*- mode: cargo-process; default-directory: "~/Perso/cargo.el/var/hello_world/tests/" -*-
Cargo-Process started at Tue Jan  5 10:22:12

cargo test it_works_another
Running /home/nlamirault/Perso/cargo.el/var/hello_world/target/debug/hello_world-9f660b2810b4c89b

running 1 test
test it_works_another ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured

Running /home/nlamirault/Perso/cargo.el/var/hello_world/target/debug/lib-dff9d964ea838ea8

running 1 test
test it_works_another ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured

Doc-tests hello_world

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured

M-x cargo-process-current-file-tests

-*- mode: cargo-process; default-directory: "~/Perso/cargo.el/var/hello_world/tests/" -*-
Cargo-Process started at Tue Jan  5 10:19:53

cargo test --test lib
     Running /home/nlamirault/Perso/cargo.el/var/hello_world/target/debug/lib-dff9d964ea838ea8

     running 3 tests
     test it_works_another ... ok
     test it_works ... ok
     test just_a_test ... ok

     test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured

Signed-off-by: Nicolas Lamirault <nicolas.lamirault@gmail.com>
name))


;; (defun cargo-process--get-current-file-tests ()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be removed now, right? :)

@kwrooijen
Copy link
Owner

Looks great, thank you! Just fix those last 2 comments and I'll gladly merge :)
Edit: One last thing! Please update the readme with the new functions!

@kwrooijen
Copy link
Owner

I'll just merge this and add my own extra changes, had some extra time on my hands :) thanks for this! 👍

kwrooijen added a commit that referenced this pull request Jan 8, 2016
Add api to run specific test
@kwrooijen kwrooijen merged commit 5edca5d into kwrooijen:master Jan 8, 2016
@kwrooijen
Copy link
Owner

Just for clarification, here are my changes. a6e367e

@nlamirault
Copy link
Contributor Author

Great

farva added a commit to farva/cargo.el that referenced this pull request Oct 30, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants