-
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.
- Loading branch information
Showing
22 changed files
with
245 additions
and
9 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 @@ | ||
{"tests/lint/LT01_noqa.sql":[]} |
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; |
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 @@ | ||
{"tests/lint/hql_file.hql":[{"range":{"start":{"line":1,"character":7},"end":{"line":1,"character":7}},"message":"Expected only single space before \"1\". Found \" \".","severity":"Error","source":"sqruff"},{"range":{"start":{"line":1,"character":11},"end":{"line":1,"character":11}},"message":"Files must end with a single trailing newline.","severity":"Error","source":"sqruff"}]} |
1 change: 1 addition & 0 deletions
1
crates/cli/tests/json/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 |
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 @@ | ||
{"tests/lint/test_fail_whitespace_before_comma.sql":[{"range":{"start":{"line":1,"character":8},"end":{"line":1,"character":8}},"message":"Column expression without alias. Use explicit `AS` clause.","severity":"Error","source":"sqruff"},{"range":{"start":{"line":1,"character":11},"end":{"line":1,"character":11}},"message":"Column expression without alias. Use explicit `AS` clause.","severity":"Error","source":"sqruff"},{"range":{"start":{"line":1,"character":9},"end":{"line":1,"character":9}},"message":"Unexpected whitespace before comma.","severity":"Error","source":"sqruff"},{"range":{"start":{"line":1,"character":11},"end":{"line":1,"character":11}},"message":"Expected single whitespace between \",\" and \"4\".","severity":"Error","source":"sqruff"},{"range":{"start":{"line":1,"character":12},"end":{"line":1,"character":12}},"message":"Files must end with a single trailing newline.","severity":"Error","source":"sqruff"}]} |
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,67 @@ | ||
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/json"); | ||
|
||
// 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()); | ||
cmd.arg("-f"); | ||
cmd.arg("json"); | ||
// Set the HOME environment variable to the fake home directory | ||
cmd.env("HOME", PathBuf::from(env!("CARGO_MANIFEST_DIR"))); | ||
|
||
// 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
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,2 +1,4 @@ | ||
pub mod formatters; | ||
pub mod github_annotation_native_formatter; | ||
pub mod json; | ||
pub mod json_types; |
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,50 @@ | ||
use std::sync::Mutex; | ||
|
||
use crate::core::{config::FluffConfig, linter::linted_file::LintedFile}; | ||
|
||
use super::{ | ||
formatters::Formatter, | ||
json_types::{Diagnostic, DiagnosticCollection, DiagnosticSeverity}, | ||
}; | ||
|
||
#[derive(Default)] | ||
pub struct JsonFormatter { | ||
violations: Mutex<DiagnosticCollection>, | ||
} | ||
|
||
impl Formatter for JsonFormatter { | ||
fn dispatch_file_violations(&self, linted_file: &LintedFile, only_fixable: bool) { | ||
let violations = linted_file.get_violations(only_fixable.then_some(true)); | ||
let mut lock = self.violations.lock().unwrap(); | ||
lock.entry(linted_file.path.clone()).or_default().extend( | ||
violations | ||
.iter() | ||
.map(|err| Diagnostic::from(err.clone())) | ||
.collect::<Vec<_>>(), | ||
); | ||
} | ||
|
||
fn has_fail(&self) -> bool { | ||
let lock = self.violations.lock().unwrap(); | ||
lock.values().any(|v| { | ||
v.iter() | ||
.any(|d| matches!(&d.severity, DiagnosticSeverity::Error)) | ||
}) | ||
} | ||
|
||
fn completion_message(&self) { | ||
let lock = self.violations.lock().unwrap(); | ||
let json = serde_json::to_string(&*lock).unwrap(); | ||
println!("{}", json); | ||
} | ||
|
||
fn dispatch_template_header( | ||
&self, | ||
_f_name: String, | ||
_linter_config: FluffConfig, | ||
_file_config: FluffConfig, | ||
) { | ||
} | ||
|
||
fn dispatch_parse_header(&self, _f_name: String) {} | ||
} |
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,100 @@ | ||
use std::collections::BTreeMap; | ||
|
||
use serde::Serialize; | ||
use sqruff_lib_core::errors::SQLBaseError; | ||
|
||
impl From<SQLBaseError> for Diagnostic { | ||
fn from(value: SQLBaseError) -> Self { | ||
Diagnostic { | ||
range: Range { | ||
start: Position::new(value.line_no as u32, value.line_pos as u32), | ||
end: Position::new(value.line_no as u32, value.line_pos as u32), | ||
}, | ||
message: value.description, | ||
severity: if value.warning { | ||
DiagnosticSeverity::Warning | ||
} else { | ||
DiagnosticSeverity::Error | ||
}, | ||
source: Some("sqruff".to_string()), | ||
// code: todo!(), | ||
// source: Some(value.get_source().to_string()), | ||
// code: Some(DiagnosticCode { | ||
// value: value.rule_code().to_string(), | ||
// target: Uri::new("".to_string()), | ||
// }), | ||
// related_information: Vec::new(), | ||
// tags: Vec::new(), | ||
} | ||
} | ||
} | ||
|
||
/// Represents a line and character position, such as the position of the cursor. | ||
#[derive(Serialize)] | ||
struct Position { | ||
/// The zero-based line value. | ||
line: u32, | ||
/// The zero-based character value. | ||
character: u32, | ||
} | ||
|
||
impl Position { | ||
/// Creates a new `Position` instance. | ||
fn new(line: u32, character: u32) -> Self { | ||
Self { line, character } | ||
} | ||
} | ||
|
||
/// A range represents an ordered pair of two positions. It is guaranteed that `start` is before or equal to `end`. | ||
#[derive(Serialize)] | ||
struct Range { | ||
/// The start position. It is before or equal to `end`. | ||
start: Position, | ||
/// The end position. It is after or equal to `start`. | ||
end: Position, | ||
} | ||
|
||
/// Represents a diagnostic, such as a compiler error or warning. Diagnostic objects are only valid in the scope of a file. | ||
#[derive(Serialize)] | ||
pub struct Diagnostic { | ||
/// The range to which this diagnostic applies. | ||
range: Range, | ||
/// The human-readable message. | ||
message: String, | ||
/// The severity, default is {@link DiagnosticSeverity::Error error}. | ||
pub severity: DiagnosticSeverity, | ||
/// A human-readable string describing the source of this diagnostic, e.g. 'typescript' or 'super lint'. | ||
source: Option<String>, | ||
// A code or identifier for this diagnostic. Should be used for later processing, e.g. when providing {@link CodeActionContext code actions}. | ||
// code: Option<DiagnosticCode>, | ||
// TODO Maybe implement | ||
// An array of related diagnostic information, e.g. when symbol-names within a scope collide all definitions can be marked via this property. | ||
// related_information: Vec<DiagnosticRelatedInformation>, | ||
// Additional metadata about the diagnostic. | ||
// tags: Vec<DiagnosticTag>, | ||
} | ||
|
||
// /// Represents a related message and source code location for a diagnostic. This should be used to point to code locations that cause or are related to a diagnostics, e.g when duplicating a symbol in a scope. | ||
// #[derive(Serialize)] | ||
// struct DiagnosticCode { | ||
// /// A code or identifier for this diagnostic. | ||
// value: String, | ||
// // TODO Maybe implement | ||
// // A target URI to open with more information about the diagnostic error. | ||
// // target: Uri, | ||
// } | ||
|
||
/// Represents the severity of diagnostics. | ||
#[derive(Serialize)] | ||
pub enum DiagnosticSeverity { | ||
/// Something not allowed by the rules of a language or other means. | ||
Error = 0, | ||
/// Something suspicious but allowed. | ||
Warning = 1, | ||
/// Something to inform about but not a problem. | ||
Information = 2, | ||
/// Something to hint to a better way of doing it, like proposing a refactoring. | ||
Hint = 3, | ||
} | ||
|
||
pub type DiagnosticCollection = BTreeMap<String, Vec<Diagnostic>>; |
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