-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: make github native a fomatter
- Loading branch information
Showing
17 changed files
with
198 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
SELECT 1; --noqa: LT01 |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
SELECT 1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
::error title=sqruff,file=tests/lint/hql_file.hql,line=1,col=7::LT01: Expected only single space before "1". Found " ". | ||
::error title=sqruff,file=tests/lint/hql_file.hql,line=1,col=11::LT12: Files must end with a single trailing newline. |
1 change: 1 addition & 0 deletions
1
crates/cli/tests/github/test_fail_whitespace_before_comma.exitcode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
SELECT 1 ,4 |
5 changes: 5 additions & 0 deletions
5
crates/cli/tests/github/test_fail_whitespace_before_comma.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
::error title=sqruff,file=tests/lint/test_fail_whitespace_before_comma.sql,line=1,col=8::AL03: Column expression without alias. Use explicit `AS` clause. | ||
::error title=sqruff,file=tests/lint/test_fail_whitespace_before_comma.sql,line=1,col=9::LT01: Unexpected whitespace before comma. | ||
::error title=sqruff,file=tests/lint/test_fail_whitespace_before_comma.sql,line=1,col=11::AL03: Column expression without alias. Use explicit `AS` clause. | ||
::error title=sqruff,file=tests/lint/test_fail_whitespace_before_comma.sql,line=1,col=11::LT01: Expected single whitespace between "," and "4". | ||
::error title=sqruff,file=tests/lint/test_fail_whitespace_before_comma.sql,line=1,col=12::LT12: Files must end with a single trailing newline. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
== [<string>] FAIL | ||
L: 1 | P: 20 | LT12 | Files must end with a single trailing newline. | ||
| [layout.end_of_file] | ||
The linter processed 1 file(s). | ||
All Finished |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
use std::fs; | ||
use std::path::PathBuf; | ||
|
||
use assert_cmd::Command; | ||
use expect_test::expect_file; | ||
|
||
fn main() { | ||
let profile = if cfg!(debug_assertions) { | ||
"debug" | ||
} else { | ||
"release" | ||
}; | ||
let mut lint_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")); | ||
lint_dir.push("tests/github"); | ||
|
||
// Iterate over each test file in the directory | ||
for entry in fs::read_dir(&lint_dir).unwrap() { | ||
let entry = entry.unwrap(); | ||
let path = entry.path(); | ||
|
||
// Check if the file has a .sql or .hql extension | ||
if path | ||
.extension() | ||
.and_then(|e| e.to_str()) | ||
.map_or(false, |ext| ext == "sql" || ext == "hql") | ||
{ | ||
// Construct the path to the sqruff binary | ||
let mut sqruff_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); | ||
sqruff_path.push(format!("../../target/{}/sqruff", profile)); | ||
|
||
// Set up the command with arguments | ||
let mut cmd = Command::new(sqruff_path); | ||
|
||
cmd.arg("lint"); | ||
cmd.arg(path.to_str().unwrap()); | ||
// Set the HOME environment variable to the fake home directory | ||
cmd.env("HOME", PathBuf::from(env!("CARGO_MANIFEST_DIR"))); | ||
cmd.env("GITHUB_ACTIONS", "true"); | ||
|
||
// Run the command and capture the output | ||
let assert = cmd.assert(); | ||
|
||
// 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 = lint_dir.to_string_lossy().to_string(); | ||
let stderr_normalized: String = stderr_str.replace(&test_dir_str, "tests/lint"); | ||
let stdout_normalized: String = stdout_str.replace(&test_dir_str, "tests/lint"); | ||
|
||
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod formatters; | ||
pub mod github_annotation_native_formatter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
use crate::core::config::FluffConfig; | ||
use crate::core::linter::linted_file::LintedFile; | ||
use std::io::{Stderr, Write}; | ||
use std::sync::atomic::{AtomicBool, Ordering}; | ||
|
||
use super::formatters::Formatter; | ||
|
||
#[derive(Debug)] | ||
pub struct GithubAnnotationNativeFormatter { | ||
output_stream: Stderr, | ||
pub has_fail: AtomicBool, | ||
} | ||
|
||
impl GithubAnnotationNativeFormatter { | ||
pub fn new(stderr: Stderr) -> Self { | ||
Self { | ||
output_stream: stderr, | ||
has_fail: AtomicBool::new(false), | ||
} | ||
} | ||
|
||
fn dispatch(&self, s: &str) { | ||
let _ignored = self.output_stream.lock().write(s.as_bytes()).unwrap(); | ||
} | ||
} | ||
|
||
impl Formatter for GithubAnnotationNativeFormatter { | ||
fn dispatch_template_header( | ||
&self, | ||
_f_name: String, | ||
_linter_config: FluffConfig, | ||
_file_config: FluffConfig, | ||
) { | ||
// No-op | ||
} | ||
|
||
fn dispatch_parse_header(&self, _f_name: String) { | ||
// No-op | ||
} | ||
|
||
fn dispatch_file_violations(&self, linted_file: &LintedFile, _only_fixable: bool) { | ||
let mut violations = linted_file.get_violations(None); | ||
|
||
violations.sort_by(|a, b| { | ||
a.line_no | ||
.cmp(&b.line_no) | ||
.then_with(|| a.line_pos.cmp(&b.line_pos)) | ||
.then_with(|| { | ||
let b = b.rule.as_ref().unwrap().code; | ||
a.rule.as_ref().unwrap().code.cmp(b) | ||
}) | ||
}); | ||
|
||
for violation in violations { | ||
let message = format!( | ||
"::error title=sqruff,file={},line={},col={}::{}: {}\n", | ||
linted_file.path, | ||
violation.line_no, | ||
violation.line_pos, | ||
violation.rule.as_ref().unwrap().code, | ||
violation.description | ||
); | ||
self.dispatch(&message); | ||
if !violation.ignore && !violation.warning { | ||
self.has_fail.store(true, Ordering::SeqCst); | ||
} | ||
} | ||
} | ||
|
||
fn has_fail(&self) -> bool { | ||
self.has_fail.load(Ordering::SeqCst) | ||
} | ||
|
||
fn completion_message(&self) { | ||
// No-op | ||
} | ||
} |