From c83f2a6799c734ab0ccd29a7065a0e240f0d5d45 Mon Sep 17 00:00:00 2001 From: Bas Bossink Date: Wed, 8 May 2019 21:13:20 +0200 Subject: [PATCH] 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 --- src/book/init.rs | 22 +++++++++++++--------- tests/init.rs | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 9 deletions(-) diff --git a/src/book/init.rs b/src/book/init.rs index 740eb27bf0..e24efafd4c 100644 --- a/src/book/init.rs +++ b/src/book/init.rs @@ -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(()) } diff --git a/tests/init.rs b/tests/init.rs index f48848c86d..ed9aed7e2a 100644 --- a/tests/init.rs +++ b/tests/init.rs @@ -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]