use std::{io::Write, path::PathBuf, process::ExitCode};
use serde::Serialize;
use serde_json::Value;
use supercov_engine::coverage_query::{CoverageFileDetailData, CoverageFileObligation};
const SHARED: &[&str] = &[
"--metric",
"--filter",
"--kind",
"--runner",
"--tag",
"--title",
"--test-file",
];
const ALL: &str = "1000000000";
pub fn command(arguments: &[String]) -> ExitCode {
let mut passed = Vec::new();
let mut output = None;
let mut format = None;
let mut index = 0;
loop {
match arguments[index].as_str() {
"--format" => {
index += 1;
format = arguments.get(index).cloned();
}
"--output" => {
index += 1;
output = arguments.get(index).map(PathBuf::from);
}
"--offset" | "--limit" => index += 1,
"--json" => {}
other => passed.push(other.to_owned()),
}
index += 1;
if index >= arguments.len() {
break;
}
}
if format.as_deref() != Some("jsonl") {
eprintln!("[supercov] gaps --format takes jsonl");
return ExitCode::from(2);
}
let Some(run) = passed.first().cloned() else {
eprintln!(
"[supercov] usage: supercov runs <run-id> gaps --format jsonl [--metric <metric>] [--output <file>]"
);
return ExitCode::from(2);
};
let shared = shared_options(&passed);
let root = match std::env::current_dir() {
Ok(root) => root,
Err(error) => {
eprintln!("[supercov] could not resolve the current directory: {error}");
return ExitCode::from(2);
}
};
let query = |arguments: Vec<String>| -> Result<Value, String> {
let (request, _) =
super::public_query::coverage_request(run.clone(), "gaps", &arguments, true)
.map_err(|error| error.error.message.clone())?;
let (output, _) = super::execute_coverage_query(&root, &request, None)
.map_err(|failure| failure.error.message.clone())?;
Ok(serde_json::json!({
"data": serde_json::to_value(&output.data).expect("plain data serializes")
}))
};
let mut gaps_arguments = passed[2..].to_vec();
gaps_arguments.extend(["--json", "--limit", ALL].map(str::to_owned));
let gaps = match query(gaps_arguments) {
Ok(gaps) => gaps,
Err(error) => {
eprintln!("[supercov] {error}");
return ExitCode::from(2);
}
};
let files = gaps["data"]["gaps"]
.as_array()
.into_iter()
.flatten()
.filter_map(|gap| gap["file"].as_str().map(str::to_owned))
.collect::<Vec<_>>();
let details = match file_details(&root, &run, &files, &shared) {
Ok(details) => details,
Err(error) => {
eprintln!("[supercov] {error}");
return ExitCode::from(2);
}
};
let mut lines = String::new();
let mut written = 0usize;
for (file, detail) in files.iter().zip(&details) {
detail
.gap_lines
.iter()
.flat_map(|gap_line| gap_line.obligations.iter().map(move |o| (gap_line, o)))
.for_each(|(gap_line, obligation)| {
let row = Row {
file,
obligation,
line_source: gap_line.source.as_deref(),
};
lines.push_str(
&serde_json::to_string(&row).expect("a row is fields named by strings"),
);
lines.push('\n');
written += 1;
});
}
match output {
Some(path) => {
if let Err(error) = supercov_engine::fault_fs::write(&path, &lines) {
eprintln!("[supercov] {}: {error}", path.display());
return ExitCode::from(2);
}
eprintln!(
"[supercov] wrote {written} uncovered obligation(s) in {} file(s) to {}",
files.len(),
path.display()
);
}
None => return printed(&mut std::io::stdout().lock(), &lines),
}
ExitCode::SUCCESS
}
fn printed(out: &mut dyn Write, lines: &str) -> ExitCode {
if let Err(error) = out.write_all(lines.as_bytes()).and_then(|()| out.flush()) {
if error.kind() != std::io::ErrorKind::BrokenPipe {
eprintln!("[supercov] could not write the gaps to standard output: {error}");
}
return ExitCode::from(2);
}
ExitCode::SUCCESS
}
fn file_details(
root: &std::path::Path,
run: &str,
files: &[String],
shared: &[String],
) -> Result<Vec<CoverageFileDetailData>, String> {
let Some(first) = files.first() else {
return Ok(Vec::new());
};
let mut arguments = vec![first.clone()];
arguments.extend(shared.iter().cloned());
arguments.extend(["--json", "--limit", ALL].map(str::to_owned));
let (request, _) =
super::public_query::coverage_request(run.to_owned(), "file", &arguments, true)
.expect("`file` takes the options `gaps` took");
let inventory = super::public_run_inventory(root).map_err(|error| error.to_string())?;
let stored = supercov_engine::run_store::select_run(&inventory, Some(&request.run_id))
.expect("the gaps query read this run");
let container = super::open_query_index_for(stored, request.test_filter().as_ref())
.map_err(|error| error.to_string())?;
let index = supercov_engine::coverage_index::CoverageIndex::new(&container)
.map_err(|error| error.to_string())?;
let view = request.view().expect("the gaps query read this view");
let read = supercov_engine::coverage_query::FileDetailSource::read(&index, view)
.map_err(|error| format!("{error:?}"))?;
Ok(files
.iter()
.map(|file| {
let (detail, _) = supercov_engine::coverage_query::coverage_file_detail_from(
&read,
supercov_engine::coverage_query::CoverageFileDetailOptions {
run: &stored.id,
view,
kind: request.kind.as_deref(),
runner: request.runner.as_deref(),
selector: file,
metric: request.metric,
offset: 0,
limit: usize::MAX,
},
)
.expect("a file the gaps named has its detail");
detail
})
.collect())
}
fn shared_options(arguments: &[String]) -> Vec<String> {
let mut shared = Vec::new();
let mut index = 0;
loop {
if SHARED.contains(&arguments[index].as_str())
&& let Some(value) = arguments.get(index + 1)
{
shared.push(arguments[index].clone());
shared.push(value.clone());
index += 1;
}
index += 1;
if index >= arguments.len() {
break shared;
}
}
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct Row<'a> {
file: &'a str,
#[serde(flatten)]
obligation: &'a CoverageFileObligation,
#[serde(skip_serializing_if = "Option::is_none")]
line_source: Option<&'a str>,
}
#[cfg(test)]
mod tests {
use super::*;
struct Failing(std::io::ErrorKind);
impl Write for Failing {
fn write(&mut self, _: &[u8]) -> std::io::Result<usize> {
Err(self.0.into())
}
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}
#[test]
fn gaps_that_could_not_be_printed_fail_the_command() {
let mut out = Vec::new();
assert_eq!(printed(&mut out, "{}\n"), ExitCode::SUCCESS);
assert_eq!(out, b"{}\n");
for kind in [std::io::ErrorKind::BrokenPipe, std::io::ErrorKind::Other] {
assert_eq!(printed(&mut Failing(kind), "{}\n"), ExitCode::from(2));
}
}
#[test]
fn an_exported_obligation_names_its_file_and_line_source() {
use supercov_engine::coverage_query::{CoverageLineObligation, CoverageOtherCoverage};
let obligation = CoverageFileObligation::Line(CoverageLineObligation {
kind: "line".into(),
id: "line:src/chart.tsx:28".into(),
line: 28,
other_coverage: CoverageOtherCoverage {
covered_elsewhere: false,
kinds: Vec::new(),
runners: Vec::new(),
tests: Vec::new(),
},
});
let exported = serde_json::to_value(Row {
file: "src/chart.tsx",
obligation: &obligation,
line_source: Some("if (!context) {"),
})
.unwrap();
assert_eq!(exported["file"], "src/chart.tsx");
assert_eq!(exported["kind"], "line");
assert_eq!(exported["line"], 28);
assert_eq!(exported["lineSource"], "if (!context) {");
let options = shared_options(&[
"run_1".into(),
"--metric".into(),
"mcdc".into(),
"--kind".into(),
"e2e".into(),
]);
assert_eq!(options, ["--metric", "mcdc", "--kind", "e2e"]);
}
}