geolith-cli 0.2.2

Rust vector tile generator — OSM + Overture Maps → protomaps PMTiles
Documentation
//! The `--dry-run` transcript of `geolith dataset refresh`, against the golden
//! the script wrote before the port.
//!
//! Both tests are ignored by default: a dry run still lists the sources over
//! the network and still reads the bucket's pointer, and the golden was
//! captured from the live data root. Set `DATA_ROOT` to that root and run:
//!
//! ```text
//! cargo test --test dataset_refresh_plan -- --ignored --nocapture
//! ```
//!
//! The run happens against a temporary root that holds only the source lists,
//! so the live root gains no lock and no history line.

use std::fs;
use std::path::Path;
use std::process::Command;

/// The transcript of a one-country dry run on India.
const GOLDEN_IN: &str = include_str!("data/refresh/dry-run-in.txt");

/// The transcript of a one-country dry run on the United States, recaptured
/// from the Rust binary once the US NDVI publish finished (22 Nov 2026): both
/// datasets are `nothing-new`.
const GOLDEN_US: &str = include_str!("data/refresh/dry-run-us.txt");

/// The datasets and countries the golden covers.
const PAIRS: [(&str, &str); 4] = [
    ("ndvi", "in"),
    ("ndvi", "us"),
    ("rainfall", "in"),
    ("rainfall", "us"),
];

/// The lines of `text` without the log prefix and with the duration digits
/// replaced by `N`, as `ORIGIN.md` describes.
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()
}

/// A temporary data root holding the source lists of `live`.
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
}

/// The transcript of a dry run on `country`.
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))
}

/// The live root the golden was captured from.
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")
}

/// The lines of a transcript with a `done:` line cut down to its pairs other
/// than NDVI's: the US golden's NDVI line was a mid-flight failure when it was
/// first captured, so the helper stayed after the recapture to keep the rest
/// of the transcript compared on its own.
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);
}