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: enable to select some formatters without --allow-missing-formatter #278

Merged
merged 1 commit into from
Apr 6, 2024
Merged
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
42 changes: 40 additions & 2 deletions src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,14 +235,16 @@ fn load_formatters(
selected_formatters: &Option<Vec<String>>,
stats: &mut Statistics,
) -> anyhow::Result<BTreeMap<FormatterName, Formatter>> {
let mut expected_count = 0;
let formatter = root.formatter;
let expected_count = match selected_formatters {
Some(f) => f.len(),
None => formatter.len(),
};
let global_excludes = root.global.map(|g| g.excludes).unwrap_or_default();
let formatters =
formatter
.into_iter()
.fold(BTreeMap::new(), |mut sum, (name, mut fmt_config)| {
expected_count += 1;
fmt_config.excludes.extend_from_slice(&global_excludes);
match Formatter::from_config(tree_root, &name, &fmt_config) {
Ok(fmt_matcher) => match selected_formatters {
Expand Down Expand Up @@ -751,6 +753,42 @@ mod tests {
assert_eq!(formatters.len(), 2);
}
#[test]
fn test_formatter_loading_selected() {
let tmpdir = utils::tmp_mkdir();

let black = tmpdir.path().join("black");
let nixpkgs_fmt = tmpdir.path().join("nixpkgs-fmt");
utils::write_binary_file(&nixpkgs_fmt, " ");

let config = format!(
"
[formatter.python]
command = {black:?}
includes = [\"*.py\"]

[formatter.nix]
command = {nixpkgs_fmt:?}
includes = [\"*.nix\"]
"
);

let root = from_string(&config).unwrap();
let tree_root = tmpdir.path();

let selected_formatters = Some(vec!["nix".into()]);
let allow_missing_formatter = false;
let mut stats = Statistics::init();

load_formatters(
root,
tree_root,
allow_missing_formatter,
&selected_formatters,
&mut stats,
)
.unwrap();
}
#[test]
#[should_panic]
fn test_formatter_loading_some_missing_formatter() {
let tmpdir = utils::tmp_mkdir();
Expand Down
Loading