-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Regression tests #422
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Michael-F-Bryan
merged 9 commits into
rust-lang:master
from
Michael-F-Bryan:regression-tests
Nov 16, 2017
Merged
Regression tests #422
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
95609cf
Created regression tests for the table of contents
Michael-F-Bryan d11e3f6
Refactoring to make the test more readable
Michael-F-Bryan dc9c1c9
Fixed some bitrot and removed the (now redundant) tests/helper module
Michael-F-Bryan d86f960
Removed the include_str!() stuff and use just the dummy book for testing
Michael-F-Bryan f8a3382
Regression tests now pass again!
Michael-F-Bryan 4266a93
Pinned a `*` dependency to use a particular version
Michael-F-Bryan 1bcd914
Made sure test mocks return errors instead of panicking
Michael-F-Bryan 65191c3
Addressed the rest of @budziq's review
Michael-F-Bryan 2a1ae82
Replaced a file open/read with file_to_string
Michael-F-Bryan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
//! This will create an entire book in a temporary directory using some | ||
//! dummy contents from the `tests/dummy-book/` directory. | ||
|
||
// Not all features are used in all test crates, so... | ||
#![allow(dead_code, unused_variables, unused_imports, unused_extern_crates)] | ||
extern crate mdbook; | ||
extern crate tempdir; | ||
extern crate walkdir; | ||
|
||
use std::path::Path; | ||
use std::fs::{self, File}; | ||
use std::io::{Read, Write}; | ||
use mdbook::errors::*; | ||
use mdbook::utils::fs::file_to_string; | ||
|
||
// The funny `self::` here is because we've got an `extern crate ...` and are | ||
// in a submodule | ||
use self::tempdir::TempDir; | ||
use self::mdbook::MDBook; | ||
use self::walkdir::WalkDir; | ||
|
||
|
||
/// Create a dummy book in a temporary directory, using the contents of | ||
/// `SUMMARY_MD` as a guide. | ||
/// | ||
/// The "Nested Chapter" file contains a code block with a single | ||
/// `assert!($TEST_STATUS)`. If you want to check MDBook's testing | ||
/// functionality, `$TEST_STATUS` can be substitute for either `true` or | ||
/// `false`. This is done using the `passing_test` parameter. | ||
#[derive(Clone, Debug, PartialEq)] | ||
pub struct DummyBook { | ||
passing_test: bool, | ||
} | ||
|
||
impl DummyBook { | ||
/// Create a new `DummyBook` with all the defaults. | ||
pub fn new() -> DummyBook { | ||
DummyBook { passing_test: true } | ||
} | ||
|
||
/// Whether the doc-test included in the "Nested Chapter" should pass or | ||
/// fail (it passes by default). | ||
pub fn with_passing_test(&mut self, test_passes: bool) -> &mut DummyBook { | ||
self.passing_test = test_passes; | ||
self | ||
} | ||
|
||
/// Write a book to a temporary directory using the provided settings. | ||
pub fn build(&self) -> Result<TempDir> { | ||
let temp = TempDir::new("dummy_book").chain_err(|| "Unable to create temp directory")?; | ||
|
||
let dummy_book_root = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/dummy_book"); | ||
recursive_copy(&dummy_book_root, temp.path()).chain_err(|| { | ||
"Couldn't copy files into a \ | ||
temporary directory" | ||
})?; | ||
|
||
let sub_pattern = if self.passing_test { "true" } else { "false" }; | ||
let file_containing_test = temp.path().join("src/first/nested.md"); | ||
replace_pattern_in_file(&file_containing_test, "$TEST_STATUS", sub_pattern)?; | ||
|
||
Ok(temp) | ||
} | ||
} | ||
|
||
fn replace_pattern_in_file(filename: &Path, from: &str, to: &str) -> Result<()> { | ||
let contents = file_to_string(filename)?; | ||
File::create(filename)?.write_all(contents.replace(from, to).as_bytes())?; | ||
|
||
Ok(()) | ||
} | ||
|
||
/// Read the contents of the provided file into memory and then iterate through | ||
/// the list of strings asserting that the file contains all of them. | ||
pub fn assert_contains_strings<P: AsRef<Path>>(filename: P, strings: &[&str]) { | ||
let filename = filename.as_ref(); | ||
let content = file_to_string(filename).expect("Couldn't read the file's contents"); | ||
|
||
for s in strings { | ||
assert!(content.contains(s), | ||
"Searching for {:?} in {}\n\n{}", | ||
s, | ||
filename.display(), | ||
content); | ||
} | ||
} | ||
|
||
|
||
|
||
/// Recursively copy an entire directory tree to somewhere else (a la `cp -r`). | ||
fn recursive_copy<A: AsRef<Path>, B: AsRef<Path>>(from: A, to: B) -> Result<()> { | ||
let from = from.as_ref(); | ||
let to = to.as_ref(); | ||
|
||
for entry in WalkDir::new(&from) { | ||
let entry = entry.chain_err(|| "Unable to inspect directory entry")?; | ||
|
||
let original_location = entry.path(); | ||
let relative = original_location.strip_prefix(&from) | ||
.expect("`original_location` is inside the `from` \ | ||
directory"); | ||
let new_location = to.join(relative); | ||
|
||
if original_location.is_file() { | ||
if let Some(parent) = new_location.parent() { | ||
fs::create_dir_all(parent).chain_err(|| "Couldn't create directory")?; | ||
} | ||
|
||
fs::copy(&original_location, &new_location).chain_err(|| { | ||
"Unable to copy file contents" | ||
})?; | ||
} | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not implement it directly? It's a single bool.