From 5e55d55f2e780400c5fdc925b2ab2d585f20ee96 Mon Sep 17 00:00:00 2001 From: Bastian Schubert Date: Tue, 11 Oct 2022 16:13:08 +0200 Subject: [PATCH] handle output files --- src/cobertura.rs | 11 +++++++-- src/main.rs | 49 ++++++++++++++++++++++++--------------- src/output.rs | 60 +++++++++++++++++++++++++++++++++++++++--------- 3 files changed, 88 insertions(+), 32 deletions(-) diff --git a/src/cobertura.rs b/src/cobertura.rs index f8d6bbd17..f97747733 100644 --- a/src/cobertura.rs +++ b/src/cobertura.rs @@ -330,7 +330,7 @@ fn get_coverage( pub fn output_cobertura( source_dir: Option<&Path>, results: &[(PathBuf, PathBuf, CovResult)], - output_file: Option<&Path>, + output_path: Option<&Path>, demangle: bool, ) { let demangle_options = DemangleOptions::name_only(); @@ -491,7 +491,14 @@ pub fn output_cobertura( .unwrap(); let result = writer.into_inner().into_inner(); - let mut file = BufWriter::new(get_target_output_writable(output_file)); + let output_file = output_path.map(|path| { + if path.is_dir() { + path.join("cobertura.xml") + } else { + path.to_path_buf() + } + }); + let mut file = BufWriter::new(get_target_output_writable(output_file.as_ref())); file.write_all(&result).unwrap(); } diff --git a/src/main.rs b/src/main.rs index ad7f17dd0..94d3ef10a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -102,11 +102,11 @@ struct Opt { )] output_types: Vec, /// Specifies the output path. - #[structopt(short, long, value_name = "PATH", alias = "output-file")] + #[structopt(short, long, value_name = "PATH", alias = "output-folder")] output_path: Option, /// Specifies the output config file. - #[structopt(long, value_name = "PATH", alias = "output-config-file")] - output_config_file: Option, + #[structopt(long, value_name = "PATH", alias = "html-output-config-file")] + html_output_config_file: Option, /// Specifies the root directory of the source files. #[structopt(short, long, value_name = "DIRECTORY", parse(from_os_str))] source_dir: Option, @@ -406,12 +406,26 @@ fn main() { let service_number = opt.service_number.unwrap_or_default(); let service_pull_request = opt.service_pull_request.unwrap_or_default(); let commit_sha = opt.commit_sha.unwrap_or_default(); + + let output_path = match opt.output_types.len() { + 0 => return, + 1 => opt.output_path.as_deref(), + _ => match opt.output_path.as_deref() { + Some(output_path) => { + if output_path.is_dir() { + Some(output_path) + } else { + panic!("output_path must be a directory when using multiple outputs"); + } + } + _ => None, + }, + }; + for output_type in &opt.output_types { match output_type { - OutputType::Ade => { - output_activedata_etl(&iterator, opt.output_path.as_deref(), demangle) - } - OutputType::Lcov => output_lcov(&iterator, opt.output_path.as_deref(), demangle), + OutputType::Ade => output_activedata_etl(&iterator, output_path, demangle), + OutputType::Lcov => output_lcov(&iterator, output_path, demangle), OutputType::Coveralls => output_coveralls( &iterator, opt.token.as_deref(), @@ -421,7 +435,7 @@ fn main() { &service_pull_request, &commit_sha, false, - opt.output_path.as_deref(), + output_path, &opt.vcs_branch, opt.parallel, demangle, @@ -435,26 +449,23 @@ fn main() { &service_pull_request, &commit_sha, true, - opt.output_path.as_deref(), + output_path, &opt.vcs_branch, opt.parallel, demangle, ), - OutputType::Files => output_files(&iterator, opt.output_path.as_deref()), - OutputType::Covdir => output_covdir(&iterator, opt.output_path.as_deref()), + OutputType::Files => output_files(&iterator, output_path), + OutputType::Covdir => output_covdir(&iterator, output_path), OutputType::Html => output_html( &iterator, - opt.output_path.as_deref(), + output_path, num_threads, opt.branch, - opt.output_config_file.as_deref(), - ), - OutputType::Cobertura => output_cobertura( - source_root.as_deref(), - &iterator, - opt.output_path.as_deref(), - demangle, + opt.html_output_config_file.as_deref(), ), + OutputType::Cobertura => { + output_cobertura(source_root.as_deref(), &iterator, output_path, demangle) + } }; } } diff --git a/src/output.rs b/src/output.rs index a543a9eb8..138119b1e 100644 --- a/src/output.rs +++ b/src/output.rs @@ -35,7 +35,7 @@ macro_rules! demangle { }}; } -pub fn get_target_output_writable(output_file: Option<&Path>) -> Box { +pub fn get_target_output_writable(output_file: Option<&PathBuf>) -> Box { let write_target: Box = match output_file { Some(output) => { if output.is_dir() { @@ -71,11 +71,18 @@ pub fn get_target_output_writable(output_file: Option<&Path>) -> Box pub fn output_activedata_etl( results: &[(PathBuf, PathBuf, CovResult)], - output_file: Option<&Path>, + output_path: Option<&Path>, demangle: bool, ) { let demangle_options = DemangleOptions::name_only(); - let mut writer = BufWriter::new(get_target_output_writable(output_file)); + let output_file = output_path.map(|path| { + if path.is_dir() { + path.join("activedata") + } else { + path.to_path_buf() + } + }); + let mut writer = BufWriter::new(get_target_output_writable(output_file.as_ref())); for (_, rel_path, result) in results { let covered: Vec = result @@ -183,8 +190,15 @@ pub fn output_activedata_etl( } } -pub fn output_covdir(results: &[(PathBuf, PathBuf, CovResult)], output_file: Option<&Path>) { - let mut writer = BufWriter::new(get_target_output_writable(output_file)); +pub fn output_covdir(results: &[(PathBuf, PathBuf, CovResult)], output_path: Option<&Path>) { + let output_file = output_path.map(|path| { + if path.is_dir() { + path.join("covdir") + } else { + path.to_path_buf() + } + }); + let mut writer = BufWriter::new(get_target_output_writable(output_file.as_ref())); let mut relative: FxHashMap>> = FxHashMap::default(); let global = Rc::new(RefCell::new(CDDirStats::new("".to_string()))); relative.insert(PathBuf::from(""), global.clone()); @@ -241,11 +255,18 @@ pub fn output_covdir(results: &[(PathBuf, PathBuf, CovResult)], output_file: Opt pub fn output_lcov( results: &[(PathBuf, PathBuf, CovResult)], - output_file: Option<&Path>, + output_path: Option<&Path>, demangle: bool, ) { let demangle_options = DemangleOptions::name_only(); - let mut writer = BufWriter::new(get_target_output_writable(output_file)); + let output_file = output_path.map(|path| { + if path.is_dir() { + path.join("lcov") + } else { + path.to_path_buf() + } + }); + let mut writer = BufWriter::new(get_target_output_writable(output_file.as_ref())); writer.write_all(b"TN:\n").unwrap(); for (_, rel_path, result) in results { @@ -428,7 +449,7 @@ pub fn output_coveralls( service_pull_request: &str, commit_sha: &str, with_function_info: bool, - output_file: Option<&Path>, + output_path: Option<&Path>, vcs_branch: &str, parallel: bool, demangle: bool, @@ -508,12 +529,29 @@ pub fn output_coveralls( obj.insert("service_job_id".to_string(), json!(service_job_id)); } - let mut writer = BufWriter::new(get_target_output_writable(output_file)); + let output_file = output_path.map(|path| { + if path.is_dir() { + path.join(format!( + "coveralls{}", + if with_function_info { "+" } else { "" } + )) + } else { + path.to_path_buf() + } + }); + let mut writer = BufWriter::new(get_target_output_writable(output_file.as_ref())); serde_json::to_writer(&mut writer, &result).unwrap(); } -pub fn output_files(results: &[(PathBuf, PathBuf, CovResult)], output_file: Option<&Path>) { - let mut writer = BufWriter::new(get_target_output_writable(output_file)); +pub fn output_files(results: &[(PathBuf, PathBuf, CovResult)], output_path: Option<&Path>) { + let output_file = output_path.map(|path| { + if path.is_dir() { + path.join("files") + } else { + path.to_path_buf() + } + }); + let mut writer = BufWriter::new(get_target_output_writable(output_file.as_ref())); for (_, rel_path, _) in results { writeln!(writer, "{}", rel_path.display()).unwrap(); }