Skip to content

Commit

Permalink
handle output files
Browse files Browse the repository at this point in the history
  • Loading branch information
Bastian Schubert committed Oct 14, 2022
1 parent de6401e commit 5e55d55
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 32 deletions.
11 changes: 9 additions & 2 deletions src/cobertura.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
}

Expand Down
49 changes: 30 additions & 19 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,11 @@ struct Opt {
)]
output_types: Vec<OutputType>,
/// 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<PathBuf>,
/// Specifies the output config file.
#[structopt(long, value_name = "PATH", alias = "output-config-file")]
output_config_file: Option<PathBuf>,
#[structopt(long, value_name = "PATH", alias = "html-output-config-file")]
html_output_config_file: Option<PathBuf>,
/// Specifies the root directory of the source files.
#[structopt(short, long, value_name = "DIRECTORY", parse(from_os_str))]
source_dir: Option<PathBuf>,
Expand Down Expand Up @@ -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(),
Expand All @@ -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,
Expand All @@ -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)
}
};
}
}
60 changes: 49 additions & 11 deletions src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ macro_rules! demangle {
}};
}

pub fn get_target_output_writable(output_file: Option<&Path>) -> Box<dyn Write> {
pub fn get_target_output_writable(output_file: Option<&PathBuf>) -> Box<dyn Write> {
let write_target: Box<dyn Write> = match output_file {
Some(output) => {
if output.is_dir() {
Expand Down Expand Up @@ -71,11 +71,18 @@ pub fn get_target_output_writable(output_file: Option<&Path>) -> Box<dyn Write>

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<u32> = result
Expand Down Expand Up @@ -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<PathBuf, Rc<RefCell<CDDirStats>>> = FxHashMap::default();
let global = Rc::new(RefCell::new(CDDirStats::new("".to_string())));
relative.insert(PathBuf::from(""), global.clone());
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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();
}
Expand Down

0 comments on commit 5e55d55

Please sign in to comment.