Merge pull request #2057 from seanpoulter/init-skip

fix(cli): init --force skips confirmation prompts
This commit is contained in:
Eric Huss 2023-04-30 14:18:30 -07:00 committed by GitHub
commit af036d9f45
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 1 deletions

View file

@ -76,3 +76,7 @@ mdbook init --ignore=git
``` ```
[building]: build.md [building]: build.md
#### --force
Skip the prompts to create a `.gitignore` and for the title for the book.

View file

@ -56,7 +56,7 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
"git" => builder.create_gitignore(true), "git" => builder.create_gitignore(true),
_ => builder.create_gitignore(false), _ => builder.create_gitignore(false),
}; };
} else { } else if !args.get_flag("force") {
println!("\nDo you want a .gitignore to be created? (y/n)"); println!("\nDo you want a .gitignore to be created? (y/n)");
if confirm() { if confirm() {
builder.create_gitignore(true); builder.create_gitignore(true);
@ -65,6 +65,8 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
config.book.title = if args.contains_id("title") { config.book.title = if args.contains_id("title") {
args.get_one::<String>("title").map(String::from) args.get_one::<String>("title").map(String::from)
} else if args.get_flag("force") {
None
} else { } else {
request_book_title() request_book_title()
}; };

24
tests/cli/init.rs Normal file
View file

@ -0,0 +1,24 @@
use crate::cli::cmd::mdbook_cmd;
use crate::dummy_book::DummyBook;
use mdbook::config::Config;
/// Run `mdbook init` with `--force` to skip the confirmation prompts
#[test]
fn base_mdbook_init_can_skip_confirmation_prompts() {
let temp = DummyBook::new().build().unwrap();
// doesn't exist before
assert!(!temp.path().join("book").exists());
let mut cmd = mdbook_cmd();
cmd.args(["init", "--force"]).current_dir(temp.path());
cmd.assert()
.success()
.stdout(predicates::str::contains("\nAll done, no errors...\n"));
let config = Config::from_disk(temp.path().join("book.toml")).unwrap();
assert_eq!(config.book.title, None);
assert!(!temp.path().join(".gitignore").exists());
}

View file

@ -1,3 +1,4 @@
mod build; mod build;
mod cmd; mod cmd;
mod init;
mod test; mod test;