From 0265cb8ec10fa7f57b41061adc1614f55c1d91a9 Mon Sep 17 00:00:00 2001 From: Keming Date: Sun, 25 May 2025 16:49:31 +0800 Subject: [PATCH] feat: raise error when the default config file is invalid Signed-off-by: Keming --- fixtures/configs/lychee.toml | 2 ++ lychee-bin/src/main.rs | 18 ++++++++++++++---- lychee-bin/tests/cli.rs | 13 +++++++++++++ 3 files changed, 29 insertions(+), 4 deletions(-) create mode 100644 fixtures/configs/lychee.toml diff --git a/fixtures/configs/lychee.toml b/fixtures/configs/lychee.toml new file mode 100644 index 0000000000..e248c70f50 --- /dev/null +++ b/fixtures/configs/lychee.toml @@ -0,0 +1,2 @@ +# this is an invalid toml config file with the default config file name +{} diff --git a/lychee-bin/src/main.rs b/lychee-bin/src/main.rs index c9966d4df1..07abbdf826 100644 --- a/lychee-bin/src/main.rs +++ b/lychee-bin/src/main.rs @@ -156,10 +156,20 @@ fn load_config() -> Result { } } else { // If no config file was explicitly provided, we try to load the default - // config file from the current directory, but it's not an error if it - // doesn't exist. - if let Ok(c) = Config::load_from_file(&PathBuf::from(LYCHEE_CONFIG_FILE)) { - opts.config.merge(c); + // config file from the current directory if the file exits. This will + // raise an error if the file is invalid, just like the explicit provided + // config file. + let default_config = PathBuf::from(LYCHEE_CONFIG_FILE); + if default_config.is_file() { + match Config::load_from_file(&default_config) { + Ok(c) => opts.config.merge(c), + Err(e) => { + bail!( + "Cannot load default configuration file `{}`: {e:?}", + default_config.display() + ); + } + } } } diff --git a/lychee-bin/tests/cli.rs b/lychee-bin/tests/cli.rs index b5cc10828b..7cd76c13a3 100644 --- a/lychee-bin/tests/cli.rs +++ b/lychee-bin/tests/cli.rs @@ -815,6 +815,19 @@ mod cli { Ok(()) } + #[test] + fn test_invalid_default_config() -> Result<()> { + let test_path = fixtures_path().join("configs"); + let mut cmd = main_command(); + cmd.current_dir(test_path) + .arg(".") + .assert() + .failure() + .stderr(contains("Cannot load default configuration file")); + + Ok(()) + } + #[tokio::test] async fn test_include_mail_config() -> Result<()> { let test_mail_address = "mailto:hello-test@testingabc.io";