From 2aff698ef2ebc1a1ac78b8fa1e7e98a0870906e4 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 27 Oct 2024 02:07:43 +0200 Subject: [PATCH] Fix format option in configuration file --- fixtures/configs/format.toml | 1 + lychee-bin/src/options.rs | 4 +++- lychee-bin/tests/cli.rs | 22 ++++++++++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 fixtures/configs/format.toml diff --git a/fixtures/configs/format.toml b/fixtures/configs/format.toml new file mode 100644 index 0000000000..d0457b334e --- /dev/null +++ b/fixtures/configs/format.toml @@ -0,0 +1 @@ +format = "json" diff --git a/lychee-bin/src/options.rs b/lychee-bin/src/options.rs index 988dc9aa60..60c259d2ea 100644 --- a/lychee-bin/src/options.rs +++ b/lychee-bin/src/options.rs @@ -46,9 +46,10 @@ const TIMEOUT_STR: &str = concatcp!(DEFAULT_TIMEOUT_SECS); const RETRY_WAIT_TIME_STR: &str = concatcp!(DEFAULT_RETRY_WAIT_TIME_SECS); /// The format to use for the final status report -#[derive(Debug, Deserialize, Default, Clone, Display, EnumIter, VariantNames)] +#[derive(Debug, Deserialize, Default, Clone, Display, EnumIter, VariantNames, PartialEq)] #[non_exhaustive] #[strum(serialize_all = "snake_case")] +#[serde(rename_all = "snake_case")] pub(crate) enum StatsFormat { #[default] Compact, @@ -544,6 +545,7 @@ impl Config { exclude_link_local: false; exclude_loopback: false; exclude_mail: false; + format: StatsFormat::default(); remap: Vec::::new(); fallback_extensions: Vec::::new(); header: Vec::::new(); diff --git a/lychee-bin/tests/cli.rs b/lychee-bin/tests/cli.rs index 637a59d8f0..6ac4dc8b4a 100644 --- a/lychee-bin/tests/cli.rs +++ b/lychee-bin/tests/cli.rs @@ -1723,4 +1723,26 @@ mod cli { Ok(()) } + + #[tokio::test] + async fn test_json_format_in_config() -> Result<()> { + let mock_server = mock_server!(StatusCode::OK); + let config = fixtures_path().join("configs").join("format.toml"); + let mut cmd = main_command(); + cmd.arg("--config") + .arg(config) + .arg("-") + .write_stdin(mock_server.uri()) + .env_clear() + .assert() + .success(); + + // Check that the output is in JSON format + let output = cmd.output().unwrap(); + let output = std::str::from_utf8(&output.stdout).unwrap(); + let json: serde_json::Value = serde_json::from_str(output)?; + assert_eq!(json["total"], 1); + + Ok(()) + } }