Skip to content

Commit

Permalink
refactor: move cli to return codes always (#910)
Browse files Browse the repository at this point in the history
* refactor: move cli to return codes always
* internal: adding printing exit codes to ui tests
  • Loading branch information
benfdking authored Oct 30, 2024
1 parent 45dc1f1 commit c8d9331
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 38 deletions.
77 changes: 40 additions & 37 deletions crates/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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,
Expand All @@ -117,35 +114,41 @@ fn main() {
.map(|path| path.files.len())
.sum::<usize>();
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 {
Expand Down
6 changes: 5 additions & 1 deletion crates/cli/tests/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,27 @@ 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");
let stdout_normalized: String = stdout_str.replace(&test_dir_str, "tests/ui");

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);
}
}
}
1 change: 1 addition & 0 deletions crates/cli/tests/ui/LT01_LT012.exitcode
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
1 change: 1 addition & 0 deletions crates/cli/tests/ui/LT01_noqa.exitcode
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
1 change: 1 addition & 0 deletions crates/cli/tests/ui/hql_file.exitcode
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1

0 comments on commit c8d9331

Please sign in to comment.