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

View File

@ -6,9 +6,11 @@ use std::path::{Path, PathBuf};
use std::fs::{self, File}; use std::fs::{self, File};
use std::io::{Read, Write}; use std::io::{Read, Write};
use std::process::Command; use std::process::Command;
use tempdir::TempDir;
use {theme, parse, utils}; use {theme, parse, utils};
use renderer::{Renderer, HtmlHandlebars}; use renderer::{Renderer, HtmlHandlebars};
use preprocess;
use errors::*; use errors::*;
use config::BookConfig; use config::BookConfig;
@ -358,15 +360,26 @@ impl MDBook {
.zip(library_paths.into_iter()) .zip(library_paths.into_iter())
.flat_map(|x| vec![x.0, x.1]) .flat_map(|x| vec![x.0, x.1])
.collect(); .collect();
let temp_dir = TempDir::new("mdbook")?;
for item in self.iter() { for item in self.iter() {
if let BookItem::Chapter(_, ref ch) = *item { 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 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); 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()?; let output = Command::new("rustdoc").arg(&path).arg("--test").args(&library_args).output()?;
if !output.status.success() { if !output.status.success() {

View File

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

View File

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