Skip to content

Commit 71cab04

Browse files
committed
Add an option to not run rustdoc blocks
This is useful for code that would be expensive to run or has some kind of external dependency (e.g. a database or server).
1 parent 0017056 commit 71cab04

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

src/doc/rustdoc.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,15 +117,24 @@ code block.
117117
// This is a testable code block (4-space indent)
118118
~~~
119119

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

123123
~~~
124124
```should_fail
125125
// This code block is expected to generate a failure when run
126126
```
127127
~~~
128128

129+
You can specify that the code block should be compiled but not run with the
130+
`no_run` directive.
131+
132+
~~~
133+
```no_run
134+
// This code will be compiled but not executed
135+
```
136+
~~~
137+
129138
Rustdoc also supplies some extra sugar for helping with some tedious
130139
documentation examples. If a line is prefixed with `# `, then the line
131140
will not show up in the HTML documentation, but it will be used when

src/librustdoc/html/markdown.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -245,14 +245,15 @@ pub fn find_testable_code(doc: &str, tests: &mut ::test::Collector) {
245245
extern fn block(_ob: *buf, text: *buf, lang: *buf, opaque: *libc::c_void) {
246246
unsafe {
247247
if text.is_null() { return }
248-
let (shouldfail, ignore) = if lang.is_null() {
249-
(false, false)
248+
let (should_fail, no_run, ignore) = if lang.is_null() {
249+
(false, false, false)
250250
} else {
251251
vec::raw::buf_as_slice((*lang).data,
252252
(*lang).size as uint, |lang| {
253253
let s = str::from_utf8(lang).unwrap();
254-
(s.contains("should_fail"), s.contains("ignore") ||
255-
s.contains("notrust"))
254+
(s.contains("should_fail"),
255+
s.contains("no_run"),
256+
s.contains("ignore") || s.contains("notrust"))
256257
})
257258
};
258259
if ignore { return }
@@ -261,7 +262,7 @@ pub fn find_testable_code(doc: &str, tests: &mut ::test::Collector) {
261262
let text = str::from_utf8(text).unwrap();
262263
let mut lines = text.lines().map(|l| stripped_filtered_line(l).unwrap_or(l));
263264
let text = lines.to_owned_vec().connect("\n");
264-
tests.add_test(text, shouldfail);
265+
tests.add_test(text, should_fail, no_run);
265266
})
266267
}
267268
}

src/librustdoc/test.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ pub fn run(input: &str, matches: &getopts::Matches) -> int {
9898
0
9999
}
100100

101-
fn runtest(test: &str, cratename: &str, libs: HashSet<Path>, should_fail: bool) {
101+
fn runtest(test: &str, cratename: &str, libs: HashSet<Path>, should_fail: bool,
102+
no_run: bool) {
102103
let test = maketest(test, cratename);
103104
let parsesess = parse::new_parse_sess();
104105
let input = driver::StrInput(test);
@@ -152,6 +153,8 @@ fn runtest(test: &str, cratename: &str, libs: HashSet<Path>, should_fail: bool)
152153
let cfg = driver::build_configuration(sess);
153154
driver::compile_input(sess, cfg, &input, &out, &None);
154155

156+
if no_run { return }
157+
155158
// Run the code!
156159
let exe = outdir.path().join("rust_out");
157160
let out = Process::output(exe.as_str().unwrap(), []);
@@ -203,7 +206,7 @@ pub struct Collector {
203206
}
204207

205208
impl Collector {
206-
pub fn add_test(&mut self, test: &str, should_fail: bool) {
209+
pub fn add_test(&mut self, test: &str, should_fail: bool, no_run: bool) {
207210
let test = test.to_owned();
208211
let name = format!("{}_{}", self.names.connect("::"), self.cnt);
209212
self.cnt += 1;
@@ -218,7 +221,7 @@ impl Collector {
218221
should_fail: false, // compiler failures are test failures
219222
},
220223
testfn: testing::DynTestFn(proc() {
221-
runtest(test, cratename, libs, should_fail);
224+
runtest(test, cratename, libs, should_fail, no_run);
222225
}),
223226
});
224227
}

0 commit comments

Comments
 (0)