cli: handle init destination error gracefully

This commit is contained in:
Yuya Nishihara 2022-11-05 17:40:13 +09:00
parent 61468ed126
commit 5d42c9ebca
2 changed files with 15 additions and 5 deletions

View file

@ -1037,12 +1037,14 @@ fn cmd_init(ui: &mut Ui, command: &CommandHelper, args: &InitArgs) -> Result<(),
));
}
let wc_path = ui.cwd().join(&args.destination);
if wc_path.exists() {
assert!(wc_path.is_dir());
} else {
fs::create_dir(&wc_path).unwrap();
match fs::create_dir(&wc_path) {
Ok(()) => {}
Err(_) if wc_path.is_dir() => {}
Err(e) => return Err(UserError(format!("Failed to create workspace: {e}"))),
}
let wc_path = wc_path.canonicalize().unwrap();
let wc_path = wc_path
.canonicalize()
.map_err(|e| UserError(format!("Failed to create workspace: {e}")))?; // raced?
if let Some(git_store_str) = &args.git_repo {
let mut git_store_path = ui.cwd().join(git_store_str);

View file

@ -173,6 +173,14 @@ fn test_init_git_internal_but_could_be_colocated() {
"###);
}
#[test]
fn test_init_git_bad_wc_path() {
let test_env = TestEnvironment::default();
std::fs::write(test_env.env_root().join("existing-file"), b"").unwrap();
let stderr = test_env.jj_cmd_failure(test_env.env_root(), &["init", "--git", "existing-file"]);
assert!(stderr.contains("Failed to create workspace"));
}
#[test]
fn test_init_local_disallowed() {
let test_env = TestEnvironment::default();