Formatter should ignore non roc files

Fixes #2683
This commit is contained in:
Pierre-Henri Trivier 2022-03-17 18:52:04 +01:00
parent dca9404772
commit 3e368a64ff
6 changed files with 17 additions and 6 deletions

View File

@ -1,4 +1,5 @@
use std::path::PathBuf;
use std::ffi::OsStr;
use std::path::{Path, PathBuf};
use crate::FormatMode;
use bumpalo::collections::Vec;
@ -39,7 +40,7 @@ fn flatten_directories(files: std::vec::Vec<PathBuf>) -> std::vec::Vec<PathBuf>
let file_path = file.path();
if file_path.is_dir() {
to_flatten.push(file_path);
} else if file_path.ends_with(".roc") {
} else if is_roc_file(&file_path) {
files.push(file_path);
}
}
@ -57,14 +58,19 @@ fn flatten_directories(files: std::vec::Vec<PathBuf>) -> std::vec::Vec<PathBuf>
error
),
}
} else {
files.push(path)
} else if is_roc_file(&path) {
files.push(path);
}
}
files
}
fn is_roc_file(path: &Path) -> bool {
let ext = path.extension().and_then(OsStr::to_str);
return matches!(ext, Some("roc"));
}
pub fn format(files: std::vec::Vec<PathBuf>, mode: FormatMode) -> Result<(), String> {
let files = flatten_directories(files);

View File

@ -73,7 +73,6 @@ mod cli_run {
fn check_format_check_as_expected(file: &Path, expects_success_exit_code: bool) {
let flags = &["--check"];
let out = run_roc(&[&["format", file.to_str().unwrap()], &flags[..]].concat());
if expects_success_exit_code {
assert!(out.status.success());
} else {
@ -940,7 +939,7 @@ mod cli_run {
// This fails, because "NotFormatted.roc" is present in this folder
check_format_check_as_expected(&fixtures_dir("format"), false);
// This doesn't fail, since only "Formatted.roc" is present in this folder
// This doesn't fail, since only "Formatted.roc" and non-roc files are present in this folder
check_format_check_as_expected(&fixtures_dir("format/formatted_directory"), true);
}
}

View File

@ -0,0 +1 @@
This is not a .roc file, and should be ignored by the formatter.

View File

@ -0,0 +1 @@
This is not a .roc file, and should be ignored by the formatter.

View File

@ -0,0 +1,3 @@
This is not a .roc file, and should be ignored by the formatter.
This file does not have an extension, to ensure the formatter does not simply test with `ends_with(".roc")`

View File

@ -0,0 +1 @@
This is not a .roc file, and should be ignored by the formatter.