Skip to content

Commit 7770ea7

Browse files
committed
rustdoc: Add #[doc(test(no_inject_crate))] attribute
So that collections doctests don't automatically fail themselves by injecting `extern crate collections` when they are mostly using the std facade.
1 parent 3d365f6 commit 7770ea7

File tree

3 files changed

+49
-11
lines changed

3 files changed

+49
-11
lines changed

src/librustdoc/html/markdown.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ pub fn render(w: &mut fmt::Formatter, s: &str, print_toc: bool) -> fmt::Result {
230230
stripped_filtered_line(l).unwrap_or(l)
231231
}).collect::<Vec<&str>>().connect("\n");
232232
let krate = krate.as_ref().map(|s| &**s);
233-
let test = test::maketest(&test, krate, false, false);
233+
let test = test::maketest(&test, krate, false, false, true);
234234
s.push_str(&format!("<span class='rusttest'>{}</span>", Escape(&test)));
235235
});
236236
s.push_str(&highlight::highlight(&text,

src/librustdoc/markdown.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ pub fn test(input: &str, libs: SearchPaths, externs: core::Externs,
143143
mut test_args: Vec<String>) -> int {
144144
let input_str = load_or_return!(input, 1, 2);
145145

146-
let mut collector = Collector::new(input.to_string(), libs, externs, true);
146+
let mut collector = Collector::new(input.to_string(), libs, externs, true, false);
147147
find_testable_code(&input_str, &mut collector);
148148
test_args.insert(0, "rustdoctest".to_string());
149149
testing::test_main(&test_args, collector.tests);

src/librustdoc/test.rs

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ pub fn run(input: &str,
7676
"rustdoc-test", None)
7777
.expect("phase_2_configure_and_expand aborted in rustdoc!");
7878

79+
let inject_crate = should_inject_crate(&krate);
80+
7981
let ctx = core::DocContext {
8082
krate: &krate,
8183
maybe_typed: core::NotTyped(sess),
@@ -100,7 +102,8 @@ pub fn run(input: &str,
100102
let mut collector = Collector::new(krate.name.to_string(),
101103
libs,
102104
externs,
103-
false);
105+
false,
106+
inject_crate);
104107
collector.fold_crate(krate);
105108

106109
test_args.insert(0, "rustdoctest".to_string());
@@ -110,13 +113,42 @@ pub fn run(input: &str,
110113
0
111114
}
112115

116+
// Look for #![doc(test(no_crate_inject))], used by crates in the std facade
117+
fn should_inject_crate(krate: &::syntax::ast::Crate) -> bool {
118+
use syntax::attr::AttrMetaMethods;
119+
120+
let mut inject_crate = true;
121+
122+
for attr in &krate.attrs {
123+
if attr.check_name("doc") {
124+
for list in attr.meta_item_list().into_iter() {
125+
for attr in list {
126+
if attr.check_name("test") {
127+
for list in attr.meta_item_list().into_iter() {
128+
for attr in list {
129+
if attr.check_name("no_crate_inject") {
130+
inject_crate = false;
131+
}
132+
}
133+
}
134+
}
135+
}
136+
}
137+
}
138+
}
139+
140+
return inject_crate;
141+
}
142+
113143
#[allow(deprecated)]
114144
fn runtest(test: &str, cratename: &str, libs: SearchPaths,
115145
externs: core::Externs,
116-
should_panic: bool, no_run: bool, as_test_harness: bool) {
146+
should_panic: bool, no_run: bool, as_test_harness: bool,
147+
inject_crate: bool) {
117148
// the test harness wants its own `main` & top level functions, so
118149
// never wrap the test in `fn main() { ... }`
119-
let test = maketest(test, Some(cratename), true, as_test_harness);
150+
let test = maketest(test, Some(cratename), true, as_test_harness,
151+
inject_crate);
120152
let input = config::Input::Str(test.to_string());
121153

122154
let sessopts = config::Options {
@@ -218,7 +250,8 @@ fn runtest(test: &str, cratename: &str, libs: SearchPaths,
218250
}
219251
}
220252

221-
pub fn maketest(s: &str, cratename: Option<&str>, lints: bool, dont_insert_main: bool) -> String {
253+
pub fn maketest(s: &str, cratename: Option<&str>, lints: bool,
254+
dont_insert_main: bool, inject_crate: bool) -> String {
222255
let (crate_attrs, everything_else) = partition_source(s);
223256

224257
let mut prog = String::new();
@@ -235,7 +268,7 @@ pub fn maketest(s: &str, cratename: Option<&str>, lints: bool, dont_insert_main:
235268

236269
// Don't inject `extern crate std` because it's already injected by the
237270
// compiler.
238-
if !s.contains("extern crate") && cratename != Some("std") {
271+
if !s.contains("extern crate") && cratename != Some("std") && inject_crate {
239272
match cratename {
240273
Some(cratename) => {
241274
if s.contains(cratename) {
@@ -267,7 +300,7 @@ fn partition_source(s: &str) -> (String, String) {
267300
let mut after = String::new();
268301

269302
for line in s.lines() {
270-
let trimline = StrExt::trim(line);
303+
let trimline = line.trim();
271304
let header = trimline.is_whitespace() ||
272305
trimline.starts_with("#![feature");
273306
if !header || after_header {
@@ -292,11 +325,12 @@ pub struct Collector {
292325
use_headers: bool,
293326
current_header: Option<String>,
294327
cratename: String,
328+
inject_crate: bool
295329
}
296330

297331
impl Collector {
298332
pub fn new(cratename: String, libs: SearchPaths, externs: core::Externs,
299-
use_headers: bool) -> Collector {
333+
use_headers: bool, inject_crate: bool) -> Collector {
300334
Collector {
301335
tests: Vec::new(),
302336
names: Vec::new(),
@@ -306,11 +340,13 @@ impl Collector {
306340
use_headers: use_headers,
307341
current_header: None,
308342
cratename: cratename,
343+
inject_crate: inject_crate
309344
}
310345
}
311346

312347
pub fn add_test(&mut self, test: String,
313-
should_panic: bool, no_run: bool, should_ignore: bool, as_test_harness: bool) {
348+
should_panic: bool, no_run: bool, should_ignore: bool,
349+
as_test_harness: bool) {
314350
let name = if self.use_headers {
315351
let s = self.current_header.as_ref().map(|s| &**s).unwrap_or("");
316352
format!("{}_{}", s, self.cnt)
@@ -321,6 +357,7 @@ impl Collector {
321357
let libs = self.libs.clone();
322358
let externs = self.externs.clone();
323359
let cratename = self.cratename.to_string();
360+
let inject_crate = self.inject_crate;
324361
debug!("Creating test {}: {}", name, test);
325362
self.tests.push(testing::TestDescAndFn {
326363
desc: testing::TestDesc {
@@ -335,7 +372,8 @@ impl Collector {
335372
externs,
336373
should_panic,
337374
no_run,
338-
as_test_harness);
375+
as_test_harness,
376+
inject_crate);
339377
}))
340378
});
341379
}

0 commit comments

Comments
 (0)