| 1 |
//! Tests for `mdbook init`. |
| 2 |
|
| 3 |
use crate::prelude::*; |
| 4 |
use mdbook_core::config::Config; |
| 5 |
use mdbook_driver::MDBook; |
| 6 |
use std::path::PathBuf; |
| 7 |
|
| 8 |
// Tests "init" with no args. |
| 9 |
#[test] |
| 10 |
fn basic_init() { |
| 11 |
let mut test = BookTest::empty(); |
| 12 |
test.run("init", |cmd| { |
| 13 |
cmd.expect_stdout(str![[r#" |
| 14 |
|
| 15 |
Do you want a .gitignore to be created? (y/n) |
| 16 |
What title would you like to give the book? |
| 17 |
|
| 18 |
All done, no errors... |
| 19 |
|
| 20 |
"#]]) |
| 21 |
.expect_stderr(str![[r#" |
| 22 |
INFO Creating a new book with stub content |
| 23 |
|
| 24 |
"#]]); |
| 25 |
}) |
| 26 |
.check_file( |
| 27 |
"book.toml", |
| 28 |
str![[r#" |
| 29 |
[book] |
| 30 |
authors = [] |
| 31 |
language = "en" |
| 32 |
|
| 33 |
"#]], |
| 34 |
) |
| 35 |
.check_file( |
| 36 |
"src/SUMMARY.md", |
| 37 |
str |
| 41 |
|
| 42 |
"#]], |
| 43 |
) |
| 44 |
.check_file( |
| 45 |
"src/chapter_1.md", |
| 46 |
str![[r#" |
| 47 |
# Chapter 1 |
| 48 |
|
| 49 |
"#]], |
| 50 |
) |
| 51 |
.check_main_file( |
| 52 |
"book/chapter_1.html", |
| 53 |
str![[r##"<h1 id="chapter-1"><a class="header" href="#chapter-1">Chapter 1</a></h1>"##]], |
| 54 |
); |
| 55 |
assert!(!test.dir.join(".gitignore").exists()); |
| 56 |
assert!(test.dir.join("book").exists()); |
| 57 |
} |
| 58 |
|
| 59 |
// Test init via API. This does a little less than the CLI does. |
| 60 |
#[test] |
| 61 |
fn init_api() { |
| 62 |
let mut test = BookTest::empty(); |
| 63 |
MDBook::init(&test.dir).build().unwrap(); |
| 64 |
test.check_file_list( |
| 65 |
".", |
| 66 |
str![[r#" |
| 67 |
book |
| 68 |
book.toml |
| 69 |
src |
| 70 |
src/SUMMARY.md |
| 71 |
src/chapter_1.md |
| 72 |
"#]], |
| 73 |
); |
| 74 |
} |
| 75 |
|
| 76 |
// Run `mdbook init` with `--force` to skip the confirmation prompts |
| 77 |
#[test] |
| 78 |
fn init_force() { |
| 79 |
let mut test = BookTest::empty(); |
| 80 |
test.run("init --force", |cmd| { |
| 81 |
cmd.expect_stdout(str![[r#" |
| 82 |
|
| 83 |
All done, no errors... |
| 84 |
|
| 85 |
"#]]) |
| 86 |
.expect_stderr(str![[r#" |
| 87 |
INFO Creating a new book with stub content |
| 88 |
|
| 89 |
"#]]); |
| 90 |
}) |
| 91 |
.check_file( |
| 92 |
"book.toml", |
| 93 |
str![[r#" |
| 94 |
[book] |
| 95 |
authors = [] |
| 96 |
language = "en" |
| 97 |
|
| 98 |
"#]], |
| 99 |
); |
| 100 |
assert!(!test.dir.join(".gitignore").exists()); |
| 101 |
} |
| 102 |
|
| 103 |
// Run `mdbook init` with `--title` without git config. |
| 104 |
// |
| 105 |
// Regression test for https://github.com/rust-lang/mdBook/issues/2485 |
| 106 |
#[test] |
| 107 |
fn no_git_config_with_title() { |
| 108 |
let mut test = BookTest::empty(); |
| 109 |
test.run("init", |cmd| { |
| 110 |
cmd.expect_stdout(str![[r#" |
| 111 |
|
| 112 |
Do you want a .gitignore to be created? (y/n) |
| 113 |
|
| 114 |
All done, no errors... |
| 115 |
|
| 116 |
"#]]) |
| 117 |
.expect_stderr(str![[r#" |
| 118 |
INFO Creating a new book with stub content |
| 119 |
|
| 120 |
"#]]) |
| 121 |
.args(&["--title", "Example title"]); |
| 122 |
}) |
| 123 |
.check_file( |
| 124 |
"book.toml", |
| 125 |
str![[r#" |
| 126 |
[book] |
| 127 |
title = "Example title" |
| 128 |
authors = [] |
| 129 |
language = "en" |
| 130 |
|
| 131 |
"#]], |
| 132 |
); |
| 133 |
assert!(!test.dir.join(".gitignore").exists()); |
| 134 |
} |
| 135 |
|
| 136 |
// Run `mdbook init` in a directory containing a SUMMARY.md should create the |
| 137 |
// files listed in the summary. |
| 138 |
#[test] |
| 139 |
fn init_from_summary() { |
| 140 |
BookTest::from_dir("init/init_from_summary") |
| 141 |
.run("init", |_| {}) |
| 142 |
.check_file( |
| 143 |
"src/intro.md", |
| 144 |
str![[r#" |
| 145 |
# intro |
| 146 |
|
| 147 |
"#]], |
| 148 |
) |
| 149 |
.check_file( |
| 150 |
"src/first.md", |
| 151 |
str![[r#" |
| 152 |
# First chapter |
| 153 |
|
| 154 |
"#]], |
| 155 |
) |
| 156 |
.check_file( |
| 157 |
"src/outro.md", |
| 158 |
str![[r#" |
| 159 |
# outro |
| 160 |
|
| 161 |
"#]], |
| 162 |
); |
| 163 |
} |
| 164 |
|
| 165 |
// Set some custom arguments for where to place the source and destination |
| 166 |
// files, then call `mdbook init`. |
| 167 |
#[test] |
| 168 |
fn init_with_custom_book_and_src_locations() { |
| 169 |
let mut test = BookTest::empty(); |
| 170 |
let mut cfg = Config::default(); |
| 171 |
cfg.book.src = PathBuf::from("in"); |
| 172 |
cfg.build.build_dir = PathBuf::from("out"); |
| 173 |
MDBook::init(&test.dir).with_config(cfg).build().unwrap(); |
| 174 |
test.check_file( |
| 175 |
"book.toml", |
| 176 |
str![[r#" |
| 177 |
[book] |
| 178 |
authors = [] |
| 179 |
src = "in" |
| 180 |
language = "en" |
| 181 |
|
| 182 |
[build] |
| 183 |
build-dir = "out" |
| 184 |
create-missing = true |
| 185 |
use-default-preprocessors = true |
| 186 |
extra-watch-dirs = [] |
| 187 |
|
| 188 |
"#]], |
| 189 |
) |
| 190 |
.check_file( |
| 191 |
"in/SUMMARY.md", |
| 192 |
str |
| 196 |
|
| 197 |
"#]], |
| 198 |
) |
| 199 |
.check_file( |
| 200 |
"in/chapter_1.md", |
| 201 |
str![[r#" |
| 202 |
# Chapter 1 |
| 203 |
|
| 204 |
"#]], |
| 205 |
); |
| 206 |
assert!(test.dir.join("out").exists()); |
| 207 |
} |
| 208 |
|
| 209 |
// Copies the theme into the initialized directory. |
| 210 |
#[test] |
| 211 |
fn copy_theme() { |
| 212 |
BookTest::empty() |
| 213 |
.run("init --theme", |_| {}) |
| 214 |
.check_file_list( |
| 215 |
".", |
| 216 |
str![[r#" |
| 217 |
book |
| 218 |
book.toml |
| 219 |
src |
| 220 |
src/SUMMARY.md |
| 221 |
src/chapter_1.md |
| 222 |
theme |
| 223 |
theme/book.js |
| 224 |
theme/css |
| 225 |
theme/css/chrome.css |
| 226 |
theme/css/general.css |
| 227 |
theme/css/print.css |
| 228 |
theme/css/variables.css |
| 229 |
theme/favicon.png |
| 230 |
theme/favicon.svg |
| 231 |
theme/fonts |
| 232 |
theme/fonts/OPEN-SANS-LICENSE.txt |
| 233 |
theme/fonts/SOURCE-CODE-PRO-LICENSE.txt |
| 234 |
theme/fonts/fonts.css |
| 235 |
theme/fonts/open-sans-v17-all-charsets-300.woff2 |
| 236 |
theme/fonts/open-sans-v17-all-charsets-300italic.woff2 |
| 237 |
theme/fonts/open-sans-v17-all-charsets-600.woff2 |
| 238 |
theme/fonts/open-sans-v17-all-charsets-600italic.woff2 |
| 239 |
theme/fonts/open-sans-v17-all-charsets-700.woff2 |
| 240 |
theme/fonts/open-sans-v17-all-charsets-700italic.woff2 |
| 241 |
theme/fonts/open-sans-v17-all-charsets-800.woff2 |
| 242 |
theme/fonts/open-sans-v17-all-charsets-800italic.woff2 |
| 243 |
theme/fonts/open-sans-v17-all-charsets-italic.woff2 |
| 244 |
theme/fonts/open-sans-v17-all-charsets-regular.woff2 |
| 245 |
theme/fonts/source-code-pro-v11-all-charsets-500.woff2 |
| 246 |
theme/highlight.css |
| 247 |
theme/highlight.js |
| 248 |
theme/index.hbs |
| 249 |
"#]], |
| 250 |
); |
| 251 |
} |