Skip to content

Add a few integration tests #2717

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions tests/testsuite/book_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -272,6 +272,7 @@ pub struct BookCommand {
expect_status: StatusCode,
expect_stderr_data: Option<snapbox::Data>,
expect_stdout_data: Option<snapbox::Data>,
stdin_data: Option<snapbox::Data>,
}

impl BookCommand {
Expand Down Expand Up @@ -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()));
Expand All @@ -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.
Expand All @@ -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");
Expand Down
34 changes: 34 additions & 0 deletions tests/testsuite/includes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,37 @@ fn rustdoc_include() {
}</code></pre></pre>
"##]]);
}

/// 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",
"<title>This is from title handler</title>",
)
.check_file_doesnt_contain(
"book/title_from_title_handler.html",
"<title>Title from summary - 1</title>",
)
.check_file_contains(
"book/title_from_summary.html",
"<title>Title from summary - 2</title>",
);
}

/// 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",
"<title>This is from title handler</title>",
)
// 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>Title from summary - 2 - Test Book Title</title>",
);
}
4 changes: 4 additions & 0 deletions tests/testsuite/includes/title_handler/src/SUMMARY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Summary

- [Title from summary - 1](./title_from_title_handler.md)
- [Title from summary - 2](./title_from_summary.md)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# This is markdown title - 2

Some content
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# This is markdown title - 1

{{#title This is from title handler}}

Some content

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[book]
title = "Test Book Title"
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Summary

- [Title from summary - 1](./title_from_title_handler.md)
- [Title from summary - 2](./title_from_summary.md)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# This is markdown title - 2

Some content
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# This is markdown title - 1

{{#title This is from title handler}}

Some content

151 changes: 151 additions & 0 deletions tests/testsuite/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -251,3 +284,121 @@ 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() {
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());
}

/// 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());
}

/// 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"

"#]],
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A placeholder file to test that `init` with an existing `theme` directory warns the user
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[user]
name = "mdBook gitconfig author"
Loading