| 1 |
//! Tests for book configuration loading. |
| 2 |
|
| 3 |
use crate::prelude::*; |
| 4 |
|
| 5 |
// Test that config can load from environment variable. |
| 6 |
#[test] |
| 7 |
fn config_from_env() { |
| 8 |
BookTest::from_dir("config/empty") |
| 9 |
.run("build", |cmd| { |
| 10 |
cmd.env("MDBOOK_BOOK__TITLE", "Custom env title"); |
| 11 |
}) |
| 12 |
.check_file_contains( |
| 13 |
"book/index.html", |
| 14 |
"<title>Chapter 1 - Custom env title</title>", |
| 15 |
); |
| 16 |
|
| 17 |
// json for some subtable |
| 18 |
// |
| 19 |
} |
| 20 |
|
| 21 |
// Test environment config with JSON. |
| 22 |
#[test] |
| 23 |
fn config_json_from_env() { |
| 24 |
// build table |
| 25 |
BookTest::from_dir("config/empty") |
| 26 |
.run("build", |cmd| { |
| 27 |
cmd.env( |
| 28 |
"MDBOOK_BOOK", |
| 29 |
r#"{"title": "My Awesome Book", "authors": ["Michael-F-Bryan"]}"#, |
| 30 |
); |
| 31 |
}) |
| 32 |
.check_file_contains( |
| 33 |
"book/index.html", |
| 34 |
"<title>Chapter 1 - My Awesome Book</title>", |
| 35 |
); |
| 36 |
|
| 37 |
// book table |
| 38 |
BookTest::from_dir("config/empty") |
| 39 |
.run("build", |cmd| { |
| 40 |
cmd.env("MDBOOK_BUILD", r#"{"build-dir": "alt"}"#); |
| 41 |
}) |
| 42 |
.check_file_contains("alt/index.html", "<title>Chapter 1</title>"); |
| 43 |
} |
| 44 |
|
| 45 |
// Test that a preprocessor receives config set in the environment. |
| 46 |
#[test] |
| 47 |
fn preprocessor_cfg_from_env() { |
| 48 |
let mut test = BookTest::from_dir("config/empty"); |
| 49 |
test.rust_program( |
| 50 |
"cat-to-file", |
| 51 |
r#" |
| 52 |
fn main() { |
| 53 |
use std::io::Read; |
| 54 |
let mut s = String::new(); |
| 55 |
std::io::stdin().read_to_string(&mut s).unwrap(); |
| 56 |
std::fs::write("out.txt", s).unwrap(); |
| 57 |
println!("{{\"items\": []}}"); |
| 58 |
} |
| 59 |
"#, |
| 60 |
) |
| 61 |
.run("build", |cmd| { |
| 62 |
cmd.env( |
| 63 |
"MDBOOK_PREPROCESSOR__CAT_TO_FILE", |
| 64 |
r#"{"command":"./cat-to-file", "array": [1,2,3], "number": 123}"#, |
| 65 |
); |
| 66 |
}); |
| 67 |
let out = read_to_string(test.dir.join("out.txt")); |
| 68 |
let (ctx, _book) = mdbook_preprocessor::parse_input(out.as_bytes()).unwrap(); |
| 69 |
let cfg: serde_json::Value = ctx.config.get("preprocessor.cat-to-file").unwrap().unwrap(); |
| 70 |
assert_eq!( |
| 71 |
cfg, |
| 72 |
serde_json::json!({ |
| 73 |
"command": "./cat-to-file", |
| 74 |
"array": [1,2,3], |
| 75 |
"number": 123, |
| 76 |
}) |
| 77 |
); |
| 78 |
} |
| 79 |
|
| 80 |
// Test that a renderer receives config set in the environment. |
| 81 |
#[test] |
| 82 |
fn output_cfg_from_env() { |
| 83 |
let mut test = BookTest::from_dir("config/empty"); |
| 84 |
test.rust_program( |
| 85 |
"cat-to-file", |
| 86 |
r#" |
| 87 |
fn main() { |
| 88 |
use std::io::Read; |
| 89 |
let mut s = String::new(); |
| 90 |
std::io::stdin().read_to_string(&mut s).unwrap(); |
| 91 |
std::fs::write("out.txt", s).unwrap(); |
| 92 |
} |
| 93 |
"#, |
| 94 |
) |
| 95 |
.run("build", |cmd| { |
| 96 |
cmd.env( |
| 97 |
"MDBOOK_OUTPUT__CAT_TO_FILE", |
| 98 |
r#"{"command":"./cat-to-file", "array": [1,2,3], "number": 123}"#, |
| 99 |
); |
| 100 |
}); |
| 101 |
let out = read_to_string(test.dir.join("book/out.txt")); |
| 102 |
let ctx = mdbook_renderer::RenderContext::from_json(out.as_bytes()).unwrap(); |
| 103 |
let cfg: serde_json::Value = ctx.config.get("output.cat-to-file").unwrap().unwrap(); |
| 104 |
assert_eq!( |
| 105 |
cfg, |
| 106 |
serde_json::json!({ |
| 107 |
"command": "./cat-to-file", |
| 108 |
"array": [1,2,3], |
| 109 |
"number": 123, |
| 110 |
}) |
| 111 |
); |
| 112 |
} |
| 113 |
|
| 114 |
// An invalid key at the top level. |
| 115 |
#[test] |
| 116 |
fn bad_config_top_level() { |
| 117 |
BookTest::init(|_| {}) |
| 118 |
.change_file("book.toml", "foo = 123") |
| 119 |
.run("build", |cmd| { |
| 120 |
cmd.expect_failure() |
| 121 |
.expect_stdout(str![[""]]) |
| 122 |
.expect_stderr(str![[r#" |
| 123 |
ERROR Invalid configuration file |
| 124 |
[TAB]Caused by: TOML parse error at line 1, column 1 |
| 125 |
| |
| 126 |
1 | foo = 123 |
| 127 |
| ^^^ |
| 128 |
unknown field `foo`, expected one of `book`, `build`, `rust`, `output`, `preprocessor` |
| 129 |
|
| 130 |
|
| 131 |
"#]]); |
| 132 |
}); |
| 133 |
} |
| 134 |
|
| 135 |
// An invalid table at the top level. |
| 136 |
#[test] |
| 137 |
fn bad_config_top_level_table() { |
| 138 |
BookTest::init(|_| {}) |
| 139 |
.change_file( |
| 140 |
"book.toml", |
| 141 |
"[other]\n\ |
| 142 |
foo = 123", |
| 143 |
) |
| 144 |
.run("build", |cmd| { |
| 145 |
cmd.expect_failure() |
| 146 |
.expect_stdout(str![[""]]) |
| 147 |
.expect_stderr(str![[r#" |
| 148 |
ERROR Invalid configuration file |
| 149 |
[TAB]Caused by: TOML parse error at line 1, column 2 |
| 150 |
| |
| 151 |
1 | [other] |
| 152 |
| ^^^^^ |
| 153 |
unknown field `other`, expected one of `book`, `build`, `rust`, `output`, `preprocessor` |
| 154 |
|
| 155 |
|
| 156 |
"#]]); |
| 157 |
}); |
| 158 |
} |
| 159 |
|
| 160 |
// An invalid key in the main book table. |
| 161 |
#[test] |
| 162 |
fn bad_config_in_book_table() { |
| 163 |
BookTest::init(|_| {}) |
| 164 |
.change_file( |
| 165 |
"book.toml", |
| 166 |
"[book]\n\ |
| 167 |
title = \"bad-config\"\n\ |
| 168 |
foo = 123" |
| 169 |
) |
| 170 |
.run("build", |cmd| { |
| 171 |
cmd.expect_failure() |
| 172 |
.expect_stdout(str![[""]]) |
| 173 |
.expect_stderr(str![[r#" |
| 174 |
ERROR Invalid configuration file |
| 175 |
[TAB]Caused by: TOML parse error at line 3, column 1 |
| 176 |
| |
| 177 |
3 | foo = 123 |
| 178 |
| ^^^ |
| 179 |
unknown field `foo`, expected one of `title`, `authors`, `description`, `src`, `language`, `text-direction` |
| 180 |
|
| 181 |
|
| 182 |
"#]]); |
| 183 |
}); |
| 184 |
} |
| 185 |
|
| 186 |
// An invalid key in the main rust table. |
| 187 |
#[test] |
| 188 |
fn bad_config_in_rust_table() { |
| 189 |
BookTest::init(|_| {}) |
| 190 |
.change_file( |
| 191 |
"book.toml", |
| 192 |
"[rust]\n\ |
| 193 |
title = \"bad-config\"\n", |
| 194 |
) |
| 195 |
.run("build", |cmd| { |
| 196 |
cmd.expect_failure() |
| 197 |
.expect_stdout(str![[""]]) |
| 198 |
.expect_stderr(str![[r#" |
| 199 |
ERROR Invalid configuration file |
| 200 |
[TAB]Caused by: TOML parse error at line 2, column 1 |
| 201 |
| |
| 202 |
2 | title = "bad-config" |
| 203 |
| ^^^^^ |
| 204 |
unknown field `title`, expected `edition` |
| 205 |
|
| 206 |
|
| 207 |
"#]]); |
| 208 |
}); |
| 209 |
} |
| 210 |
|
| 211 |
// An invalid top-level key in the environment. |
| 212 |
#[test] |
| 213 |
fn env_invalid_config_key() { |
| 214 |
BookTest::from_dir("config/empty").run("build", |cmd| { |
| 215 |
cmd.env("MDBOOK_FOO", "testing") |
| 216 |
.expect_stdout(str![[""]]) |
| 217 |
.expect_stderr(str![[r#" |
| 218 |
INFO Book building has started |
| 219 |
INFO Running the html backend |
| 220 |
INFO HTML book written to `[ROOT]/book` |
| 221 |
|
| 222 |
"#]]); |
| 223 |
}); |
| 224 |
} |
| 225 |
|
| 226 |
// An invalid value in the environment. |
| 227 |
#[test] |
| 228 |
fn env_invalid_value() { |
| 229 |
BookTest::from_dir("config/empty") |
| 230 |
.run("build", |cmd| { |
| 231 |
cmd.env("MDBOOK_BOOK", r#"{"titlez": "typo"}"#) |
| 232 |
.expect_failure() |
| 233 |
.expect_stdout(str![[""]]) |
| 234 |
.expect_stderr(str![[r#" |
| 235 |
ERROR unknown field `titlez`, expected one of `title`, `authors`, `description`, `src`, `language`, `text-direction` |
| 236 |
|
| 237 |
|
| 238 |
"#]]); |
| 239 |
}) |
| 240 |
.run("build", |cmd| { |
| 241 |
cmd.env("MDBOOK_BOOK__TITLE", r#"{"looks like obj": "abc"}"#) |
| 242 |
.expect_failure() |
| 243 |
.expect_stdout(str![[""]]) |
| 244 |
.expect_stderr(str![[r#" |
| 245 |
ERROR invalid type: map, expected a string |
| 246 |
in `title` |
| 247 |
|
| 248 |
|
| 249 |
"#]]); |
| 250 |
}) |
| 251 |
// This is not valid JSON, so falls back to be interpreted as a string. |
| 252 |
.run("build", |cmd| { |
| 253 |
cmd.env("MDBOOK_BOOK__TITLE", r#"{braces}"#) |
| 254 |
.expect_stdout(str![[""]]) |
| 255 |
.expect_stderr(str![[r#" |
| 256 |
INFO Book building has started |
| 257 |
INFO Running the html backend |
| 258 |
INFO HTML book written to `[ROOT]/book` |
| 259 |
|
| 260 |
"#]]); |
| 261 |
}) |
| 262 |
.check_file_contains("book/index.html", "<title>Chapter 1 - {braces}</title>"); |
| 263 |
} |
| 264 |
|
| 265 |
// Replacing the entire book table from the environment. |
| 266 |
#[test] |
| 267 |
fn env_entire_book_table() { |
| 268 |
BookTest::init(|_| {}) |
| 269 |
.change_file( |
| 270 |
"book.toml", |
| 271 |
"[book]\n\ |
| 272 |
title = \"config title\"\n\ |
| 273 |
", |
| 274 |
) |
| 275 |
.run("build", |cmd| { |
| 276 |
cmd.env("MDBOOK_BOOK", r#"{"description": "custom description"}"#); |
| 277 |
}) |
| 278 |
// The book.toml title is removed. |
| 279 |
.check_file_contains("book/index.html", "<title>Chapter 1</title>") |
| 280 |
.check_file_contains( |
| 281 |
"book/index.html", |
| 282 |
r#"<meta name="description" content="custom description">"#, |
| 283 |
); |
| 284 |
} |
| 285 |
|
| 286 |
// Replacing the entire output or preprocessor table from the environment. |
| 287 |
#[test] |
| 288 |
fn env_entire_output_preprocessor_table() { |
| 289 |
BookTest::from_dir("config/empty") |
| 290 |
.rust_program( |
| 291 |
"mdbook-my-preprocessor", |
| 292 |
r#" |
| 293 |
fn main() { |
| 294 |
let mut args = std::env::args().skip(1); |
| 295 |
if args.next().as_deref() == Some("supports") { |
| 296 |
return; |
| 297 |
} |
| 298 |
use std::io::Read; |
| 299 |
let mut s = String::new(); |
| 300 |
std::io::stdin().read_to_string(&mut s).unwrap(); |
| 301 |
assert!(s.contains("custom preprocessor config")); |
| 302 |
println!("{{\"items\": []}}"); |
| 303 |
} |
| 304 |
"#, |
| 305 |
) |
| 306 |
.rust_program( |
| 307 |
"mdbook-my-output", |
| 308 |
r#" |
| 309 |
fn main() { |
| 310 |
use std::io::Read; |
| 311 |
let mut s = String::new(); |
| 312 |
std::io::stdin().read_to_string(&mut s).unwrap(); |
| 313 |
assert!(s.contains("custom output config")); |
| 314 |
eprintln!("preprocessor saw custom config"); |
| 315 |
} |
| 316 |
"#, |
| 317 |
) |
| 318 |
.run("build", |cmd| { |
| 319 |
let mut paths: Vec<_> = |
| 320 |
std::env::split_paths(&std::env::var_os("PATH").unwrap_or_default()).collect(); |
| 321 |
paths.push(cmd.dir.clone()); |
| 322 |
let path = std::env::join_paths(paths).unwrap().into_string().unwrap(); |
| 323 |
|
| 324 |
cmd.env( |
| 325 |
"MDBOOK_OUTPUT", |
| 326 |
r#"{"my-output": {"foo": "custom output config"}}"#, |
| 327 |
) |
| 328 |
.env( |
| 329 |
"MDBOOK_PREPROCESSOR", |
| 330 |
r#"{"my-preprocessor": {"foo": "custom preprocessor config"}}"#, |
| 331 |
) |
| 332 |
.env("PATH", path) |
| 333 |
.expect_stdout(str![[""]]) |
| 334 |
.expect_stderr(str![[r#" |
| 335 |
INFO Book building has started |
| 336 |
INFO Running the my-output backend |
| 337 |
INFO Invoking the "my-output" renderer |
| 338 |
preprocessor saw custom config |
| 339 |
|
| 340 |
"#]]); |
| 341 |
}) |
| 342 |
// No HTML output |
| 343 |
.check_file_list("book", str![[""]]); |
| 344 |
} |