Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: move cli to return codes always and check #910

Merged
merged 2 commits into from
Oct 30, 2024
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
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
Loading