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

test(analyser): add debug_test #198

Merged
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
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions crates/pglt_analyse/src/rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ pub trait Rule: RuleMeta + Sized {
}

/// Diagnostic object returned by a single analysis rule
#[derive(Debug, Diagnostic)]
#[derive(Debug, Diagnostic, PartialEq)]
pub struct RuleDiagnostic {
#[category]
pub(crate) category: &'static Category,
Expand All @@ -109,7 +109,7 @@ pub struct RuleDiagnostic {
pub(crate) rule_advice: RuleAdvice,
}

#[derive(Debug, Default)]
#[derive(Debug, Default, PartialEq)]
/// It contains possible advices to show when printing a diagnostic that belong to the rule
pub struct RuleAdvice {
pub(crate) details: Vec<Detail>,
Expand All @@ -118,7 +118,7 @@ pub struct RuleAdvice {
pub(crate) code_suggestion_list: Vec<CodeSuggestionAdvice<MarkupBuf>>,
}

#[derive(Debug, Default)]
#[derive(Debug, Default, PartialEq)]
pub struct SuggestionList {
pub(crate) message: MarkupBuf,
pub(crate) list: Vec<MarkupBuf>,
Expand Down Expand Up @@ -160,7 +160,7 @@ impl Advices for RuleAdvice {
}
}

#[derive(Debug)]
#[derive(Debug, PartialEq)]
pub struct Detail {
pub log_category: LogCategory,
pub message: MarkupBuf,
Expand Down
14 changes: 14 additions & 0 deletions crates/pglt_analyser/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,20 @@ This will cause the documentation generator to ensure the rule does emit
exactly one diagnostic for this code, and to include a snapshot for the
diagnostic in the resulting documentation page.

### Testing the Rule

#### Quick Test

To quickly test your rule, head to the `pglt_analyser/src/lib.rs` file and modify the `debug_test` function.

You should:

- remove the `#[ignore]` macro if present
- change the content of the `SQL` static `&str` to whatever you need
- pass your group and rule to the `RuleFilter::Rule(..)`

If you run the test, you'll see any diagnostics your rule created in your console.

### Code generation

For simplicity, use `just` to run all the commands with:
Expand Down
4 changes: 4 additions & 0 deletions crates/pglt_analyser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ pglt_analyse = { workspace = true }
pglt_console = { workspace = true }
pglt_query_ext = { workspace = true }
serde = { workspace = true }

[dev-dependencies]
pglt_diagnostics = { workspace = true }
termcolor = { workspace = true }
58 changes: 58 additions & 0 deletions crates/pglt_analyser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,61 @@ impl<'a> Analyser<'a> {
.collect::<Vec<_>>()
}
}

#[cfg(test)]
mod tests {
use core::slice;

use pglt_analyse::{AnalyserOptions, AnalysisFilter, RuleFilter};
use pglt_console::{
fmt::{Formatter, Termcolor},
markup, Markup,
};
use pglt_diagnostics::PrintDiagnostic;
use termcolor::NoColor;

use crate::Analyser;

#[ignore]
#[test]
fn debug_test() {
fn markup_to_string(markup: Markup) -> String {
let mut buffer = Vec::new();
let mut write = Termcolor(NoColor::new(&mut buffer));
let mut fmt = Formatter::new(&mut write);
fmt.write_markup(markup).unwrap();

String::from_utf8(buffer).unwrap()
}

const SQL: &str = r#"alter table test drop column id;"#;
let rule_filter = RuleFilter::Rule("safety", "banDropColumn");

let filter = AnalysisFilter {
enabled_rules: Some(slice::from_ref(&rule_filter)),
..Default::default()
};

let ast = pglt_query_ext::parse(SQL).expect("failed to parse SQL");

let options = AnalyserOptions::default();

let analyser = Analyser::new(crate::AnalyserConfig {
options: &options,
filter,
});

let results = analyser.run(crate::AnalyserContext { root: &ast });

println!("*******************");
for result in &results {
let text = markup_to_string(markup! {
{PrintDiagnostic::simple(result)}
});
eprintln!("{}", text);
}
println!("*******************");

// assert_eq!(results, vec![]);
}
}
2 changes: 1 addition & 1 deletion crates/pglt_diagnostics/src/advice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ where
}
}

#[derive(Debug)]
#[derive(Debug, PartialEq)]
/// Utility type implementing [Advices] that emits a
/// code suggestion with the provided text
pub struct CodeSuggestionAdvice<M> {
Expand Down
2 changes: 1 addition & 1 deletion crates/pglt_diagnostics/src/display/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use termcolor::NoColor;
/// message: MessageAndDescription
/// }
/// ```
#[derive(Clone, Deserialize, Serialize)]
#[derive(Clone, Deserialize, Serialize, PartialEq)]
pub struct MessageAndDescription {
/// Shown when medium supports custom markup
message: MarkupBuf,
Expand Down