Skip to content
Merged
Show file tree
Hide file tree
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
41 changes: 28 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,18 +347,30 @@ fn main() {
match opts {
CommandlineOpts { check: true, .. } => {
let text_diffs: Arc<Mutex<Vec<String>>> = Arc::new(Mutex::new(Vec::new()));

iterate_formatted(&opts, &|(file_path, before, after)| match after {
None => {}
Some(fmtted) => {
let diff = TextDiff::from_lines(before, &fmtted);
let path_string = file_path.to_str().unwrap();
text_diffs.lock().unwrap().push(format!(
"{}",
diff.unified_diff().header(path_string, path_string)
));
}
});
let errors_count: Arc<Mutex<usize>> = Arc::new(Mutex::new(0));

iterate_input_files(
&opts,
&|(file_path, before)| match rubyfmt_string(&opts, before) {
Ok(None) => {}
Ok(Some(fmtted)) => {
let diff = TextDiff::from_lines(before, &fmtted);
let path_string = file_path.to_str().unwrap();
text_diffs.lock().unwrap().push(format!(
"{}",
diff.unified_diff().header(path_string, path_string)
));
}
Err(e) => {
handle_rubyfmt_error(
e,
&file_path.display().to_string(),
ErrorExit::NoExit,
);
*errors_count.lock().unwrap() += 1;
}
},
);

let all_diffs = text_diffs.lock().unwrap();

Expand All @@ -370,7 +382,10 @@ fn main() {
diffs_reported += 1
}
}
if diffs_reported > 0 {
let errors = *errors_count.lock().unwrap();
if errors > 0 {
exit(rubyfmt::FormatError::SyntaxError as i32);
} else if diffs_reported > 0 {
exit(rubyfmt::FormatError::DiffDetected as i32);
} else {
exit(0)
Expand Down
40 changes: 40 additions & 0 deletions tests/cli_interface_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,46 @@ fn format_input_file_without_changes() {
assert_eq!("a(1, 2, 3)\n", read_to_string(file_one.path()).unwrap());
}

#[test]
fn test_check_flag_with_syntax_error() {
let mut file = NamedTempFile::new().unwrap();
// Use actually invalid Ruby syntax (incomplete def statement)
writeln!(file, "def (").unwrap();

let output = Command::cargo_bin("rubyfmt-main")
.unwrap()
.arg(file.path())
.arg("--check")
.assert()
.code(1)
.failure();

let stderr = String::from_utf8_lossy(&output.get_output().stderr);
assert!(
stderr.contains("syntax error"),
"Expected stderr to contain 'syntax error', got: {}",
stderr
);
}

#[test]
fn test_check_flag_stdin_with_syntax_error() {
let output = Command::cargo_bin("rubyfmt-main")
.unwrap()
.arg("--check")
.write_stdin("def (")
.assert()
.code(1)
.failure();

let stderr = String::from_utf8_lossy(&output.get_output().stderr);
assert!(
stderr.contains("syntax error"),
"Expected stderr to contain 'syntax error', got: {}",
stderr
);
}

#[test]
fn test_format_respects_opt_in_header() {
let mut file_one = NamedTempFile::new().unwrap();
Expand Down