#![allow(clippy::unwrap_used)]
use std::io::Write;
use std::process::Command;
const GEOLITH: &str = env!("CARGO_BIN_EXE_geolith");
fn write_dem() -> tempfile::NamedTempFile {
use tiff::encoder::{TiffEncoder, colortype};
use tiff::tags::Tag;
let mut buffer = std::io::Cursor::new(Vec::new());
{
let mut encoder = TiffEncoder::new(&mut buffer).expect("tiff encoder");
let mut image = encoder
.new_image::<colortype::Gray32Float>(4, 4)
.expect("image encoder");
let directory = image.encoder();
directory
.write_tag(Tag::Unknown(33550), &[10.0_f64, 10.0, 0.0][..])
.expect("pixel scale");
directory
.write_tag(
Tag::Unknown(33922),
&[0.0_f64, 0.0, 0.0, 400_000.0, 5_100_000.0, 0.0][..],
)
.expect("tiepoint");
directory
.write_tag(
Tag::Unknown(34735),
&[1_u32, 1, 0, 1, 3072, 0, 1, 32_632][..],
)
.expect("geokeys");
let values: Vec<f32> = (0..16).map(|i| 1000.0 + i as f32).collect();
image.write_data(&values).expect("write samples");
}
use std::io::Seek as _;
buffer.rewind().expect("rewind");
let mut file = tempfile::Builder::new()
.suffix(".tif")
.tempfile()
.expect("temp file");
file.write_all(&buffer.into_inner()).expect("write tiff");
file.flush().expect("flush");
file
}
#[test]
fn binary_renders_terrain_from_a_geotiff() {
let dem = write_dem();
let dir = tempfile::tempdir().expect("tempdir");
let output = dir.path().join("terrain.pmtiles");
let status = Command::new(GEOLITH)
.args([
"terrain",
"--dem",
dem.path().to_str().expect("utf-8 path"),
"--output",
output.to_str().expect("utf-8 path"),
"--min-zoom",
"12",
"--max-zoom",
"13",
"--tile-size",
"256",
])
.output()
.expect("binary runs");
assert!(
status.status.success(),
"terrain run failed: {}",
String::from_utf8_lossy(&status.stderr)
);
let bytes = std::fs::read(&output).expect("archive written");
assert_eq!(&bytes[..7], b"PMTiles");
}
#[test]
fn binary_reports_a_missing_dem() {
let dir = tempfile::tempdir().expect("tempdir");
let output = dir.path().join("out.pmtiles");
let result = Command::new(GEOLITH)
.args([
"terrain",
"--dem",
"/nonexistent/dem.tif",
"--output",
output.to_str().expect("utf-8 path"),
])
.output()
.expect("binary runs");
assert!(
!result.status.success(),
"a missing DEM must exit non-zero so pipelines fail loudly"
);
}
#[test]
fn binary_rejects_an_unknown_flag() {
let result = Command::new(GEOLITH)
.args(["terrain", "--not-a-flag"])
.output()
.expect("binary runs");
assert!(!result.status.success());
assert!(
String::from_utf8_lossy(&result.stderr).contains("--not-a-flag"),
"clap names the offending flag"
);
}
#[test]
fn binary_renders_subcommand_help() {
for verb in ["terrain", "splat-tiles", "raster", "reconstruct"] {
let result = Command::new(GEOLITH)
.args([verb, "--help"])
.output()
.expect("binary runs");
assert!(result.status.success(), "{verb} --help exits zero");
let stdout = String::from_utf8_lossy(&result.stdout);
assert!(
stdout.contains(&format!("geolith {verb}")),
"{verb} --help renders its own usage: {stdout}"
);
}
}
#[test]
fn binary_renders_dataset_help() {
let result = Command::new(GEOLITH)
.args(["dataset", "--help"])
.output()
.expect("binary runs");
assert!(result.status.success(), "dataset --help exits zero");
let stdout = String::from_utf8_lossy(&result.stdout);
for action in ["areas", "normals", "build"] {
assert!(
stdout.contains(action),
"dataset --help lists {action}: {stdout}"
);
}
}
#[test]
fn binary_requires_arguments_for_the_tile_pipeline() {
let result = Command::new(GEOLITH).output().expect("binary runs");
assert!(
!result.status.success(),
"a bare invocation cannot run the tile pipeline"
);
}
#[test]
fn reconstruct_subcommand_parses_and_requires_video() {
let result = Command::new(GEOLITH)
.args(["reconstruct", "--output", "/tmp/recon-out"])
.output()
.expect("binary runs");
assert!(!result.status.success(), "missing --video must fail");
let stderr = String::from_utf8_lossy(&result.stderr);
assert!(
stderr.contains("--video"),
"clap names the missing flag: {stderr}"
);
}
#[test]
fn reconstruct_subcommand_accepts_full_flag_set() {
let result = Command::new(GEOLITH)
.args([
"reconstruct",
"--video",
"/tmp/does-not-exist.mp4",
"--output",
"/tmp/recon-out",
"--fps",
"3",
"--max-frames",
"120",
"--train-iters",
"15000",
"--longitude",
"25.96",
"--latitude",
"58.73",
"--crop-percentile",
"0.1",
])
.output()
.expect("binary runs");
assert!(!result.status.success(), "missing video file must fail");
let stderr = String::from_utf8_lossy(&result.stderr);
assert!(
!stderr.contains("error: unexpected"),
"no clap parse error: {stderr}"
);
assert!(
stderr.contains("video not found"),
"the error names the missing video: {stderr}"
);
}
fn failing_stderr(args: &[&str]) -> String {
let result = Command::new(GEOLITH)
.args(args)
.output()
.expect("binary runs");
assert!(
!result.status.success(),
"{args:?} must exit non-zero: {}",
String::from_utf8_lossy(&result.stdout)
);
let stderr = String::from_utf8_lossy(&result.stderr).into_owned();
stderr
.lines()
.find(|line| line.starts_with("error: "))
.unwrap_or_else(|| panic!("no `error:` line in {stderr}"))
.to_owned()
}
#[test]
fn a_missing_dem_error_names_the_file() {
let dir = tempfile::tempdir().expect("tempdir");
let output = dir.path().join("out.pmtiles");
let stderr = failing_stderr(&[
"terrain",
"--dem",
"/nonexistent/geolith-dem.tif",
"--output",
output.to_str().expect("utf-8 path"),
]);
assert!(stderr.contains("/nonexistent/geolith-dem.tif"), "{stderr}");
}
#[test]
fn a_missing_splat_ply_error_names_the_file() {
let dir = tempfile::tempdir().expect("tempdir");
let output = dir.path().join("tileset");
let stderr = failing_stderr(&[
"splat-tiles",
"--ply",
"/nonexistent/geolith-scene.ply",
"--output",
output.to_str().expect("utf-8 path"),
"--longitude",
"7.9",
"--latitude",
"45.9",
"--altitude",
"2250",
]);
assert!(
stderr.contains("/nonexistent/geolith-scene.ply"),
"{stderr}"
);
}
#[test]
fn a_missing_dataset_config_error_names_the_file() {
let stderr = failing_stderr(&[
"dataset",
"build",
"--config",
"/nonexistent/geolith-build-config.json",
"--rasters",
"/nonexistent/rasters",
"--areas",
"/nonexistent/areas",
"--out",
"/nonexistent/out",
]);
assert!(
stderr.contains("/nonexistent/geolith-build-config.json"),
"{stderr}"
);
}
#[test]
fn a_missing_routing_pbf_error_names_the_file() {
let dir = tempfile::tempdir().expect("tempdir");
let output = dir.path().join("routing.glx");
let stderr = failing_stderr(&[
"--output-format",
"routing",
"--osm-pbf",
"/nonexistent/geolith-planet.osm.pbf",
"--output",
output.to_str().expect("utf-8 path"),
]);
assert!(
stderr.contains("/nonexistent/geolith-planet.osm.pbf"),
"{stderr}"
);
}
#[test]
fn a_missing_geocoding_data_dir_error_names_the_path() {
let dir = tempfile::tempdir().expect("tempdir");
let output = dir.path().join("places.tsv");
let stderr = failing_stderr(&[
"--output-format",
"geocoding",
"--data",
"/nonexistent/geolith-overture",
"--output",
output.to_str().expect("utf-8 path"),
]);
assert!(stderr.contains("/nonexistent/geolith-overture"), "{stderr}");
}
#[test]
fn a_tile_build_without_sources_names_the_data_dir() {
let dir = tempfile::tempdir().expect("tempdir");
let output = dir.path().join("out.pmtiles");
let stderr = failing_stderr(&[
"--data",
"/nonexistent/geolith-overture",
"--natural-earth",
"/nonexistent/ne.sqlite",
"--water-polygons",
"/nonexistent/water",
"--land-polygons",
"/nonexistent/land",
"--output",
output.to_str().expect("utf-8 path"),
]);
assert!(stderr.contains("/nonexistent/geolith-overture"), "{stderr}");
}
#[test]
fn an_unsupported_output_format_error_names_the_value() {
let stderr = failing_stderr(&[
"--output-format",
"osrm",
"--data",
"/nonexistent/geolith-overture",
"--natural-earth",
"/nonexistent/ne.sqlite",
"--water-polygons",
"/nonexistent/water",
"--land-polygons",
"/nonexistent/land",
"--output",
"/nonexistent/out",
]);
assert!(stderr.contains("osrm"), "{stderr}");
}