Skip to content

Add an option to not run rustdoc blocks #12777

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
merged 1 commit into from
Mar 9, 2014
Merged
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
13 changes: 11 additions & 2 deletions src/doc/rustdoc.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,24 @@ code block.
// This is a testable code block (4-space indent)
~~~

In addition to the `ignore` directive, you can specify that the test's execution
should fail with the `should_fail` directive.
You can specify that the test's execution should fail with the `should_fail`
directive.

~~~
```should_fail
// This code block is expected to generate a failure when run
```
~~~

You can specify that the code block should be compiled but not run with the
`no_run` directive.

~~~
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

```no_run
// This code will be compiled but not executed
```
~~~

Rustdoc also supplies some extra sugar for helping with some tedious
documentation examples. If a line is prefixed with `# `, then the line
will not show up in the HTML documentation, but it will be used when
Expand Down
11 changes: 6 additions & 5 deletions src/librustdoc/html/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,14 +245,15 @@ pub fn find_testable_code(doc: &str, tests: &mut ::test::Collector) {
extern fn block(_ob: *buf, text: *buf, lang: *buf, opaque: *libc::c_void) {
unsafe {
if text.is_null() { return }
let (shouldfail, ignore) = if lang.is_null() {
(false, false)
let (should_fail, no_run, ignore) = if lang.is_null() {
(false, false, false)
} else {
vec::raw::buf_as_slice((*lang).data,
(*lang).size as uint, |lang| {
let s = str::from_utf8(lang).unwrap();
(s.contains("should_fail"), s.contains("ignore") ||
s.contains("notrust"))
(s.contains("should_fail"),
s.contains("no_run"),
s.contains("ignore") || s.contains("notrust"))
})
};
if ignore { return }
Expand All @@ -261,7 +262,7 @@ pub fn find_testable_code(doc: &str, tests: &mut ::test::Collector) {
let text = str::from_utf8(text).unwrap();
let mut lines = text.lines().map(|l| stripped_filtered_line(l).unwrap_or(l));
let text = lines.to_owned_vec().connect("\n");
tests.add_test(text, shouldfail);
tests.add_test(text, should_fail, no_run);
})
}
}
Expand Down
9 changes: 6 additions & 3 deletions src/librustdoc/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ pub fn run(input: &str, matches: &getopts::Matches) -> int {
0
}

fn runtest(test: &str, cratename: &str, libs: HashSet<Path>, should_fail: bool) {
fn runtest(test: &str, cratename: &str, libs: HashSet<Path>, should_fail: bool,
no_run: bool) {
let test = maketest(test, cratename);
let parsesess = parse::new_parse_sess();
let input = driver::StrInput(test);
Expand Down Expand Up @@ -152,6 +153,8 @@ fn runtest(test: &str, cratename: &str, libs: HashSet<Path>, should_fail: bool)
let cfg = driver::build_configuration(sess);
driver::compile_input(sess, cfg, &input, &out, &None);

if no_run { return }

// Run the code!
let exe = outdir.path().join("rust_out");
let out = Process::output(exe.as_str().unwrap(), []);
Expand Down Expand Up @@ -203,7 +206,7 @@ pub struct Collector {
}

impl Collector {
pub fn add_test(&mut self, test: &str, should_fail: bool) {
pub fn add_test(&mut self, test: &str, should_fail: bool, no_run: bool) {
let test = test.to_owned();
let name = format!("{}_{}", self.names.connect("::"), self.cnt);
self.cnt += 1;
Expand All @@ -218,7 +221,7 @@ impl Collector {
should_fail: false, // compiler failures are test failures
},
testfn: testing::DynTestFn(proc() {
runtest(test, cratename, libs, should_fail);
runtest(test, cratename, libs, should_fail, no_run);
}),
});
}
Expand Down