Skip to content

Commit 98aa9e8

Browse files
committed
feat(scanner): detect #[test] and #[cfg(test)] mod blocks as test entries
Wires test_attribute_patterns and adds a sibling conditional_test_attributes plugin knob; scanner walks tree-sitter attribute_item nodes and cascades the annotated item's range to every contained function. Synthesizes language defaults when no project plugin is configured so built-in languages benefit. Generic across plugins -- Java picks up @test from the same mechanism.
1 parent 239d1fd commit 98aa9e8

3 files changed

Lines changed: 393 additions & 3 deletions

File tree

‎src/health.rs‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,15 @@ pub struct LanguagePluginConfig {
155155
/// Path patterns matching test modules (in addition to `test_path_patterns`).
156156
pub test_module_patterns: Vec<String>,
157157
/// Source-line attributes/decorators that mark a function as a test
158-
/// (e.g. `#[test]`, `@Test`).
158+
/// (e.g. `#[test]`, `@Test`). Matched against the (trimmed) line
159+
/// immediately above each function, walking past blank/comment lines.
159160
pub test_attribute_patterns: Vec<String>,
161+
/// Attributes whose presence marks an entire scope (mod, function, or
162+
/// any nested item) as test-only code, cascading to every function
163+
/// contained within. Canonical example: Rust `#[cfg(test)]`. Matched
164+
/// against the leading text of `attribute_item`-style nodes via
165+
/// tree-sitter so multi-line attribute spellings still resolve.
166+
pub conditional_test_attributes: Vec<String>,
160167
/// Tree-sitter node kinds representing function parameter declarations.
161168
pub parameter_node_kinds: Vec<String>,
162169
/// Tree-sitter node kinds that increment cyclomatic complexity (`if`,
@@ -208,6 +215,7 @@ impl Default for LanguagePluginConfig {
208215
entry_point_patterns: Vec::new(),
209216
test_module_patterns: Vec::new(),
210217
test_attribute_patterns: Vec::new(),
218+
conditional_test_attributes: Vec::new(),
211219
parameter_node_kinds: Vec::new(),
212220
complexity_node_kinds: Vec::new(),
213221
logical_operator_kinds: Vec::new(),
@@ -4863,7 +4871,7 @@ mod tests {
48634871
#[test]
48644872
fn plugin_config_round_trips_extended_semantic_fields() {
48654873
let config: RaysenseConfig = toml::from_str(
4866-
r#"
4874+
r##"
48674875
[[scan.plugins]]
48684876
name = "toy"
48694877
extensions = ["toy"]
@@ -4874,11 +4882,12 @@ module_prefix_directives = ["package "]
48744882
entry_point_patterns = ["main", "init"]
48754883
test_module_patterns = ["tests/*"]
48764884
test_attribute_patterns = ["@Test"]
4885+
conditional_test_attributes = ["#[cfg(test)]"]
48774886
parameter_node_kinds = ["parameter"]
48784887
complexity_node_kinds = ["if_expression", "while_expression"]
48794888
logical_operator_kinds = ["&&", "||"]
48804889
abstract_base_classes = ["Base", "Abstract"]
4881-
"#,
4890+
"##,
48824891
)
48834892
.expect("plugin config with new fields parses");
48844893

@@ -4895,6 +4904,7 @@ abstract_base_classes = ["Base", "Abstract"]
48954904
assert_eq!(plugin.entry_point_patterns, vec!["main", "init"]);
48964905
assert_eq!(plugin.test_module_patterns, vec!["tests/*"]);
48974906
assert_eq!(plugin.test_attribute_patterns, vec!["@Test"]);
4907+
assert_eq!(plugin.conditional_test_attributes, vec!["#[cfg(test)]"]);
48984908
assert_eq!(plugin.parameter_node_kinds, vec!["parameter"]);
48994909
assert_eq!(
49004910
plugin.complexity_node_kinds,
@@ -4927,6 +4937,7 @@ extensions = ["min"]
49274937
assert!(plugin.entry_point_patterns.is_empty());
49284938
assert!(plugin.test_module_patterns.is_empty());
49294939
assert!(plugin.test_attribute_patterns.is_empty());
4940+
assert!(plugin.conditional_test_attributes.is_empty());
49304941
assert!(plugin.parameter_node_kinds.is_empty());
49314942
assert!(plugin.complexity_node_kinds.is_empty());
49324943
assert!(plugin.logical_operator_kinds.is_empty());

‎src/mcp.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2800,6 +2800,7 @@ fn config_schema() -> Value {
28002800
"entry_point_patterns": {"type": "array", "items": {"type": "string"}},
28012801
"test_module_patterns": {"type": "array", "items": {"type": "string"}},
28022802
"test_attribute_patterns": {"type": "array", "items": {"type": "string"}},
2803+
"conditional_test_attributes": {"type": "array", "items": {"type": "string"}},
28032804
"parameter_node_kinds": {"type": "array", "items": {"type": "string"}},
28042805
"complexity_node_kinds": {"type": "array", "items": {"type": "string"}},
28052806
"logical_operator_kinds": {"type": "array", "items": {"type": "string"}},

0 commit comments

Comments
 (0)