Skip to content

Commit 5445458

Browse files
committed
Add tests for various environment config issues
These currently aren't working as expected.
1 parent ef476a7 commit 5445458

File tree

1 file changed

+133
-0
lines changed

1 file changed

+133
-0
lines changed

tests/testsuite/config.rs

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,139 @@ ERROR Invalid configuration file
204204
unknown field `title`, expected `edition`
205205
206206
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_failure()
217+
.expect_stdout(str![[""]])
218+
.expect_stderr(str![[r#"
219+
220+
thread 'main' ([..]) panicked at [..]
221+
unreachable: invalid key `foo`
222+
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
223+
224+
"#]]);
225+
});
226+
}
227+
228+
// An invalid value in the environment.
229+
#[test]
230+
fn env_invalid_value() {
231+
BookTest::from_dir("config/empty")
232+
.run("build", |cmd| {
233+
cmd.env("MDBOOK_BOOK", r#"{"titlez": "typo"}"#)
234+
.expect_stdout(str![[""]])
235+
.expect_stderr(str![[r#"
236+
INFO Book building has started
237+
INFO Running the html backend
238+
INFO HTML book written to `[ROOT]/book`
239+
240+
"#]]);
241+
})
242+
.run("build", |cmd| {
243+
cmd.env("MDBOOK_BOOK__TITLE", r#"{"looks like obj": "abc"}"#)
244+
.expect_stdout(str![[""]])
245+
.expect_stderr(str![[r#"
246+
INFO Book building has started
247+
INFO Running the html backend
248+
INFO HTML book written to `[ROOT]/book`
249+
250+
"#]]);
251+
})
252+
.check_file_contains("book/index.html", "<title>Chapter 1</title>")
253+
.run("build", |cmd| {
254+
cmd.env("MDBOOK_BOOK__TITLE", r#"{braces}"#)
255+
.expect_stdout(str![[""]])
256+
.expect_stderr(str![[r#"
257+
INFO Book building has started
258+
INFO Running the html backend
259+
INFO HTML book written to `[ROOT]/book`
260+
261+
"#]]);
262+
})
263+
.check_file_contains("book/index.html", "<title>Chapter 1 - {braces}</title>");
264+
}
265+
266+
// Replacing the entire book table from the environment.
267+
#[test]
268+
fn env_entire_book_table() {
269+
BookTest::init(|_| {})
270+
.change_file(
271+
"book.toml",
272+
"[book]\n\
273+
title = \"config title\"\n\
274+
",
275+
)
276+
.run("build", |cmd| {
277+
cmd.env("MDBOOK_BOOK", r#"{"description": "custom description"}"#);
278+
})
279+
.check_file_contains("book/index.html", "<title>Chapter 1 - config title</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+
}
315+
"#,
316+
)
317+
.run("build", |cmd| {
318+
let mut paths: Vec<_> =
319+
std::env::split_paths(&std::env::var_os("PATH").unwrap_or_default()).collect();
320+
paths.push(cmd.dir.clone());
321+
let path = std::env::join_paths(paths).unwrap().into_string().unwrap();
322+
323+
cmd.env(
324+
"MDBOOK_OUTPUT",
325+
r#"{"my-output": {"foo": "custom output config"}}"#,
326+
)
327+
.env(
328+
"MDBOOK_PREPROCESSOR",
329+
r#"{"my-preprocessor": {"foo": "custom preprocessor config"}}"#,
330+
)
331+
.env("PATH", path)
332+
.expect_failure()
333+
.expect_stdout(str![[""]])
334+
.expect_stderr(str![[r#"
335+
336+
thread 'main' ([..]) panicked at [..]
337+
unreachable: invalid key `output`
338+
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
339+
207340
"#]]);
208341
});
209342
}

0 commit comments

Comments
 (0)