From 5e6043548dbccc316ce0a49ac79615f0b06f19f6 Mon Sep 17 00:00:00 2001 From: Krishan Mistry Date: Wed, 28 May 2025 12:22:36 +0100 Subject: [PATCH 01/13] Add test for init with `--ignore git` passed --- tests/testsuite/init.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/testsuite/init.rs b/tests/testsuite/init.rs index e5989eb62c..14a37c1dc5 100644 --- a/tests/testsuite/init.rs +++ b/tests/testsuite/init.rs @@ -251,3 +251,25 @@ theme/index.hbs "#]], ); } + +/// Run `mdbook init` with `--ignore git` to create a `.gitignore` file +#[test] +fn init_with_ignore_git_creates_gitignore() { + let mut test = BookTest::empty(); + test.run("init --ignore git", |cmd| { + cmd.expect_stdout(str![[r#" +What title would you like to give the book? + +All done, no errors... + +"#]]); + }) + .check_file( + ".gitignore", + str![[r#" +book + +"#]], + ); + assert!(test.dir.join(".gitignore").exists()); +} From 153bbe8abbff799b54a1b7b1383463f482e350f3 Mon Sep 17 00:00:00 2001 From: Krishan Mistry Date: Wed, 28 May 2025 12:23:25 +0100 Subject: [PATCH 02/13] Add test for init with `--ignore none` passed --- tests/testsuite/init.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/testsuite/init.rs b/tests/testsuite/init.rs index 14a37c1dc5..854f17646d 100644 --- a/tests/testsuite/init.rs +++ b/tests/testsuite/init.rs @@ -273,3 +273,18 @@ book ); assert!(test.dir.join(".gitignore").exists()); } + +/// Run `mdbook init` with explicit `--ignore none` to not create a `.gitignore` file +#[test] +fn init_with_ignore_none() { + let mut test = BookTest::empty(); + test.run("init --ignore none", |cmd| { + cmd.expect_stdout(str![[r#" +What title would you like to give the book? + +All done, no errors... + +"#]]); + }); + assert!(!test.dir.join(".gitignore").exists()); +} From f63432cee7c619534af1ac213127a3e536efc8d9 Mon Sep 17 00:00:00 2001 From: Krishan Mistry Date: Wed, 28 May 2025 12:30:08 +0100 Subject: [PATCH 03/13] Add test for init where a git username is present --- tests/testsuite/init.rs | 20 +++++++++++++++++++ .../gitconfig_with_user_name.gitconfig | 2 ++ 2 files changed, 22 insertions(+) create mode 100644 tests/testsuite/init/init_with_gitconfig/gitconfig_with_user_name.gitconfig diff --git a/tests/testsuite/init.rs b/tests/testsuite/init.rs index 854f17646d..2a87faa121 100644 --- a/tests/testsuite/init.rs +++ b/tests/testsuite/init.rs @@ -288,3 +288,23 @@ All done, no errors... }); assert!(!test.dir.join(".gitignore").exists()); } + +/// Run `mdbook init` with a custom gitconfig file which contains an author name +#[test] +fn init_obtains_author_from_gitconfig() { + let mut test = BookTest::from_dir("init/init_with_gitconfig"); + test.run("init", |cmd| { + let git_config_path = cmd.dir.join("gitconfig_with_user_name.gitconfig"); + cmd.env("GIT_CONFIG_GLOBAL", git_config_path.to_str().unwrap()); + }) + .check_file( + "book.toml", + str![[r#" +[book] +authors = ["mdBook gitconfig author"] +language = "en" +src = "src" + +"#]], + ); +} diff --git a/tests/testsuite/init/init_with_gitconfig/gitconfig_with_user_name.gitconfig b/tests/testsuite/init/init_with_gitconfig/gitconfig_with_user_name.gitconfig new file mode 100644 index 0000000000..83b1ec8a20 --- /dev/null +++ b/tests/testsuite/init/init_with_gitconfig/gitconfig_with_user_name.gitconfig @@ -0,0 +1,2 @@ +[user] + name = "mdBook gitconfig author" \ No newline at end of file From 9d4a38cb6a77a5528b6a7e407757055908fc9a18 Mon Sep 17 00:00:00 2001 From: Krishan Mistry Date: Wed, 28 May 2025 12:41:24 +0100 Subject: [PATCH 04/13] Add option to pass in STDIN data for a BookTest --- tests/testsuite/book_test.rs | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/tests/testsuite/book_test.rs b/tests/testsuite/book_test.rs index 427c38d18a..18f47cbfc0 100644 --- a/tests/testsuite/book_test.rs +++ b/tests/testsuite/book_test.rs @@ -2,10 +2,9 @@ use mdbook::book::BookBuilder; use mdbook::MDBook; -use snapbox::IntoData; +use snapbox::{cmd::Command, IntoData}; use std::collections::BTreeMap; use std::path::{Path, PathBuf}; -use std::process::Command; use std::sync::atomic::{AtomicU32, Ordering}; /// Test number used for generating unique temp directory names. @@ -227,6 +226,7 @@ impl BookTest { expect_status: StatusCode::Success, expect_stderr_data: None, expect_stdout_data: None, + stdin_data: None, }; f(&mut cmd); cmd.run(); @@ -272,6 +272,7 @@ pub struct BookCommand { expect_status: StatusCode, expect_stderr_data: Option, expect_stdout_data: Option, + stdin_data: Option, } impl BookCommand { @@ -299,6 +300,12 @@ impl BookCommand { self } + /// Sets the stdin data to use when running the command. + pub fn stdin(&mut self, input: impl snapbox::IntoData) -> &mut Self { + self.stdin_data = Some(input.into_data()); + self + } + /// Adds arguments to the command to run. pub fn args(&mut self, args: &[&str]) -> &mut Self { self.args.extend(args.into_iter().map(|t| t.to_string())); @@ -314,7 +321,8 @@ impl BookCommand { /// Runs the command, and verifies the output. fn run(&mut self) { let mut cmd = Command::new(env!("CARGO_BIN_EXE_mdbook")); - cmd.current_dir(&self.dir) + cmd = cmd + .current_dir(&self.dir) .args(&self.args) .env_remove("RUST_LOG") // Don't read the system git config which is out of our control. @@ -328,11 +336,15 @@ impl BookCommand { for (k, v) in &self.env { match v { - Some(v) => cmd.env(k, v), - None => cmd.env_remove(k), + Some(v) => cmd = cmd.env(k, v), + None => cmd = cmd.env_remove(k), }; } + if let Some(stdin_data) = &self.stdin_data { + cmd = cmd.stdin(stdin_data); + } + let output = cmd.output().expect("mdbook should be runnable"); let stdout = std::str::from_utf8(&output.stdout).expect("stdout is not utf8"); let stderr = std::str::from_utf8(&output.stderr).expect("stderr is not utf8"); From 03c776882921668d3103c429f00c8afaf8d0e6f3 Mon Sep 17 00:00:00 2001 From: Krishan Mistry Date: Wed, 28 May 2025 12:44:04 +0100 Subject: [PATCH 05/13] Add test for init to verify prompt responses work --- tests/testsuite/init.rs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/tests/testsuite/init.rs b/tests/testsuite/init.rs index 2a87faa121..514dffc308 100644 --- a/tests/testsuite/init.rs +++ b/tests/testsuite/init.rs @@ -101,6 +101,39 @@ src = "src" assert!(!test.dir.join(".gitignore").exists()); } +// Run `mdbook init` and provide responses to the prompts to create a `.gitignore` file and set a book title. +#[test] +fn init_with_prompts() { + let mut test = BookTest::empty(); + test.run("init", |cmd| { + cmd.stdin("y\nMy Book Title\n") + .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" +title = "My Book Title" + +"#]], + ); + assert!(test.dir.join(".gitignore").exists()); +} + // Run `mdbook init` with `--title` without git config. // // Regression test for https://github.com/rust-lang/mdBook/issues/2485 From e49501fdb9a0dd5585035a8993560cc79f0aa9ba Mon Sep 17 00:00:00 2001 From: Krishan Mistry Date: Wed, 28 May 2025 12:47:27 +0100 Subject: [PATCH 06/13] Add test for init with theme when theme directory already exists --- tests/testsuite/init.rs | 61 +++++++++++++++++++ .../theme/placeholder_theme_file.md | 1 + 2 files changed, 62 insertions(+) create mode 100644 tests/testsuite/init/init_with_existing_theme/theme/placeholder_theme_file.md diff --git a/tests/testsuite/init.rs b/tests/testsuite/init.rs index 514dffc308..9bcaabf949 100644 --- a/tests/testsuite/init.rs +++ b/tests/testsuite/init.rs @@ -285,6 +285,67 @@ theme/index.hbs ); } +/// Runs `mdbook init --theme` in a directory which already contains a theme directory +#[test] +fn existing_theme() { + BookTest::from_dir("init/init_with_existing_theme") + .run("init --theme", |cmd| { + cmd.expect_stdout(str![[r#" + +Copying the default theme to [ROOT]/theme +This could potentially overwrite files already present in that directory. + +Are you sure you want to continue? (y/n) +All done, no errors... + +"#]]) + .expect_stderr(str![[r#" +[TIMESTAMP] [INFO] (mdbook::book::init): Creating a new book with stub content + +"#]]) + .stdin("y\n") + .args(&["--ignore", "none", "--title", "My Book Title"]); + }) + .check_file_list( + ".", + str![[r#" +book +book.toml +src +src/SUMMARY.md +src/chapter_1.md +theme +theme/book.js +theme/css +theme/css/chrome.css +theme/css/general.css +theme/css/print.css +theme/css/variables.css +theme/favicon.png +theme/favicon.svg +theme/fonts +theme/fonts/OPEN-SANS-LICENSE.txt +theme/fonts/SOURCE-CODE-PRO-LICENSE.txt +theme/fonts/fonts.css +theme/fonts/open-sans-v17-all-charsets-300.woff2 +theme/fonts/open-sans-v17-all-charsets-300italic.woff2 +theme/fonts/open-sans-v17-all-charsets-600.woff2 +theme/fonts/open-sans-v17-all-charsets-600italic.woff2 +theme/fonts/open-sans-v17-all-charsets-700.woff2 +theme/fonts/open-sans-v17-all-charsets-700italic.woff2 +theme/fonts/open-sans-v17-all-charsets-800.woff2 +theme/fonts/open-sans-v17-all-charsets-800italic.woff2 +theme/fonts/open-sans-v17-all-charsets-italic.woff2 +theme/fonts/open-sans-v17-all-charsets-regular.woff2 +theme/fonts/source-code-pro-v11-all-charsets-500.woff2 +theme/highlight.css +theme/highlight.js +theme/index.hbs +theme/placeholder_theme_file.md +"#]], + ); +} + /// Run `mdbook init` with `--ignore git` to create a `.gitignore` file #[test] fn init_with_ignore_git_creates_gitignore() { diff --git a/tests/testsuite/init/init_with_existing_theme/theme/placeholder_theme_file.md b/tests/testsuite/init/init_with_existing_theme/theme/placeholder_theme_file.md new file mode 100644 index 0000000000..ba4c70ff88 --- /dev/null +++ b/tests/testsuite/init/init_with_existing_theme/theme/placeholder_theme_file.md @@ -0,0 +1 @@ +A placeholder file to test that `init` with an existing `theme` directory warns the user \ No newline at end of file From 477aa63ea7f4ac5dea5b47d0085e32723298c6cd Mon Sep 17 00:00:00 2001 From: Krishan Mistry Date: Wed, 28 May 2025 17:16:11 +0100 Subject: [PATCH 07/13] Add tests for `{{#title}}` include --- tests/testsuite/includes.rs | 34 +++++++++++++++++++ .../includes/title_handler/src/SUMMARY.md | 4 +++ .../title_handler/src/title_from_summary.md | 3 ++ .../src/title_from_title_handler.md | 6 ++++ .../title_handler_with_book_title/book.toml | 2 ++ .../src/SUMMARY.md | 4 +++ .../src/title_from_summary.md | 3 ++ .../src/title_from_title_handler.md | 6 ++++ 8 files changed, 62 insertions(+) create mode 100644 tests/testsuite/includes/title_handler/src/SUMMARY.md create mode 100644 tests/testsuite/includes/title_handler/src/title_from_summary.md create mode 100644 tests/testsuite/includes/title_handler/src/title_from_title_handler.md create mode 100644 tests/testsuite/includes/title_handler_with_book_title/book.toml create mode 100644 tests/testsuite/includes/title_handler_with_book_title/src/SUMMARY.md create mode 100644 tests/testsuite/includes/title_handler_with_book_title/src/title_from_summary.md create mode 100644 tests/testsuite/includes/title_handler_with_book_title/src/title_from_title_handler.md diff --git a/tests/testsuite/includes.rs b/tests/testsuite/includes.rs index 312caede8a..8164b3190f 100644 --- a/tests/testsuite/includes.rs +++ b/tests/testsuite/includes.rs @@ -110,3 +110,37 @@ fn rustdoc_include() { } "##]]); } + +/// Checks the behavior of `{{#title}}` include. +#[test] +fn test_title_include() { + BookTest::from_dir("includes/title_handler") + .check_file_contains( + "book/title_from_title_handler.html", + "This is from title handler", + ) + .check_file_doesnt_contain( + "book/title_from_title_handler.html", + "Title from summary - 1", + ) + .check_file_contains( + "book/title_from_summary.html", + "Title from summary - 2", + ); +} + +/// Checks the behavior of `{{#title}}` include when a book title is present. +#[test] +fn test_title_include_with_book_title() { + BookTest::from_dir("includes/title_handler_with_book_title") + // Checks that the title from the title handler is used when present + .check_file_contains( + "book/title_from_title_handler.html", + "This is from title handler", + ) + // Checks that the title from the summary appended with the book title is used when no title handler is present + .check_file_contains( + "book/title_from_summary.html", + "Title from summary - 2 - Test Book Title", + ); +} diff --git a/tests/testsuite/includes/title_handler/src/SUMMARY.md b/tests/testsuite/includes/title_handler/src/SUMMARY.md new file mode 100644 index 0000000000..6c4890495f --- /dev/null +++ b/tests/testsuite/includes/title_handler/src/SUMMARY.md @@ -0,0 +1,4 @@ +# Summary + +- [Title from summary - 1](./title_from_title_handler.md) +- [Title from summary - 2](./title_from_summary.md) diff --git a/tests/testsuite/includes/title_handler/src/title_from_summary.md b/tests/testsuite/includes/title_handler/src/title_from_summary.md new file mode 100644 index 0000000000..f83eba49d1 --- /dev/null +++ b/tests/testsuite/includes/title_handler/src/title_from_summary.md @@ -0,0 +1,3 @@ +# This is markdown title - 2 + +Some content diff --git a/tests/testsuite/includes/title_handler/src/title_from_title_handler.md b/tests/testsuite/includes/title_handler/src/title_from_title_handler.md new file mode 100644 index 0000000000..8baa3c4b38 --- /dev/null +++ b/tests/testsuite/includes/title_handler/src/title_from_title_handler.md @@ -0,0 +1,6 @@ +# This is markdown title - 1 + +{{#title This is from title handler}} + +Some content + diff --git a/tests/testsuite/includes/title_handler_with_book_title/book.toml b/tests/testsuite/includes/title_handler_with_book_title/book.toml new file mode 100644 index 0000000000..06b091f755 --- /dev/null +++ b/tests/testsuite/includes/title_handler_with_book_title/book.toml @@ -0,0 +1,2 @@ +[book] +title = "Test Book Title" diff --git a/tests/testsuite/includes/title_handler_with_book_title/src/SUMMARY.md b/tests/testsuite/includes/title_handler_with_book_title/src/SUMMARY.md new file mode 100644 index 0000000000..6c4890495f --- /dev/null +++ b/tests/testsuite/includes/title_handler_with_book_title/src/SUMMARY.md @@ -0,0 +1,4 @@ +# Summary + +- [Title from summary - 1](./title_from_title_handler.md) +- [Title from summary - 2](./title_from_summary.md) diff --git a/tests/testsuite/includes/title_handler_with_book_title/src/title_from_summary.md b/tests/testsuite/includes/title_handler_with_book_title/src/title_from_summary.md new file mode 100644 index 0000000000..f83eba49d1 --- /dev/null +++ b/tests/testsuite/includes/title_handler_with_book_title/src/title_from_summary.md @@ -0,0 +1,3 @@ +# This is markdown title - 2 + +Some content diff --git a/tests/testsuite/includes/title_handler_with_book_title/src/title_from_title_handler.md b/tests/testsuite/includes/title_handler_with_book_title/src/title_from_title_handler.md new file mode 100644 index 0000000000..8baa3c4b38 --- /dev/null +++ b/tests/testsuite/includes/title_handler_with_book_title/src/title_from_title_handler.md @@ -0,0 +1,6 @@ +# This is markdown title - 1 + +{{#title This is from title handler}} + +Some content + From 81d5ff717757710631f2c481c3b4c07071851b2e Mon Sep 17 00:00:00 2001 From: Krishan Mistry Date: Thu, 29 May 2025 10:29:31 +0100 Subject: [PATCH 08/13] Add test for default contents of 404 file --- tests/testsuite/renderer.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/testsuite/renderer.rs b/tests/testsuite/renderer.rs index 1e1624474f..29794aff96 100644 --- a/tests/testsuite/renderer.rs +++ b/tests/testsuite/renderer.rs @@ -262,3 +262,12 @@ fn legacy_relative_command_path() { }) .check_file("book/output", "test"); } + +/// Render 404 defaults to standard content. +#[test] +fn render_404_default() { + BookTest::from_dir("build/basic_build").check_main_file("book/404.html", str![[r##" +

Document not found (404)

+

This URL is invalid, sorry. Please use the navigation bar or search to continue.

+"##]]); +} From ab0878503e93a4ff7d8696e5da016edc2069dbbe Mon Sep 17 00:00:00 2001 From: Krishan Mistry Date: Thu, 29 May 2025 10:31:43 +0100 Subject: [PATCH 09/13] Add test for contents of 404 file from 404.md --- tests/testsuite/renderer.rs | 9 +++++++++ .../testsuite/renderer/render_404/use_404_md/src/404.md | 1 + .../renderer/render_404/use_404_md/src/SUMMARY.md | 0 3 files changed, 10 insertions(+) create mode 100644 tests/testsuite/renderer/render_404/use_404_md/src/404.md create mode 100644 tests/testsuite/renderer/render_404/use_404_md/src/SUMMARY.md diff --git a/tests/testsuite/renderer.rs b/tests/testsuite/renderer.rs index 29794aff96..fb7b9669c5 100644 --- a/tests/testsuite/renderer.rs +++ b/tests/testsuite/renderer.rs @@ -271,3 +271,12 @@ fn render_404_default() {

This URL is invalid, sorry. Please use the navigation bar or search to continue.

"##]]); } + +/// Render 404 with custom content provided in `404.md`. +#[test] +fn render_404_with_custom_content() { + BookTest::from_dir("renderer/render_404/use_404_md").check_main_file( + "book/404.html", + str!["

I'm using contents in 404.md!

"], + ); +} diff --git a/tests/testsuite/renderer/render_404/use_404_md/src/404.md b/tests/testsuite/renderer/render_404/use_404_md/src/404.md new file mode 100644 index 0000000000..0aa60167df --- /dev/null +++ b/tests/testsuite/renderer/render_404/use_404_md/src/404.md @@ -0,0 +1 @@ +I'm using contents in 404.md! \ No newline at end of file diff --git a/tests/testsuite/renderer/render_404/use_404_md/src/SUMMARY.md b/tests/testsuite/renderer/render_404/use_404_md/src/SUMMARY.md new file mode 100644 index 0000000000..e69de29bb2 From 8357fcafa0755c52d700a4f0846552070b07e284 Mon Sep 17 00:00:00 2001 From: Krishan Mistry Date: Thu, 29 May 2025 10:35:07 +0100 Subject: [PATCH 10/13] Add test for 404 file configured via `output.html.input-404` --- tests/testsuite/renderer.rs | 15 +++++++++++++++ .../render_404/explicitly_defined/book.toml | 2 ++ .../render_404/explicitly_defined/src/404.md | 1 + .../render_404/explicitly_defined/src/SUMMARY.md | 0 .../explicitly_defined/src/custom_404_filename.md | 1 + 5 files changed, 19 insertions(+) create mode 100644 tests/testsuite/renderer/render_404/explicitly_defined/book.toml create mode 100644 tests/testsuite/renderer/render_404/explicitly_defined/src/404.md create mode 100644 tests/testsuite/renderer/render_404/explicitly_defined/src/SUMMARY.md create mode 100644 tests/testsuite/renderer/render_404/explicitly_defined/src/custom_404_filename.md diff --git a/tests/testsuite/renderer.rs b/tests/testsuite/renderer.rs index fb7b9669c5..38ed4a9adc 100644 --- a/tests/testsuite/renderer.rs +++ b/tests/testsuite/renderer.rs @@ -280,3 +280,18 @@ fn render_404_with_custom_content() { str!["

I'm using contents in 404.md!

"], ); } + +/// Render a 404 error page with custom content provided by the file declared by output.html.input-404 in `book.toml`. +/// This produces a 404 error page with the custom file name provided in the `input-404` field. +#[test] +fn render_404_from_custom_declared_file() { + let mut test = BookTest::from_dir("renderer/render_404/explicitly_defined"); + test.check_main_file( + "book/custom_404_filename.html", + str!["

This is my 404 content in custom_404_filename.md

"], + ); + + // Check that the custom 404.md file present is not used or copied to the book output directory. + assert!(test.dir.join("src/404.md").exists()); + assert!(!test.dir.join("book/404.html").exists()); +} diff --git a/tests/testsuite/renderer/render_404/explicitly_defined/book.toml b/tests/testsuite/renderer/render_404/explicitly_defined/book.toml new file mode 100644 index 0000000000..9808467b13 --- /dev/null +++ b/tests/testsuite/renderer/render_404/explicitly_defined/book.toml @@ -0,0 +1,2 @@ +[output.html] +input-404 = "custom_404_filename.md" \ No newline at end of file diff --git a/tests/testsuite/renderer/render_404/explicitly_defined/src/404.md b/tests/testsuite/renderer/render_404/explicitly_defined/src/404.md new file mode 100644 index 0000000000..0aa60167df --- /dev/null +++ b/tests/testsuite/renderer/render_404/explicitly_defined/src/404.md @@ -0,0 +1 @@ +I'm using contents in 404.md! \ No newline at end of file diff --git a/tests/testsuite/renderer/render_404/explicitly_defined/src/SUMMARY.md b/tests/testsuite/renderer/render_404/explicitly_defined/src/SUMMARY.md new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/testsuite/renderer/render_404/explicitly_defined/src/custom_404_filename.md b/tests/testsuite/renderer/render_404/explicitly_defined/src/custom_404_filename.md new file mode 100644 index 0000000000..0182b990af --- /dev/null +++ b/tests/testsuite/renderer/render_404/explicitly_defined/src/custom_404_filename.md @@ -0,0 +1 @@ +This is my 404 content in custom_404_filename.md \ No newline at end of file From 2188f30aefe0989226ba7fcb72c8acd5e354eb32 Mon Sep 17 00:00:00 2001 From: Krishan Mistry Date: Thu, 29 May 2025 10:48:01 +0100 Subject: [PATCH 11/13] Add test for configuring `output.html.input-404` to a file which doesn't exist --- tests/testsuite/renderer.rs | 7 +++++++ .../explicitly_defined_doesnt_exist_panics/book.toml | 2 ++ .../explicitly_defined_doesnt_exist_panics/src/SUMMARY.md | 0 3 files changed, 9 insertions(+) create mode 100644 tests/testsuite/renderer/render_404/explicitly_defined_doesnt_exist_panics/book.toml create mode 100644 tests/testsuite/renderer/render_404/explicitly_defined_doesnt_exist_panics/src/SUMMARY.md diff --git a/tests/testsuite/renderer.rs b/tests/testsuite/renderer.rs index 38ed4a9adc..f820ad3d5b 100644 --- a/tests/testsuite/renderer.rs +++ b/tests/testsuite/renderer.rs @@ -295,3 +295,10 @@ fn render_404_from_custom_declared_file() { assert!(test.dir.join("src/404.md").exists()); assert!(!test.dir.join("book/404.html").exists()); } + +/// Declaring a file which doesn't exist via output.html.input-404 in `book.toml` causes a panic +#[test] +#[should_panic(expected = "unable to open 404 input file")] +fn render_404_from_custom_declared_file_which_doesnt_exist_fails() { + BookTest::from_dir("renderer/render_404/explicitly_defined_doesnt_exist_panics").build(); +} diff --git a/tests/testsuite/renderer/render_404/explicitly_defined_doesnt_exist_panics/book.toml b/tests/testsuite/renderer/render_404/explicitly_defined_doesnt_exist_panics/book.toml new file mode 100644 index 0000000000..fa55288d8e --- /dev/null +++ b/tests/testsuite/renderer/render_404/explicitly_defined_doesnt_exist_panics/book.toml @@ -0,0 +1,2 @@ +[output.html] +input-404 = "custom_404_filename_which_doesnt_exist.md" \ No newline at end of file diff --git a/tests/testsuite/renderer/render_404/explicitly_defined_doesnt_exist_panics/src/SUMMARY.md b/tests/testsuite/renderer/render_404/explicitly_defined_doesnt_exist_panics/src/SUMMARY.md new file mode 100644 index 0000000000..e69de29bb2 From 74544f0ced52ddd6eb9d5ca87ac49ddaf23eeba8 Mon Sep 17 00:00:00 2001 From: Krishan Mistry Date: Thu, 29 May 2025 12:11:00 +0100 Subject: [PATCH 12/13] Add toc fold tests --- tests/testsuite/toc.rs | 64 +++++++++++++++++ .../fold_tests_expected/fold_disabled.html | 62 ++++++++++++++++ .../fold_enabled_level_0.html | 71 +++++++++++++++++++ .../fold_enabled_level_1.html | 71 +++++++++++++++++++ .../fold_enabled_level_2.html | 71 +++++++++++++++++++ .../fold_enabled_level_3.html | 71 +++++++++++++++++++ 6 files changed, 410 insertions(+) create mode 100644 tests/testsuite/toc/fold_tests_expected/fold_disabled.html create mode 100644 tests/testsuite/toc/fold_tests_expected/fold_enabled_level_0.html create mode 100644 tests/testsuite/toc/fold_tests_expected/fold_enabled_level_1.html create mode 100644 tests/testsuite/toc/fold_tests_expected/fold_enabled_level_2.html create mode 100644 tests/testsuite/toc/fold_tests_expected/fold_enabled_level_3.html diff --git a/tests/testsuite/toc.rs b/tests/testsuite/toc.rs index eafbdc4857..3089052e89 100644 --- a/tests/testsuite/toc.rs +++ b/tests/testsuite/toc.rs @@ -5,6 +5,7 @@ use anyhow::Context; use mdbook::errors::*; use select::document::Document; use select::predicate::{Attr, Class, Name, Predicate}; +use snapbox::{file, IntoData}; use std::fs; const TOC_TOP_LEVEL: &[&str] = &[ @@ -185,3 +186,66 @@ fn summary_with_markdown_formatting() { "#]], ); } + +/// Helper function to test the table of contents with different fold settings +fn toc_folds_helper(fold_enable: bool, fold_level: usize, expected: impl IntoData) { + BookTest::from_dir("toc/basic_toc") + .change_file( + "book.toml", + format!( + r##" +[output.html.fold] +enable = {} +level = {} +"##, + fold_enable, fold_level + ) + .as_str(), + ) + .build() + .check_toc_js(expected); +} + +#[test] +fn toc_folds_disabled() { + // Folds disabled from level 0 + toc_folds_helper( + false, + 0, + file!["toc/fold_tests_expected/fold_disabled.html"], + ); + // Folds disabled from any level should be the same as level 0 + toc_folds_helper( + false, + 2, + file!["toc/fold_tests_expected/fold_disabled.html"], + ); +} + +#[test] +fn toc_folds_enabled() { + // Folds enabled from level 0 + toc_folds_helper( + true, + 0, + file!["toc/fold_tests_expected/fold_enabled_level_0.html"], + ); + // Folds enabled from level 1 + toc_folds_helper( + true, + 1, + file!["toc/fold_tests_expected/fold_enabled_level_1.html"], + ); + // Folds enabled from level 2 + toc_folds_helper( + true, + 2, + file!["toc/fold_tests_expected/fold_enabled_level_2.html"], + ); + // Folds enabled from level 3 + toc_folds_helper( + true, + 3, + file!["toc/fold_tests_expected/fold_enabled_level_3.html"], + ); +} diff --git a/tests/testsuite/toc/fold_tests_expected/fold_disabled.html b/tests/testsuite/toc/fold_tests_expected/fold_disabled.html new file mode 100644 index 0000000000..57e8ee2f10 --- /dev/null +++ b/tests/testsuite/toc/fold_tests_expected/fold_disabled.html @@ -0,0 +1,62 @@ +
    +
  1. +Prefix 1 +
  2. +
  3. +Prefix 2 +
  4. +
  5. + + With Readme +
  6. +
  7. +
      +
    1. + + Nested Index +
    2. +
    3. + + Nested two +
    4. +
    +
  8. +
  9. +
    + Draft
    +
  10. +
  11. +
  12. +
  13. +
  14. +
  15. Deep Nest
  16. +
  17. + + Deep Nest 1 +
  18. +
  19. +
      +
    1. + + Deep Nest 2 +
    2. +
    3. +
        +
      1. + + Deep Nest 3 +
      2. +
      +
    4. +
    +
  20. +
  21. +
  22. +
  23. +
  24. +Suffix 1 +
  25. +
  26. +Suffix 2 +
  27. +
\ No newline at end of file diff --git a/tests/testsuite/toc/fold_tests_expected/fold_enabled_level_0.html b/tests/testsuite/toc/fold_tests_expected/fold_enabled_level_0.html new file mode 100644 index 0000000000..c950719b2c --- /dev/null +++ b/tests/testsuite/toc/fold_tests_expected/fold_enabled_level_0.html @@ -0,0 +1,71 @@ +
    +
  1. +Prefix 1 +
  2. +
  3. +Prefix 2 +
  4. +
  5. + + With Readme + +
    +
    +
  6. +
  7. +
      +
    1. + + Nested Index +
    2. +
    3. + + Nested two +
    4. +
    +
  8. +
  9. +
    + Draft
    +
  10. +
  11. +
  12. +
  13. +
  14. +
  15. Deep Nest
  16. +
  17. + + Deep Nest 1 + +
    +
    +
  18. +
  19. +
      +
    1. + + Deep Nest 2 + +
      +
      +
    2. +
    3. +
        +
      1. + + Deep Nest 3 +
      2. +
      +
    4. +
    +
  20. +
  21. +
  22. +
  23. +
  24. +Suffix 1 +
  25. +
  26. +Suffix 2 +
  27. +
\ No newline at end of file diff --git a/tests/testsuite/toc/fold_tests_expected/fold_enabled_level_1.html b/tests/testsuite/toc/fold_tests_expected/fold_enabled_level_1.html new file mode 100644 index 0000000000..c5acd1fdda --- /dev/null +++ b/tests/testsuite/toc/fold_tests_expected/fold_enabled_level_1.html @@ -0,0 +1,71 @@ +
    +
  1. +Prefix 1 +
  2. +
  3. +Prefix 2 +
  4. +
  5. + + With Readme + +
    +
    +
  6. +
  7. +
      +
    1. + + Nested Index +
    2. +
    3. + + Nested two +
    4. +
    +
  8. +
  9. +
    + Draft
    +
  10. +
  11. +
  12. +
  13. +
  14. +
  15. Deep Nest
  16. +
  17. + + Deep Nest 1 + +
    +
    +
  18. +
  19. +
      +
    1. + + Deep Nest 2 + +
      +
      +
    2. +
    3. +
        +
      1. + + Deep Nest 3 +
      2. +
      +
    4. +
    +
  20. +
  21. +
  22. +
  23. +
  24. +Suffix 1 +
  25. +
  26. +Suffix 2 +
  27. +
\ No newline at end of file diff --git a/tests/testsuite/toc/fold_tests_expected/fold_enabled_level_2.html b/tests/testsuite/toc/fold_tests_expected/fold_enabled_level_2.html new file mode 100644 index 0000000000..a864934e2b --- /dev/null +++ b/tests/testsuite/toc/fold_tests_expected/fold_enabled_level_2.html @@ -0,0 +1,71 @@ +
    +
  1. +Prefix 1 +
  2. +
  3. +Prefix 2 +
  4. +
  5. + + With Readme + +
    +
    +
  6. +
  7. +
      +
    1. + + Nested Index +
    2. +
    3. + + Nested two +
    4. +
    +
  8. +
  9. +
    + Draft
    +
  10. +
  11. +
  12. +
  13. +
  14. +
  15. Deep Nest
  16. +
  17. + + Deep Nest 1 + +
    +
    +
  18. +
  19. +
      +
    1. + + Deep Nest 2 + +
      +
      +
    2. +
    3. +
        +
      1. + + Deep Nest 3 +
      2. +
      +
    4. +
    +
  20. +
  21. +
  22. +
  23. +
  24. +Suffix 1 +
  25. +
  26. +Suffix 2 +
  27. +
\ No newline at end of file diff --git a/tests/testsuite/toc/fold_tests_expected/fold_enabled_level_3.html b/tests/testsuite/toc/fold_tests_expected/fold_enabled_level_3.html new file mode 100644 index 0000000000..30a35f119c --- /dev/null +++ b/tests/testsuite/toc/fold_tests_expected/fold_enabled_level_3.html @@ -0,0 +1,71 @@ +
    +
  1. +Prefix 1 +
  2. +
  3. +Prefix 2 +
  4. +
  5. + + With Readme + +
    +
    +
  6. +
  7. +
      +
    1. + + Nested Index +
    2. +
    3. + + Nested two +
    4. +
    +
  8. +
  9. +
    + Draft
    +
  10. +
  11. +
  12. +
  13. +
  14. +
  15. Deep Nest
  16. +
  17. + + Deep Nest 1 + +
    +
    +
  18. +
  19. +
      +
    1. + + Deep Nest 2 + +
      +
      +
    2. +
    3. +
        +
      1. + + Deep Nest 3 +
      2. +
      +
    4. +
    +
  20. +
  21. +
  22. +
  23. +
  24. +Suffix 1 +
  25. +
  26. +Suffix 2 +
  27. +
\ No newline at end of file From 0ecba259f831c92b426c33828b4bb690e312fb7c Mon Sep 17 00:00:00 2001 From: Krishan Mistry Date: Thu, 29 May 2025 13:44:54 +0100 Subject: [PATCH 13/13] Add test to check that renderer utilizes PATH --- tests/testsuite/renderer.rs | 44 +++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/tests/testsuite/renderer.rs b/tests/testsuite/renderer.rs index f820ad3d5b..a087f4f1c6 100644 --- a/tests/testsuite/renderer.rs +++ b/tests/testsuite/renderer.rs @@ -4,6 +4,7 @@ use crate::prelude::*; use mdbook::errors::Result; use mdbook::renderer::{RenderContext, Renderer}; use snapbox::IntoData; +use std::env; use std::fs::File; use std::sync::{Arc, Mutex}; @@ -75,6 +76,49 @@ fn failing_command() { }); } +// Test that renderer utilizes PATH to find mdbook-FOO executables. +#[test] +fn renderer_utilizes_path() { + let mut test = BookTest::init(|_| {}); + test.rust_program( + "custom_directory/mdbook-in_path", + r#" + fn main() { + // Read from stdin to avoid random pipe failures on Linux. + use std::io::Read; + let mut s = String::new(); + std::io::stdin().read_to_string(&mut s).unwrap(); + println!("Hello World!"); + } + "#, + ) + .change_file( + "book.toml", + "[output.in_path]\n\ +", + ); + let custom_directory_location = test.dir.join("custom_directory"); + + test.run("build", |cmd| { + let current_path = env::var("PATH").unwrap_or_default(); + let mut paths = env::split_paths(¤t_path).collect::>(); + paths.insert(0, custom_directory_location.clone()); + let new_path = env::join_paths(paths).unwrap(); + cmd.env("PATH", new_path.into_string().unwrap()); + + cmd.expect_stdout(str![[r#" +Hello World! + +"#]]) + .expect_stderr(str![[r#" +[TIMESTAMP] [INFO] (mdbook::book): Book building has started +[TIMESTAMP] [INFO] (mdbook::book): Running the in_path backend +[TIMESTAMP] [INFO] (mdbook::renderer): Invoking the "in_path" renderer + +"#]]); + }); +} + // Renderer command is missing. #[test] fn missing_renderer() {