Skip to content

Commit d6a5c21

Browse files
test(analyser): add debug_test (#198)
1 parent 46b679a commit d6a5c21

File tree

7 files changed

+84
-6
lines changed

7 files changed

+84
-6
lines changed

Cargo.lock

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/pglt_analyse/src/rule.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ pub trait Rule: RuleMeta + Sized {
9494
}
9595

9696
/// Diagnostic object returned by a single analysis rule
97-
#[derive(Debug, Diagnostic)]
97+
#[derive(Debug, Diagnostic, PartialEq)]
9898
pub struct RuleDiagnostic {
9999
#[category]
100100
pub(crate) category: &'static Category,
@@ -109,7 +109,7 @@ pub struct RuleDiagnostic {
109109
pub(crate) rule_advice: RuleAdvice,
110110
}
111111

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

121-
#[derive(Debug, Default)]
121+
#[derive(Debug, Default, PartialEq)]
122122
pub struct SuggestionList {
123123
pub(crate) message: MarkupBuf,
124124
pub(crate) list: Vec<MarkupBuf>,
@@ -160,7 +160,7 @@ impl Advices for RuleAdvice {
160160
}
161161
}
162162

163-
#[derive(Debug)]
163+
#[derive(Debug, PartialEq)]
164164
pub struct Detail {
165165
pub log_category: LogCategory,
166166
pub message: MarkupBuf,

crates/pglt_analyser/CONTRIBUTING.md

+14
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,20 @@ This will cause the documentation generator to ensure the rule does emit
280280
exactly one diagnostic for this code, and to include a snapshot for the
281281
diagnostic in the resulting documentation page.
282282

283+
### Testing the Rule
284+
285+
#### Quick Test
286+
287+
To quickly test your rule, head to the `pglt_analyser/src/lib.rs` file and modify the `debug_test` function.
288+
289+
You should:
290+
291+
- remove the `#[ignore]` macro if present
292+
- change the content of the `SQL` static `&str` to whatever you need
293+
- pass your group and rule to the `RuleFilter::Rule(..)`
294+
295+
If you run the test, you'll see any diagnostics your rule created in your console.
296+
283297
### Code generation
284298

285299
For simplicity, use `just` to run all the commands with:

crates/pglt_analyser/Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,7 @@ pglt_analyse = { workspace = true }
1616
pglt_console = { workspace = true }
1717
pglt_query_ext = { workspace = true }
1818
serde = { workspace = true }
19+
20+
[dev-dependencies]
21+
pglt_diagnostics = { workspace = true }
22+
termcolor = { workspace = true }

crates/pglt_analyser/src/lib.rs

+58
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,61 @@ impl<'a> Analyser<'a> {
6565
.collect::<Vec<_>>()
6666
}
6767
}
68+
69+
#[cfg(test)]
70+
mod tests {
71+
use core::slice;
72+
73+
use pglt_analyse::{AnalyserOptions, AnalysisFilter, RuleFilter};
74+
use pglt_console::{
75+
fmt::{Formatter, Termcolor},
76+
markup, Markup,
77+
};
78+
use pglt_diagnostics::PrintDiagnostic;
79+
use termcolor::NoColor;
80+
81+
use crate::Analyser;
82+
83+
#[ignore]
84+
#[test]
85+
fn debug_test() {
86+
fn markup_to_string(markup: Markup) -> String {
87+
let mut buffer = Vec::new();
88+
let mut write = Termcolor(NoColor::new(&mut buffer));
89+
let mut fmt = Formatter::new(&mut write);
90+
fmt.write_markup(markup).unwrap();
91+
92+
String::from_utf8(buffer).unwrap()
93+
}
94+
95+
const SQL: &str = r#"alter table test drop column id;"#;
96+
let rule_filter = RuleFilter::Rule("safety", "banDropColumn");
97+
98+
let filter = AnalysisFilter {
99+
enabled_rules: Some(slice::from_ref(&rule_filter)),
100+
..Default::default()
101+
};
102+
103+
let ast = pglt_query_ext::parse(SQL).expect("failed to parse SQL");
104+
105+
let options = AnalyserOptions::default();
106+
107+
let analyser = Analyser::new(crate::AnalyserConfig {
108+
options: &options,
109+
filter,
110+
});
111+
112+
let results = analyser.run(crate::AnalyserContext { root: &ast });
113+
114+
println!("*******************");
115+
for result in &results {
116+
let text = markup_to_string(markup! {
117+
{PrintDiagnostic::simple(result)}
118+
});
119+
eprintln!("{}", text);
120+
}
121+
println!("*******************");
122+
123+
// assert_eq!(results, vec![]);
124+
}
125+
}

crates/pglt_diagnostics/src/advice.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ where
195195
}
196196
}
197197

198-
#[derive(Debug)]
198+
#[derive(Debug, PartialEq)]
199199
/// Utility type implementing [Advices] that emits a
200200
/// code suggestion with the provided text
201201
pub struct CodeSuggestionAdvice<M> {

crates/pglt_diagnostics/src/display/message.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use termcolor::NoColor;
1818
/// message: MessageAndDescription
1919
/// }
2020
/// ```
21-
#[derive(Clone, Deserialize, Serialize)]
21+
#[derive(Clone, Deserialize, Serialize, PartialEq)]
2222
pub struct MessageAndDescription {
2323
/// Shown when medium supports custom markup
2424
message: MarkupBuf,

0 commit comments

Comments
 (0)