Skip to content

Commit a820713

Browse files
Fix CLI overrides not applying on top of resolved configuration (#926)
* Apply CLI overrides when configuration is found * Update changelog
1 parent 51da190 commit a820713

File tree

3 files changed

+77
-4
lines changed

3 files changed

+77
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Fixed
11+
12+
- Fixed CLI overrides not applying on top of a resolved `stylua.toml` file ([#925](https://github.com/JohnnyMorganz/StyLua/issues/925))
13+
1014
## [2.0.0] - 2024-11-17
1115

1216
### Breaking Changes

src/cli/config.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,9 @@ impl ConfigResolver<'_> {
113113
match config_file {
114114
Some(file_path) => {
115115
debug!("config: found config at {}", file_path.display());
116-
read_config_file(&file_path).map(Some)
116+
let config = read_and_apply_overrides(&file_path, self.opt)?;
117+
debug!("config: {:#?}", config);
118+
Ok(Some(config))
117119
}
118120
None => Ok(None),
119121
}

src/cli/main.rs

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,9 +271,6 @@ fn format(opt: opt::Opt) -> Result<i32> {
271271
let opt_for_config_resolver = opt.clone();
272272
let mut config_resolver = config::ConfigResolver::new(&opt_for_config_resolver)?;
273273

274-
// TODO:
275-
// debug!("config: {:#?}", config);
276-
277274
// Create range if provided
278275
let range = if opt.range_start.is_some() || opt.range_end.is_some() {
279276
Some(Range::from_values(opt.range_start, opt.range_end))
@@ -953,4 +950,74 @@ mod tests {
953950

954951
cwd.close().unwrap();
955952
}
953+
954+
#[test]
955+
fn test_uses_cli_overrides_instead_of_default_configuration() {
956+
let cwd = construct_tree!({
957+
"foo.lua": "local x = \"hello\"",
958+
});
959+
960+
let mut cmd = create_stylua();
961+
cmd.current_dir(cwd.path())
962+
.args(["--quote-style", "AutoPreferSingle", "."])
963+
.assert()
964+
.success();
965+
966+
cwd.child("foo.lua").assert("local x = 'hello'\n");
967+
968+
cwd.close().unwrap();
969+
}
970+
971+
#[test]
972+
fn test_uses_cli_overrides_instead_of_default_configuration_stdin_filepath() {
973+
let cwd = construct_tree!({
974+
"foo.lua": "local x = \"hello\"",
975+
});
976+
977+
let mut cmd = create_stylua();
978+
cmd.current_dir(cwd.path())
979+
.args(["--quote-style", "AutoPreferSingle", "-"])
980+
.write_stdin("local x = \"hello\"")
981+
.assert()
982+
.success()
983+
.stdout("local x = 'hello'\n");
984+
985+
cwd.close().unwrap();
986+
}
987+
988+
#[test]
989+
fn test_uses_cli_overrides_instead_of_found_configuration() {
990+
let cwd = construct_tree!({
991+
"stylua.toml": "quote_style = 'AutoPreferDouble'",
992+
"foo.lua": "local x = \"hello\"",
993+
});
994+
995+
let mut cmd = create_stylua();
996+
cmd.current_dir(cwd.path())
997+
.args(["--quote-style", "AutoPreferSingle", "."])
998+
.assert()
999+
.success();
1000+
1001+
cwd.child("foo.lua").assert("local x = 'hello'\n");
1002+
1003+
cwd.close().unwrap();
1004+
}
1005+
1006+
#[test]
1007+
fn test_uses_cli_overrides_instead_of_found_configuration_stdin_filepath() {
1008+
let cwd = construct_tree!({
1009+
"stylua.toml": "quote_style = 'AutoPreferDouble'",
1010+
"foo.lua": "local x = \"hello\"",
1011+
});
1012+
1013+
let mut cmd = create_stylua();
1014+
cmd.current_dir(cwd.path())
1015+
.args(["--quote-style", "AutoPreferSingle", "-"])
1016+
.write_stdin("local x = \"hello\"")
1017+
.assert()
1018+
.success()
1019+
.stdout("local x = 'hello'\n");
1020+
1021+
cwd.close().unwrap();
1022+
}
9561023
}

0 commit comments

Comments
 (0)