diff --git a/cli/src/format.rs b/cli/src/format.rs index d57724d721..9975332a93 100644 --- a/cli/src/format.rs +++ b/cli/src/format.rs @@ -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) -> std::vec::Vec 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) -> std::vec::Vec 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, mode: FormatMode) -> Result<(), String> { let files = flatten_directories(files); diff --git a/cli/tests/cli_run.rs b/cli/tests/cli_run.rs index 0a9213731d..edfc41ec70 100644 --- a/cli/tests/cli_run.rs +++ b/cli/tests/cli_run.rs @@ -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); } } diff --git a/cli/tests/fixtures/format/NotARocFile.txt b/cli/tests/fixtures/format/NotARocFile.txt new file mode 100644 index 0000000000..ea65efc5d0 --- /dev/null +++ b/cli/tests/fixtures/format/NotARocFile.txt @@ -0,0 +1 @@ +This is not a .roc file, and should be ignored by the formatter. \ No newline at end of file diff --git a/cli/tests/fixtures/format/formatted_directory/NestedNotARocFile.txt b/cli/tests/fixtures/format/formatted_directory/NestedNotARocFile.txt new file mode 100644 index 0000000000..ea65efc5d0 --- /dev/null +++ b/cli/tests/fixtures/format/formatted_directory/NestedNotARocFile.txt @@ -0,0 +1 @@ +This is not a .roc file, and should be ignored by the formatter. \ No newline at end of file diff --git a/cli/tests/fixtures/format/formatted_directory/NotARocFile b/cli/tests/fixtures/format/formatted_directory/NotARocFile new file mode 100644 index 0000000000..63b7992a35 --- /dev/null +++ b/cli/tests/fixtures/format/formatted_directory/NotARocFile @@ -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")` \ No newline at end of file diff --git a/cli/tests/fixtures/format/formatted_directory/ignored/NotARocFile.txt b/cli/tests/fixtures/format/formatted_directory/ignored/NotARocFile.txt new file mode 100644 index 0000000000..ea65efc5d0 --- /dev/null +++ b/cli/tests/fixtures/format/formatted_directory/ignored/NotARocFile.txt @@ -0,0 +1 @@ +This is not a .roc file, and should be ignored by the formatter. \ No newline at end of file