Repository: xd009642/tarpaulin
Pull request #1503 (2024-04-04 22:33:48)

Fix `include-test` config to include non-std test
<!--- When contributing to tarpaulin all contributions should branch off the -->
<!--- develop branch as master is only used for releases. --> 

<!--- Check CONTRIBUTING.md for more information-->

<!--- Please describe the changes in the PR and them motivation below -->
Motivation:
The original issue is sorta described here: https://github.com/xd009642/tarpaulin/issues/351#issuecomment-2036231706. 
But tl;dr version is when `include-tests` is set, `#[tokio::test]` is not covered in the coverage report. 

Changes in this PR:
Added an additional condition to `visit_fn` to check whether the `PathSegment`s ends in `test`, such as `#[tokio::test]` or `#[some_fancy_crate::test]`. 

I tested this tarpaulin version against the issue that I was originally encountering, and it seems to fix it.  
Before where `#[tokio::test]` was ignored: https://app.codecov.io/gh/kube-rs/kube/blob/cov-tweak/kube-runtime%2Fsrc%2Fcontroller%2Ffuture_hash_map.rs#L85
After where it's now included: https://app.codecov.io/gh/tyrone-wu/kube/blob/main/kube-runtime%2Fsrc%2Fcontroller%2Ffuture_hash_map.rs#L85

xd009642: Cool thanks for this, don't worry about the beta and nightly failures, it's something I'm currently (slowly) working on :sweat: 

Patch:
diff --git a/src/source_analysis/items.rs b/src/source_analysis/items.rs
index babad14..602fd6b 100644
--- a/src/source_analysis/items.rs
+++ b/src/source_analysis/items.rs
@@ -103,7 +103,8 @@ impl SourceAnalysis {
         for attr in &func.attrs {
             if let Ok(x) = attr.parse_meta() {
                 let id = x.path();
-                if id.is_ident("test") {
+                if id.is_ident("test") || id.segments.last().is_some_and(|seg| seg.ident == "test")
+                {
                     test_func = true;
                 } else if id.is_ident("derive") {
                     let analysis = self.get_line_analysis(ctx.file.to_path_buf());


Test patch:
diff --git a/src/source_analysis/tests.rs b/src/source_analysis/tests.rs
index fa0337c..9b93015 100644
--- a/src/source_analysis/tests.rs
+++ b/src/source_analysis/tests.rs
@@ -518,6 +518,60 @@ fn filter_nonstd_tests() {
     assert!(lines.ignore.contains(&Lines::Line(3)));
 }
 
+#[test]
+fn include_nonstd_tests() {
+    let mut config = Config::default();
+    config.set_include_tests(true);
+
+    let ctx = Context {
+        config: &config,
+        file_contents: "#[cfg(test)]
+            mod tests {
+                #[tokio::test(worker_threads = 1)]
+                fn boo(){
+                    assert!(true);
+                }
+            }",
+        file: Path::new(""),
+        ignore_mods: RefCell::new(HashSet::new()),
+    };
+    let parser = parse_file(ctx.file_contents).unwrap();
+    let mut analysis = SourceAnalysis::new();
+    analysis.process_items(&parser.items, &ctx);
+    let lines = analysis.get_line_analysis(ctx.file.to_path_buf());
+    assert!(!lines.ignore.contains(&Lines::Line(5)));
+
+    let ctx = Context {
+        config: &config,
+        file_contents: "#[tokio::test(worker_threads = 1)]
+                fn boo(){
+                    assert!(true);
+                }",
+        file: Path::new(""),
+        ignore_mods: RefCell::new(HashSet::new()),
+    };
+    let parser = parse_file(ctx.file_contents).unwrap();
+    let mut analysis = SourceAnalysis::new();
+    analysis.process_items(&parser.items, &ctx);
+    let lines = analysis.get_line_analysis(ctx.file.to_path_buf());
+    assert!(!lines.ignore.contains(&Lines::Line(3)));
+
+    let ctx = Context {
+        config: &config,
+        file_contents: "#[some_fancy_crate::test]
+                fn boo(){
+                    assert!(true);
+                }",
+        file: Path::new(""),
+        ignore_mods: RefCell::new(HashSet::new()),
+    };
+    let parser = parse_file(ctx.file_contents).unwrap();
+    let mut analysis = SourceAnalysis::new();
+    analysis.process_items(&parser.items, &ctx);
+    let lines = analysis.get_line_analysis(ctx.file.to_path_buf());
+    assert!(!lines.ignore.contains(&Lines::Line(3)));
+}
+
 #[test]
 fn filter_test_utilities() {
     let mut config = Config::default();