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

Better error message when changing to a directory that doesn't exist #109

Closed
jyn514 opened this issue Nov 18, 2020 · 4 comments · Fixed by #110
Closed

Better error message when changing to a directory that doesn't exist #109

jyn514 opened this issue Nov 18, 2020 · 4 comments · Fixed by #110
Labels
enhancement Improve the expected

Comments

@jyn514
Copy link

jyn514 commented Nov 18, 2020

Currently, the error looks like this:

thread 'reports_broken_links' panicked at 'called `Result::unwrap()` on an `Err` value: Os { code: 2, kind: NotFound, message: "No such file or directory" }', /home/joshua/.local/lib/cargo/registry/src/github.aaakk.us.kg-1ecc6299db9ec823/assert_cmd-1.0.1/src/assert.rs:59:36

The actual issue is that I tried to change to a directory that doesn't exist:

	Command::cargo_bin("cargo-deadlinks")
		.unwrap()
		.arg("deadlinks")
		.current_dir("./tests/broken_links")  // this directory does not exist

This is made worse by the backtrace, which points to the wrong line:

   3: core::result::Result<T,E>::unwrap
             at /home/joshua/.local/lib/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/result.rs:973
   4: <&mut std::process::Command as assert_cmd::assert::OutputAssertExt>::assert
             at /home/joshua/.local/lib/cargo/registry/src/github.aaakk.us.kg-1ecc6299db9ec823/assert_cmd-1.0.1/src/assert.rs:59
   5: broken_links::reports_broken_links
             at ./tests/broken_links.rs:9  // this is `Command::cargo_bin`, not `current_dir`
@epage
Copy link
Contributor

epage commented Nov 23, 2020

at ./tests/broken_links.rs:9 // this is Command::cargo_bin, not current_dir

Pure speculation: the panic doesn't happen in current_dir but when you call asset(). Since its a single expression, the backtrace points to the first line of the expression

Using the new #[track_caller] would help in that it'd point to your code, rather than assert_cmd code, but that isn't available for trait items and we're using extension traits.

Not sure what there is that we could do at this point. Any ideas?

@jyn514
Copy link
Author

jyn514 commented Nov 24, 2020

It looks like the panic comes from here:

let output = self.output().unwrap();

I think if you removed the unwrap() and returned an error instead it would be more helpful, or if you want to avoid changing the API you could panic with 'failed to run the command' or something like that.

@epage
Copy link
Contributor

epage commented Nov 24, 2020

I think if you removed the unwrap() and returned an error instead it would be more helpful,

The caller is wanting to assert on the result of the run, so panicing seems appropriate there. If caller needs access to the error, they should explicitly call .output().

However, you are right that we should have a custom panic! in there rather than just an unwrap.

@epage epage added the enhancement Improve the expected label Nov 24, 2020
epage pushed a commit to epage/assert_cmd that referenced this issue Nov 24, 2020
Users had to infer the reason for the panic based on the location.
Instead, we should tell them what failed so they can more quickly get up
and going on fixing it.

Fixes assert-rs#109
epage pushed a commit to epage/assert_cmd that referenced this issue Nov 24, 2020
Users had to infer the reason for the panic based on the location.
Instead, we should tell them what failed so they can more quickly get up
and going on fixing it.

Fixes assert-rs#109
@epage
Copy link
Contributor

epage commented Nov 24, 2020

1.0.2 should now be released with a proper panic message

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improve the expected
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants