diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index 4bf41be1c..d933c53fc 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -56,7 +56,7 @@ fn main() { move |path: &Path| ignore_file.is_ignored(path) }; - match cli.command { + let status_code = match cli.command { Commands::Lint(LintArgs { paths, format }) => { let mut linter = linter(config, format); let result = linter.lint_paths(paths, false, &ignorer); @@ -83,19 +83,16 @@ fn main() { eprintln!("The linter processed {count} file(s)."); linter.formatter_mut().unwrap().completion_message(); - - std::process::exit( - if linter - .formatter() - .unwrap() - .has_fail - .load(std::sync::atomic::Ordering::SeqCst) - { - 1 - } else { - 0 - }, - ) + if linter + .formatter() + .unwrap() + .has_fail + .load(std::sync::atomic::Ordering::SeqCst) + { + 1 + } else { + 0 + } } Commands::Fix(FixArgs { paths, @@ -117,35 +114,41 @@ fn main() { .map(|path| path.files.len()) .sum::(); println!("{} files processed, nothing to fix.", count_files); - return; - } - - if !force { - match check_user_input() { - Some(true) => { - eprintln!("Attempting fixes..."); - } - Some(false) => return, - None => { - eprintln!("Invalid input, please enter 'Y' or 'N'"); - eprintln!("Aborting..."); - return; + 0 + } else { + if !force { + match check_user_input() { + Some(true) => { + eprintln!("Attempting fixes..."); + } + Some(false) => return, + None => { + eprintln!("Invalid input, please enter 'Y' or 'N'"); + eprintln!("Aborting..."); + return; + } } } - } - for linted_dir in result.paths { - for mut file in linted_dir.files { - let path = std::mem::take(&mut file.path); - let write_buff = file.fix_string(); - std::fs::write(path, write_buff).unwrap(); + for linted_dir in result.paths { + for mut file in linted_dir.files { + let path = std::mem::take(&mut file.path); + let write_buff = file.fix_string(); + std::fs::write(path, write_buff).unwrap(); + } } - } - linter.formatter_mut().unwrap().completion_message(); + linter.formatter_mut().unwrap().completion_message(); + 0 + } } - Commands::Lsp => sqruff_lsp::run(), - } + Commands::Lsp => { + sqruff_lsp::run(); + 0 + } + }; + + std::process::exit(status_code); } fn linter(config: FluffConfig, format: Format) -> Linter { diff --git a/crates/cli/tests/ui.rs b/crates/cli/tests/ui.rs index 38382497b..fb01badba 100644 --- a/crates/cli/tests/ui.rs +++ b/crates/cli/tests/ui.rs @@ -37,16 +37,19 @@ fn main() { // Run the command and capture the output let assert = cmd.assert(); - // Construct the expected output file path + // Construct the expected output file paths let mut expected_output_path_stderr = path.clone(); expected_output_path_stderr.set_extension("stderr"); let mut expected_output_path_stdout = path.clone(); expected_output_path_stdout.set_extension("stdout"); + let mut expected_output_path_exitcode = path.clone(); + expected_output_path_exitcode.set_extension("exitcode"); // Read the expected output let output = assert.get_output(); let stderr_str = std::str::from_utf8(&output.stderr).unwrap(); let stdout_str = std::str::from_utf8(&output.stdout).unwrap(); + let exit_code_str = output.status.code().unwrap().to_string(); let test_dir_str = test_dir.to_string_lossy().to_string(); let stderr_normalized: String = stderr_str.replace(&test_dir_str, "tests/ui"); @@ -54,6 +57,7 @@ fn main() { expect_file![expected_output_path_stderr].assert_eq(&stderr_normalized); expect_file![expected_output_path_stdout].assert_eq(&stdout_normalized); + expect_file![expected_output_path_exitcode].assert_eq(&exit_code_str); } } } diff --git a/crates/cli/tests/ui/LT01_LT012.exitcode b/crates/cli/tests/ui/LT01_LT012.exitcode new file mode 100644 index 000000000..56a6051ca --- /dev/null +++ b/crates/cli/tests/ui/LT01_LT012.exitcode @@ -0,0 +1 @@ +1 \ No newline at end of file diff --git a/crates/cli/tests/ui/LT01_noqa.exitcode b/crates/cli/tests/ui/LT01_noqa.exitcode new file mode 100644 index 000000000..c22708346 --- /dev/null +++ b/crates/cli/tests/ui/LT01_noqa.exitcode @@ -0,0 +1 @@ +0 \ No newline at end of file diff --git a/crates/cli/tests/ui/hql_file.exitcode b/crates/cli/tests/ui/hql_file.exitcode new file mode 100644 index 000000000..56a6051ca --- /dev/null +++ b/crates/cli/tests/ui/hql_file.exitcode @@ -0,0 +1 @@ +1 \ No newline at end of file diff --git a/crates/cli/tests/ui/test_fail_whitespace_before_comma.exitcode b/crates/cli/tests/ui/test_fail_whitespace_before_comma.exitcode new file mode 100644 index 000000000..56a6051ca --- /dev/null +++ b/crates/cli/tests/ui/test_fail_whitespace_before_comma.exitcode @@ -0,0 +1 @@ +1 \ No newline at end of file