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
This commit is contained in:
parent
5163c5ab75
commit
506996808b
|
@ -176,15 +176,19 @@ impl BookBuilder {
|
|||
let src_dir = self.root.join(&self.config.book.src);
|
||||
|
||||
let summary = src_dir.join("SUMMARY.md");
|
||||
let mut f = File::create(&summary).chain_err(|| "Unable to create SUMMARY.md")?;
|
||||
writeln!(f, "# Summary")?;
|
||||
writeln!(f)?;
|
||||
writeln!(f, "- [Chapter 1](./chapter_1.md)")?;
|
||||
|
||||
let chapter_1 = src_dir.join("chapter_1.md");
|
||||
let mut f = File::create(&chapter_1).chain_err(|| "Unable to create chapter_1.md")?;
|
||||
writeln!(f, "# Chapter 1")?;
|
||||
if !summary.exists() {
|
||||
trace!("No summary found creating stub summary and chapter_1.md.");
|
||||
let mut f = File::create(&summary).chain_err(|| "Unable to create SUMMARY.md")?;
|
||||
writeln!(f, "# Summary")?;
|
||||
writeln!(f)?;
|
||||
writeln!(f, "- [Chapter 1](./chapter_1.md)")?;
|
||||
|
||||
let chapter_1 = src_dir.join("chapter_1.md");
|
||||
let mut f = File::create(&chapter_1).chain_err(|| "Unable to create chapter_1.md")?;
|
||||
writeln!(f, "# Chapter 1")?;
|
||||
} else {
|
||||
trace!("Existing summary found, no need to create stub files.");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
|
@ -4,6 +4,8 @@ extern crate tempfile;
|
|||
use mdbook::config::Config;
|
||||
use mdbook::MDBook;
|
||||
use std::fs;
|
||||
use std::fs::File;
|
||||
use std::io::prelude::*;
|
||||
use std::path::PathBuf;
|
||||
use tempfile::Builder as TempFileBuilder;
|
||||
|
||||
|
@ -27,6 +29,36 @@ fn base_mdbook_init_should_create_default_content() {
|
|||
}
|
||||
}
|
||||
|
||||
/// 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();
|
||||
static SUMMARY: &'static str = r#"# Summary
|
||||
|
||||
[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);
|
||||
}
|
||||
}
|
||||
|
||||
/// Set some custom arguments for where to place the source and destination
|
||||
/// files, then call `mdbook init`.
|
||||
#[test]
|
||||
|
|
Loading…
Reference in New Issue