From b40c785b6442d7d4d507aef8fdf30df0df2bb7a8 Mon Sep 17 00:00:00 2001 From: Matthias Date: Fri, 13 May 2022 18:53:16 +0200 Subject: [PATCH] Also dump excluded links (#615) This is a minimally invasive version, which allows to grep for `[excluded]`. The reason for exclusion would require more work and it's debatable if it adds any value, because it might make grepping harder and the source of exclusion is easily deducatable from the commandline parameters or the `.lycheeignore` file. Fixes #587. --- fixtures/TEST_DUMP_EXCLUDE.txt | 3 +++ lychee-bin/src/commands/dump.rs | 27 +++++++++++++++++---------- lychee-bin/tests/cli.rs | 29 +++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 10 deletions(-) create mode 100644 fixtures/TEST_DUMP_EXCLUDE.txt diff --git a/fixtures/TEST_DUMP_EXCLUDE.txt b/fixtures/TEST_DUMP_EXCLUDE.txt new file mode 100644 index 0000000..dfcddf4 --- /dev/null +++ b/fixtures/TEST_DUMP_EXCLUDE.txt @@ -0,0 +1,3 @@ +https://example.com +https://example.org +https://example.com/foo/bar \ No newline at end of file diff --git a/lychee-bin/src/commands/dump.rs b/lychee-bin/src/commands/dump.rs index 06a6c03..ad10b64 100644 --- a/lychee-bin/src/commands/dump.rs +++ b/lychee-bin/src/commands/dump.rs @@ -18,15 +18,18 @@ where while let Some(request) = requests.next().await { let request = request?; - if params.client.is_excluded(&request.uri) { - continue; - } - // Avoid panic on broken pipe. // See https://github.com/rust-lang/rust/issues/46016 // This can occur when piping the output of lychee // to another program like `grep`. - if let Err(e) = write(&request, params.cfg.verbose) { + + let excluded = params.client.is_excluded(&request.uri); + let verbose = params.cfg.verbose; + + if excluded && !verbose { + continue; + } + if let Err(e) = write(&request, verbose, excluded) { if e.kind() != io::ErrorKind::BrokenPipe { eprintln!("{e}"); return Ok(ExitCode::UnexpectedFailure); @@ -38,13 +41,17 @@ where } /// Dump request to stdout -/// Only print source in verbose mode. This way the normal link output -/// can be fed into another tool without data mangling. -fn write(request: &Request, verbose: bool) -> io::Result<()> { - let output = if verbose { +fn write(request: &Request, verbose: bool, excluded: bool) -> io::Result<()> { + let request = if verbose { + // Only print source in verbose mode. This way the normal link output + // can be fed into another tool without data mangling. request.to_string() } else { request.uri.to_string() }; - writeln!(io::stdout(), "{}", output) + if excluded { + writeln!(io::stdout(), "{} [excluded]", request) + } else { + writeln!(io::stdout(), "{}", request) + } } diff --git a/lychee-bin/tests/cli.rs b/lychee-bin/tests/cli.rs index ad396da..dc71b90 100644 --- a/lychee-bin/tests/cli.rs +++ b/lychee-bin/tests/cli.rs @@ -666,4 +666,33 @@ mod cli { .success(); Ok(()) } + + #[test] + fn test_print_excluded_links_in_verbose_mode() -> Result<()> { + let test_path = fixtures_path().join("TEST_DUMP_EXCLUDE.txt"); + let mut cmd = main_command(); + + cmd.arg("--dump") + .arg("--verbose") + .arg("--exclude") + .arg("example.com*") + .arg("--") + .arg(&test_path) + .assert() + .success() + .stdout(contains(format!( + "https://example.com/ ({}) [excluded]", + test_path.display() + ))) + .stdout(contains(format!( + "https://example.org/ ({})", + test_path.display() + ))) + .stdout(contains(format!( + "https://example.com/foo/bar ({}) [excluded]", + test_path.display() + ))); + + Ok(()) + } }