Skip to content

Commit 9ce47fa

Browse files
committed
Add visit_ignored_arguments configuration
1 parent 42bd5ef commit 9ce47fa

6 files changed

Lines changed: 54 additions & 18 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,8 @@ A configuration file allows one to tailor Necessist's behavior with respect to a
430430

431431
- `ignored_tests`: A list of strings. A test whose name exactly matches a string in the list is ignored. For Mocha-based frameworks (e.g., Anchor and Hardhat), a test name is considered to be a message passed to `it`.
432432

433+
- `visit_ignored_arguments`: A boolean indicating whether Necessist should visit the arguments of ignored functions, methods, and macros. The default is `false`.
434+
433435
- `walkable_functions`: A list of strings interpreted as [patterns]. If a test calls a function that matches the pattern, and the function is declared in the same file as the test, then statements and method calls are removed from the function as though it were a test.
434436

435437
### Patterns

backends/src/generic_visitor.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,9 @@ macro_rules! visit_maybe_macro_call {
120120
}
121121

122122
// smoelius: Return false (i.e., don't descend into the call arguments) only if
123-
// the call or method call is ignored.
124-
!$args.is_ignored_as_call && !$args.is_ignored_as_method_call
123+
// the call or method call is ignored and `visit_ignored_arguments` is false.
124+
(!$args.is_ignored_as_call && !$args.is_ignored_as_method_call)
125+
|| $this.config.visit_ignored_arguments()
125126
} else {
126127
true
127128
}

core/src/config.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub struct Compiled {
1717
ignored_methods: Vec<Regex>,
1818
ignored_path_disambiguation: IgnoredPathDisambiguation,
1919
ignored_tests: Vec<String>,
20+
visit_ignored_arguments: bool,
2021
walkable_functions: Vec<Regex>,
2122
}
2223

@@ -42,6 +43,10 @@ impl Compiled {
4243
self.ignored_tests.iter().any(|s| name == s)
4344
}
4445
#[must_use]
46+
pub fn visit_ignored_arguments(&self) -> bool {
47+
self.visit_ignored_arguments
48+
}
49+
#[must_use]
4550
pub fn is_walkable_function(&self, name: &str) -> bool {
4651
self.walkable_functions.iter().any(|re| re.is_match(name))
4752
}
@@ -60,6 +65,8 @@ pub struct Toml {
6065
#[serde(default)]
6166
pub ignored_tests: Vec<String>,
6267
#[serde(default)]
68+
pub visit_ignored_arguments: Option<bool>,
69+
#[serde(default)]
6370
pub walkable_functions: Vec<String>,
6471
#[serde(flatten)]
6572
pub other: BTreeMap<String, toml::Value>,
@@ -94,6 +101,7 @@ impl Toml {
94101
ignored_methods,
95102
ignored_path_disambiguation,
96103
ignored_tests,
104+
visit_ignored_arguments,
97105
walkable_functions,
98106
other: _,
99107
} = other;
@@ -105,6 +113,13 @@ impl Toml {
105113
return None;
106114
}
107115

116+
if self.visit_ignored_arguments.is_some()
117+
&& other.visit_ignored_arguments.is_some()
118+
&& self.visit_ignored_arguments != *visit_ignored_arguments
119+
{
120+
return None;
121+
}
122+
108123
self.ignored_functions.extend_from_slice(ignored_functions);
109124
self.ignored_macros.extend_from_slice(ignored_macros);
110125
self.ignored_methods.extend_from_slice(ignored_methods);
@@ -113,6 +128,7 @@ impl Toml {
113128
self.ignored_path_disambiguation = *ignored_path_disambiguation;
114129

115130
self.ignored_tests.extend_from_slice(ignored_tests);
131+
self.visit_ignored_arguments = *visit_ignored_arguments;
116132
self.walkable_functions
117133
.extend_from_slice(walkable_functions);
118134

@@ -126,6 +142,7 @@ impl Toml {
126142
ignored_methods,
127143
ignored_path_disambiguation,
128144
ignored_tests,
145+
visit_ignored_arguments,
129146
walkable_functions,
130147
other: _,
131148
} = self;
@@ -141,6 +158,7 @@ impl Toml {
141158
ignored_methods,
142159
ignored_path_disambiguation: ignored_path_disambiguation.unwrap_or_default(),
143160
ignored_tests,
161+
visit_ignored_arguments: visit_ignored_arguments.unwrap_or(false),
144162
walkable_functions,
145163
})
146164
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
ignored_macros = [
22
"say_hello"
33
]
4+
ignored_functions = [
5+
"ignored_function"
6+
]
47
ignored_methods = [
58
"ignored_method"
69
]
10+
visit_ignored_arguments = true

fixtures/valid_pattern/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ fn foo() -> S {
4040
S { field: T }
4141
}
4242

43+
fn ignored_function<T>(_: T) {}
44+
45+
fn noop() {}
46+
4347
#[test]
4448
fn test() {
4549
say_hello!();
@@ -60,4 +64,8 @@ fn test() {
6064
bar.field.method().baz();
6165
bar.field.ignored_method();
6266
bar.field.ignored_method().baz();
67+
68+
ignored_function(foo().method());
69+
70+
noop();
6371
}
Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
1-
20 candidates in 1 test in 1 source file
1+
23 candidates in 1 test in 1 source file
22
fixtures/valid_pattern/src/lib.rs: dry running
33
fixtures/valid_pattern/src/lib.rs: mutilating
4-
fixtures/valid_pattern/src/lib.rs:49:5-49:20: `foo().method();` passed
5-
fixtures/valid_pattern/src/lib.rs:50:5-50:26: `foo().method().baz();` passed
6-
fixtures/valid_pattern/src/lib.rs:51:5-51:28: `foo().ignored_method();` passed
7-
fixtures/valid_pattern/src/lib.rs:52:5-52:34: `foo().ignored_method().baz();` passed
8-
fixtures/valid_pattern/src/lib.rs:54:5-54:18: `bar.method();` passed
9-
fixtures/valid_pattern/src/lib.rs:55:5-55:24: `bar.method().baz();` passed
10-
fixtures/valid_pattern/src/lib.rs:57:5-57:32: `bar.ignored_method().baz();` passed
11-
fixtures/valid_pattern/src/lib.rs:59:5-59:24: `bar.field.method();` passed
12-
fixtures/valid_pattern/src/lib.rs:60:5-60:30: `bar.field.method().baz();` passed
13-
fixtures/valid_pattern/src/lib.rs:49:10-49:19: `.method()` passed
14-
fixtures/valid_pattern/src/lib.rs:50:19-50:25: `.baz()` passed
15-
fixtures/valid_pattern/src/lib.rs:52:27-52:33: `.baz()` passed
16-
fixtures/valid_pattern/src/lib.rs:55:17-55:23: `.baz()` passed
17-
fixtures/valid_pattern/src/lib.rs:57:25-57:31: `.baz()` passed
18-
fixtures/valid_pattern/src/lib.rs:60:23-60:29: `.baz()` passed
4+
fixtures/valid_pattern/src/lib.rs:53:5-53:20: `foo().method();` passed
5+
fixtures/valid_pattern/src/lib.rs:54:5-54:26: `foo().method().baz();` passed
6+
fixtures/valid_pattern/src/lib.rs:55:5-55:28: `foo().ignored_method();` passed
7+
fixtures/valid_pattern/src/lib.rs:56:5-56:34: `foo().ignored_method().baz();` passed
8+
fixtures/valid_pattern/src/lib.rs:58:5-58:18: `bar.method();` passed
9+
fixtures/valid_pattern/src/lib.rs:59:5-59:24: `bar.method().baz();` passed
10+
fixtures/valid_pattern/src/lib.rs:61:5-61:32: `bar.ignored_method().baz();` passed
11+
fixtures/valid_pattern/src/lib.rs:63:5-63:24: `bar.field.method();` passed
12+
fixtures/valid_pattern/src/lib.rs:64:5-64:30: `bar.field.method().baz();` passed
13+
fixtures/valid_pattern/src/lib.rs:66:5-66:38: `bar.field.ignored_method().baz();` passed
14+
fixtures/valid_pattern/src/lib.rs:53:10-53:19: `.method()` passed
15+
fixtures/valid_pattern/src/lib.rs:54:19-54:25: `.baz()` passed
16+
fixtures/valid_pattern/src/lib.rs:56:27-56:33: `.baz()` passed
17+
fixtures/valid_pattern/src/lib.rs:59:17-59:23: `.baz()` passed
18+
fixtures/valid_pattern/src/lib.rs:61:25-61:31: `.baz()` passed
19+
fixtures/valid_pattern/src/lib.rs:64:23-64:29: `.baz()` passed
20+
fixtures/valid_pattern/src/lib.rs:66:31-66:37: `.baz()` passed
21+
fixtures/valid_pattern/src/lib.rs:68:27-68:36: `.method()` passed

0 commit comments

Comments
 (0)