Repository: xd009642/tarpaulin
Pull request #1201 (2023-01-25 21:04:09)

Save event log to report directory
This keeps things somewhat consistent as it's a report and makes it easier for people to keep project root clean of extraneous files

<!--- 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 9866875..7112cdb 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -3,6 +3,12 @@
 From 2019 onwards, all notable changes to tarpaulin will be documented in this
 file.
 
+## [Unreleased]
+### Changed
+- Dumped traces are now saved to reports output directory
+- Change event log name to print datetime stamps without colons or slashes so they'll save in other
+operating systems
+
 ## [0.24.0] 2023-01-24
 ### Added
 - Merge rustdocflags field from `cargo/config.toml` with env tarpaulin sets
diff --git a/src/event_log.rs b/src/event_log.rs
index 17ecce0..2272d76 100644
--- a/src/event_log.rs
+++ b/src/event_log.rs
@@ -1,4 +1,5 @@
 use crate::cargo::TestBinary;
+use crate::config::Config;
 #[cfg(ptrace_supported)]
 use crate::ptrace_control::*;
 #[cfg(ptrace_supported)]
@@ -14,7 +15,7 @@ use serde::{Deserialize, Serialize};
 use std::cell::RefCell;
 use std::collections::HashSet;
 use std::fs::File;
-use std::path::{Path, PathBuf};
+use std::path::PathBuf;
 use std::time::Instant;
 use tracing::{info, warn};
 
@@ -165,14 +166,17 @@ pub struct EventLog {
     #[serde(skip)]
     start: Option<Instant>,
     manifest_paths: HashSet<PathBuf>,
+    #[serde(skip)]
+    output_folder: PathBuf,
 }
 
 impl EventLog {
-    pub fn new(manifest_paths: HashSet<PathBuf>) -> Self {
+    pub fn new(manifest_paths: HashSet<PathBuf>, config: &Config) -> Self {
         Self {
             events: RefCell::new(vec![]),
             start: Some(Instant::now()),
             manifest_paths,
+            output_folder: config.output_dir(),
         }
     }
 
@@ -214,11 +218,8 @@ impl EventLog {
 
 impl Drop for EventLog {
     fn drop(&mut self) {
-        let fname = format!(
-            "tarpaulin_{}.json",
-            Local::now().to_rfc3339_opts(SecondsFormat::Secs, false)
-        );
-        let path = Path::new(&fname);
+        let fname = format!("tarpaulin_{}.json", Local::now().format("%Y%m%d%H%M%S"));
+        let path = self.output_folder.join(fname);
         info!("Serializing tarpaulin debug log to {}", path.display());
         if let Ok(output) = File::create(path) {
             if let Err(e) = serde_json::to_writer(output, self) {
diff --git a/src/lib.rs b/src/lib.rs
index ff018fd..0200a8c 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -126,7 +126,16 @@ pub fn trace(configs: &[Config]) -> Result<TraceMap, RunError> {
 
 fn create_logger(configs: &[Config]) -> Option<EventLog> {
     if configs.iter().any(|c| c.dump_traces) {
-        Some(EventLog::new(configs.iter().map(|x| x.root()).collect()))
+        let config = if let Some(c) = configs.iter().find(|c| c.output_directory.is_some()) {
+            c
+        } else {
+            &configs[0]
+        };
+
+        Some(EventLog::new(
+            configs.iter().map(|x| x.root()).collect(),
+            config,
+        ))
     } else {
         None
     }


Test patch:
diff --git a/tests/mod.rs b/tests/mod.rs
index 8a32970..3e4f5c8 100755
--- a/tests/mod.rs
+++ b/tests/mod.rs
@@ -120,7 +120,7 @@ pub fn check_percentage_with_config(
     let event_log = if config.dump_traces {
         let mut paths = HashSet::new();
         paths.insert(config.manifest().clone());
-        Some(EventLog::new(paths))
+        Some(EventLog::new(paths, &config))
     } else {
         None
     };
@@ -586,6 +586,7 @@ fn output_dir_workspace() {
     config.set_engine(TraceEngine::Llvm);
     config.set_ignore_tests(false);
     config.set_clean(false);
+    config.dump_traces = true;
     config.generate.push(OutputFile::Lcov);
     config.generate.push(OutputFile::Html);
     config.generate.push(OutputFile::Xml);
@@ -606,10 +607,17 @@ fn output_dir_workspace() {
             output.insert(file_name);
         }
     }
-    assert!(output.contains("cobertura.xml"));
-    assert!(output.contains("lcov.info"));
-    assert!(output.contains("tarpaulin-report.html"));
-    assert!(output.contains("tarpaulin-report.json"));
-    assert_eq!(output.len(), 4);
+    assert!(output.remove("cobertura.xml"));
+    assert!(output.remove("lcov.info"));
+    assert!(output.remove("tarpaulin-report.html"));
+    assert!(output.remove("tarpaulin-report.json"));
+    assert_eq!(output.len(), 1);
+
+    for event_log in &output {
+        let events = report_dir.join(event_log);
+        let log = fs::read(events).unwrap();
+        // We can deserialize event log so it must be good
+        serde_json::from_slice::<EventLog>(log.as_slice()).unwrap();
+    }
 }
 }