Repository: xd009642/tarpaulin
Pull request #1091 (2022-09-11 15:35:17)

Make --ignore-tests the default
This will automatically exclude test code from the coverage results. This is partially due to some llvm coverage issues with spawning executables from tests so I'd rather just cut out the false negatives from test code entirely.

<!--- 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 -->



Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 834f310..4da53d3 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,7 @@ file.
 
 ### Changed
 - Update quick-xml to 0.25
+- Make --ignore-tests the default and add a flag --include-tests to reapply the old behaviour
 
 ## [0.21.0] 2022-08-30
 ### Changed
diff --git a/src/config/mod.rs b/src/config/mod.rs
index 5844259..db34ef0 100644
--- a/src/config/mod.rs
+++ b/src/config/mod.rs
@@ -40,7 +40,7 @@ pub struct Config {
     pub run_ignored: bool,
     /// Flag to ignore test functions in coverage statistics
     #[serde(rename = "ignore-tests")]
-    pub ignore_tests: bool,
+    ignore_tests: bool,
     /// Ignore panic macros in code.
     #[serde(rename = "ignore-panics")]
     pub ignore_panics: bool,
@@ -177,6 +177,9 @@ pub struct Config {
     engine: RefCell<TraceEngine>,
     /// Specifying per-config rust flags
     pub rustflags: Option<String>,
+    #[serde(rename = "include-tests")]
+    /// Inverse of ignore_tests
+    include_tests: bool,
 }
 
 fn default_test_timeout() -> Duration {
@@ -194,7 +197,8 @@ impl Default for Config {
             root: Default::default(),
             run_ignored: false,
             all_targets: false,
-            ignore_tests: false,
+            ignore_tests: true,
+            include_tests: false,
             ignore_panics: false,
             force_clean: true,
             skip_clean: false,
@@ -285,7 +289,8 @@ impl<'a> From<&'a ArgMatches<'a>> for ConfigWrapper {
             color: value_t!(args.value_of("color"), Color).unwrap_or(Color::Auto),
             run_types: get_run_types(args),
             run_ignored: args.is_present("ignored"),
-            ignore_tests: args.is_present("ignore-tests"),
+            ignore_tests: !args.is_present("include-tests"),
+            include_tests: args.is_present("include-tests"),
             ignore_panics: args.is_present("ignore-panics"),
             force_clean,
             skip_clean: !force_clean,
@@ -378,6 +383,15 @@ impl Config {
         self.skip_clean = !clean;
     }
 
+    pub fn set_ignore_tests(&mut self, ignore: bool) {
+        self.ignore_tests = ignore;
+        self.include_tests = !ignore;
+    }
+
+    pub fn ignore_tests(&self) -> bool {
+        self.ignore_tests || !self.include_tests
+    }
+
     pub fn force_clean(&self) -> bool {
         // default is force clean true skip clean false. So if one isn't default we pick that one
         // as precedence.
@@ -552,6 +566,7 @@ impl Config {
         self.line_coverage |= other.line_coverage;
         self.branch_coverage |= other.branch_coverage;
         self.dump_traces |= other.dump_traces;
+        self.ignore_tests = self.ignore_tests() && other.ignore_tests();
         self.offline |= other.offline;
         if self.manifest != other.manifest && self.manifest == default_manifest() {
             self.manifest = other.manifest.clone();
diff --git a/src/main.rs b/src/main.rs
index 5d87c4c..79e379c 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -95,7 +95,8 @@ fn from_args() -> ArgMatches<'static> {
                  --debug 'Show debug output - this is used for diagnosing issues with tarpaulin'
                  --dump-traces 'Log tracing events and save to a json file. Also, enabled when --debug is used'
                  --verbose -v 'Show extra output'
-                 --ignore-tests 'Ignore lines of test functions when collecting coverage'
+                 --ignore-tests 'Ignore lines of test functions when collecting coverage (default)'
+                 --include-tests 'Include lines of test functions when collecting coverage'
                  --ignore-panics 'Ignore panic macros in tests'
                  --count   'Counts the number of hits during coverage'
                  --ignored -i 'Run ignored tests as well'
diff --git a/src/source_analysis/attributes.rs b/src/source_analysis/attributes.rs
index 3b01a57..7912d2e 100644
--- a/src/source_analysis/attributes.rs
+++ b/src/source_analysis/attributes.rs
@@ -15,7 +15,7 @@ impl SourceAnalysis {
                         let mut skip = false;
                         for c in &ml.nested {
                             if let NestedMeta::Meta(Meta::Path(ref i)) = c {
-                                skip |= i.is_ident("test") && ctx.config.ignore_tests;
+                                skip |= i.is_ident("test") && ctx.config.ignore_tests();
                             }
                         }
                         if skip {
diff --git a/src/source_analysis/items.rs b/src/source_analysis/items.rs
index ad41f65..95365ff 100644
--- a/src/source_analysis/items.rs
+++ b/src/source_analysis/items.rs
@@ -59,7 +59,7 @@ impl SourceAnalysis {
                     }
                     check_insides = false;
                     break;
-                } else if ctx.config.ignore_tests && x.path().is_ident("cfg") {
+                } else if ctx.config.ignore_tests() && x.path().is_ident("cfg") {
                     if let Meta::List(ref ml) = x {
                         for nested in &ml.nested {
                             if let NestedMeta::Meta(Meta::Path(ref i)) = *nested {
@@ -119,7 +119,7 @@ impl SourceAnalysis {
             }
         }
         if ignore_span
-            || (test_func && ctx.config.ignore_tests)
+            || (test_func && ctx.config.ignore_tests())
             || (ignored_attr && !ctx.config.run_ignored)
         {
             let analysis = self.get_line_analysis(ctx.file.to_path_buf());
diff --git a/src/source_analysis/mod.rs b/src/source_analysis/mod.rs
index e1a1ccd..403879f 100644
--- a/src/source_analysis/mod.rs
+++ b/src/source_analysis/mod.rs
@@ -307,7 +307,7 @@ impl SourceAnalysis {
         filtered_files: &mut HashSet<PathBuf>,
     ) {
         if let Some(file) = path.to_str() {
-            let skip_cause_test = config.ignore_tests && path.starts_with(root.join("tests"));
+            let skip_cause_test = config.ignore_tests() && path.starts_with(root.join("tests"));
             let skip_cause_example = path.starts_with(root.join("examples"))
                 && !config.run_types.contains(&RunType::Examples);
             if self.is_ignored_module(path) {


Test patch:
diff --git a/src/source_analysis/tests.rs b/src/source_analysis/tests.rs
index f767651..07ad7c9 100644
--- a/src/source_analysis/tests.rs
+++ b/src/source_analysis/tests.rs
@@ -386,9 +386,10 @@ fn filter_macros() {
 
 #[test]
 fn filter_tests() {
-    let config = Config::default();
+    let mut config = Config::default();
+    config.set_ignore_tests(false);
     let mut igconfig = Config::default();
-    igconfig.ignore_tests = true;
+    igconfig.set_ignore_tests(true);
 
     let ctx = Context {
         config: &config,
@@ -452,7 +453,7 @@ fn filter_tests() {
 #[test]
 fn filter_test_utilities() {
     let mut config = Config::default();
-    config.ignore_tests = true;
+    config.set_ignore_tests(true);
 
     let ctx = Context {
         config: &config,
@@ -473,7 +474,8 @@ fn filter_test_utilities() {
     assert!(lines.ignore.contains(&Lines::Line(3)));
     assert!(lines.ignore.contains(&Lines::Line(4)));
 
-    let config = Config::default();
+    let mut config = Config::default();
+    config.set_ignore_tests(false);
 
     let ctx = Context {
         config: &config,
diff --git a/src/test_loader.rs b/src/test_loader.rs
index a3e41da..6369187 100644
--- a/src/test_loader.rs
+++ b/src/test_loader.rs
@@ -279,7 +279,7 @@ fn get_line_addresses(
                 let temp_map = temp_map
                     .into_iter()
                     .filter(|&(ref k, _)| {
-                        !(config.ignore_tests && k.path.starts_with(&project.join("tests")))
+                        !(config.ignore_tests() && k.path.starts_with(&project.join("tests")))
                     })
                     .filter(|&(ref k, _)| !(config.exclude_path(&k.path)))
                     .filter(|&(ref k, _)| {
diff --git a/tests/mod.rs b/tests/mod.rs
index caf5b97..de806fb 100644
--- a/tests/mod.rs
+++ b/tests/mod.rs
@@ -30,6 +30,7 @@ pub fn check_percentage_with_cli_args(minimum_coverage: f64, has_lines: bool, ar
              --debug 'Show debug output - this is used for diagnosing issues with tarpaulin'
              --verbose -v 'Show extra output'
              --root -r [DIR] 'directory'
+             --include-tests 'include tests in your tests'
              --implicit-test-threads 'Don't supply an explicit `--test-threads` argument to tarpaulin. By default tarpaulin will infer the default rustc would pick if not ran via tarpaulin and set it'"
         ).get_matches_from(args);
 
@@ -97,6 +98,7 @@ pub fn check_percentage_with_config(
 
 pub fn check_percentage(project_name: &str, minimum_coverage: f64, has_lines: bool) {
     let mut config = Config::default();
+    config.set_ignore_tests(false);
     config.set_clean(false);
     check_percentage_with_config(project_name, minimum_coverage, has_lines, config);
 }
@@ -379,6 +381,7 @@ fn no_test_args() {
         "--root".to_string(),
         test_dir.display().to_string(),
         "--implicit-test-threads".to_string(),
+        "--include-tests".to_string(),
     ];
     check_percentage_with_cli_args(1.0, true, &args);
 }
@@ -388,6 +391,7 @@ fn dot_rs_in_dir_name() {
     // issue #857
     let mut config = Config::default();
     config.set_clean(false);
+    config.set_ignore_tests(false);
 
     let restore_dir = env::current_dir().unwrap();
     let test_dir = get_test_path("not_a_file.rs");
@@ -410,6 +414,7 @@ fn kill_used_in_test() {
     let mut config = Config::default();
     config.follow_exec = true;
     config.set_clean(false);
+    config.set_ignore_tests(false);
     // Currently 2 false negatives, but if it was only covering the integration test max coverage
     // is 75% so this is high enough to prove it works
     check_percentage_with_config("kill_proc", 0.9f64, true, config);
diff --git a/tests/workspaces.rs b/tests/workspaces.rs
index 5ce2fc1..43c8b22 100644
--- a/tests/workspaces.rs
+++ b/tests/workspaces.rs
@@ -15,6 +15,7 @@ fn package_exclude() {
     config.manifest = test_dir;
     config.manifest.push("Cargo.toml");
     config.set_clean(false);
+    config.set_ignore_tests(false);
 
     config.all = true;
     let result = launch_tarpaulin(&config, &None);
@@ -45,6 +46,7 @@ fn package_exclude() {
 fn specify_package() {
     let mut config = Config::default();
     config.set_clean(false);
+    config.set_ignore_tests(false);
 
     let test_dir = get_test_path("workspace");
     env::set_current_dir(&test_dir).unwrap();