Skip to content

Commit

Permalink
fix: enable to select some formatters without `--allow-missing-format…
Browse files Browse the repository at this point in the history
…ter` (#278)
  • Loading branch information
nokazn authored Apr 6, 2024
1 parent 8f57f1b commit b802b97
Showing 1 changed file with 40 additions and 2 deletions.
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

0 comments on commit b802b97

Please sign in to comment.