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

Give a better error message if Cargo.toml is not present #67

Merged
merged 1 commit into from
Oct 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
#### Fixes

* Take `CARGO_TARGET_DIR` into account when looking for the target directory. [PR#66]
* Give a better error message if Cargo.toml is not present. [PR#67]

[PR#66]: https://github.com/deadlinks/cargo-deadlinks/pull/66
[PR#67]: https://github.com/deadlinks/cargo-deadlinks/pull/67

#### Changes

Expand Down
23 changes: 18 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,23 @@ pub fn metadata_run(additional_args: Option<String>) -> Result<Metadata, ()> {
cmd.arg(&additional_args);
}

let output = cmd.output().unwrap();
let stdout = std::str::from_utf8(&output.stdout).unwrap();
let meta = serde_json::from_str(stdout).unwrap();
Ok(meta)
let fail_msg = "failed to run `cargo metadata` - do you have cargo installed?";
let output = cmd
.stdout(process::Stdio::piped())
.spawn()
.expect(fail_msg)
.wait_with_output()
.expect(fail_msg);

if !output.status.success() {
// don't need more info because we didn't capture stderr;
// hopefully `cargo metadata` gave a useful error, but if not we can't do
// anything
return Err(());
}

let stdout = std::str::from_utf8(&output.stdout).expect("invalid UTF8");
Ok(serde_json::from_str(stdout).expect("invalid JSON"))
}

/// Initalizes the logger according to the provided config flags.
Expand Down Expand Up @@ -143,7 +156,7 @@ fn determine_dir() -> PathBuf {
manifest.target_directory.join("doc").join(package_name)
}
Err(_) => {
error!("Could not find a Cargo.toml.");
error!("help: if this is not a cargo directory, use `--dir`");
::std::process::exit(1);
}
}
Expand Down
26 changes: 20 additions & 6 deletions tests/simple_project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ extern crate assert_cmd;
extern crate predicates;

use assert_cmd::prelude::*;
use predicate::str::contains;
use predicates::prelude::*;
use std::env;
use std::process::Command;

mod simple_project {
Expand Down Expand Up @@ -38,10 +40,22 @@ mod simple_project {
}

#[test]
fn it_checks_okay_project_correctly() {
use predicate::str::contains;
fn it_gives_help_if_cargo_toml_missing() {
Command::cargo_bin("cargo-deadlinks")
.unwrap()
.arg("deadlinks")
.current_dir(env::temp_dir())
.assert()
.failure()
.stderr(
contains("help: if this is not a cargo directory, use `--dir`")
.and(contains("error: could not find `Cargo.toml`")),
);
}

std::env::remove_var("CARGO_TARGET_DIR");
#[test]
fn it_checks_okay_project_correctly() {
env::remove_var("CARGO_TARGET_DIR");

// cargo-deadlinks fails when docs have not been generated before
remove_all("./tests/simple_project/target");
Expand All @@ -63,12 +77,12 @@ mod simple_project {
assert_doc().success();

// NOTE: can't be parallel because of use of `set_var`
std::env::set_var("CARGO_TARGET_DIR", "target2");
env::set_var("CARGO_TARGET_DIR", "target2");
remove_all("./tests/simple_project/target2");
assert_doc().success();

std::env::remove_var("CARGO_TARGET_DIR");
std::env::set_var("CARGO_BUILD_TARGET", "x86_64-unknown-linux-gnu");
env::remove_var("CARGO_TARGET_DIR");
env::set_var("CARGO_BUILD_TARGET", "x86_64-unknown-linux-gnu");
remove_all("./tests/simple_project/target");
// This currently breaks due to a cargo bug: https://github.com/rust-lang/cargo/issues/8791
assert_doc().failure();
Expand Down