Skip to content

Commit

Permalink
Fix issue 832 (rust-lang#841)
Browse files Browse the repository at this point in the history
* Add if around stub summary creation

Check if an existing SUMMARY.md is present to prevent overwriting it
with the stub SUMMARY.md.

[rust-lang#832]

* Add test for existing SUMMARY.md
  • Loading branch information
basbossink authored and Dylan-DPC committed May 8, 2019
1 parent 286891c commit c83f2a6
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/book/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}

Expand Down
32 changes: 32 additions & 0 deletions tests/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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]
Expand Down

0 comments on commit c83f2a6

Please sign in to comment.