From e8199304e501d004fd2ebcbddf25dc795464dc38 Mon Sep 17 00:00:00 2001 From: Ben King <9087625+benfdking@users.noreply.github.com> Date: Wed, 30 Oct 2024 20:51:22 +0100 Subject: [PATCH 1/2] refactor: move cli to return codes always --- crates/cli/src/main.rs | 80 ++++++++++++++++++++++-------------------- 1 file changed, 42 insertions(+), 38 deletions(-) diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index 4bf41be1c..55794c251 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,17 @@ 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 +115,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(); + 0 } - - linter.formatter_mut().unwrap().completion_message(); } - Commands::Lsp => sqruff_lsp::run(), - } + Commands::Lsp => { + sqruff_lsp::run(); + 0 + } + }; + + std::process::exit(status_code); } fn linter(config: FluffConfig, format: Format) -> Linter { From 465f5f596072c1222f91a90222b83fd97496a2be Mon Sep 17 00:00:00 2001 From: Ben King <9087625+benfdking@users.noreply.github.com> Date: Wed, 30 Oct 2024 20:57:46 +0100 Subject: [PATCH 2/2] internal: adding printing exit codes to ui tests --- crates/cli/src/main.rs | 5 ++--- crates/cli/tests/ui.rs | 6 +++++- crates/cli/tests/ui/LT01_LT012.exitcode | 1 + crates/cli/tests/ui/LT01_noqa.exitcode | 1 + crates/cli/tests/ui/hql_file.exitcode | 1 + .../cli/tests/ui/test_fail_whitespace_before_comma.exitcode | 1 + 6 files changed, 11 insertions(+), 4 deletions(-) create mode 100644 crates/cli/tests/ui/LT01_LT012.exitcode create mode 100644 crates/cli/tests/ui/LT01_noqa.exitcode create mode 100644 crates/cli/tests/ui/hql_file.exitcode create mode 100644 crates/cli/tests/ui/test_fail_whitespace_before_comma.exitcode diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index 55794c251..d933c53fc 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -93,7 +93,6 @@ fn main() { } else { 0 } - } Commands::Fix(FixArgs { paths, @@ -130,7 +129,7 @@ fn main() { } } } - + for linted_dir in result.paths { for mut file in linted_dir.files { let path = std::mem::take(&mut file.path); @@ -138,7 +137,7 @@ fn main() { std::fs::write(path, write_buff).unwrap(); } } - + linter.formatter_mut().unwrap().completion_message(); 0 } 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