Skip to content

Commit

Permalink
Fix CLI overrides not applying on top of resolved configuration (#926)
Browse files Browse the repository at this point in the history
* Apply CLI overrides when configuration is found

* Update changelog
  • Loading branch information
JohnnyMorganz authored Nov 17, 2024
1 parent 51da190 commit a820713
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion src/cli/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
Expand Down
73 changes: 70 additions & 3 deletions src/cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,6 @@ fn format(opt: opt::Opt) -> Result<i32> {
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))
Expand Down Expand Up @@ -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();
}
}

0 comments on commit a820713

Please sign in to comment.