From f6bc52ff126bb1bcfa05de81f69961ada6252b6b Mon Sep 17 00:00:00 2001 From: KillTheMule Date: Mon, 19 Oct 2020 23:24:47 +0200 Subject: [PATCH] Print out errors without the full path When --debug is given, still show the full path --- src/lib.rs | 35 +++++++++++++++++++++++-------- src/main.rs | 16 +++++++++----- tests/simple_project.rs | 37 +++++++++++++++++++++++++++++++++ tests/simple_project/src/lib.rs | 2 ++ 4 files changed, 76 insertions(+), 14 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 35299c5..4ca5e27 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -42,15 +42,34 @@ pub struct FileError { impl fmt::Display for FileError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "Found invalid urls in {}:", self.path.display(),)?; - for err in &self.errors { - writeln!(f)?; - write!(f, "\t{}", err)?; - } + write!(f, "{}", self.print_shortened(None))?; Ok(()) } } +impl FileError { + pub fn print_shortened(&self, prefix: Option<&Path>) -> String { + let prefix = prefix.unwrap_or_else(|| Path::new("")); + let filepath = self.path.strip_prefix(&prefix).unwrap_or(&self.path); + let mut ret = format!("Found invalid urls in {}:", filepath.display()); + + for e in &self.errors { + use CheckError::*; + + match e { + Http(_) => ret.push_str(&format!("\n\t{}", e)), + File(epath) => { + let epath = epath.strip_prefix(&prefix).unwrap_or(&epath); + ret.push_str("\n\tLinked file at path "); + ret.push_str(&format!("{}", epath.display())); + ret.push_str(" does not exist!"); + } + } + } + ret + } +} + fn is_html_file(entry: &DirEntry) -> bool { match entry.path().extension() { Some(e) => e.to_str().map(|ext| ext == "html").unwrap_or(false), @@ -80,10 +99,8 @@ pub fn unavailable_urls<'a>( if errors.is_empty() { None } else { - Some(FileError { - path: entry.path().to_owned(), - errors, - }) + let path = entry.path().to_owned(); + Some(FileError { path, errors }) } }) } diff --git a/src/main.rs b/src/main.rs index 497ebe1..99a82b0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -74,7 +74,6 @@ fn main() { .as_ref() .map_or_else(determine_dir, |dir| vec![dir.into()]); - let ctx: CheckContext = args.into(); let mut errors = false; for dir in dirs { let dir = match dir.canonicalize() { @@ -87,7 +86,7 @@ fn main() { } }; log::info!("checking directory {:?}", dir); - if walk_dir(&dir, &ctx) { + if walk_dir(&dir, &args) { errors = true; } } @@ -182,16 +181,23 @@ fn determine_dir() -> Vec { /// Traverses a given path recursively, checking all *.html files found. /// /// Returns whether an error occurred. -fn walk_dir(dir_path: &Path, ctx: &CheckContext) -> bool { +fn walk_dir(dir_path: &Path, args: &MainArgs) -> bool { let pool = ThreadPoolBuilder::new() .num_threads(num_cpus::get()) .build() .unwrap(); + let ctx = CheckContext { + check_http: args.flag_check_http, + }; pool.install(|| { - unavailable_urls(dir_path, ctx) + unavailable_urls(dir_path, &ctx) .map(|err| { - error!("{}", err); + if args.flag_debug { + error!("{}", err); + } else { + error!("{}", err.print_shortened(Some(dir_path))); + } true }) // |||||| diff --git a/tests/simple_project.rs b/tests/simple_project.rs index 34fb95d..b9da130 100644 --- a/tests/simple_project.rs +++ b/tests/simple_project.rs @@ -93,6 +93,43 @@ mod simple_project { &[("CARGO_BUILD_TARGET", "x86_64-unknown-linux-gnu")], ) .failure(); + + // fn it_shortens_path_on_error + remove_all("./tests/simple_project/target"); + assert_doc("./tests/simple_project", &[]).success(); + std::fs::remove_file("./tests/simple_project/target/doc/simple_project/fn.bar.html") + .unwrap(); + + // without --debug, paths are shortened + Command::cargo_bin("cargo-deadlinks") + .unwrap() + .arg("deadlinks") + .current_dir("./tests/simple_project") + .assert() + .failure() + .stderr( + contains("Linked file at path fn.bar.html does not exist!") + .and(contains("Found invalid urls in fn.foo.html:")), + ); + + // with --debug, paths are not shortened + Command::cargo_bin("cargo-deadlinks") + .unwrap() + .arg("deadlinks") + .arg("--debug") + .current_dir("./tests/simple_project") + .assert() + .failure() + .stderr( + contains( + "cargo-deadlinks/tests/simple_project/ta\ + rget/doc/simple_project/struct.Tmp.html:", + ) + .and(contains( + "cargo-deadlinks/tests/simple_proj\ + ect/target/doc/simple_project/fn.bar.html does not exist!", + )), + ); } } diff --git a/tests/simple_project/src/lib.rs b/tests/simple_project/src/lib.rs index f0d2339..6e5c82b 100644 --- a/tests/simple_project/src/lib.rs +++ b/tests/simple_project/src/lib.rs @@ -6,6 +6,8 @@ pub fn foo() {} /// Bar function pub fn bar() {} +/// [Correct link](crate::bar) +pub struct Tmp {} #[cfg(test)] mod tests { #[test]