use std::fs;
use std::path::Path;
use std::process::Command;
const GOLDEN_IN: &str = include_str!("data/refresh/dry-run-in.txt");
const GOLDEN_US: &str = include_str!("data/refresh/dry-run-us.txt");
const PAIRS: [(&str, &str); 4] = [
("ndvi", "in"),
("ndvi", "us"),
("rainfall", "in"),
("rainfall", "us"),
];
fn normalise(text: &str) -> Vec<String> {
text.lines()
.filter(|line| !line.trim().is_empty())
.map(|line| {
let body = match line.split_once(' ') {
Some((stamp, rest))
if stamp.len() == 24
&& stamp.ends_with('Z')
&& stamp.contains('T')
&& stamp.contains('.') =>
{
rest
}
_ => line,
};
match (body.rfind('('), body.ends_with("s)")) {
(Some(at), true) if body[at..].chars().any(char::is_numeric) => {
format!("{}{}s)", &body[..=at], "N")
}
_ => body.to_owned(),
}
})
.collect()
}
fn fixture_root(live: &Path) -> tempfile::TempDir {
let dir = tempfile::tempdir().expect("a temp dir");
for (dataset, country) in PAIRS {
let from = live
.join("datasets")
.join(dataset)
.join(country)
.join("lists/series.txt");
let to = dir
.path()
.join("datasets")
.join(dataset)
.join(country)
.join("lists");
fs::create_dir_all(&to).expect("mkdir");
if from.is_file() {
fs::copy(&from, to.join("series.txt")).expect("copy");
}
}
dir
}
fn transcript(root: &Path, country: &str) -> Vec<String> {
let output = Command::new(env!("CARGO_BIN_EXE_geolith"))
.args(["dataset", "refresh", "--dry-run", "--country", country])
.env("DATA_ROOT", root)
.env("DATASETS_REFRESH_NO_ALERT", "1")
.output()
.expect("running geolith");
assert!(
output.status.success(),
"exit {:?}: {}",
output.status.code(),
String::from_utf8_lossy(&output.stderr)
);
normalise(&String::from_utf8_lossy(&output.stdout))
}
fn live_root() -> std::path::PathBuf {
std::env::var_os("DATA_ROOT")
.map(std::path::PathBuf::from)
.expect("set DATA_ROOT to the root the golden was captured from")
}
fn without_ndvi(lines: Vec<String>) -> Vec<String> {
lines
.into_iter()
.map(|line| match line.strip_prefix("done: ") {
Some(rest) => {
let kept: Vec<&str> = rest
.split(", ")
.filter(|pair| !pair.contains("ndvi"))
.collect();
format!("done: {}", kept.join(", "))
}
None => line,
})
.filter(|line| !line.contains("ndvi"))
.collect()
}
#[test]
#[ignore = "lists the CHIRPS host and reads the bucket's pointer"]
fn the_india_dry_run_matches_the_script_line_for_line() {
let live = live_root();
let fixture = fixture_root(&live);
let got = transcript(fixture.path(), "in");
let want = normalise(GOLDEN_IN);
assert_eq!(got, want);
}
#[test]
#[ignore = "lists the CHIRPS host and reads the bucket's pointer"]
fn the_us_dry_run_matches_the_script_line_for_line() {
let live = live_root();
let fixture = fixture_root(&live);
let got = transcript(fixture.path(), "us");
let want = normalise(GOLDEN_US);
assert_eq!(got, want);
}
#[test]
#[ignore = "lists the CHIRPS host and reads the bucket's pointer"]
fn the_us_dry_run_still_matches_its_other_pairs_alone() {
let live = live_root();
let fixture = fixture_root(&live);
let got = without_ndvi(transcript(fixture.path(), "us"));
let want = without_ndvi(normalise(GOLDEN_US));
assert_eq!(got, want);
}