Skip to content

Commit e6f2b2f

Browse files
christos-hjonaro00
andauthored
feat(cargo-shuttle): add project name to the default directory, ask again if path is rejected (#1654)
* misc: add project name to the default directory * fix: tests * fix: tests * feat: ask again if path choice is denied * fix: update test --------- Co-authored-by: jonaro00 <[email protected]>
1 parent c386702 commit e6f2b2f

File tree

2 files changed

+40
-30
lines changed

2 files changed

+40
-30
lines changed

cargo-shuttle/src/lib.rs

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -398,31 +398,34 @@ impl Shuttle {
398398
let path = if needs_path {
399399
let path = args
400400
.path
401-
.to_str()
402-
.context("path arg should always be set")?;
401+
.join(project_args.name.as_ref().expect("name should be set"));
403402

404-
println!("Where should we create this project?");
405-
let directory_str: String = Input::with_theme(&theme)
406-
.with_prompt("Directory")
407-
.default(path.to_owned())
408-
.interact()?;
409-
println!();
403+
loop {
404+
println!("Where should we create this project?");
410405

411-
let path = args::create_and_parse_path(OsString::from(directory_str))?;
406+
let directory_str: String = Input::with_theme(&theme)
407+
.with_prompt("Directory")
408+
.default(format!("{}", path.display()))
409+
.interact()?;
410+
println!();
412411

413-
if std::fs::read_dir(&path)
414-
.expect("init dir to exist and list entries")
415-
.count()
416-
> 0
417-
&& !Confirm::with_theme(&theme)
418-
.with_prompt("Target directory is not empty. Are you sure?")
419-
.default(true)
420-
.interact()?
421-
{
422-
return Ok(CommandOutcome::Ok);
423-
}
412+
let path = args::create_and_parse_path(OsString::from(directory_str))?;
413+
414+
if std::fs::read_dir(&path)
415+
.expect("init dir to exist and list entries")
416+
.count()
417+
> 0
418+
&& !Confirm::with_theme(&theme)
419+
.with_prompt("Target directory is not empty. Are you sure?")
420+
.default(true)
421+
.interact()?
422+
{
423+
println!();
424+
continue;
425+
}
424426

425-
path
427+
break path;
428+
}
426429
} else {
427430
args.path.clone()
428431
};

cargo-shuttle/tests/integration/init.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ fn interactive_rocket_init() -> Result<(), Box<dyn std::error::Error>> {
233233
session.send_line("my-project")?;
234234
session.exp_string("Where should we create this project?")?;
235235
session.exp_string("Directory")?;
236-
session.send_line(temp_dir_path.to_str().unwrap())?;
236+
session.send_line(temp_dir_path.join("my-project").to_str().unwrap())?;
237237
session.exp_string(
238238
"Shuttle works with a range of web frameworks. Which one do you want to use?",
239239
)?;
@@ -246,7 +246,7 @@ fn interactive_rocket_init() -> Result<(), Box<dyn std::error::Error>> {
246246
session.flush()?;
247247
session.exp_string("no")?;
248248

249-
assert_valid_rocket_project(temp_dir_path.as_path(), "my-project");
249+
assert_valid_rocket_project(&temp_dir_path.as_path().join("my-project"), "my-project");
250250

251251
Ok(())
252252
}
@@ -268,7 +268,7 @@ fn interactive_rocket_init_manually_choose_template() -> Result<(), Box<dyn std:
268268
session.send_line("my-project")?;
269269
session.exp_string("Where should we create this project?")?;
270270
session.exp_string("Directory")?;
271-
session.send_line(temp_dir_path.to_str().unwrap())?;
271+
session.send_line(temp_dir_path.join("my-project").to_str().unwrap())?;
272272
session.exp_string(
273273
"Shuttle works with a range of web frameworks. Which one do you want to use?",
274274
)?;
@@ -281,7 +281,7 @@ fn interactive_rocket_init_manually_choose_template() -> Result<(), Box<dyn std:
281281
session.flush()?;
282282
session.exp_string("no")?;
283283

284-
assert_valid_rocket_project(temp_dir_path.as_path(), "my-project");
284+
assert_valid_rocket_project(&temp_dir_path.as_path().join("my-project"), "my-project");
285285

286286
Ok(())
287287
}
@@ -310,14 +310,14 @@ fn interactive_rocket_init_dont_prompt_framework() -> Result<(), Box<dyn std::er
310310
session.send_line("my-project")?;
311311
session.exp_string("Where should we create this project?")?;
312312
session.exp_string("Directory")?;
313-
session.send_line(temp_dir_path.to_str().unwrap())?;
313+
session.send_line(temp_dir_path.join("my-project").to_str().unwrap())?;
314314
session.exp_string("Creating project")?;
315315
session.exp_string("container on Shuttle?")?;
316316
session.send("n")?;
317317
session.flush()?;
318318
session.exp_string("no")?;
319319

320-
assert_valid_rocket_project(temp_dir_path.as_path(), "my-project");
320+
assert_valid_rocket_project(&temp_dir_path.as_path().join("my-project"), "my-project");
321321

322322
Ok(())
323323
}
@@ -343,7 +343,7 @@ fn interactive_rocket_init_dont_prompt_name() -> Result<(), Box<dyn std::error::
343343

344344
session.exp_string("Where should we create this project?")?;
345345
session.exp_string("Directory")?;
346-
session.send_line(temp_dir_path.to_str().unwrap())?;
346+
session.send_line(temp_dir_path.join("my-project").to_str().unwrap())?;
347347
session.exp_string(
348348
"Shuttle works with a range of web frameworks. Which one do you want to use?",
349349
)?;
@@ -356,7 +356,7 @@ fn interactive_rocket_init_dont_prompt_name() -> Result<(), Box<dyn std::error::
356356
session.flush()?;
357357
session.exp_string("no")?;
358358

359-
assert_valid_rocket_project(temp_dir_path.as_path(), "my-project");
359+
assert_valid_rocket_project(&temp_dir_path.as_path().join("my-project"), "my-project");
360360

361361
Ok(())
362362
}
@@ -384,6 +384,13 @@ fn interactive_rocket_init_prompt_path_dirty_dir() -> Result<(), Box<dyn std::er
384384
]);
385385
let mut session = rexpect::session::spawn_command(command, Some(EXPECT_TIMEOUT_MS))?;
386386

387+
session.exp_string("Where should we create this project?")?;
388+
session.exp_string("Directory")?;
389+
session.send_line(temp_dir_path.to_str().unwrap())?;
390+
session.exp_string("Target directory is not empty. Are you sure?")?;
391+
session.send("n")?;
392+
session.flush()?;
393+
session.exp_string("no")?;
387394
session.exp_string("Where should we create this project?")?;
388395
session.exp_string("Directory")?;
389396
session.send_line(temp_dir_path.to_str().unwrap())?;
@@ -397,7 +404,7 @@ fn interactive_rocket_init_prompt_path_dirty_dir() -> Result<(), Box<dyn std::er
397404
session.flush()?;
398405
session.exp_string("no")?;
399406

400-
assert_valid_rocket_project(temp_dir_path.as_path(), "my-project");
407+
assert_valid_rocket_project(&temp_dir_path, "my-project");
401408

402409
Ok(())
403410
}

0 commit comments

Comments
 (0)