Skip to content

Commit

Permalink
[mozilla#496 multiple outputs]
Browse files Browse the repository at this point in the history
  • Loading branch information
Bastian Schubert committed Nov 2, 2022
1 parent 6528990 commit 07fe36b
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 92 deletions.
25 changes: 14 additions & 11 deletions src/cobertura.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ use quick_xml::{
Writer,
};
use rustc_hash::FxHashMap;
use std::io::{BufWriter, Cursor, Write};
use std::path::Path;
use std::time::{SystemTime, UNIX_EPOCH};
use std::{
io::{BufWriter, Cursor, Write},
path::PathBuf,
};
use symbolic_common::Name;
use symbolic_demangle::{Demangle, DemangleOptions};

use crate::defs::CovResultIter;
use crate::output::get_target_output_writable;
use crate::{output::get_target_output_writable, CovResult};

macro_rules! demangle {
($name: expr, $demangle: expr, $options: expr) => {{
Expand Down Expand Up @@ -229,12 +231,13 @@ impl ToString for ConditionType {
}

fn get_coverage(
results: CovResultIter,
results: &[(PathBuf, PathBuf, CovResult)],
sources: Vec<String>,
demangle: bool,
demangle_options: DemangleOptions,
) -> Coverage {
let packages: Vec<Package> = results
.iter()
.map(|(_, rel_path, result)| {
let all_lines: Vec<u32> = result.lines.iter().map(|(k, _)| k).cloned().collect();

Expand All @@ -246,13 +249,12 @@ fn get_coverage(
}
start_indexes.sort_unstable();

let functions = result.functions;
let result_lines = result.lines;
let result_branches = result.branches;
// let result_lines = result.lines.clone();
// let result_branches = result.branches.clone();

let line_from_number = |number| {
let hits = result_lines.get(&number).cloned().unwrap_or_default();
if let Some(branches) = result_branches.get(&number) {
let hits = result.lines.get(&number).cloned().unwrap_or_default();
if let Some(branches) = result.branches.get(&number) {
let conditions = branches
.iter()
.enumerate()
Expand All @@ -272,7 +274,8 @@ fn get_coverage(
}
};

let methods: Vec<Method> = functions
let methods: Vec<Method> = result
.functions
.iter()
.map(|(name, function)| {
let mut func_end = end;
Expand Down Expand Up @@ -329,7 +332,7 @@ fn get_coverage(

pub fn output_cobertura(
source_dir: Option<&Path>,
results: CovResultIter,
results: &[(PathBuf, PathBuf, CovResult)],
output_file: Option<&Path>,
demangle: bool,
) {
Expand Down
1 change: 0 additions & 1 deletion src/defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ pub type JobSender = Sender<Option<WorkItem>>;

pub type CovResultMap = FxHashMap<String, CovResult>;
pub type SyncCovResultMap = Mutex<CovResultMap>;
pub type CovResultIter = Box<dyn Iterator<Item = (PathBuf, PathBuf, CovResult)>>;

#[derive(Debug, Default)]
pub struct CDStats {
Expand Down
121 changes: 60 additions & 61 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ struct Opt {
short = "t",
long,
long_help = "\
Sets a custom output type:\n\
Comma separated list of custom output types:\n\
- *html* for a HTML coverage report;\n\
- *coveralls* for the Coveralls specific format;\n\
- *lcov* for the lcov INFO format;\n\
Expand All @@ -96,26 +96,18 @@ struct Opt {
- *ade* for the ActiveData-ETL specific format;\n\
- *files* to only return a list of files.\n\
- *markdown* for human easy read.\n\
- *cobertura* for output in coberura format.\n\
",
value_name = "OUTPUT TYPE",
default_value = "lcov",
requires_ifs = &[
("coveralls", "coveralls-auth"),
("coveralls+", "coveralls-auth"),
],
possible_values = &[
"ade",
"lcov",
"coveralls",
"coveralls+",
"files",
"covdir",
"html",
"cobertura",
"markdown",
],
use_delimiter = true
)]
output_type: OutputType,
output_types: Vec<OutputType>,
/// Specifies the output path.
#[structopt(short, long, value_name = "PATH", alias = "output-file")]
output_path: Option<PathBuf>,
Expand Down Expand Up @@ -422,52 +414,59 @@ fn main() {
file_filter,
);

match opt.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::Coveralls => output_coveralls(
iterator,
opt.token.as_deref(),
opt.service_name.as_deref(),
&opt.service_number.unwrap_or_default(),
opt.service_job_id.as_deref(),
&opt.service_pull_request.unwrap_or_default(),
&opt.commit_sha.unwrap_or_default(),
false,
opt.output_path.as_deref(),
&opt.vcs_branch,
opt.parallel,
demangle,
),
OutputType::CoverallsPlus => output_coveralls(
iterator,
opt.token.as_deref(),
opt.service_name.as_deref(),
&opt.service_number.unwrap_or_default(),
opt.service_job_id.as_deref(),
&opt.service_pull_request.unwrap_or_default(),
&opt.commit_sha.unwrap_or_default(),
true,
opt.output_path.as_deref(),
&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::Html => output_html(
iterator,
opt.output_path.as_deref(),
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,
),
OutputType::Markdown => output_markdown(iterator, opt.output_path.as_deref()),
};
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();
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::Coveralls => output_coveralls(
&iterator,
opt.token.as_deref(),
opt.service_name.as_deref(),
&service_number,
opt.service_job_id.as_deref(),
&service_pull_request,
&commit_sha,
false,
opt.output_path.as_deref(),
&opt.vcs_branch,
opt.parallel,
demangle,
),
OutputType::CoverallsPlus => output_coveralls(
&iterator,
opt.token.as_deref(),
opt.service_name.as_deref(),
&service_number,
opt.service_job_id.as_deref(),
&service_pull_request,
&commit_sha,
true,
opt.output_path.as_deref(),
&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::Html => output_html(
&iterator,
opt.output_path.as_deref(),
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,
),
OutputType::Markdown => output_markdown(&iterator, opt.output_path.as_deref()),
};
}
}
34 changes: 21 additions & 13 deletions src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ pub fn get_target_output_writable(output_file: Option<&Path>) -> Box<dyn Write>
write_target
}

pub fn output_activedata_etl(results: CovResultIter, output_file: Option<&Path>, demangle: bool) {
pub fn output_activedata_etl(
results: &[(PathBuf, PathBuf, CovResult)],
output_file: Option<&Path>,
demangle: bool,
) {
let demangle_options = DemangleOptions::name_only();
let mut writer = BufWriter::new(get_target_output_writable(output_file));

Expand Down Expand Up @@ -180,7 +184,7 @@ pub fn output_activedata_etl(results: CovResultIter, output_file: Option<&Path>,
}
}

pub fn output_covdir(results: CovResultIter, output_file: Option<&Path>) {
pub fn output_covdir(results: &[(PathBuf, PathBuf, CovResult)], output_file: Option<&Path>) {
let mut writer = BufWriter::new(get_target_output_writable(output_file));
let mut relative: FxHashMap<PathBuf, Rc<RefCell<CDDirStats>>> = FxHashMap::default();
let global = Rc::new(RefCell::new(CDDirStats::new("".to_string())));
Expand Down Expand Up @@ -226,7 +230,7 @@ pub fn output_covdir(results: CovResultIter, output_file: Option<&Path>) {

prev_stats.borrow_mut().files.push(CDFileStats::new(
path.file_name().unwrap().to_str().unwrap().to_string(),
result.lines,
result.lines.clone(),
));
}

Expand All @@ -236,7 +240,11 @@ pub fn output_covdir(results: CovResultIter, output_file: Option<&Path>) {
serde_json::to_writer(&mut writer, &global.into_json()).unwrap();
}

pub fn output_lcov(results: CovResultIter, output_file: Option<&Path>, demangle: bool) {
pub fn output_lcov(
results: &[(PathBuf, PathBuf, CovResult)],
output_file: Option<&Path>,
demangle: bool,
) {
let demangle_options = DemangleOptions::name_only();
let mut writer = BufWriter::new(get_target_output_writable(output_file));
writer.write_all(b"TN:\n").unwrap();
Expand Down Expand Up @@ -413,7 +421,7 @@ fn get_coveralls_git_info(commit_sha: &str, vcs_branch: &str) -> Value {
}

pub fn output_coveralls(
results: CovResultIter,
results: &[(PathBuf, PathBuf, CovResult)],
repo_token: Option<&str>,
service_name: Option<&str>,
service_number: &str,
Expand Down Expand Up @@ -455,7 +463,7 @@ pub fn output_coveralls(
if !with_function_info {
source_files.push(json!({
"name": rel_path,
"source_digest": get_digest(abs_path),
"source_digest": get_digest(abs_path.clone()),
"coverage": coverage,
"branches": branches,
}));
Expand All @@ -471,7 +479,7 @@ pub fn output_coveralls(

source_files.push(json!({
"name": rel_path,
"source_digest": get_digest(abs_path),
"source_digest": get_digest(abs_path.clone()),
"coverage": coverage,
"branches": branches,
"functions": functions,
Expand Down Expand Up @@ -505,15 +513,15 @@ pub fn output_coveralls(
serde_json::to_writer(&mut writer, &result).unwrap();
}

pub fn output_files(results: CovResultIter, output_file: Option<&Path>) {
pub fn output_files(results: &[(PathBuf, PathBuf, CovResult)], output_file: Option<&Path>) {
let mut writer = BufWriter::new(get_target_output_writable(output_file));
for (_, rel_path, _) in results {
writeln!(writer, "{}", rel_path.display()).unwrap();
}
}

pub fn output_html(
results: CovResultIter,
results: &[(PathBuf, PathBuf, CovResult)],
output_dir: Option<&Path>,
num_threads: usize,
branch_enabled: bool,
Expand Down Expand Up @@ -559,9 +567,9 @@ pub fn output_html(
for (abs_path, rel_path, result) in results {
sender
.send(Some(HtmlItem {
abs_path,
rel_path,
result,
abs_path: abs_path.to_path_buf(),
rel_path: rel_path.to_path_buf(),
result: result.clone(),
}))
.unwrap();
}
Expand All @@ -587,7 +595,7 @@ pub fn output_html(
html::gen_coverage_json(&global.stats, &config, &output);
}

pub fn output_markdown(results: CovResultIter, output_file: Option<&Path>) {
pub fn output_markdown(results: &[(PathBuf, PathBuf, CovResult)], output_file: Option<&Path>) {
#[derive(Tabled)]
struct LineSummary {
file: String,
Expand Down
8 changes: 2 additions & 6 deletions src/path_rewriting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ pub fn rewrite_paths(
to_keep_dirs: &[impl AsRef<str>],
filter_option: Option<bool>,
file_filter: crate::FileFilter,
) -> CovResultIter {
) -> Vec<(PathBuf, PathBuf, CovResult)> {
let to_ignore_globset = to_globset(to_ignore_dirs);
let to_keep_globset = to_globset(to_keep_dirs);

Expand Down Expand Up @@ -341,11 +341,7 @@ pub fn rewrite_paths(
Some((abs_path, rel_path, result))
});

Box::new(
results
.collect::<Vec<(PathBuf, PathBuf, CovResult)>>()
.into_iter(),
)
results.collect()
}

#[cfg(test)]
Expand Down

0 comments on commit 07fe36b

Please sign in to comment.