Skip to content

Commit f30ed77

Browse files
Add option to display warnings in rustdoc
1 parent 06fb4d2 commit f30ed77

File tree

6 files changed

+67
-10
lines changed

6 files changed

+67
-10
lines changed

Diff for: src/librustdoc/core.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ pub fn run_core(search_paths: SearchPaths,
104104
externs: config::Externs,
105105
input: Input,
106106
triple: Option<String>,
107-
maybe_sysroot: Option<PathBuf>) -> (clean::Crate, RenderInfo)
107+
maybe_sysroot: Option<PathBuf>,
108+
allow_warnings: bool) -> (clean::Crate, RenderInfo)
108109
{
109110
// Parse, resolve, and typecheck the given crate.
110111

@@ -119,7 +120,7 @@ pub fn run_core(search_paths: SearchPaths,
119120
maybe_sysroot: maybe_sysroot,
120121
search_paths: search_paths,
121122
crate_types: vec![config::CrateTypeRlib],
122-
lint_opts: vec![(warning_lint, lint::Allow)],
123+
lint_opts: if !allow_warnings { vec![(warning_lint, lint::Allow)] } else { vec![] },
123124
lint_cap: Some(lint::Allow),
124125
externs: externs,
125126
target_triple: triple.unwrap_or(config::host_triple().to_string()),

Diff for: src/librustdoc/lib.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ pub fn opts() -> Vec<RustcOptGroup> {
172172
or `#![doc(html_playground_url=...)]`",
173173
"URL")),
174174
unstable(optflag("", "enable-commonmark", "to enable commonmark doc rendering/testing")),
175+
unstable(optflag("", "display-warnings", "to print code warnings when testing doc")),
175176
]
176177
}
177178

@@ -279,14 +280,16 @@ pub fn main_args(args: &[String]) -> isize {
279280
let crate_name = matches.opt_str("crate-name");
280281
let playground_url = matches.opt_str("playground-url");
281282
let maybe_sysroot = matches.opt_str("sysroot").map(PathBuf::from);
283+
let display_warnings = matches.opt_present("display-warnings");
282284

283285
match (should_test, markdown_input) {
284286
(true, true) => {
285-
return markdown::test(input, cfgs, libs, externs, test_args, maybe_sysroot, render_type)
287+
return markdown::test(input, cfgs, libs, externs, test_args, maybe_sysroot, render_type,
288+
display_warnings)
286289
}
287290
(true, false) => {
288291
return test::run(input, cfgs, libs, externs, test_args, crate_name, maybe_sysroot,
289-
render_type)
292+
render_type, display_warnings)
290293
}
291294
(false, true) => return markdown::render(input,
292295
output.unwrap_or(PathBuf::from("doc")),
@@ -388,13 +391,15 @@ where R: 'static + Send, F: 'static + Send + FnOnce(Output) -> R {
388391

389392
let cr = PathBuf::from(cratefile);
390393
info!("starting to run rustc");
394+
let display_warnings = matches.opt_present("display-warnings");
391395

392396
let (tx, rx) = channel();
393397
rustc_driver::monitor(move || {
394398
use rustc::session::config::Input;
395399

396400
let (mut krate, renderinfo) =
397-
core::run_core(paths, cfgs, externs, Input::File(cr), triple, maybe_sysroot);
401+
core::run_core(paths, cfgs, externs, Input::File(cr), triple, maybe_sysroot,
402+
display_warnings);
398403

399404
info!("finished with rustc");
400405

Diff for: src/librustdoc/markdown.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ pub fn render(input: &str, mut output: PathBuf, matches: &getopts::Matches,
150150
/// Run any tests/code examples in the markdown file `input`.
151151
pub fn test(input: &str, cfgs: Vec<String>, libs: SearchPaths, externs: Externs,
152152
mut test_args: Vec<String>, maybe_sysroot: Option<PathBuf>,
153-
render_type: RenderType) -> isize {
153+
render_type: RenderType, display_warnings: bool) -> isize {
154154
let input_str = match load_string(input) {
155155
Ok(s) => s,
156156
Err(LoadStringError::ReadFail) => return 1,
@@ -166,6 +166,9 @@ pub fn test(input: &str, cfgs: Vec<String>, libs: SearchPaths, externs: Externs,
166166
old_find_testable_code(&input_str, &mut collector, DUMMY_SP);
167167
find_testable_code(&input_str, &mut collector, DUMMY_SP);
168168
test_args.insert(0, "rustdoctest".to_string());
169+
if display_warnings {
170+
test_args.insert(1, "--display-stdout".to_string());
171+
}
169172
testing::test_main(&test_args, collector.tests);
170173
0
171174
}

Diff for: src/librustdoc/test.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ pub fn run(input: &str,
5858
mut test_args: Vec<String>,
5959
crate_name: Option<String>,
6060
maybe_sysroot: Option<PathBuf>,
61-
render_type: RenderType)
61+
render_type: RenderType,
62+
display_warnings: bool)
6263
-> isize {
6364
let input_path = PathBuf::from(input);
6465
let input = config::Input::File(input_path.clone());
@@ -125,6 +126,9 @@ pub fn run(input: &str,
125126
}
126127

127128
test_args.insert(0, "rustdoctest".to_string());
129+
if display_warnings {
130+
test_args.insert(1, "--display-stdout".to_string());
131+
}
128132

129133
testing::test_main(&test_args,
130134
collector.tests.into_iter().collect());

Diff for: src/libtest/lib.rs

+46-3
Original file line numberDiff line numberDiff line change
@@ -304,13 +304,14 @@ pub fn test_main_static(tests: &[TestDescAndFn]) {
304304
test_main(&args, owned_tests)
305305
}
306306

307-
#[derive(Copy, Clone)]
307+
#[derive(Copy, Clone, Debug)]
308308
pub enum ColorConfig {
309309
AutoColor,
310310
AlwaysColor,
311311
NeverColor,
312312
}
313313

314+
#[derive(Debug)]
314315
pub struct TestOpts {
315316
pub list: bool,
316317
pub filter: Option<String>,
@@ -324,6 +325,7 @@ pub struct TestOpts {
324325
pub quiet: bool,
325326
pub test_threads: Option<usize>,
326327
pub skip: Vec<String>,
328+
pub display_stdout: bool,
327329
}
328330

329331
impl TestOpts {
@@ -342,6 +344,7 @@ impl TestOpts {
342344
quiet: false,
343345
test_threads: None,
344346
skip: vec![],
347+
display_stdout: false,
345348
}
346349
}
347350
}
@@ -369,7 +372,8 @@ fn optgroups() -> Vec<getopts::OptGroup> {
369372
getopts::optopt("", "color", "Configure coloring of output:
370373
auto = colorize if stdout is a tty and tests are run on serially (default);
371374
always = always colorize output;
372-
never = never colorize output;", "auto|always|never")]
375+
never = never colorize output;", "auto|always|never"),
376+
getopts::optflag("", "display-stdout", "to print stdout even if the test succeeds")]
373377
}
374378

375379
fn usage(binary: &str) {
@@ -481,6 +485,7 @@ pub fn parse_opts(args: &[String]) -> Option<OptRes> {
481485
quiet: quiet,
482486
test_threads: test_threads,
483487
skip: matches.opt_strs("skip"),
488+
display_stdout: matches.opt_present("display-stdout"),
484489
};
485490

486491
Some(Ok(test_opts))
@@ -521,7 +526,9 @@ struct ConsoleTestState<T> {
521526
measured: usize,
522527
metrics: MetricMap,
523528
failures: Vec<(TestDesc, Vec<u8>)>,
529+
not_failures: Vec<(TestDesc, Vec<u8>)>,
524530
max_name_len: usize, // number of columns to fill when aligning names
531+
display_stdout: bool,
525532
}
526533

527534
impl<T: Write> ConsoleTestState<T> {
@@ -547,7 +554,9 @@ impl<T: Write> ConsoleTestState<T> {
547554
measured: 0,
548555
metrics: MetricMap::new(),
549556
failures: Vec::new(),
557+
not_failures: Vec::new(),
550558
max_name_len: 0,
559+
display_stdout: opts.display_stdout,
551560
})
552561
}
553562

@@ -703,9 +712,38 @@ impl<T: Write> ConsoleTestState<T> {
703712
Ok(())
704713
}
705714

715+
pub fn write_outputs(&mut self) -> io::Result<()> {
716+
self.write_plain("\nsuccesses:\n")?;
717+
let mut successes = Vec::new();
718+
let mut stdouts = String::new();
719+
for &(ref f, ref stdout) in &self.not_failures {
720+
successes.push(f.name.to_string());
721+
if !stdout.is_empty() {
722+
stdouts.push_str(&format!("---- {} stdout ----\n\t", f.name));
723+
let output = String::from_utf8_lossy(stdout);
724+
stdouts.push_str(&output);
725+
stdouts.push_str("\n");
726+
}
727+
}
728+
if !stdouts.is_empty() {
729+
self.write_plain("\n")?;
730+
self.write_plain(&stdouts)?;
731+
}
732+
733+
self.write_plain("\nsuccesses:\n")?;
734+
successes.sort();
735+
for name in &successes {
736+
self.write_plain(&format!(" {}\n", name))?;
737+
}
738+
Ok(())
739+
}
740+
706741
pub fn write_run_finish(&mut self) -> io::Result<bool> {
707742
assert!(self.passed + self.failed + self.ignored + self.measured == self.total);
708743

744+
if self.display_stdout {
745+
self.write_outputs()?;
746+
}
709747
let success = self.failed == 0;
710748
if !success {
711749
self.write_failures()?;
@@ -824,7 +862,10 @@ pub fn run_tests_console(opts: &TestOpts, tests: Vec<TestDescAndFn>) -> io::Resu
824862
st.write_log_result(&test, &result)?;
825863
st.write_result(&result)?;
826864
match result {
827-
TrOk => st.passed += 1,
865+
TrOk => {
866+
st.passed += 1;
867+
st.not_failures.push((test, stdout));
868+
}
828869
TrIgnored => st.ignored += 1,
829870
TrMetrics(mm) => {
830871
let tname = test.name;
@@ -901,6 +942,8 @@ fn should_sort_failures_before_printing_them() {
901942
max_name_len: 10,
902943
metrics: MetricMap::new(),
903944
failures: vec![(test_b, Vec::new()), (test_a, Vec::new())],
945+
display_stdout: false,
946+
not_failures: Vec::new(),
904947
};
905948

906949
st.write_failures().unwrap();

Diff for: src/tools/compiletest/src/main.rs

+1
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ pub fn test_opts(config: &Config) -> test::TestOpts {
336336
test_threads: None,
337337
skip: vec![],
338338
list: false,
339+
display_stdout: false,
339340
}
340341
}
341342

0 commit comments

Comments
 (0)