minor style improvements

This commit is contained in:
Anton-4 2024-05-08 14:06:14 +02:00
parent d43037602e
commit 16f3969fc3
No known key found for this signature in database
GPG key ID: 0971D718C0A9B937

View file

@ -64,7 +64,7 @@ fn is_roc_file(path: &Path) -> bool {
pub fn format_files(files: std::vec::Vec<PathBuf>, mode: FormatMode) -> Result<(), String> { pub fn format_files(files: std::vec::Vec<PathBuf>, mode: FormatMode) -> Result<(), String> {
let arena = Bump::new(); let arena = Bump::new();
let mut files_to_reformat = Vec::new(); //for tracking files which need to be formatted let mut files_to_reformat = Vec::new(); // to track which files failed `roc format --check`
for file in flatten_directories(files) { for file in flatten_directories(files) {
let src = std::fs::read_to_string(&file).unwrap(); let src = std::fs::read_to_string(&file).unwrap();
@ -73,8 +73,8 @@ pub fn format_files(files: std::vec::Vec<PathBuf>, mode: FormatMode) -> Result<(
Ok(buf) => { Ok(buf) => {
match mode { match mode {
FormatMode::CheckOnly => { FormatMode::CheckOnly => {
// If we notice that this file needs to be formatted, add it to the file // If a file fails `format --check`, add it to the file
// list for reporting afterwards // list for reporting afterwards.
if buf.as_str() != src { if buf.as_str() != src {
files_to_reformat.push(file.display().to_string()); files_to_reformat.push(file.display().to_string());
} }
@ -154,11 +154,11 @@ pub fn format_files(files: std::vec::Vec<PathBuf>, mode: FormatMode) -> Result<(
}, },
} }
} }
// After processing all files, check if there are any files that needed reformatting // After processing all files, check if any files failed `format --check`
if !files_to_reformat.is_empty() { if !files_to_reformat.is_empty() {
let file_list = files_to_reformat.join(", "); let file_list = files_to_reformat.join(", ");
return Err(format!( return Err(format!(
"The following files need to be reformatted: {}", "The following file(s) failed `roc format --check`:\n\t{}\nYou can fix this with `roc format filename.roc`.",
file_list file_list
)); ));
} }
@ -254,9 +254,9 @@ mod tests {
use super::*; use super::*;
use std::fs::File; use std::fs::File;
use std::io::Write; use std::io::Write;
use tempfile::tempdir; use tempfile::{tempdir, TempDir};
const FORMATTED_CONTENT: &str = r#"app [main] { pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.10.0/vNe6s9hWzoTZtFmNkvEICPErI9ptji_ySjicO6CkucY.tar.br" } const FORMATTED_ROC: &str = r#"app [main] { pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.10.0/vNe6s9hWzoTZtFmNkvEICPErI9ptji_ySjicO6CkucY.tar.br" }
import pf.Stdout import pf.Stdout
import pf.Task import pf.Task
@ -264,7 +264,7 @@ import pf.Task
main = main =
Stdout.line! "I'm a Roc application!""#; Stdout.line! "I'm a Roc application!""#;
const UNFORMATTED_CONTENT: &str = r#"app [main] { pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.10.0/vNe6s9hWzoTZtFmNkvEICPErI9ptji_ySjicO6CkucY.tar.br" } const UNFORMATTED_ROC: &str = r#"app [main] { pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.10.0/vNe6s9hWzoTZtFmNkvEICPErI9ptji_ySjicO6CkucY.tar.br" }
import pf.Stdout import pf.Stdout
@ -283,57 +283,60 @@ main =
file_path file_path
} }
fn cleanup_temp_dir(dir: TempDir) {
let result = dir.close();
assert!(result.is_ok(), "Failed to delete temp directory");
}
#[test] #[test]
fn test_single_file_needs_reformatting() { fn test_single_file_needs_reformatting() {
let dir = tempdir().unwrap(); let dir = tempdir().unwrap();
let file_path = setup_test_file(dir.path(), "test1.roc", UNFORMATTED_CONTENT); let file_path = setup_test_file(dir.path(), "test1.roc", UNFORMATTED_ROC);
let result = format_files(vec![file_path.clone()], FormatMode::CheckOnly); let result = format_files(vec![file_path.clone()], FormatMode::CheckOnly);
assert!(result.is_err()); assert!(result.is_err());
assert_eq!( assert_eq!(
result.unwrap_err(), result.unwrap_err(),
format!( format!(
"The following files need to be reformatted: {}", "The following file(s) failed `roc format --check`:\n\t{}\nYou can fix this with `roc format filename.roc`.",
&file_path.as_path().to_str().unwrap() &file_path.as_path().to_str().unwrap()
) )
); );
//ensure tmp files are cleaned up
let result = dir.close(); cleanup_temp_dir(dir);
assert!(result.is_ok(), "Failed to delete temp directory");
} }
#[test] #[test]
fn test_multiple_files_needs_reformatting() { fn test_multiple_files_needs_reformatting() {
let dir = tempdir().unwrap(); let dir = tempdir().unwrap();
let file1 = setup_test_file(dir.path(), "test1.roc", UNFORMATTED_CONTENT); let file1 = setup_test_file(dir.path(), "test1.roc", UNFORMATTED_ROC);
let file2 = setup_test_file(dir.path(), "test2.roc", UNFORMATTED_CONTENT); let file2 = setup_test_file(dir.path(), "test2.roc", UNFORMATTED_ROC);
let result = format_files(vec![file1, file2], FormatMode::CheckOnly); let result = format_files(vec![file1, file2], FormatMode::CheckOnly);
assert!(result.is_err()); assert!(result.is_err());
let error_message = result.unwrap_err(); let error_message = result.unwrap_err();
assert!(error_message.contains("test1.roc") && error_message.contains("test2.roc")); assert!(error_message.contains("test1.roc") && error_message.contains("test2.roc"));
//ensure tmp files are cleaned up
let result = dir.close(); cleanup_temp_dir(dir);
assert!(result.is_ok(), "Failed to delete temp directory");
} }
#[test] #[test]
fn test_no_files_need_reformatting() { fn test_no_files_need_reformatting() {
let dir = tempdir().unwrap(); let dir = tempdir().unwrap();
let file_path = setup_test_file(dir.path(), "formatted.roc", FORMATTED_CONTENT); let file_path = setup_test_file(dir.path(), "formatted.roc", FORMATTED_ROC);
let result = format_files(vec![file_path], FormatMode::CheckOnly); let result = format_files(vec![file_path], FormatMode::CheckOnly);
assert!(result.is_ok()); assert!(result.is_ok());
//ensure tmp files are cleaned up
let result = dir.close(); cleanup_temp_dir(dir);
assert!(result.is_ok(), "Failed to delete temp directory");
} }
#[test] #[test]
fn test_some_files_need_reformatting() { fn test_some_files_need_reformatting() {
let dir = tempdir().unwrap(); let dir = tempdir().unwrap();
let file_formatted = setup_test_file(dir.path(), "formatted.roc", FORMATTED_CONTENT); let file_formatted = setup_test_file(dir.path(), "formatted.roc", FORMATTED_ROC);
let file1_unformated = setup_test_file(dir.path(), "test1.roc", UNFORMATTED_CONTENT); let file1_unformated = setup_test_file(dir.path(), "test1.roc", UNFORMATTED_ROC);
let file2_unformated = setup_test_file(dir.path(), "test2.roc", UNFORMATTED_CONTENT); let file2_unformated = setup_test_file(dir.path(), "test2.roc", UNFORMATTED_ROC);
let result = format_files( let result = format_files(
vec![file_formatted, file1_unformated, file2_unformated], vec![file_formatted, file1_unformated, file2_unformated],
@ -343,8 +346,7 @@ main =
let error_message = result.unwrap_err(); let error_message = result.unwrap_err();
assert!(error_message.contains("test1.roc") && error_message.contains("test2.roc")); assert!(error_message.contains("test1.roc") && error_message.contains("test2.roc"));
assert!(!error_message.contains("formatted.roc")); assert!(!error_message.contains("formatted.roc"));
//ensure tmp files are cleaned up
let result = dir.close(); cleanup_temp_dir(dir);
assert!(result.is_ok(), "Failed to delete temp directory");
} }
} }