Skip to content

Commit f8e2c99

Browse files
committed
fix: returned error codes on fix
1 parent 07a5424 commit f8e2c99

File tree

10 files changed

+143
-10
lines changed

10 files changed

+143
-10
lines changed

crates/cli/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ name = "bench"
2424
path = "src/bin/bench.rs"
2525
bench = false
2626

27+
28+
[[test]]
29+
name = "fix_return_code"
30+
harness = false
31+
2732
[[test]]
2833
name = "config_not_found"
2934
harness = false

crates/cli/src/commands_fix.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,26 @@ pub(crate) fn run_fix(
4545
}
4646
}
4747

48+
let any_unfixable_errors = result.paths.iter().any(|path| {
49+
path.files
50+
.iter()
51+
.any(|file| !file.get_violations(Some(false)).is_empty())
52+
});
53+
4854
for linted_dir in result.paths {
4955
for mut file in linted_dir.files {
5056
let path = std::mem::take(&mut file.path);
5157
let write_buff = file.fix_string();
5258
std::fs::write(path, write_buff).unwrap();
5359
}
5460
}
55-
5661
linter.formatter_mut().unwrap().completion_message();
57-
0
62+
63+
if any_unfixable_errors {
64+
1
65+
} else {
66+
0
67+
}
5868
}
5969
}
6070

crates/cli/tests/configure_rule.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ fn main() {
77
configure_rule();
88
}
99

10-
///
1110
fn configure_rule() {
1211
let profile = if cfg!(debug_assertions) {
1312
"debug"
@@ -51,7 +50,7 @@ fn configure_rule() {
5150
.arg("--config")
5251
.arg(&config)
5352
.arg(&sql_path);
54-
cmd.current_dir(&cargo_folder);
53+
cmd.current_dir(cargo_folder);
5554

5655
// Set the HOME environment variable to the fake home directory
5756
cmd.env("HOME", PathBuf::from(env!("CARGO_MANIFEST_DIR")));

crates/cli/tests/fix_return_code.rs

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
use core::str;
2+
use std::path::{Path, PathBuf};
3+
4+
use assert_cmd::Command;
5+
6+
fn main() {
7+
fix_return_code();
8+
}
9+
10+
fn fix_return_code() {
11+
let profile = if cfg!(debug_assertions) {
12+
"debug"
13+
} else {
14+
"release"
15+
};
16+
17+
// Tests needed
18+
// STDIN
19+
// - Fix, do nothing -> 0
20+
// - Fix, fix everything -> 0
21+
// - Fix, fix some not all -> 1
22+
// TODO File
23+
// - Fix, do nothing -> 0
24+
// - Fix, fix everything -> 0
25+
// - Fix, fix some not all -> 1
26+
27+
let cargo_folder = Path::new(env!("CARGO_MANIFEST_DIR"));
28+
let mut sqruff_path = PathBuf::from(cargo_folder);
29+
sqruff_path.push(format!("../../target/{}/sqruff", profile));
30+
31+
// STDIN - do nothing
32+
let mut cmd = Command::new(sqruff_path.clone());
33+
cmd.env("HOME", PathBuf::from(env!("CARGO_MANIFEST_DIR")));
34+
cmd.arg("fix").arg("-f").arg("human").arg("-");
35+
cmd.current_dir(cargo_folder);
36+
cmd.write_stdin("SELECT foo FROM bar;\n");
37+
38+
// Run the command and capture the output
39+
// let assert = cmd.assert();
40+
// let output = assert.get_output();
41+
42+
// assert!(output.status.code().unwrap().to_string() == "0");
43+
44+
// let stdout_str = str::from_utf8(&output.stdout).unwrap();
45+
// let stderr_str = str::from_utf8(&output.stderr).unwrap();
46+
// assert_eq!(stdout_str, "SELECT foo FROM bar;\n\n");
47+
// assert_eq!(stderr_str, "");
48+
49+
// STDIN - nothing to fix
50+
let config_file = cargo_folder.join("tests/fix_return_code/fix_everything.cfg");
51+
let mut cmd = Command::new(sqruff_path.clone());
52+
cmd.env("HOME", PathBuf::from(env!("CARGO_MANIFEST_DIR")));
53+
cmd.arg("fix")
54+
.arg("-f")
55+
.arg("human")
56+
.arg("--config")
57+
.arg(&config_file)
58+
.arg("-");
59+
cmd.write_stdin("SELECT foo AS bar FROM tabs");
60+
61+
let assert = cmd.assert();
62+
let output = assert.get_output();
63+
64+
let stdout_str = str::from_utf8(&output.stdout).unwrap();
65+
let stderr_str = str::from_utf8(&output.stderr).unwrap();
66+
assert_eq!(stdout_str, "SELECT foo AS bar FROM tabs\n");
67+
assert_eq!(stderr_str, "");
68+
assert_eq!(output.status.code().unwrap(), 0);
69+
70+
// STDIN - fix everything
71+
let config_file = cargo_folder.join("tests/fix_return_code/fix_everything.cfg");
72+
let mut cmd = Command::new(sqruff_path.clone());
73+
cmd.env("HOME", PathBuf::from(env!("CARGO_MANIFEST_DIR")));
74+
cmd.arg("fix")
75+
.arg("-f")
76+
.arg("human")
77+
.arg("--config")
78+
.arg(&config_file)
79+
.arg("-");
80+
cmd.write_stdin("SELECT foo bar FROM tabs");
81+
82+
let assert = cmd.assert();
83+
let output = assert.get_output();
84+
85+
let stdout_str = str::from_utf8(&output.stdout).unwrap();
86+
let stderr_str = str::from_utf8(&output.stderr).unwrap();
87+
assert_eq!(stdout_str, "SELECT foo AS bar FROM tabs\n");
88+
assert_eq!(stderr_str, "== [<string>] FAIL\nL: 1 | P: 12 | AL02 | Implicit/explicit aliasing of columns.\n | [aliasing.column]\n");
89+
assert_eq!(output.status.code().unwrap(), 0);
90+
91+
// STDIN - fix some not all
92+
let config_file = cargo_folder.join("tests/fix_return_code/fix_some.cfg");
93+
let mut cmd = Command::new(sqruff_path.clone());
94+
cmd.env("HOME", PathBuf::from(env!("CARGO_MANIFEST_DIR")));
95+
cmd.arg("fix")
96+
.arg("-f")
97+
.arg("human")
98+
.arg("--config")
99+
.arg(&config_file)
100+
.arg("-");
101+
cmd.write_stdin("SELECT foo bar, * FROM tabs");
102+
103+
let assert = cmd.assert();
104+
let output = assert.get_output();
105+
106+
let stdout_str = str::from_utf8(&output.stdout).unwrap();
107+
let stderr_str = str::from_utf8(&output.stderr).unwrap();
108+
assert_eq!(stdout_str, "SELECT foo AS bar, * FROM tabs\n");
109+
assert_eq!(stderr_str, "== [<string>] FAIL\nL: 1 | P: 1 | AM04 | Outermost query should produce known number of columns.\n | [ambiguous.column_count]\nL: 1 | P: 12 | AL02 | Implicit/explicit aliasing of columns.\n | [aliasing.column]\n");
110+
assert_eq!(output.status.code().unwrap(), 1);
111+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[sqruff]
2+
rules = AL02
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[sqruff]
2+
rules = AL02, AM04

crates/lib-core/src/errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ impl SQLBaseError {
5656

5757
impl SqlError for SQLBaseError {
5858
fn fixable(&self) -> bool {
59-
false
59+
self.fixable
6060
}
6161

6262
fn rule_code(&self) -> Option<&'static str> {

crates/lib/src/core/linter/linted_file.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::ops::Range;
33
use crate::core::rules::noqa::IgnoreMask;
44
use itertools::Itertools;
55
use rustc_hash::FxHashSet;
6-
use sqruff_lib_core::errors::SQLBaseError;
6+
use sqruff_lib_core::errors::{SQLBaseError, SqlError};
77
use sqruff_lib_core::parser::segments::fix::FixPatch;
88
use sqruff_lib_core::templaters::base::{RawFileSlice, TemplatedFile};
99

@@ -17,11 +17,11 @@ pub struct LintedFile {
1717
}
1818

1919
impl LintedFile {
20-
pub fn get_violations(&self, fixable: Option<bool>) -> Vec<SQLBaseError> {
21-
if let Some(fixable) = fixable {
20+
pub fn get_violations(&self, is_fixable: Option<bool>) -> Vec<SQLBaseError> {
21+
if let Some(is_fixable) = is_fixable {
2222
self.violations
2323
.iter()
24-
.filter(|v| v.fixable == fixable)
24+
.filter(|v| v.fixable() == is_fixable)
2525
.cloned()
2626
.collect_vec()
2727
} else {

crates/lib/src/rules/aliasing/al02.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ impl Rule for RuleAL02 {
4343
Ok(rule.erased())
4444
}
4545

46+
fn is_fix_compatible(&self) -> bool {
47+
true
48+
}
49+
4650
fn name(&self) -> &'static str {
4751
"aliasing.column"
4852
}

docs/rules.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ Implicit/explicit aliasing of columns.
108108

109109
**Groups:** `all`, `core`, `aliasing`
110110

111-
**Fixable:** No
111+
**Fixable:** Yes
112112

113113
**Anti-pattern**
114114

0 commit comments

Comments
 (0)