From 332da3fa24234a90a63ac8da6be19657c056ab7f Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Sun, 17 Nov 2024 22:00:19 +0100 Subject: [PATCH] Apply CLI overrides when configuration is found --- src/cli/config.rs | 4 ++- src/cli/main.rs | 73 +++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 73 insertions(+), 4 deletions(-) diff --git a/src/cli/config.rs b/src/cli/config.rs index 48e3b861..65a58b45 100644 --- a/src/cli/config.rs +++ b/src/cli/config.rs @@ -113,7 +113,9 @@ impl ConfigResolver<'_> { match config_file { Some(file_path) => { debug!("config: found config at {}", file_path.display()); - read_config_file(&file_path).map(Some) + let config = read_and_apply_overrides(&file_path, self.opt)?; + debug!("config: {:#?}", config); + Ok(Some(config)) } None => Ok(None), } diff --git a/src/cli/main.rs b/src/cli/main.rs index 045388cd..48f1a0e1 100644 --- a/src/cli/main.rs +++ b/src/cli/main.rs @@ -271,9 +271,6 @@ fn format(opt: opt::Opt) -> Result { let opt_for_config_resolver = opt.clone(); let mut config_resolver = config::ConfigResolver::new(&opt_for_config_resolver)?; - // TODO: - // debug!("config: {:#?}", config); - // Create range if provided let range = if opt.range_start.is_some() || opt.range_end.is_some() { Some(Range::from_values(opt.range_start, opt.range_end)) @@ -953,4 +950,74 @@ mod tests { cwd.close().unwrap(); } + + #[test] + fn test_uses_cli_overrides_instead_of_default_configuration() { + let cwd = construct_tree!({ + "foo.lua": "local x = \"hello\"", + }); + + let mut cmd = create_stylua(); + cmd.current_dir(cwd.path()) + .args(["--quote-style", "AutoPreferSingle", "."]) + .assert() + .success(); + + cwd.child("foo.lua").assert("local x = 'hello'\n"); + + cwd.close().unwrap(); + } + + #[test] + fn test_uses_cli_overrides_instead_of_default_configuration_stdin_filepath() { + let cwd = construct_tree!({ + "foo.lua": "local x = \"hello\"", + }); + + let mut cmd = create_stylua(); + cmd.current_dir(cwd.path()) + .args(["--quote-style", "AutoPreferSingle", "-"]) + .write_stdin("local x = \"hello\"") + .assert() + .success() + .stdout("local x = 'hello'\n"); + + cwd.close().unwrap(); + } + + #[test] + fn test_uses_cli_overrides_instead_of_found_configuration() { + let cwd = construct_tree!({ + "stylua.toml": "quote_style = 'AutoPreferDouble'", + "foo.lua": "local x = \"hello\"", + }); + + let mut cmd = create_stylua(); + cmd.current_dir(cwd.path()) + .args(["--quote-style", "AutoPreferSingle", "."]) + .assert() + .success(); + + cwd.child("foo.lua").assert("local x = 'hello'\n"); + + cwd.close().unwrap(); + } + + #[test] + fn test_uses_cli_overrides_instead_of_found_configuration_stdin_filepath() { + let cwd = construct_tree!({ + "stylua.toml": "quote_style = 'AutoPreferDouble'", + "foo.lua": "local x = \"hello\"", + }); + + let mut cmd = create_stylua(); + cmd.current_dir(cwd.path()) + .args(["--quote-style", "AutoPreferSingle", "-"]) + .write_stdin("local x = \"hello\"") + .assert() + .success() + .stdout("local x = 'hello'\n"); + + cwd.close().unwrap(); + } }