Repository: xd009642/tarpaulin
Pull request #1304 (2023-06-14 22:18:46)

Fix all-targets
<!--- 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 c200680..75d49d2 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,9 @@ file.
 - Match `cargo test` behaviour for --no-fail-fast and report coverage when option is selected
 - Simplify cargo version parsing for rust installed via distro package managers
 
+### Fixed
+- Fix handling of `--all-targets` flag
+
 ## [0.25.2] 2023-04-04
 ### Added
 - Filtering for other test attributes such as `#[tokio::test]`
diff --git a/Cargo.lock b/Cargo.lock
index 79793e7..2dca824 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -344,9 +344,9 @@ dependencies = [
 
 [[package]]
 name = "gimli"
-version = "0.27.2"
+version = "0.27.3"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "ad0a93d233ebf96623465aad4046a8d3aa4da22d4f4beba5388838c8a434bbb4"
+checksum = "b6c80984affa11d98d1b88b66ac8853f143217b399d3c74116778ff8fdb4ed2e"
 dependencies = [
  "fallible-iterator 0.2.0",
  "indexmap",
diff --git a/Cargo.toml b/Cargo.toml
index c2d7b0a..16fb078 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -26,7 +26,7 @@ chrono = "0.4"
 clap = "2.34.0"
 coveralls-api = "0.5.0"
 fallible-iterator = "0.3.0"
-gimli = "0.27.2"
+gimli = "0.27.3"
 git2 = "0.17"
 humantime-serde = "1"
 indexmap = { version = "~1.8", features = ["serde-1"] }
diff --git a/src/cargo.rs b/src/cargo.rs
index dbe93b2..3e45cb5 100644
--- a/src/cargo.rs
+++ b/src/cargo.rs
@@ -592,9 +592,6 @@ fn init_args(test_cmd: &mut Command, config: &Config) {
         test_cmd.arg("--features");
         test_cmd.arg(features);
     }
-    if config.all_targets {
-        test_cmd.arg("--all-targets");
-    }
     if config.all_features {
         test_cmd.arg("--all-features");
     }
diff --git a/src/config/mod.rs b/src/config/mod.rs
index f2d77fb..66cba61 100644
--- a/src/config/mod.rs
+++ b/src/config/mod.rs
@@ -50,8 +50,6 @@ pub struct Config {
     /// The opposite of --force-clean
     #[serde(rename = "skip-clean")]
     skip_clean: bool,
-    #[serde(rename = "all-targets")]
-    pub all_targets: bool,
     /// Verbose flag for printing information to the user
     pub verbose: bool,
     /// Debug flag for printing internal debugging information to the user
@@ -207,7 +205,6 @@ impl Default for Config {
             config: None,
             root: Default::default(),
             run_ignored: false,
-            all_targets: false,
             ignore_tests: true,
             include_tests: false,
             ignore_panics: false,
@@ -311,7 +308,6 @@ impl<'a> From<&'a ArgMatches<'a>> for ConfigWrapper {
             force_clean,
             skip_clean: !force_clean,
             no_fail_fast: args.is_present("no-fail-fast"),
-            all_targets: args.is_present("all-targets"),
             follow_exec: args.is_present("follow-exec"),
             verbose,
             debug,
@@ -455,6 +451,11 @@ impl Config {
         self.profraw_folder = path;
     }
 
+    /// Sets the target dir explicitly
+    pub fn set_target_dir(&mut self, target_dir: PathBuf) {
+        self.target_dir = Some(target_dir);
+    }
+
     pub fn doctest_dir(&self) -> PathBuf {
         // https://github.com/rust-lang/rust/issues/98690
         let mut result = self.target_dir();
@@ -620,7 +621,6 @@ impl Config {
         self.count |= other.count;
         self.all_features |= other.all_features;
         self.implicit_test_threads |= other.implicit_test_threads;
-        self.all_targets |= other.all_targets;
         self.line_coverage |= other.line_coverage;
         self.branch_coverage |= other.branch_coverage;
         self.dump_traces |= other.dump_traces;
@@ -1338,7 +1338,6 @@ mod tests {
         assert!(config.debug);
         assert!(config.verbose);
         assert!(config.dump_traces);
-        assert!(config.all_targets);
         assert!(config.ignore_panics);
         assert!(config.count);
         assert!(config.run_ignored);
diff --git a/src/main.rs b/src/main.rs
index 47685ed..bfd4680 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -90,7 +90,7 @@ fn from_args() -> ArgMatches<'static> {
                  --bench [NAME]... 'Test only the specified bench target'
                  --benches 'Test all benches'
                  --doc 'Test only this library's documentation'
-                 --all-targets 'Test all targets'
+                 --all-targets 'Test all targets (excluding doctests)'
                  --no-fail-fast 'Run all tests regardless of failure'
                  --profile [NAME] 'Build artefacts with the specified profile'
                  --debug 'Show debug output - this is used for diagnosing issues with tarpaulin'


Test patch:
diff --git a/tests/data/all_test_types/.gitignore b/tests/data/all_test_types/.gitignore
new file mode 100644
index 0000000..0d6776c
--- /dev/null
+++ b/tests/data/all_test_types/.gitignore
@@ -0,0 +1,1 @@
+*_target/
diff --git a/tests/test_types.rs b/tests/test_types.rs
index 440b0f1..433515b 100644
--- a/tests/test_types.rs
+++ b/tests/test_types.rs
@@ -23,9 +23,12 @@ fn mix_test_types() {
     let restore_dir = env::current_dir().unwrap();
     let test_dir = get_test_path("all_test_types");
     env::set_current_dir(&test_dir).unwrap();
-    let mut manifest = test_dir;
+    let mut manifest = test_dir.clone();
     manifest.push("Cargo.toml");
     config.set_manifest(manifest);
+    let mut target = test_dir;
+    target.push("mix_target");
+    config.set_target_dir(target);
 
     let (res, ret) = launch_tarpaulin(&config, &None).unwrap();
     assert_eq!(ret, 0);
@@ -51,9 +54,12 @@ fn only_test_coverage() {
     let restore_dir = env::current_dir().unwrap();
     let test_dir = get_test_path("all_test_types");
     env::set_current_dir(&test_dir).unwrap();
-    let mut manifest = test_dir;
+    let mut manifest = test_dir.clone();
     manifest.push("Cargo.toml");
     config.set_manifest(manifest);
+    let mut target = test_dir;
+    target.push("only_test_target");
+    config.set_target_dir(target);
     config.set_profraw_folder(PathBuf::from("only_test_coverage"));
 
     let (res, ret) = launch_tarpaulin(&config, &None).unwrap();
@@ -70,6 +76,39 @@ fn only_test_coverage() {
     }
 }
 
+#[test]
+fn all_targets_coverage() {
+    setup_logging(Color::Never, false, false);
+    let mut config = Config::default();
+    config.set_clean(false);
+    config.test_timeout = Duration::from_secs(60);
+    config.run_types = vec![RunType::AllTargets];
+    let restore_dir = env::current_dir().unwrap();
+    let test_dir = get_test_path("all_test_types");
+    env::set_current_dir(&test_dir).unwrap();
+    let mut manifest = test_dir.clone();
+    manifest.push("Cargo.toml");
+    config.set_manifest(manifest);
+    let mut target = test_dir;
+    target.push("all_target");
+    config.set_target_dir(target);
+    config.set_profraw_folder(PathBuf::from("all_targets_coverage"));
+
+    let (res, ret) = launch_tarpaulin(&config, &None).unwrap();
+    assert_eq!(ret, 0);
+    env::set_current_dir(restore_dir).unwrap();
+
+    for f in res.files() {
+        let f_name = f.file_name().unwrap().to_str().unwrap();
+        if f_name.contains("doc") {
+            assert_eq!(res.covered_in_path(f), 0);
+        } else {
+            assert!(res.covered_in_path(f) > 0);
+        }
+    }
+}
+
+
 #[test]
 fn only_example_coverage() {
     setup_logging(Color::Never, false, false);
@@ -80,9 +119,12 @@ fn only_example_coverage() {
     let restore_dir = env::current_dir().unwrap();
     let test_dir = get_test_path("all_test_types");
     env::set_current_dir(&test_dir).unwrap();
-    let mut manifest = test_dir;
+    let mut manifest = test_dir.clone();
     manifest.push("Cargo.toml");
     config.set_manifest(manifest);
+    let mut target = test_dir;
+    target.push("example_target");
+    config.set_target_dir(target);
     config.set_profraw_folder(PathBuf::from("only_example_coverage"));
 
     let (res, ret) = launch_tarpaulin(&config, &None).unwrap();
@@ -110,9 +152,12 @@ fn only_bench_coverage() {
     let restore_dir = env::current_dir().unwrap();
     let test_dir = get_test_path("all_test_types");
     env::set_current_dir(&test_dir).unwrap();
-    let mut manifest = test_dir;
+    let mut manifest = test_dir.clone();
     manifest.push("Cargo.toml");
     config.set_manifest(manifest);
+    let mut target = test_dir;
+    target.push("bench_target");
+    config.set_target_dir(target);
     config.set_profraw_folder(PathBuf::from("only_bench_coverage"));
 
     let (res, ret) = launch_tarpaulin(&config, &None).unwrap();
@@ -140,9 +185,12 @@ fn only_doctest_coverage() {
     let restore_dir = env::current_dir().unwrap();
     let test_dir = get_test_path("all_test_types");
     env::set_current_dir(&test_dir).unwrap();
-    let mut manifest = test_dir;
+    let mut manifest = test_dir.clone();
     manifest.push("Cargo.toml");
     config.set_manifest(manifest);
+    let mut target = test_dir;
+    target.push("doc_target");
+    config.set_target_dir(target);
     config.set_profraw_folder(PathBuf::from("only_doctest_coverage"));
 
     let (res, ret) = launch_tarpaulin(&config, &None).unwrap();