|
| 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!("{{\"sections\": []}}"); |
| 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 | +} |
0 commit comments