2017-11-18 20:41:04 +08:00
|
|
|
use mdbook::config::Config;
|
2018-07-24 01:45:01 +08:00
|
|
|
use mdbook::MDBook;
|
|
|
|
use std::fs;
|
2019-05-09 03:13:20 +08:00
|
|
|
use std::fs::File;
|
|
|
|
use std::io::prelude::*;
|
2018-07-24 01:45:01 +08:00
|
|
|
use std::path::PathBuf;
|
2018-03-27 07:47:37 +08:00
|
|
|
use tempfile::Builder as TempFileBuilder;
|
2017-07-10 18:17:19 +08:00
|
|
|
|
2017-08-02 22:29:28 +08:00
|
|
|
/// Run `mdbook init` in an empty directory and make sure the default files
|
|
|
|
/// are created.
|
2017-07-10 18:17:19 +08:00
|
|
|
#[test]
|
2017-08-02 22:29:28 +08:00
|
|
|
fn base_mdbook_init_should_create_default_content() {
|
2017-07-10 18:17:19 +08:00
|
|
|
let created_files = vec!["book", "src", "src/SUMMARY.md", "src/chapter_1.md"];
|
|
|
|
|
2018-03-27 07:47:37 +08:00
|
|
|
let temp = TempFileBuilder::new().prefix("mdbook").tempdir().unwrap();
|
2017-07-10 18:17:19 +08:00
|
|
|
for file in &created_files {
|
|
|
|
assert!(!temp.path().join(file).exists());
|
|
|
|
}
|
|
|
|
|
2017-11-18 22:07:08 +08:00
|
|
|
MDBook::init(temp.path()).build().unwrap();
|
2017-07-10 18:17:19 +08:00
|
|
|
|
|
|
|
for file in &created_files {
|
2017-09-30 22:11:47 +08:00
|
|
|
let target = temp.path().join(file);
|
|
|
|
println!("{}", target.display());
|
|
|
|
assert!(target.exists(), "{} doesn't exist", file);
|
2017-07-10 18:17:19 +08:00
|
|
|
}
|
2020-06-22 22:36:37 +08:00
|
|
|
|
|
|
|
let contents = fs::read_to_string(temp.path().join("book.toml")).unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
contents,
|
|
|
|
"[book]\nauthors = []\nlanguage = \"en\"\nmultilingual = false\nsrc = \"src\"\n"
|
|
|
|
);
|
2017-07-10 18:17:19 +08:00
|
|
|
}
|
2017-07-10 18:23:51 +08:00
|
|
|
|
2019-05-09 03:13:20 +08:00
|
|
|
/// Run `mdbook init` in a directory containing a SUMMARY.md should create the
|
|
|
|
/// files listed in the summary.
|
|
|
|
#[test]
|
|
|
|
fn run_mdbook_init_should_create_content_from_summary() {
|
|
|
|
let created_files = vec!["intro.md", "first.md", "outro.md"];
|
|
|
|
|
|
|
|
let temp = TempFileBuilder::new().prefix("mdbook").tempdir().unwrap();
|
|
|
|
let src_dir = temp.path().join("src");
|
|
|
|
fs::create_dir_all(src_dir.clone()).unwrap();
|
2019-06-01 00:01:02 +08:00
|
|
|
static SUMMARY: &str = r#"# Summary
|
2019-05-09 03:13:20 +08:00
|
|
|
|
|
|
|
[intro](intro.md)
|
|
|
|
|
|
|
|
- [First chapter](first.md)
|
|
|
|
|
|
|
|
[outro](outro.md)
|
|
|
|
|
|
|
|
"#;
|
|
|
|
|
|
|
|
let mut summary = File::create(src_dir.join("SUMMARY.md")).unwrap();
|
|
|
|
summary.write_all(SUMMARY.as_bytes()).unwrap();
|
|
|
|
MDBook::init(temp.path()).build().unwrap();
|
|
|
|
|
|
|
|
for file in &created_files {
|
|
|
|
let target = src_dir.join(file);
|
|
|
|
println!("{}", target.display());
|
|
|
|
assert!(target.exists(), "{} doesn't exist", file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-02 22:29:28 +08:00
|
|
|
/// Set some custom arguments for where to place the source and destination
|
|
|
|
/// files, then call `mdbook init`.
|
2017-07-10 18:23:51 +08:00
|
|
|
#[test]
|
2017-08-02 22:29:28 +08:00
|
|
|
fn run_mdbook_init_with_custom_book_and_src_locations() {
|
2017-07-10 18:23:51 +08:00
|
|
|
let created_files = vec!["out", "in", "in/SUMMARY.md", "in/chapter_1.md"];
|
|
|
|
|
2018-03-27 07:47:37 +08:00
|
|
|
let temp = TempFileBuilder::new().prefix("mdbook").tempdir().unwrap();
|
2017-07-10 18:23:51 +08:00
|
|
|
for file in &created_files {
|
2017-11-18 20:41:04 +08:00
|
|
|
assert!(
|
|
|
|
!temp.path().join(file).exists(),
|
|
|
|
"{} shouldn't exist yet!",
|
|
|
|
file
|
|
|
|
);
|
2017-07-10 18:23:51 +08:00
|
|
|
}
|
|
|
|
|
2017-11-18 20:41:04 +08:00
|
|
|
let mut cfg = Config::default();
|
|
|
|
cfg.book.src = PathBuf::from("in");
|
|
|
|
cfg.build.build_dir = PathBuf::from("out");
|
2017-07-10 18:23:51 +08:00
|
|
|
|
2017-11-18 22:16:35 +08:00
|
|
|
MDBook::init(temp.path()).with_config(cfg).build().unwrap();
|
2017-07-10 18:23:51 +08:00
|
|
|
|
|
|
|
for file in &created_files {
|
2017-09-30 22:11:47 +08:00
|
|
|
let target = temp.path().join(file);
|
2017-11-18 20:41:04 +08:00
|
|
|
assert!(
|
|
|
|
target.exists(),
|
|
|
|
"{} should have been created by `mdbook init`",
|
|
|
|
file
|
|
|
|
);
|
2017-07-10 18:23:51 +08:00
|
|
|
}
|
2020-11-11 03:45:36 +08:00
|
|
|
|
|
|
|
let contents = fs::read_to_string(temp.path().join("book.toml")).unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
contents,
|
2022-11-08 06:08:31 +08:00
|
|
|
"[book]\nauthors = []\nlanguage = \"en\"\nmultilingual = false\nsrc = \"in\"\n\n[build]\nbuild-dir = \"out\"\ncreate-missing = true\nextra-watch-dirs = []\nuse-default-preprocessors = true\n"
|
2020-11-11 03:45:36 +08:00
|
|
|
);
|
2017-07-10 18:23:51 +08:00
|
|
|
}
|
2017-12-09 17:46:39 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn book_toml_isnt_required() {
|
2018-03-27 07:47:37 +08:00
|
|
|
let temp = TempFileBuilder::new().prefix("mdbook").tempdir().unwrap();
|
2018-01-07 22:10:48 +08:00
|
|
|
let md = MDBook::init(temp.path()).build().unwrap();
|
2017-12-09 17:46:39 +08:00
|
|
|
|
2017-11-18 23:21:59 +08:00
|
|
|
let _ = fs::remove_file(temp.path().join("book.toml"));
|
2017-12-09 17:46:39 +08:00
|
|
|
|
2017-12-10 20:13:46 +08:00
|
|
|
md.build().unwrap();
|
2017-12-09 17:46:39 +08:00
|
|
|
}
|
2021-01-07 08:29:38 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn copy_theme() {
|
|
|
|
let temp = TempFileBuilder::new().prefix("mdbook").tempdir().unwrap();
|
|
|
|
MDBook::init(temp.path()).copy_theme(true).build().unwrap();
|
|
|
|
let expected = vec![
|
|
|
|
"book.js",
|
|
|
|
"css/chrome.css",
|
|
|
|
"css/general.css",
|
|
|
|
"css/print.css",
|
|
|
|
"css/variables.css",
|
2023-01-18 19:38:46 +08:00
|
|
|
"css/exclude.css",
|
2021-01-07 08:29:38 +08:00
|
|
|
"favicon.png",
|
|
|
|
"favicon.svg",
|
|
|
|
"highlight.css",
|
|
|
|
"highlight.js",
|
|
|
|
"index.hbs",
|
|
|
|
];
|
|
|
|
let theme_dir = temp.path().join("theme");
|
|
|
|
let mut actual: Vec<_> = walkdir::WalkDir::new(&theme_dir)
|
|
|
|
.into_iter()
|
|
|
|
.filter_map(|e| e.ok())
|
|
|
|
.filter(|e| !e.file_type().is_dir())
|
|
|
|
.map(|e| {
|
|
|
|
e.path()
|
|
|
|
.strip_prefix(&theme_dir)
|
|
|
|
.unwrap()
|
|
|
|
.to_str()
|
|
|
|
.unwrap()
|
|
|
|
.replace('\\', "/")
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
actual.sort();
|
|
|
|
assert_eq!(actual, expected);
|
|
|
|
}
|