Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CLI overrides not applying on top of resolved configuration #926

Merged
merged 2 commits into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}
Loading