Skip to content

Commit

Permalink
Merge pull request #2057 from seanpoulter/init-skip
Browse files Browse the repository at this point in the history
fix(cli): init --force skips confirmation prompts
  • Loading branch information
ehuss authored Apr 30, 2023
2 parents 41567b0 + b9c6b32 commit af036d9
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
4 changes: 4 additions & 0 deletions guide/src/cli/init.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,7 @@ mdbook init --ignore=git
```

[building]: build.md

#### --force

Skip the prompts to create a `.gitignore` and for the title for the book.
4 changes: 3 additions & 1 deletion src/cmd/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
"git" => builder.create_gitignore(true),
_ => builder.create_gitignore(false),
};
} else {
} else if !args.get_flag("force") {
println!("\nDo you want a .gitignore to be created? (y/n)");
if confirm() {
builder.create_gitignore(true);
Expand All @@ -65,6 +65,8 @@ pub fn execute(args: &ArgMatches) -> Result<()> {

config.book.title = if args.contains_id("title") {
args.get_one::<String>("title").map(String::from)
} else if args.get_flag("force") {
None
} else {
request_book_title()
};
Expand Down
24 changes: 24 additions & 0 deletions tests/cli/init.rs
Original file line number Diff line number Diff line change
@@ -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());
}
1 change: 1 addition & 0 deletions tests/cli/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod build;
mod cmd;
mod init;
mod test;

0 comments on commit af036d9

Please sign in to comment.