use std::fs;
use std::path::Path;
use std::process::{Command, Output};
const BUILD: &str = "2026-11-20.20261120T0000Z";
fn dataset(home: &Path, config: Option<&Path>, args: &[&str]) -> Output {
dataset_with(home, config, &[], args)
}
fn dataset_with(
home: &Path,
config: Option<&Path>,
vars: &[(&str, &Path)],
args: &[&str],
) -> Output {
let mut command = Command::new(env!("CARGO_BIN_EXE_geolith"));
command
.env_clear()
.current_dir(home)
.envs(vars.iter().copied())
.env("HOME", home)
.env("DATA_ROOT", home.join("data"))
.env(
"GEOLITH_REPO",
Path::new(env!("CARGO_MANIFEST_DIR")).join(".."),
)
.arg("dataset");
if let Some(config) = config {
command.arg("--config").arg(config);
}
command.args(args).output().expect("running geolith")
}
fn stderr(output: &Output) -> String {
String::from_utf8_lossy(&output.stderr).into_owned()
}
fn config(home: &Path, json: &str) -> std::path::PathBuf {
let path = home.join("geolith.json");
fs::write(&path, json).expect("write the config");
path
}
#[test]
fn an_upload_with_an_empty_config_names_the_missing_endpoint() {
let home = tempfile::tempdir().expect("a temp dir");
let file = config(home.path(), "{}");
let output = dataset(home.path(), Some(&file), &["upload", BUILD]);
assert_eq!(output.status.code(), Some(1), "{}", stderr(&output));
assert!(
stderr(&output).contains("storage.endpoint is not set; see geolith.example.json"),
"{}",
stderr(&output)
);
}
#[test]
fn a_publish_with_no_config_file_at_all_names_the_same_thing() {
let home = tempfile::tempdir().expect("a temp dir");
let output = dataset(home.path(), None, &["publish", BUILD, "--dry-run"]);
assert_eq!(output.status.code(), Some(1), "{}", stderr(&output));
assert!(
stderr(&output).contains("storage.endpoint is not set"),
"{}",
stderr(&output)
);
}
#[test]
fn an_endpoint_without_a_bucket_names_the_bucket() {
let home = tempfile::tempdir().expect("a temp dir");
let file = config(
home.path(),
r#"{"storage":{"endpoint":"http://127.0.0.1:9"}}"#,
);
let output = dataset(home.path(), Some(&file), &["upload", BUILD]);
assert!(
stderr(&output).contains("storage.bucket is not set; see geolith.example.json"),
"{}",
stderr(&output)
);
}
#[test]
fn a_bucket_flag_other_than_the_configured_one_is_refused_before_any_request() {
let home = tempfile::tempdir().expect("a temp dir");
let file = config(
home.path(),
r#"{"storage":{"endpoint":"http://127.0.0.1:9","bucket":"made-up-bucket"}}"#,
);
let output = dataset(
home.path(),
Some(&file),
&["upload", BUILD, "--bucket", "somebody-elses"],
);
assert_eq!(output.status.code(), Some(1), "{}", stderr(&output));
let text = stderr(&output);
assert!(text.contains("bucket \"somebody-elses\" refused"), "{text}");
assert!(text.contains("made-up-bucket"), "{text}");
}
#[test]
fn an_endpoint_of_another_account_is_refused_before_any_request() {
let home = tempfile::tempdir().expect("a temp dir");
let file = config(
home.path(),
r#"{"storage":{"endpoint":"https://acct2.store.example.test","bucket":"b",
"account_id":"acct1"}}"#,
);
let output = dataset(home.path(), Some(&file), &["upload", BUILD, "--dry-run"]);
let text = stderr(&output);
assert!(
text.contains("acct2.store.example.test is not of storage.account_id"),
"{text}"
);
}
#[test]
fn a_named_config_file_that_is_missing_is_named() {
let home = tempfile::tempdir().expect("a temp dir");
let missing = home.path().join("nowhere.json");
let output = dataset(home.path(), Some(&missing), &["keys", BUILD]);
assert_eq!(output.status.code(), Some(1), "{}", stderr(&output));
let text = stderr(&output);
assert!(text.contains("nowhere.json"), "{text}");
assert!(text.contains("does not exist"), "{text}");
}
#[test]
fn a_misspelt_field_is_named_instead_of_ignored() {
let home = tempfile::tempdir().expect("a temp dir");
let file = config(home.path(), r#"{"storage":{"buckett":"x"}}"#);
let output = dataset(home.path(), Some(&file), &["keys", BUILD]);
let text = stderr(&output);
assert!(text.contains("unknown field `buckett`"), "{text}");
assert!(text.contains("geolith.json"), "{text}");
}
fn env_file(path: &Path, text: &str) {
fs::write(path, text).expect("write the env file");
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt as _;
fs::set_permissions(path, fs::Permissions::from_mode(0o600)).expect("chmod");
}
}
#[test]
fn a_dot_env_in_the_working_directory_supplies_the_store() {
let home = tempfile::tempdir().expect("a temp dir");
env_file(
&home.path().join(".env"),
"DATASETS_R2_ENDPOINT=http://127.0.0.1:9\nDATASETS_BUCKET=dotenv-bucket\n",
);
let output = dataset(
home.path(),
None,
&["upload", BUILD, "--bucket", "somebody-elses"],
);
assert_eq!(output.status.code(), Some(1), "{}", stderr(&output));
let text = stderr(&output);
assert!(text.contains("bucket \"somebody-elses\" refused"), "{text}");
assert!(text.contains("dotenv-bucket"), "{text}");
}
#[test]
fn geolith_env_file_names_the_file_and_a_wide_one_is_refused() {
let home = tempfile::tempdir().expect("a temp dir");
let named = home.path().join("ops.env");
env_file(&named, "DATASETS_R2_ENDPOINT=http://127.0.0.1:9\n");
let output = dataset_with(
home.path(),
None,
&[("GEOLITH_ENV_FILE", &named)],
&["upload", BUILD],
);
assert!(
stderr(&output).contains("storage.bucket is not set"),
"{}",
stderr(&output)
);
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt as _;
fs::set_permissions(&named, fs::Permissions::from_mode(0o644)).expect("chmod");
let output = dataset_with(
home.path(),
None,
&[("GEOLITH_ENV_FILE", &named)],
&["keys", BUILD],
);
assert_eq!(output.status.code(), Some(1), "{}", stderr(&output));
let text = stderr(&output);
assert!(text.contains("chmod 600"), "{text}");
assert!(!text.contains("127.0.0.1"), "{text}");
}
let gone = home.path().join("gone.env");
let output = dataset_with(
home.path(),
None,
&[("GEOLITH_ENV_FILE", &gone)],
&["keys", BUILD],
);
assert_eq!(output.status.code(), Some(1), "{}", stderr(&output));
assert!(stderr(&output).contains("gone.env"), "{}", stderr(&output));
}
#[test]
fn the_env_example_parses_and_holds_only_placeholders() {
let home = tempfile::tempdir().expect("a temp dir");
let example = Path::new(env!("CARGO_MANIFEST_DIR")).join("../.env.example");
let text = fs::read_to_string(&example).expect(".env.example is at the repo root");
let copy = home.path().join("copy.env");
env_file(©, &text);
let output = dataset_with(
home.path(),
None,
&[("GEOLITH_ENV_FILE", ©)],
&["keys", BUILD],
);
let error = stderr(&output);
assert!(
!error.contains("env file")
&& !error.contains("storage.")
&& !error.contains("publish.")
&& !error.contains("DATASETS_"),
"{error}"
);
for operator_value in [
"geolith-cloud-data",
"tiles/datasets",
"geolith.app",
"cloudflarestorage",
"RELEASE_PAT",
] {
assert!(!text.contains(operator_value), "{operator_value}");
}
}
#[test]
fn the_example_file_parses_and_holds_no_value_of_a_real_deployment() {
let home = tempfile::tempdir().expect("a temp dir");
let example = Path::new(env!("CARGO_MANIFEST_DIR")).join("../geolith.example.json");
let text = fs::read_to_string(&example).expect("geolith.example.json is at the repo root");
let output = dataset(home.path(), Some(&example), &["keys", BUILD]);
let error = stderr(&output);
assert!(
!error.contains("config file") && !error.contains("unknown field"),
"{error}"
);
for operator_value in [
"geolith-cloud-data",
"tiles/datasets",
"geolith.app",
"cloudflarestorage",
] {
assert!(!text.contains(operator_value), "{operator_value}");
}
}