#![allow(clippy::unwrap_used)]
use clap::Parser;
use geolith_cli::config::Config;
const BASE_ARGS: [&str; 11] = [
"geolith",
"--data",
"/tmp/data",
"--osm-pbf",
"/tmp/planet.osm.pbf",
"--natural-earth",
"/tmp/ne.sqlite",
"--water-polygons",
"/tmp/water",
"--land-polygons",
"/tmp/land",
];
fn parse_with(extra: &[&str]) -> Config {
let mut args: Vec<&str> = BASE_ARGS.to_vec();
args.extend_from_slice(extra);
Config::parse_from(args)
}
#[test]
fn config_default_max_zoom() {
let config = parse_with(&["--output", "/tmp/out.pmtiles"]);
assert_eq!(config.max_zoom, 15, "Default max_zoom should be 15");
}
#[test]
fn config_custom_max_zoom() {
let config = parse_with(&["--output", "/tmp/out.pmtiles", "--max-zoom", "10"]);
assert_eq!(config.max_zoom, 10);
}
#[test]
fn config_bbox_parsing() {
let config = parse_with(&["--output", "/tmp/out.pmtiles", "--bbox", "1.0,2.0,3.0,4.0"]);
assert_eq!(config.bbox, Some("1.0,2.0,3.0,4.0".to_string()));
}
#[test]
fn config_no_bbox() {
let config = parse_with(&["--output", "/tmp/out.pmtiles"]);
assert!(config.bbox.is_none());
}
#[test]
fn config_reclaim_source_after_read_defaults_off() {
let config = parse_with(&["--output", "/tmp/out.pmtiles"]);
assert!(
!config.reclaim_source_after_read,
"source files must not be reclaimed unless explicitly requested"
);
}
#[test]
fn config_reclaim_source_after_read_opt_in() {
let config = parse_with(&[
"--output",
"/tmp/out.pmtiles",
"--reclaim-source-after-read",
]);
assert!(config.reclaim_source_after_read);
}
#[test]
fn config_reclaim_chunks_and_disk_check() {
let config = parse_with(&["--output", "/tmp/out.pmtiles"]);
assert!(!config.reclaim_chunks, "chunks are kept unless asked");
assert!(!config.no_disk_check, "the disk check is on by default");
let config = parse_with(&[
"--output",
"/tmp/out.pmtiles",
"--reclaim-chunks",
"--no-disk-check",
]);
assert!(config.reclaim_chunks);
assert!(config.no_disk_check);
let tiles = config.tiles().expect("tiles config");
assert!(tiles.reclaim_chunks);
assert!(!tiles.disk_check);
}
#[test]
fn config_all_flags() {
let config = Config::parse_from([
"geolith",
"--data",
"/tmp/overture",
"--osm-pbf",
"/tmp/planet.osm.pbf",
"--natural-earth",
"/tmp/ne.sqlite",
"--water-polygons",
"/tmp/water",
"--land-polygons",
"/tmp/land",
"--output",
"/tmp/planet.pmtiles",
"--bbox",
"-122.5,37.7,-122.3,37.8",
"--max-zoom",
"12",
"--threads",
"4",
"--tmpdir",
"/tmp/chunks",
"--india-boundary",
"/tmp/india.geojson",
"--buildings-source",
"osm",
"--roads-source",
"osm",
]);
assert_eq!(
config
.data
.as_deref()
.and_then(|p| p.to_str())
.expect("data path"),
"/tmp/overture"
);
assert_eq!(
config
.output
.as_deref()
.and_then(|p| p.to_str())
.expect("output path"),
"/tmp/planet.pmtiles"
);
assert_eq!(config.bbox, Some("-122.5,37.7,-122.3,37.8".to_string()));
assert_eq!(config.max_zoom, 12);
assert_eq!(config.threads, Some(4));
assert_eq!(
config
.tmpdir
.as_ref()
.expect("tmpdir")
.to_str()
.expect("tmpdir str"),
"/tmp/chunks"
);
assert!(config.has_india_boundary());
assert_eq!(config.buildings_source, "osm");
assert_eq!(config.roads_source, "osm");
}
#[test]
fn config_short_output_flag() {
let config = parse_with(&["-o", "/tmp/short.pmtiles"]);
assert_eq!(
config
.output
.as_deref()
.and_then(|p| p.to_str())
.expect("output path"),
"/tmp/short.pmtiles"
);
}
#[test]
fn config_source_helpers() {
let config = parse_with(&["--output", "/tmp/out.pmtiles"]);
assert!(config.uses_osm(&config.buildings_source));
assert!(config.uses_overture(&config.buildings_source));
assert!(config.uses_overture(&config.divisions_source));
assert!(!config.uses_osm(&config.divisions_source));
}
#[test]
fn config_layer_source_filtering() {
use geolith_cli::layers::Layer;
let config = Config::parse_from([
"geolith",
"--data",
"/tmp/data",
"--osm-pbf",
"/tmp/planet.osm.pbf",
"--natural-earth",
"/tmp/ne.sqlite",
"--water-polygons",
"/tmp/water",
"--land-polygons",
"/tmp/land",
"--output",
"/tmp/out.pmtiles",
"--buildings-source",
"osm",
"--roads-source",
"overture",
"--landcover-source",
"none",
]);
assert!(!config.overture_enabled(Layer::Buildings));
assert!(config.osm_enabled(Layer::Buildings));
assert!(config.overture_enabled(Layer::Roads));
assert!(!config.osm_enabled(Layer::Roads));
assert!(config.overture_enabled(Layer::Pois));
assert!(config.osm_enabled(Layer::Pois));
assert!(config.overture_enabled(Layer::Landuse));
assert!(config.osm_enabled(Layer::Landuse));
assert!(!config.overture_enabled(Layer::Landcover));
assert!(config.overture_enabled(Layer::Earth));
assert!(config.osm_enabled(Layer::Earth));
assert!(config.overture_enabled(Layer::Water));
assert!(config.osm_enabled(Layer::Water)); assert!(config.osm_enabled(Layer::Places));
}
fn missing_message(flags: &[&str]) -> String {
let mut s = String::from("error: the following required arguments were not provided:\n");
for f in flags {
s.push_str(" ");
s.push_str(f);
s.push('\n');
}
s.push_str(
"\nUsage: geolith --data <DATA> --natural-earth <NATURAL_EARTH> \
--water-polygons <WATER_POLYGONS> --land-polygons <LAND_POLYGONS> --output <OUTPUT>\n",
);
s
}
#[test]
fn tiles_reports_each_missing_path_flag() {
let all = [
("--data", "--data <DATA>"),
("--natural-earth", "--natural-earth <NATURAL_EARTH>"),
("--water-polygons", "--water-polygons <WATER_POLYGONS>"),
("--land-polygons", "--land-polygons <LAND_POLYGONS>"),
("--output", "--output <OUTPUT>"),
];
for (skip, usage) in all {
let mut argv = vec!["geolith"];
for (flag, _) in all.iter().filter(|(f, _)| *f != skip) {
argv.extend([*flag, "/x"]);
}
let err = geolith_cli::cli::parse_from(argv).expect_err("a tiles flag is missing");
assert_eq!(err.kind(), clap::error::ErrorKind::MissingRequiredArgument);
let text = err.render().to_string();
assert!(
text.starts_with(&missing_message(&[usage])),
"missing {skip}:\n{text}"
);
}
let err = geolith_cli::cli::parse_from(["geolith"]).expect_err("every tiles flag is missing");
let usages: Vec<&str> = all.iter().map(|(_, u)| *u).collect();
assert!(
err.render()
.to_string()
.starts_with(&missing_message(&usages))
);
}
#[test]
fn routing_needs_only_the_pbf_and_the_output() {
let command = geolith_cli::cli::parse_from([
"geolith",
"--output-format",
"routing",
"--osm-pbf",
"/tmp/berlin.osm.pbf",
"--routing-output",
"/tmp/berlin.glx",
])
.expect("routing reads no tile sources");
let geolith_cli::cli::Command::Tiles(config) = command else {
panic!("the default command parses the build config");
};
assert_eq!(config.data, None);
assert_eq!(config.output, None);
assert!(config.check_required().is_ok());
}
#[test]
fn geocoding_needs_data_and_an_output_path() {
let err = geolith_cli::cli::parse_from(["geolith", "--output-format", "geocoding"])
.expect_err("geocoding reads --data");
assert!(
err.render()
.to_string()
.contains(" --data <DATA>\n --output <OUTPUT>\n")
);
let ok = geolith_cli::cli::parse_from([
"geolith",
"--output-format",
"geocoding",
"--data",
"/tmp/overture",
"--geocoding-output",
"/tmp/places.glx",
]);
assert!(ok.is_ok());
}
#[test]
fn reuse_flags_keep_their_names_and_defaults() {
let config = parse_with(&["--output", "/tmp/out.pmtiles"]);
assert!(config.reuse.node_cache, "node caching is on by default");
assert!(!config.reuse.resume, "resume is opt-in");
let config = parse_with(&["--output", "/tmp/out.pmtiles", "--resume"]);
assert!(config.reuse.resume);
assert!(config.reuse.node_cache);
}
const PLANET_CHAIN: [&str; 36] = [
"geolith",
"--data",
"/data/overture",
"--osm-pbf",
"/data/planet.osm.pbf",
"--natural-earth",
"/data/natural_earth.sqlite",
"--land-polygons",
"/data/land-polygons",
"--water-polygons",
"/data/water-polygons",
"--india-boundary",
"/data/india-boundary.geojson",
"--output",
"/data/planet.pmtiles",
"--pois-source",
"osm",
"--buildings-source",
"overture",
"--roads-source",
"overture",
"--places-source",
"overture",
"--water-source",
"overture",
"--landuse-source",
"overture",
"--no-conflate",
"--max-zoom",
"15",
"--node-cache",
"--node-store",
"/data/nodes.store",
"--tmpdir",
"/data/tmp",
"--reclaim-source-after-read",
];
#[test]
fn the_planet_chain_command_line_parses() {
for restart in [false, true] {
let mut args: Vec<&str> = PLANET_CHAIN.to_vec();
args.extend(["--reclaim-chunks", "--no-disk-check"]);
if restart {
args.push("--resume");
}
let config = Config::try_parse_from(args).expect("the planet chain parses");
assert_eq!(config.pois_source, "osm");
assert_eq!(config.buildings_source, "overture");
assert_eq!(config.roads_source, "overture");
assert_eq!(config.water_source, "overture");
assert_eq!(config.landuse_source, "overture");
assert_eq!(config.max_zoom, 15);
assert!(config.no_conflate);
assert!(config.reuse.node_cache);
assert_eq!(config.reuse.resume, restart);
assert!(config.reclaim_source_after_read);
assert!(config.reclaim_chunks);
assert!(config.no_disk_check);
assert!(config.has_india_boundary());
assert_eq!(
config.places_source.as_deref(),
Some("overture"),
"the flag is read, so that it can be reported as having no effect"
);
}
}
#[test]
fn places_source_is_accepted_and_reported_as_having_no_effect() {
let with = parse_with(&["--output", "/tmp/out.pmtiles", "--places-source", "osm"]);
let warnings = with.deprecation_warnings();
assert_eq!(warnings.len(), 1, "{warnings:?}");
assert!(warnings[0].contains("--places-source"), "{warnings:?}");
assert!(warnings[0].contains("no effect"), "{warnings:?}");
let without = parse_with(&["--output", "/tmp/out.pmtiles"]);
assert!(without.deprecation_warnings().is_empty());
}
fn parse_error_kind(extra: &[&str]) -> Option<clap::error::ErrorKind> {
let mut args: Vec<&str> = BASE_ARGS.to_vec();
args.extend_from_slice(&["--output", "/tmp/out.pmtiles"]);
args.extend_from_slice(extra);
Config::try_parse_from(args).err().map(|e| e.kind())
}
#[test]
fn zero_threads_is_refused_at_parse_time() {
assert_eq!(
parse_error_kind(&["--threads", "0"]),
Some(clap::error::ErrorKind::ValueValidation)
);
assert_eq!(parse_error_kind(&["--threads", "1"]), None);
}
#[test]
fn a_max_zoom_above_15_is_refused_at_parse_time() {
assert_eq!(
parse_error_kind(&["--max-zoom", "16"]),
Some(clap::error::ErrorKind::ValueValidation)
);
assert_eq!(parse_error_kind(&["--max-zoom", "15"]), None);
assert_eq!(parse_error_kind(&["--max-zoom", "0"]), None);
}
#[test]
fn an_unknown_geocoding_format_is_refused_at_parse_time() {
assert_eq!(
parse_error_kind(&["--geocoding-format", "glxx"]),
Some(clap::error::ErrorKind::InvalidValue)
);
for format in ["glx", "pg-copy", "photon", "pelias"] {
assert_eq!(
parse_error_kind(&["--geocoding-format", format]),
None,
"{format}"
);
}
}
#[test]
fn a_typo_in_a_source_or_tile_format_flag_is_refused_at_parse_time() {
for flag in [
"--buildings-source",
"--roads-source",
"--pois-source",
"--water-source",
"--landuse-source",
] {
assert_eq!(
parse_error_kind(&[flag, "ovrture"]),
Some(clap::error::ErrorKind::InvalidValue),
"{flag}"
);
for value in ["osm", "overture", "both"] {
assert_eq!(parse_error_kind(&[flag, value]), None, "{flag} {value}");
}
}
for flag in [
"--divisions-source",
"--landcover-source",
"--bathymetry-source",
] {
assert_eq!(
parse_error_kind(&[flag, "osm"]),
Some(clap::error::ErrorKind::InvalidValue),
"{flag} takes overture or none"
);
for value in ["overture", "none"] {
assert_eq!(parse_error_kind(&[flag, value]), None, "{flag} {value}");
}
}
assert_eq!(
parse_error_kind(&["--tile-format", "mtl"]),
Some(clap::error::ErrorKind::InvalidValue)
);
for value in ["mvt", "mlt"] {
assert_eq!(parse_error_kind(&["--tile-format", value]), None);
}
}