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

Shorten path names when --debug is not passed #20

Merged
merged 1 commit into from
Oct 22, 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
35 changes: 26 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(&epath.display().to_string());
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),
Expand Down Expand Up @@ -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 })
}
})
}
16 changes: 11 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -87,7 +86,7 @@ fn main() {
}
};
log::info!("checking directory {:?}", dir);
if walk_dir(&dir, &ctx) {
if walk_dir(&dir, &args) {
errors = true;
}
}
Expand Down Expand Up @@ -182,16 +181,23 @@ fn determine_dir() -> Vec<PathBuf> {
/// 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
})
// ||||||
Expand Down
37 changes: 37 additions & 0 deletions tests/simple_project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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/fn.foo.html:",
)
.and(contains(
"cargo-deadlinks/tests/simple_proj\
ect/target/doc/simple_project/fn.bar.html does not exist!",
)),
);
}
}

Expand Down
2 changes: 2 additions & 0 deletions tests/simple_project/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ pub fn foo() {}
/// Bar function
pub fn bar() {}

/// [Correct link](crate::bar)
pub struct Tmp {}
#[cfg(test)]
mod tests {
#[test]
Expand Down