Skip to content

Commit

Permalink
Add tests for CLI behavior
Browse files Browse the repository at this point in the history
This caught a bug! Parse errors should be printed to stderr, not stdout.
  • Loading branch information
jyn514 authored and Joshua Nelson committed Nov 27, 2020
1 parent 4e58697 commit 94854e0
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/bin/cargo-deadlinks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ fn main() {
let args: MainArgs = match parse_args() {
Ok(args) => args,
Err(err) => {
println!("error: {}", err);
eprintln!("error: {}", err);
std::process::exit(1)
}
};
Expand Down
1 change: 1 addition & 0 deletions tests/broken_links/hardcoded-target/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<a href="./x.html"></a>
9 changes: 9 additions & 0 deletions tests/cli_args/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "cli_args"
version = "0.1.0"
authors = ["Joshua Nelson <jyn514@gmail.com>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
2 changes: 2 additions & 0 deletions tests/cli_args/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
//! Links to [Private]
struct Private;
68 changes: 67 additions & 1 deletion tests/simple_project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ extern crate assert_cmd;
extern crate predicates;

use assert_cmd::prelude::*;
use predicate::str::contains;
use predicate::str::{contains, starts_with};
use predicates::prelude::*;
use std::env;
use std::path::Path;
Expand Down Expand Up @@ -142,3 +142,69 @@ mod workspace {
assert_doc("./tests/workspace", &[]).success();
}
}

mod cli_args {
use super::*;

#[test]
fn it_passes_arguments_through_to_cargo() {
remove_all("./tests/cli_args/target");
deadlinks()
.current_dir("./tests/cli_args")
.arg("--")
.arg("--document-private-items")
.assert()
.success();
assert!(Path::new("./tests/cli_args/target/doc/cli_args/struct.Private.html").exists());
}

#[test]
fn it_exits_with_success_on_info_queries() {
for arg in &["-h", "--help", "-V", "--version"] {
deadlinks().arg(arg).assert().success();
}
}

#[test]
fn dir_works() {
deadlinks()
.arg("--dir")
.arg("./tests/broken_links/hardcoded-target")
.assert()
.failure()
.stdout(contains("Found invalid urls"));
}

#[test]
fn missing_deadlinks_gives_helpful_error() {
Command::cargo_bin("cargo-deadlinks")
.unwrap()
.assert()
.failure()
.stderr(contains("should be run as `cargo deadlinks`"));
}

#[test]
fn too_many_args_is_an_error() {
deadlinks()
.arg("x")
.assert()
.failure()
.stderr(contains("error:").and(contains("x")));
}

#[test]
fn version_contains_binary_name() {
Command::cargo_bin("deadlinks")
.unwrap()
.arg("--version")
.assert()
.stdout(starts_with("deadlinks "));
Command::cargo_bin("cargo-deadlinks")
.unwrap()
.arg("deadlinks")
.arg("--version")
.assert()
.stdout(starts_with("cargo-deadlinks "));
}
}
7 changes: 0 additions & 7 deletions tests/simple_project/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,3 @@ pub fn bar() {}

/// [Correct link](crate::bar)
pub struct Tmp {}
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}

0 comments on commit 94854e0

Please sign in to comment.