From bffdb0b03d7b94af26083053191a4d77feb4fb1a Mon Sep 17 00:00:00 2001 From: Sean Poulter Date: Mon, 3 Apr 2023 23:00:40 -0400 Subject: [PATCH 1/3] fix(cli): init --force skips confirmation prompts --- src/cmd/init.rs | 4 +++- tests/cli/init.rs | 24 ++++++++++++++++++++++++ tests/cli/mod.rs | 1 + 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 tests/cli/init.rs diff --git a/src/cmd/init.rs b/src/cmd/init.rs index d8ce93d1..3a60d975 100644 --- a/src/cmd/init.rs +++ b/src/cmd/init.rs @@ -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); @@ -65,6 +65,8 @@ pub fn execute(args: &ArgMatches) -> Result<()> { config.book.title = if args.contains_id("title") { args.get_one::("title").map(String::from) + } else if args.get_flag("force") { + None } else { request_book_title() }; diff --git a/tests/cli/init.rs b/tests/cli/init.rs new file mode 100644 index 00000000..7f992237 --- /dev/null +++ b/tests/cli/init.rs @@ -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()); +} diff --git a/tests/cli/mod.rs b/tests/cli/mod.rs index 989f443f..152b4ee9 100644 --- a/tests/cli/mod.rs +++ b/tests/cli/mod.rs @@ -1,3 +1,4 @@ mod build; mod cmd; +mod init; mod test; From 0003072623b6de669c6f563fd9f49c09afec3154 Mon Sep 17 00:00:00 2001 From: Sean Poulter Date: Mon, 3 Apr 2023 08:36:47 -0400 Subject: [PATCH 2/3] docs(cli): Add docs for --force --- guide/src/cli/init.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/guide/src/cli/init.md b/guide/src/cli/init.md index 99c0be09..5d0f9032 100644 --- a/guide/src/cli/init.md +++ b/guide/src/cli/init.md @@ -68,3 +68,7 @@ Create a `.gitignore` file configured to ignore the `book` directory created whe If not supplied, an interactive prompt will ask whether it should be created. [building]: build.md + +#### --force + +Skip the prompts to create a `.gitignore` and for the title for the book. From b9c6b326b7a65996884d881efd39ec08b22c4fe5 Mon Sep 17 00:00:00 2001 From: Sean Poulter Date: Mon, 3 Apr 2023 08:36:57 -0400 Subject: [PATCH 3/3] style(tests): Fixed issues reported by clippy --- tests/cli/init.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/cli/init.rs b/tests/cli/init.rs index 7f992237..6bd12274 100644 --- a/tests/cli/init.rs +++ b/tests/cli/init.rs @@ -12,7 +12,7 @@ fn base_mdbook_init_can_skip_confirmation_prompts() { assert!(!temp.path().join("book").exists()); let mut cmd = mdbook_cmd(); - cmd.args(&["init", "--force"]).current_dir(temp.path()); + cmd.args(["init", "--force"]).current_dir(temp.path()); cmd.assert() .success() .stdout(predicates::str::contains("\nAll done, no errors...\n"));