Skip to content
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

add static file support #137

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ fn main() {
}
```


Finally, in `tests/skeptic.rs` put the following macros to tie the
generated test cases to `cargo test`:

Expand All @@ -75,14 +74,14 @@ test`.
Rust Skeptic is not based on rustdoc. It behaves similarly in many
cases, but not all. Here's the lowdown on the Skeptic system.

*Note: [this `README.md` file itself is tested by Rust
_Note: [this `README.md` file itself is tested by Rust
Skeptic](/testing/build.rs).
Because it is illustrating how to use markdown syntax, the markup on
this document itself is funky, and so is the output below,
particularly when illustrating Markdown's code fences
(<code>```rust</code>).*
(<code>```rust</code>)._

*You must ask for `rust` code blocks explicitly to get Rust testing*,
_You must ask for `rust` code blocks explicitly to get Rust testing_,
with <code>```rust</code>. This is different from rustdoc, which
assumes code blocks are Rust. The reason for this is that common
Markdown parsers, like that used on GitHub, also do not assume Rust by
Expand Down Expand Up @@ -115,8 +114,8 @@ GitHub-compatible). These words change how the test is interpreted:

### `ignore` Info String

The `ignore` info string causes the test to be completely ignored. It will not
be compiled or run during testing. This can be useful if an example is written
The `ignore` info string causes the test to be completely ignored. It will not
be compiled or run during testing. This can be useful if an example is written
in Rust (and you want it highlighted as such) but it is known to be incomplete
(so it cannot compile as-is).

Expand All @@ -131,7 +130,7 @@ fn do_amazing_thing() -> i32 {
### `no_run` Info String

The `no_run` info string causes the example code not to be run during testing.
Code marked with `no_run` will however still be compiled. This is useful for
Code marked with `no_run` will however still be compiled. This is useful for
examples/test that may have side effects or dependencies which are not desirable
in a testing situation.

Expand Down Expand Up @@ -163,11 +162,11 @@ fn main() {

## Skeptic Templates

Unlike rustdoc, *Skeptic does not modify examples before testing by
default*. Skeptic examples are placed in a '.rs' file, compiled, then
Unlike rustdoc, _Skeptic does not modify examples before testing by
default_. Skeptic examples are placed in a '.rs' file, compiled, then
run.

This means that - *by default* - Skeptic examples require a `main`
This means that - _by default_ - Skeptic examples require a `main`
function, as in all the examples above. Implicit wrapping of examples
in `main`, and custom injection of `extern crate` statements and crate
attributes are controlled through templates.
Expand Down Expand Up @@ -242,6 +241,10 @@ fn main() {{
```
````

## Static files

To add static files for your tests add them in the `skeptic-static` folder, the content will be copied over before compiling and running your tests.

## Rustdoc-style undisplayed lines with `# `

Like rustdoc, skeptic will remove preceding `# ` from any lines of
Expand Down
1 change: 1 addition & 0 deletions skeptic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ glob = "0.3"
pulldown-cmark = { version = "0.9", default-features = false }
tempfile = "3"
walkdir = "2.2"
fs_extra = "1.2"

[dev-dependencies]
unindent = "0.1"
14 changes: 14 additions & 0 deletions skeptic/src/rt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::time::SystemTime;

use cargo_metadata::Edition;
use error_chain::error_chain;
use fs_extra::dir::{copy, CopyOptions};
use walkdir::WalkDir;

pub fn compile_test(root_dir: &str, out_dir: &str, target_triple: &str, test_text: &str) {
Expand Down Expand Up @@ -44,6 +45,19 @@ fn handle_test(
.unwrap();
let testcase_path = out_dir.path().join("test.rs");
fs::write(&testcase_path, test_text.as_bytes()).unwrap();
let static_dir = format!("{}/skeptic-static/", root_dir);

if Path::new(&static_dir).is_dir() {
copy(
static_dir,
out_dir.path(),
&CopyOptions {
content_only: true,
..CopyOptions::new()
},
)
.unwrap();
}

// OK, here's where a bunch of magic happens using assumptions
// about cargo internals. We are going to use rustc to compile
Expand Down
1 change: 1 addition & 0 deletions testing/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ fn main() {
"tests/hashtag-test.md",
"tests/should-panic-test.md",
"tests/section-names.md",
"tests/static-file-test.md",
]);
}
1 change: 1 addition & 0 deletions testing/skeptic-static/file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello world!
9 changes: 9 additions & 0 deletions testing/tests/static-file-test.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Files from skeptic-static should be copied over before compiling and running tests

```rust
fn main() {
// The contents of file.txt is "Hello world!"
let str_from_file = include_str!("file.txt");
assert_eq!(str_from_file, "Hello world!");
}
```