diff --git a/zingo-cli/Cargo.toml b/zingo-cli/Cargo.toml index b881b1f9c..4479bb604 100644 --- a/zingo-cli/Cargo.toml +++ b/zingo-cli/Cargo.toml @@ -26,3 +26,6 @@ shellwords = { workspace = true } tokio = { workspace = true } tracing-subscriber = { workspace = true } zip32 = { workspace = true } + +[dev-dependencies] +tempfile = "3" diff --git a/zingo-cli/src/lib.rs b/zingo-cli/src/lib.rs index 1f7efeffd..df1ed133c 100644 --- a/zingo-cli/src/lib.rs +++ b/zingo-cli/src/lib.rs @@ -398,7 +398,16 @@ If you don't remember the block height, you can pass '--birthday 0' to scan from }; let data_dir = if let Some(dir) = matches.get_one::("data-dir") { - PathBuf::from(dir.clone()) + let path = PathBuf::from(dir.clone()); + if path.is_file() { + return Err(format!( + "--data-dir must be a directory, not a file.\n\ + You provided: {dir}\n\ + Hint: use the parent directory instead, e.g. --data-dir {}", + path.parent().map(|p| p.display().to_string()).unwrap_or_else(|| ".".into()) + )); + } + path } else { PathBuf::from("wallets") }; diff --git a/zingo-cli/src/tests.rs b/zingo-cli/src/tests.rs index 5c3b91b61..c9a5531ec 100644 --- a/zingo-cli/src/tests.rs +++ b/zingo-cli/src/tests.rs @@ -500,6 +500,14 @@ mod config_template { let err = fill(&[examples::BIN_NAME, "--server", "https://example.com"]).unwrap_err(); assert!(err.contains("scheme")); } + + #[test] + fn data_dir_is_a_file() { + let tmp = tempfile::NamedTempFile::new().unwrap(); + let file_path = tmp.path().to_str().unwrap(); + let err = fill(&[examples::BIN_NAME, "--data-dir", file_path]).unwrap_err(); + assert!(err.contains("must be a directory, not a file")); + } } mod zingo_config {