diff --git a/tests/load.rs b/tests/load.rs new file mode 100644 index 00000000..0ec6dc8d --- /dev/null +++ b/tests/load.rs @@ -0,0 +1,176 @@ +mod dummy_book; + +use crate::dummy_book::{assert_contains_strings, DummyBook}; +use mdbook::book::parse_summary; +use mdbook::book::{Summary, SummaryItem}; +use mdbook::config::Config; +use mdbook::utils::fs::write_file; +use mdbook::MDBook; +use std::fs; +use std::fs::File; +use std::io::Read; + +#[test] +fn load_with_default_config() { + let temp = DummyBook::new().build().unwrap(); + assert!(!temp.path().join("book.toml").exists()); + + let md = MDBook::load(temp.path()).unwrap(); + + md.build().unwrap(); + + let index_html = temp.path().join("book").join("index.html"); + assert_contains_strings(index_html, &vec![r#"Dummy Book"#]); +} + +#[test] +fn load_with_book_toml_implicit() { + let temp = DummyBook::new().build().unwrap(); + + let book_toml = r#" +[book] +title = "implicit" + "#; + + write_file(&temp.path(), "book.toml", book_toml.as_bytes()).unwrap(); + + assert!(temp.path().join("book.toml").exists()); + + let md = MDBook::load(temp.path()).unwrap(); + + md.build().unwrap(); + + let index_html = temp.path().join("book").join("index.html"); + assert_contains_strings(index_html, &vec![r#"Dummy Book - implicit"#]); +} + +#[test] +fn load_with_book_toml_explicit() { + let temp = DummyBook::new().build().unwrap(); + + let book_toml = r#" +[book] +title = "explicit" + "#; + + write_file(&temp.path(), "book.toml", book_toml.as_bytes()).unwrap(); + + assert!(temp.path().join("book.toml").exists()); + + let md = MDBook::load_with_config_file(temp.path(), &temp.path().join("book.toml")).unwrap(); + + md.build().unwrap(); + + let index_html = temp.path().join("book").join("index.html"); + assert_contains_strings(index_html, &vec![r#"Dummy Book - explicit"#]); +} + +#[test] +fn load_with_alternate_toml() { + let temp = DummyBook::new().build().unwrap(); + + let alternate_toml = r#" +[book] +title = "alternate" + "#; + + write_file(&temp.path(), "not-book.toml", alternate_toml.as_bytes()).unwrap(); + + assert!(!temp.path().join("book.toml").exists()); + assert!(temp.path().join("not-book.toml").exists()); + + let md = + MDBook::load_with_config_file(temp.path(), &temp.path().join("not-book.toml")).unwrap(); + + md.build().unwrap(); + + let index_html = temp.path().join("book").join("index.html"); + assert_contains_strings( + index_html, + &vec![r#"Dummy Book - alternate"#], + ); +} + +#[test] +fn load_with_alternate_toml_with_book_toml_present() { + let temp = DummyBook::new().build().unwrap(); + + let book_toml = r#" +[book] +title = "book" + "#; + + write_file(&temp.path(), "book.toml", book_toml.as_bytes()).unwrap(); + + let alternate_toml = r#" +[book] +title = "not book" + "#; + + write_file(&temp.path(), "not-book.toml", alternate_toml.as_bytes()).unwrap(); + + assert!(temp.path().join("book.toml").exists()); + assert!(temp.path().join("not-book.toml").exists()); + + let md = + MDBook::load_with_config_file(temp.path(), &temp.path().join("not-book.toml")).unwrap(); + + md.build().unwrap(); + + let index_html = temp.path().join("book").join("index.html"); + assert_contains_strings(index_html, &vec![r#"Dummy Book - not book"#]); +} + +#[test] +fn load_with_config_default() { + let temp = DummyBook::new().build().unwrap(); + let cfg = Config::default(); + + let md = MDBook::load_with_config(temp.path(), cfg).unwrap(); + md.build().unwrap(); + + let index_html = temp.path().join("book").join("index.html"); + assert_contains_strings(index_html, &vec![r#"Dummy Book"#]); +} + +#[test] +fn load_with_config_from_disk() { + let temp = DummyBook::new().build().unwrap(); + + let book_toml = r#" +[book] +title = "book" + "#; + + write_file(&temp.path(), "book.toml", book_toml.as_bytes()).unwrap(); + + let cfg = Config::from_disk(&temp.path().join("book.toml")).unwrap(); + + let md = MDBook::load_with_config(temp.path(), cfg).unwrap(); + md.build().unwrap(); + + let index_html = temp.path().join("book").join("index.html"); + assert_contains_strings(index_html, &vec![r#"Dummy Book - book"#]); +} + +#[test] +fn load_with_config_and_summary() { + let temp = DummyBook::new().build().unwrap(); + + let cfg = Config::default(); + let summary = fs::read_to_string(temp.path().join("src").join("SUMMARY.md")).unwrap(); + let summary = parse_summary(&summary).unwrap(); + + let md = MDBook::load_with_config_and_summary(temp.path(), cfg, summary).unwrap(); + md.build().unwrap(); + + let index_html = temp.path().join("book").join("index.html"); + assert_contains_strings(index_html, &vec![r#"Dummy Book"#]); +} + +#[test] +#[should_panic] +fn try_load_with_missing_file() { + let temp = DummyBook::new().build().unwrap(); + MDBook::load_with_config_file(temp.path(), &temp.path().join("not-there.toml")).unwrap(); +}