From daa5be4c3ac77ed3385b2afedbbc625ec9a35b89 Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 5 Jul 2021 01:35:36 +0200 Subject: [PATCH] Add/change file link tests --- lychee-bin/tests/cli.rs | 27 +++++++++++++++++---------- lychee-bin/tests/local_files.rs | 5 ++--- lychee-lib/src/client.rs | 17 ++++++++++++++++- 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/lychee-bin/tests/cli.rs b/lychee-bin/tests/cli.rs index b42f988..6d0819b 100644 --- a/lychee-bin/tests/cli.rs +++ b/lychee-bin/tests/cli.rs @@ -130,15 +130,22 @@ mod cli { /// Test unsupported URI schemes #[test] - fn test_unsupported_uri_schemes() -> Result<()> { - test_json_output!( - "TEST_SCHEMES.txt", - MockResponseStats { - total: 1, - successful: 1, - ..MockResponseStats::default() - } - ) + fn test_unsupported_uri_schemes() { + let mut cmd = main_command(); + let test_schemes_path = fixtures_path().join("TEST_SCHEMES.txt"); + + // Exclude file link because it doesn't exist on the filesystem. + // (File URIs are absolute paths, which we don't have.) + // Nevertheless, the `file` scheme should be recognized. + cmd.arg(test_schemes_path) + .arg("--exclude") + .arg("file://") + .env_clear() + .assert() + .success() + .stdout(contains("Total............2")) + .stdout(contains("Successful.......1")) + .stdout(contains("Excluded.........1")); } #[test] @@ -364,7 +371,7 @@ mod cli { .assert() .success(); - let expected = r#"{"total":10,"successful":10,"failures":0,"timeouts":0,"redirects":0,"excludes":0,"errors":0,"fail_map":{}}"#; + let expected = r#"{"total":11,"successful":11,"failures":0,"timeouts":0,"redirects":0,"excludes":0,"errors":0,"fail_map":{}}"#; let output = fs::read_to_string(&outfile)?; assert_eq!(output.split_whitespace().collect::(), expected); fs::remove_file(outfile)?; diff --git a/lychee-bin/tests/local_files.rs b/lychee-bin/tests/local_files.rs index ddd0ed2..11574e1 100644 --- a/lychee-bin/tests/local_files.rs +++ b/lychee-bin/tests/local_files.rs @@ -19,8 +19,7 @@ mod cli { writeln!(index, r#"Foo"#)?; let foo_path = dir.path().join("foo.html"); - let mut foo = File::create(&foo_path)?; - writeln!(foo, r#"example"#)?; + File::create(&foo_path)?; let mut cmd = main_command(); cmd.arg(index_path) @@ -30,7 +29,7 @@ mod cli { .assert() .success() .stdout(contains("Total............1")) - .stdout(contains("example.org")); + .stdout(contains("foo.html")); Ok(()) } diff --git a/lychee-lib/src/client.rs b/lychee-lib/src/client.rs index a69167c..e8d477f 100644 --- a/lychee-lib/src/client.rs +++ b/lychee-lib/src/client.rs @@ -287,10 +287,14 @@ where #[cfg(test)] mod test { - use std::time::{Duration, Instant}; + use std::{ + fs::File, + time::{Duration, Instant}, + }; use http::{header::HeaderMap, StatusCode}; use reqwest::header; + use tempfile::tempdir; use super::ClientBuilder; use crate::{mock_server, test_utils::get_mock_client_response}; @@ -375,6 +379,17 @@ mod test { assert!(res.status().is_success()); } + #[tokio::test] + async fn test_file() { + let dir = tempdir().unwrap(); + let file = dir.path().join("temp"); + File::create(file).unwrap(); + let uri = format!("file://{}", dir.path().join("temp").to_str().unwrap()); + + let res = get_mock_client_response(uri).await; + assert!(res.status().is_success()); + } + #[tokio::test] async fn test_custom_headers() { // See https://github.com/rust-lang/crates.io/issues/788