From a8207130bc40dce73c8258b5d0dbd6b6e8a9fb64 Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Sun, 17 Nov 2024 22:06:03 +0100 Subject: [PATCH] Fix CLI overrides not applying on top of resolved configuration (#926) * Apply CLI overrides when configuration is found * Update changelog --- CHANGELOG.md | 4 +++ src/cli/config.rs | 4 ++- src/cli/main.rs | 73 +++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 77 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1cd8f2d2..194c7a36 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed + +- Fixed CLI overrides not applying on top of a resolved `stylua.toml` file ([#925](https://github.com/JohnnyMorganz/StyLua/issues/925)) + ## [2.0.0] - 2024-11-17 ### Breaking Changes 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(); + } }