Skip to content

Commit 5069968

Browse files
basbossinkDylan-DPC
authored andcommitted
Fix issue 832 (#841)
* Add if around stub summary creation Check if an existing SUMMARY.md is present to prevent overwriting it with the stub SUMMARY.md. [#832] * Add test for existing SUMMARY.md
1 parent 5163c5a commit 5069968

File tree

2 files changed

+45
-9
lines changed

2 files changed

+45
-9
lines changed

src/book/init.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -176,15 +176,19 @@ impl BookBuilder {
176176
let src_dir = self.root.join(&self.config.book.src);
177177

178178
let summary = src_dir.join("SUMMARY.md");
179-
let mut f = File::create(&summary).chain_err(|| "Unable to create SUMMARY.md")?;
180-
writeln!(f, "# Summary")?;
181-
writeln!(f)?;
182-
writeln!(f, "- [Chapter 1](./chapter_1.md)")?;
183-
184-
let chapter_1 = src_dir.join("chapter_1.md");
185-
let mut f = File::create(&chapter_1).chain_err(|| "Unable to create chapter_1.md")?;
186-
writeln!(f, "# Chapter 1")?;
187-
179+
if !summary.exists() {
180+
trace!("No summary found creating stub summary and chapter_1.md.");
181+
let mut f = File::create(&summary).chain_err(|| "Unable to create SUMMARY.md")?;
182+
writeln!(f, "# Summary")?;
183+
writeln!(f)?;
184+
writeln!(f, "- [Chapter 1](./chapter_1.md)")?;
185+
186+
let chapter_1 = src_dir.join("chapter_1.md");
187+
let mut f = File::create(&chapter_1).chain_err(|| "Unable to create chapter_1.md")?;
188+
writeln!(f, "# Chapter 1")?;
189+
} else {
190+
trace!("Existing summary found, no need to create stub files.");
191+
}
188192
Ok(())
189193
}
190194

tests/init.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ extern crate tempfile;
44
use mdbook::config::Config;
55
use mdbook::MDBook;
66
use std::fs;
7+
use std::fs::File;
8+
use std::io::prelude::*;
79
use std::path::PathBuf;
810
use tempfile::Builder as TempFileBuilder;
911

@@ -27,6 +29,36 @@ fn base_mdbook_init_should_create_default_content() {
2729
}
2830
}
2931

32+
/// Run `mdbook init` in a directory containing a SUMMARY.md should create the
33+
/// files listed in the summary.
34+
#[test]
35+
fn run_mdbook_init_should_create_content_from_summary() {
36+
let created_files = vec!["intro.md", "first.md", "outro.md"];
37+
38+
let temp = TempFileBuilder::new().prefix("mdbook").tempdir().unwrap();
39+
let src_dir = temp.path().join("src");
40+
fs::create_dir_all(src_dir.clone()).unwrap();
41+
static SUMMARY: &'static str = r#"# Summary
42+
43+
[intro](intro.md)
44+
45+
- [First chapter](first.md)
46+
47+
[outro](outro.md)
48+
49+
"#;
50+
51+
let mut summary = File::create(src_dir.join("SUMMARY.md")).unwrap();
52+
summary.write_all(SUMMARY.as_bytes()).unwrap();
53+
MDBook::init(temp.path()).build().unwrap();
54+
55+
for file in &created_files {
56+
let target = src_dir.join(file);
57+
println!("{}", target.display());
58+
assert!(target.exists(), "{} doesn't exist", file);
59+
}
60+
}
61+
3062
/// Set some custom arguments for where to place the source and destination
3163
/// files, then call `mdbook init`.
3264
#[test]

0 commit comments

Comments
 (0)