1 use mdbook::config::Config;
2 use mdbook::MDBook;
3 use std::fs;
4 use std::fs::File;
5 use std::io::prelude::*;
6 use std::path::PathBuf;
7 use tempfile::Builder as TempFileBuilder;
8 
9 /// Run `mdbook init` in an empty directory and make sure the default files
10 /// are created.
11 #[test]
base_mdbook_init_should_create_default_content()12 fn base_mdbook_init_should_create_default_content() {
13     let created_files = vec!["book", "src", "src/SUMMARY.md", "src/chapter_1.md"];
14 
15     let temp = TempFileBuilder::new().prefix("mdbook").tempdir().unwrap();
16     for file in &created_files {
17         assert!(!temp.path().join(file).exists());
18     }
19 
20     MDBook::init(temp.path()).build().unwrap();
21 
22     for file in &created_files {
23         let target = temp.path().join(file);
24         println!("{}", target.display());
25         assert!(target.exists(), "{} doesn't exist", file);
26     }
27 
28     let contents = fs::read_to_string(temp.path().join("book.toml")).unwrap();
29     assert_eq!(
30         contents,
31         "[book]\nauthors = []\nlanguage = \"en\"\nmultilingual = false\nsrc = \"src\"\n"
32     );
33 }
34 
35 /// Run `mdbook init` in a directory containing a SUMMARY.md should create the
36 /// files listed in the summary.
37 #[test]
run_mdbook_init_should_create_content_from_summary()38 fn run_mdbook_init_should_create_content_from_summary() {
39     let created_files = vec!["intro.md", "first.md", "outro.md"];
40 
41     let temp = TempFileBuilder::new().prefix("mdbook").tempdir().unwrap();
42     let src_dir = temp.path().join("src");
43     fs::create_dir_all(src_dir.clone()).unwrap();
44     static SUMMARY: &str = r#"# Summary
45 
46 [intro](intro.md)
47 
48 - [First chapter](first.md)
49 
50 [outro](outro.md)
51 
52 "#;
53 
54     let mut summary = File::create(src_dir.join("SUMMARY.md")).unwrap();
55     summary.write_all(SUMMARY.as_bytes()).unwrap();
56     MDBook::init(temp.path()).build().unwrap();
57 
58     for file in &created_files {
59         let target = src_dir.join(file);
60         println!("{}", target.display());
61         assert!(target.exists(), "{} doesn't exist", file);
62     }
63 }
64 
65 /// Set some custom arguments for where to place the source and destination
66 /// files, then call `mdbook init`.
67 #[test]
run_mdbook_init_with_custom_book_and_src_locations()68 fn run_mdbook_init_with_custom_book_and_src_locations() {
69     let created_files = vec!["out", "in", "in/SUMMARY.md", "in/chapter_1.md"];
70 
71     let temp = TempFileBuilder::new().prefix("mdbook").tempdir().unwrap();
72     for file in &created_files {
73         assert!(
74             !temp.path().join(file).exists(),
75             "{} shouldn't exist yet!",
76             file
77         );
78     }
79 
80     let mut cfg = Config::default();
81     cfg.book.src = PathBuf::from("in");
82     cfg.build.build_dir = PathBuf::from("out");
83 
84     MDBook::init(temp.path()).with_config(cfg).build().unwrap();
85 
86     for file in &created_files {
87         let target = temp.path().join(file);
88         assert!(
89             target.exists(),
90             "{} should have been created by `mdbook init`",
91             file
92         );
93     }
94 
95     let contents = fs::read_to_string(temp.path().join("book.toml")).unwrap();
96     assert_eq!(
97         contents,
98         "[book]\nauthors = []\nlanguage = \"en\"\nmultilingual = false\nsrc = \"in\"\n\n[build]\nbuild-dir = \"out\"\ncreate-missing = true\nuse-default-preprocessors = true\n"
99     );
100 }
101 
102 #[test]
book_toml_isnt_required()103 fn book_toml_isnt_required() {
104     let temp = TempFileBuilder::new().prefix("mdbook").tempdir().unwrap();
105     let md = MDBook::init(temp.path()).build().unwrap();
106 
107     let _ = fs::remove_file(temp.path().join("book.toml"));
108 
109     md.build().unwrap();
110 }
111 
112 #[test]
copy_theme()113 fn copy_theme() {
114     let temp = TempFileBuilder::new().prefix("mdbook").tempdir().unwrap();
115     MDBook::init(temp.path()).copy_theme(true).build().unwrap();
116     let expected = vec![
117         "book.js",
118         "css/chrome.css",
119         "css/general.css",
120         "css/print.css",
121         "css/variables.css",
122         "favicon.png",
123         "favicon.svg",
124         "highlight.css",
125         "highlight.js",
126         "index.hbs",
127     ];
128     let theme_dir = temp.path().join("theme");
129     let mut actual: Vec<_> = walkdir::WalkDir::new(&theme_dir)
130         .into_iter()
131         .filter_map(|e| e.ok())
132         .filter(|e| !e.file_type().is_dir())
133         .map(|e| {
134             e.path()
135                 .strip_prefix(&theme_dir)
136                 .unwrap()
137                 .to_str()
138                 .unwrap()
139                 .replace('\\', "/")
140         })
141         .collect();
142     actual.sort();
143     assert_eq!(actual, expected);
144 }
145