From 6fdd7b4a174498d977c43194e3821dd4b72542c5 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Mon, 21 Apr 2025 18:56:10 -0700 Subject: [PATCH] Migrate base_mdbook_init_should_create_default_content to BookTest --- tests/init.rs | 26 --------------- tests/testsuite/init.rs | 73 +++++++++++++++++++++++++++++++++++++++++ tests/testsuite/main.rs | 1 + 3 files changed, 74 insertions(+), 26 deletions(-) create mode 100644 tests/testsuite/init.rs diff --git a/tests/init.rs b/tests/init.rs index 9b2d8ea5..4b6ee5fa 100644 --- a/tests/init.rs +++ b/tests/init.rs @@ -7,32 +7,6 @@ use std::io::prelude::*; use std::path::PathBuf; use tempfile::Builder as TempFileBuilder; -/// Run `mdbook init` in an empty directory and make sure the default files -/// are created. -#[test] -fn base_mdbook_init_should_create_default_content() { - let created_files = vec!["book", "src", "src/SUMMARY.md", "src/chapter_1.md"]; - - let temp = TempFileBuilder::new().prefix("mdbook").tempdir().unwrap(); - for file in &created_files { - assert!(!temp.path().join(file).exists()); - } - - MDBook::init(temp.path()).build().unwrap(); - - for file in &created_files { - let target = temp.path().join(file); - println!("{}", target.display()); - assert!(target.exists(), "{file} doesn't exist"); - } - - let contents = fs::read_to_string(temp.path().join("book.toml")).unwrap(); - assert_eq!( - contents, - "[book]\nauthors = []\nlanguage = \"en\"\nsrc = \"src\"\n" - ); -} - /// Run `mdbook init` in a directory containing a SUMMARY.md should create the /// files listed in the summary. #[test] diff --git a/tests/testsuite/init.rs b/tests/testsuite/init.rs new file mode 100644 index 00000000..9e185184 --- /dev/null +++ b/tests/testsuite/init.rs @@ -0,0 +1,73 @@ +//! Tests for `mdbook init`. + +use crate::prelude::*; +use mdbook::MDBook; + +// Tests "init" with no args. +#[test] +fn basic_init() { + let mut test = BookTest::empty(); + test.run("init", |cmd| { + cmd.expect_stdout(str![[r#" + +Do you want a .gitignore to be created? (y/n) +What title would you like to give the book? + +All done, no errors... + +"#]]) + .expect_stderr(str![[r#" +[TIMESTAMP] [INFO] (mdbook::book::init): Creating a new book with stub content + +"#]]); + }) + .check_file( + "book.toml", + str![[r#" +[book] +authors = [] +language = "en" +src = "src" + +"#]], + ) + .check_file( + "src/SUMMARY.md", + str![[r#" +# Summary + +- [Chapter 1](./chapter_1.md) + +"#]], + ) + .check_file( + "src/chapter_1.md", + str![[r#" +# Chapter 1 + +"#]], + ) + .check_main_file( + "book/chapter_1.html", + str![[r##"

Chapter 1

"##]], + ); + assert!(!test.dir.join(".gitignore").exists()); + assert!(test.dir.join("book").exists()); +} + +// Test init via API. This does a little less than the CLI does. +#[test] +fn init_api() { + let mut test = BookTest::empty(); + MDBook::init(&test.dir).build().unwrap(); + test.check_file_list( + ".", + str![[r#" +book +book.toml +src +src/SUMMARY.md +src/chapter_1.md +"#]], + ); +} diff --git a/tests/testsuite/main.rs b/tests/testsuite/main.rs index 6141c4f7..59d9e28a 100644 --- a/tests/testsuite/main.rs +++ b/tests/testsuite/main.rs @@ -7,6 +7,7 @@ mod build; mod cli; mod includes; mod index; +mod init; mod prelude { pub use crate::book_test::BookTest;