From 0c580c32c47ab4d76eff27e4353dfe0d7e6ccb64 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Mon, 18 Nov 2024 11:08:26 -0800 Subject: [PATCH 1/2] Add regression test for mdbook init title with no git config Regression test for https://github.com/rust-lang/mdBook/issues/2485 --- tests/cli/init.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tests/cli/init.rs b/tests/cli/init.rs index 6bd1227437..6007673a81 100644 --- a/tests/cli/init.rs +++ b/tests/cli/init.rs @@ -22,3 +22,26 @@ fn base_mdbook_init_can_skip_confirmation_prompts() { assert!(!temp.path().join(".gitignore").exists()); } + +/// Run `mdbook init` with `--title` without git config. +/// +/// Regression test for https://github.com/rust-lang/mdBook/issues/2485 +#[test] +fn no_git_config_with_title() { + let temp = DummyBook::new().build().unwrap(); + + // doesn't exist before + assert!(!temp.path().join("book").exists()); + + let mut cmd = mdbook_cmd(); + cmd.args(["init", "--title", "Example title"]) + .current_dir(temp.path()) + .env("GIT_CONFIG_GLOBAL", "") + .env("GIT_CONFIG_NOSYSTEM", "1"); + 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); +} From 8f024dabc3d191b04c8a831b1e5b10c153111a8b Mon Sep 17 00:00:00 2001 From: eureka-cpu Date: Sun, 17 Nov 2024 03:02:01 -0800 Subject: [PATCH 2/2] fix init --title option failure when git user is not configured --- src/cmd/init.rs | 2 +- tests/cli/init.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cmd/init.rs b/src/cmd/init.rs index 2c6415b6d9..f15fb96865 100644 --- a/src/cmd/init.rs +++ b/src/cmd/init.rs @@ -74,9 +74,9 @@ pub fn execute(args: &ArgMatches) -> Result<()> { if let Some(author) = get_author_name() { debug!("Obtained user name from gitconfig: {:?}", author); config.book.authors.push(author); - builder.with_config(config); } + builder.with_config(config); builder.build()?; println!("\nAll done, no errors..."); diff --git a/tests/cli/init.rs b/tests/cli/init.rs index 6007673a81..51c2bfa389 100644 --- a/tests/cli/init.rs +++ b/tests/cli/init.rs @@ -43,5 +43,5 @@ fn no_git_config_with_title() { .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_eq!(config.book.title.as_deref(), Some("Example title")); }