Skip to content

Commit 721df43

Browse files
authored
Merge pull request #800 from JasonOA888/fix/cli-create-new-world-706
fix: allow CLI to create new world in non-existent directory
2 parents 4110129 + 15ea716 commit 721df43

1 file changed

Lines changed: 27 additions & 11 deletions

File tree

src/args.rs

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,7 @@ pub struct Args {
9292
}
9393

9494
/// Validates CLI arguments after parsing.
95-
/// For Java Edition: `--path` is required and must point to an existing directory
96-
/// where a new world will be created automatically.
95+
/// For Java Edition: `--path` is required. If the directory doesn't exist, it will be created.
9796
/// For Bedrock Edition (`--bedrock`): `--path` is optional (defaults to Desktop output).
9897
pub fn validate_args(args: &Args) -> Result<(), String> {
9998
if args.bedrock {
@@ -107,7 +106,8 @@ pub fn validate_args(args: &Args) -> Result<(), String> {
107106
}
108107
}
109108
} else {
110-
// Java: path is required and must be an existing directory
109+
// Java: path is required. If it exists, it must be a directory.
110+
// If it doesn't exist, create_new_world will create it.
111111
match &args.path {
112112
None => {
113113
return Err(
@@ -116,12 +116,13 @@ pub fn validate_args(args: &Args) -> Result<(), String> {
116116
);
117117
}
118118
Some(ref path) => {
119-
if !path.exists() {
120-
return Err(format!("Path does not exist: {}", path.display()));
121-
}
122-
if !path.is_dir() {
123-
return Err(format!("Path is not a directory: {}", path.display()));
119+
if path.exists() && !path.is_dir() {
120+
return Err(format!(
121+
"Path exists but is not a directory: {}",
122+
path.display()
123+
));
124124
}
125+
// If path doesn't exist, that's OK - create_new_world will create it
125126
}
126127
}
127128
}
@@ -289,18 +290,33 @@ mod tests {
289290
}
290291

291292
#[test]
292-
fn test_java_path_must_exist() {
293+
fn test_java_nonexistent_path_is_ok() {
294+
// Java: nonexistent paths are OK - create_new_world will create them
295+
let tmp = tempfile::tempdir().unwrap();
296+
let nonexistent = tmp.path().join("does_not_exist");
293297
let cmd = [
294298
"arnis",
295299
"--output-dir",
296-
"/nonexistent/path",
300+
nonexistent.to_str().unwrap(),
297301
"--bbox",
298302
"1,2,3,4",
299303
];
300304
let args = Args::parse_from(cmd.iter());
301305
let result = validate_args(&args);
306+
assert!(result.is_ok());
307+
}
308+
309+
#[test]
310+
fn test_java_path_exists_but_is_file_fails() {
311+
// Java: if path exists but is a file, fail
312+
let tmpfile = tempfile::NamedTempFile::new().unwrap();
313+
let tmp_path = tmpfile.path().to_str().unwrap();
314+
315+
let cmd = ["arnis", "--output-dir", tmp_path, "--bbox", "1,2,3,4"];
316+
let args = Args::parse_from(cmd.iter());
317+
let result = validate_args(&args);
302318
assert!(result.is_err());
303-
assert!(result.unwrap_err().contains("does not exist"));
319+
assert!(result.unwrap_err().contains("not a directory"));
304320
}
305321

306322
#[test]

0 commit comments

Comments
 (0)