#![allow(clippy::unwrap_used, clippy::expect_used)]
use std::fs;
use std::path::{Path, PathBuf};
use ligase::cache::Cache;
use ligase::dim::Dimensions;
use ligase::exec::{self, Host, Options, Outcome, Recipe, RunReport, Stores, Task};
use ligase::graph::{Argument, Graph, NodeId, StepSpec};
use ligase::note::Note;
use ligase::prov::{self, Prov, bundle};
use ligase::types::{DataKind, Dataset};
use tempfile::TempDir;
const STORE_PATH: &str = "/Users/kelyn/ligase/.ligase/store/62/7db9fa0cbac.bam";
fn vcf(sample: &str) -> String {
format!(
"##fileformat=VCFv4.3\n\
##contig=<ID=K03455.1,length=20>\n\
#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\tFORMAT\t{sample}\n\
K03455.1\t5\t.\tA\tG\t30\tPASS\t.\tGT\t1\n"
)
}
struct Bench {
dir: TempDir,
root: PathBuf,
cache: Cache,
prov: Prov,
graph: Graph,
tasks: Vec<Task>,
}
impl Bench {
fn new(sample: &str) -> (Self, NodeId) {
let dir = TempDir::new().unwrap();
let root = dir.path().join(".ligase");
let cache = Cache::open(&root).unwrap();
let prov = Prov::open(&root).unwrap();
let mut graph = Graph::new();
let path = dir.path().join("calls.vcf");
fs::write(&path, vcf(sample)).unwrap();
let input = graph.add_input(
"calls",
Dataset::new(DataKind::Variants, Some(path), Dimensions::default()),
);
let node = graph
.add_step(StepSpec {
name: "call".to_owned(),
output: DataKind::Variants,
coordinates: None,
conversion: None,
arguments: vec![Argument {
param: "calls".to_owned(),
declared: DataKind::Variants,
node: input,
}],
})
.unwrap();
let tasks = vec![Task {
node,
code: "def call(): ...".to_owned(),
recipe: Recipe::Shell("gzip -c {calls} > {out}".to_owned()),
tools: Vec::new(),
parameters: Vec::new(),
citations: Vec::new(),
seed: None,
}];
(
Self {
dir,
root,
cache,
prov,
graph,
tasks,
},
node,
)
}
fn run(&self, roots: &[NodeId]) -> RunReport {
self.run_with(roots, &Refusing)
}
fn run_with(&self, roots: &[NodeId], host: &dyn Host) -> RunReport {
exec::run(
&self.graph,
roots,
&self.tasks,
&Stores {
cache: &self.cache,
prov: &self.prov,
},
&Options::default(),
host,
)
}
fn by_callback(mut self) -> Self {
for task in &mut self.tasks {
task.recipe = Recipe::Callback(0);
}
self
}
fn clean(&self) -> (Cache, Prov) {
let root = self.dir.path().join(".clean");
(Cache::open(&root).unwrap(), Prov::open(&root).unwrap())
}
}
struct Refusing;
impl Host for Refusing {
fn call(
&self,
_id: u64,
_out: &Path,
_inputs: &[(String, Vec<PathBuf>)],
) -> Result<Vec<Note>, String> {
Err("no callbacks in these tests".to_owned())
}
}
struct Noticing;
impl Host for Noticing {
fn call(
&self,
_id: u64,
out: &Path,
_inputs: &[(String, Vec<PathBuf>)],
) -> Result<Vec<Note>, String> {
fs::write(out, "whatever a body writes\n").map_err(|error| error.to_string())?;
Ok(vec![Note::ReadingWithoutCoverage])
}
}
#[test]
fn a_run_whose_output_names_a_file_says_so() {
let (bench, node) = Bench::new(STORE_PATH);
let report = bench.run(&[node]);
let result = report.result(node).expect("the step ran");
assert!(report.failures.is_empty(), "{:?}", report.failures);
assert_eq!(
result.notes,
vec![Note::SampleIsAFile {
sample: STORE_PATH.to_owned()
}],
);
let text = exec::render(&bench.graph, &[node], &report);
assert!(text.contains("noticed"), "{text}");
assert!(text.contains(STORE_PATH), "{text}");
assert!(text.contains("addreplacerg"), "{text}");
}
#[test]
fn a_run_whose_output_names_a_sample_says_nothing() {
let (bench, node) = Bench::new("SRR8525904");
let report = bench.run(&[node]);
assert!(report.result(node).unwrap().notes.is_empty());
let text = exec::render(&bench.graph, &[node], &report);
assert!(!text.contains("noticed"), "{text}");
}
#[test]
fn a_step_served_from_the_cache_still_says_it() {
let (bench, node) = Bench::new(STORE_PATH);
assert_eq!(
bench.run(&[node]).result(node).unwrap().outcome,
Outcome::Ran
);
let again = bench.run(&[node]);
let result = again.result(node).unwrap();
assert_eq!(result.outcome, Outcome::Cached);
assert_eq!(
result.notes,
vec![Note::SampleIsAFile {
sample: STORE_PATH.to_owned()
}],
);
}
#[test]
fn provenance_records_what_was_noticed() {
let (bench, node) = Bench::new(STORE_PATH);
let report = bench.run(&[node]);
let key = report.result(node).unwrap().key;
let record = bench.prov.lookup(key).unwrap().expect("a record");
assert_eq!(
record.output_notes,
vec![Note::SampleIsAFile {
sample: STORE_PATH.to_owned()
}],
);
}
#[test]
fn a_note_survives_the_cache_being_thrown_away() {
let (bench, node) = Bench::new(STORE_PATH);
let report = bench.run(&[node]);
let key = report.result(node).unwrap().key;
fs::remove_file(bench.root.join("cache.sqlite")).unwrap();
let record = bench.prov.lookup(key).unwrap().expect("a record");
assert_eq!(record.output_notes.len(), 1);
}
#[test]
fn the_methods_section_says_an_identifier_is_a_file() {
let (bench, node) = Bench::new(STORE_PATH);
let report = bench.run(&[node]);
let methods = prov::methods(&bench.graph, &[node], &bench.tasks, &report, &bench.prov).unwrap();
assert_eq!(methods.notices().len(), 1);
let text = methods.text();
assert!(text.contains(STORE_PATH), "{text}");
assert!(text.contains("not a sample"), "{text}");
let paragraph = methods.paragraph();
assert!(!paragraph.contains(STORE_PATH), "{paragraph}");
}
#[test]
fn a_write_up_with_nothing_wrong_says_nothing() {
let (bench, node) = Bench::new("SRR8525904");
let report = bench.run(&[node]);
let methods = prov::methods(&bench.graph, &[node], &bench.tasks, &report, &bench.prov).unwrap();
assert!(methods.notices().is_empty());
assert!(!methods.text().contains("not a sample"));
}
#[test]
fn a_bundle_records_what_was_noticed_and_warns_in_its_readme() {
let (bench, node) = Bench::new(STORE_PATH);
let report = bench.run(&[node]);
let methods = prov::methods(&bench.graph, &[node], &bench.tasks, &report, &bench.prov).unwrap();
let into = bench.dir.path().join("bundle");
let summary = bundle::write(
&into,
&bundle::Request {
graph: &bench.graph,
roots: &[node],
report: &report,
prov: &bench.prov,
methods: &methods,
include: &[],
queries: None,
thin: false,
},
)
.unwrap();
assert!(summary.files.iter().any(|name| name == "notes.tsv"));
let notes = fs::read_to_string(into.join("notes.tsv")).unwrap();
assert!(notes.contains("sample-is-a-file"), "{notes}");
assert!(notes.contains(STORE_PATH), "{notes}");
let readme = fs::read_to_string(into.join("README.md")).unwrap();
assert!(readme.contains("What was noticed"), "{readme}");
}
#[test]
fn a_bundle_with_nothing_wrong_still_has_the_file_and_no_warning() {
let (bench, node) = Bench::new("SRR8525904");
let report = bench.run(&[node]);
let methods = prov::methods(&bench.graph, &[node], &bench.tasks, &report, &bench.prov).unwrap();
let into = bench.dir.path().join("bundle");
bundle::write(
&into,
&bundle::Request {
graph: &bench.graph,
roots: &[node],
report: &report,
prov: &bench.prov,
methods: &methods,
include: &[],
queries: None,
thin: false,
},
)
.unwrap();
let notes = fs::read_to_string(into.join("notes.tsv")).unwrap();
assert_eq!(notes.lines().count(), 1, "the header, and nothing under it");
let readme = fs::read_to_string(into.join("README.md")).unwrap();
assert!(!readme.contains("What was noticed"), "{readme}");
}
#[test]
fn verifying_a_bundle_reports_an_output_that_will_not_travel() {
let (bench, node) = Bench::new(STORE_PATH);
let report = bench.run(&[node]);
let methods = prov::methods(&bench.graph, &[node], &bench.tasks, &report, &bench.prov).unwrap();
let into = bench.dir.path().join("bundle");
bundle::write(
&into,
&bundle::Request {
graph: &bench.graph,
roots: &[node],
report: &report,
prov: &bench.prov,
methods: &methods,
include: &[],
queries: None,
thin: false,
},
)
.unwrap();
let (cache, prov) = bench.clean();
let fresh = exec::run(
&bench.graph,
&[node],
&bench.tasks,
&Stores {
cache: &cache,
prov: &prov,
},
&Options::default(),
&Refusing,
);
let verification = bundle::verify(&into, &bench.graph, &[node], &fresh).unwrap();
assert_eq!(verification.noticed.len(), 1);
let text = verification.text();
assert!(text.contains("where a sample identifier belongs"), "{text}");
assert!(text.contains(STORE_PATH), "{text}");
}
#[test]
fn verifying_an_ordinary_bundle_says_nothing_about_notes() {
let (bench, node) = Bench::new("SRR8525904");
let report = bench.run(&[node]);
let methods = prov::methods(&bench.graph, &[node], &bench.tasks, &report, &bench.prov).unwrap();
let into = bench.dir.path().join("bundle");
bundle::write(
&into,
&bundle::Request {
graph: &bench.graph,
roots: &[node],
report: &report,
prov: &bench.prov,
methods: &methods,
include: &[],
queries: None,
thin: false,
},
)
.unwrap();
let (cache, prov) = bench.clean();
let fresh = exec::run(
&bench.graph,
&[node],
&bench.tasks,
&Stores {
cache: &cache,
prov: &prov,
},
&Options::default(),
&Refusing,
);
let verification = bundle::verify(&into, &bench.graph, &[node], &fresh).unwrap();
assert!(verification.noticed.is_empty());
assert!(!verification.text().contains("sample identifier belongs"));
}
#[test]
fn a_run_says_what_a_body_noticed_about_its_own_work() {
let (bench, node) = Bench::new("SRR8525904");
let bench = bench.by_callback();
let report = bench.run_with(&[node], &Noticing);
assert!(report.failures.is_empty(), "{:?}", report.failures);
let result = report.result(node).expect("the step ran");
assert_eq!(result.notes, vec![Note::ReadingWithoutCoverage]);
let text = exec::render(&bench.graph, &[node], &report);
assert!(text.contains("noticed"), "{text}");
assert!(text.contains("coverage is unknown"), "{text}");
assert!(text.contains("coverage=aln"), "{text}");
}
#[test]
fn a_body_served_from_the_cache_still_says_what_it_noticed() {
let (bench, node) = Bench::new("SRR8525904");
let bench = bench.by_callback();
assert_eq!(
bench
.run_with(&[node], &Noticing)
.result(node)
.unwrap()
.outcome,
Outcome::Ran
);
let again = bench.run_with(&[node], &Refusing);
let result = again.result(node).unwrap();
assert_eq!(result.outcome, Outcome::Cached);
assert_eq!(result.notes, vec![Note::ReadingWithoutCoverage]);
let text = exec::render(&bench.graph, &[node], &again);
assert!(text.contains("coverage is unknown"), "{text}");
}
#[test]
fn provenance_and_a_bundle_keep_what_a_body_noticed() {
let (bench, node) = Bench::new("SRR8525904");
let bench = bench.by_callback();
let report = bench.run_with(&[node], &Noticing);
let key = report.result(node).unwrap().key;
let record = bench.prov.lookup(key).unwrap().expect("a record");
assert_eq!(record.output_notes, vec![Note::ReadingWithoutCoverage]);
let methods = prov::methods(&bench.graph, &[node], &bench.tasks, &report, &bench.prov).unwrap();
assert_eq!(methods.notices().len(), 1);
let said = methods
.text()
.split_whitespace()
.collect::<Vec<_>>()
.join(" ");
assert!(said.contains("no alignment was supplied"), "{said}");
assert!(
said.contains("every score in this report a floor"),
"{said}"
);
let into = bench.dir.path().join("bundle");
bundle::write(
&into,
&bundle::Request {
graph: &bench.graph,
roots: &[node],
report: &report,
prov: &bench.prov,
methods: &methods,
include: &[],
queries: None,
thin: false,
},
)
.unwrap();
let notes = fs::read_to_string(into.join("notes.tsv")).unwrap();
assert!(notes.contains("reading-without-coverage"), "{notes}");
let readme = fs::read_to_string(into.join("README.md")).unwrap();
assert!(readme.contains("What was noticed"), "{readme}");
}
#[test]
fn a_hedge_is_not_an_excuse_for_a_hash_that_moved() {
let (bench, node) = Bench::new("SRR8525904");
let bench = bench.by_callback();
let report = bench.run_with(&[node], &Noticing);
let methods = prov::methods(&bench.graph, &[node], &bench.tasks, &report, &bench.prov).unwrap();
let into = bench.dir.path().join("bundle");
bundle::write(
&into,
&bundle::Request {
graph: &bench.graph,
roots: &[node],
report: &report,
prov: &bench.prov,
methods: &methods,
include: &[],
queries: None,
thin: false,
},
)
.unwrap();
let verification = bundle::verify(&into, &bench.graph, &[node], &report).unwrap();
assert!(
verification.noticed.is_empty(),
"{:?}",
verification.noticed
);
assert!(!verification.text().contains("coverage is unknown"));
}