Fixed `mdbook test` for {{#playpen file.rs}}

- now `mdbook test` does full link expansion to temp file prior to running
- also minor reformat and cleanup of `HtmlHandlebars::render_item`
This commit is contained in:
Michal Budzynski 2017-08-07 01:36:29 +02:00 committed by Michał Budzyński
parent c36eca15c2
commit ddf31dcc08
4 changed files with 49 additions and 49 deletions

View File

@ -28,6 +28,7 @@ env_logger = "0.4.0"
toml = { version = "0.4", features = ["serde"] }
open = "1.1"
regex = "0.2.1"
tempdir = "0.3.4"
# Watch feature
notify = { version = "4.0", optional = true }
@ -39,10 +40,6 @@ iron = { version = "0.5", optional = true }
staticfile = { version = "0.4", optional = true }
ws = { version = "0.7", optional = true}
# Tests
[dev-dependencies]
tempdir = "0.3.4"
[build-dependencies]
error-chain = "0.10"

View File

@ -6,9 +6,11 @@ use std::path::{Path, PathBuf};
use std::fs::{self, File};
use std::io::{Read, Write};
use std::process::Command;
use tempdir::TempDir;
use {theme, parse, utils};
use renderer::{Renderer, HtmlHandlebars};
use preprocess;
use errors::*;
use config::BookConfig;
@ -358,15 +360,26 @@ impl MDBook {
.zip(library_paths.into_iter())
.flat_map(|x| vec![x.0, x.1])
.collect();
let temp_dir = TempDir::new("mdbook")?;
for item in self.iter() {
if let BookItem::Chapter(_, ref ch) = *item {
if ch.path != PathBuf::new() {
if !ch.path.as_os_str().is_empty() {
let path = self.get_source().join(&ch.path);
let base = path.parent().ok_or_else(
|| String::from("Invalid bookitem path!"),
)?;
let content = utils::fs::file_to_string(&path)?;
// Parse and expand links
let content = preprocess::links::replace_all(&content, base)?;
println!("[*]: Testing file: {:?}", path);
//write preprocessed file to tempdir
let path = temp_dir.path().join(&ch.path);
let mut tmpf = utils::fs::create_file(&path)?;
tmpf.write_all(content.as_bytes())?;
let output = Command::new("rustdoc").arg(&path).arg("--test").args(&library_args).output()?;
if !output.status.success() {

View File

@ -85,8 +85,6 @@ extern crate serde_derive;
extern crate serde;
#[macro_use]
extern crate serde_json;
#[cfg(test)]
extern crate tempdir;
mod parse;

View File

@ -33,24 +33,17 @@ impl HtmlHandlebars {
// FIXME: This should be made DRY-er and rely less on mutable state
match *item {
BookItem::Chapter(_, ref ch) |
BookItem::Affix(ref ch) => {
if ch.path != PathBuf::new() {
BookItem::Affix(ref ch) if !ch.path.as_os_str().is_empty() => {
let path = ctx.book.get_source().join(&ch.path);
debug!("[*]: Opening file: {:?}", path);
let mut f = File::open(&path)?;
let mut content: String = String::new();
debug!("[*]: Reading file");
f.read_to_string(&mut content)?;
let content = utils::fs::file_to_string(&path)?;
let base = path.parent().ok_or_else(
|| String::from("Invalid bookitem path!"),
)?;
// Parse and expand links
if let Some(p) = path.parent() {
content = preprocess::links::replace_all(&content, p)?;
}
content = utils::render_markdown(&content, ctx.book.get_curly_quotes());
let content = preprocess::links::replace_all(&content, base)?;
let content = utils::render_markdown(&content, ctx.book.get_curly_quotes());
print_content.push_str(&content);
// Update the context with data for this file
@ -82,7 +75,6 @@ impl HtmlHandlebars {
if ctx.is_index {
self.render_index(ctx.book, ch, &ctx.destination)?;
}
}
},
_ => {},
}