@@ -29,20 +29,21 @@ use std::sync::atomic::{AtomicUsize, Ordering};
29
29
use std:: sync:: { Arc , Mutex } ;
30
30
31
31
use crate :: clean:: { types:: AttributesExt , Attributes } ;
32
- use crate :: config:: Options ;
32
+ use crate :: config:: Options as RustdocOptions ;
33
33
use crate :: html:: markdown:: { self , ErrorCodes , Ignore , LangString } ;
34
34
use crate :: lint:: init_lints;
35
35
use crate :: passes:: span_of_attrs;
36
36
37
+ /// Options that apply to all doctests in a crate or Markdown file (for `rustdoc foo.md`).
37
38
#[ derive( Clone , Default ) ]
38
- crate struct TestOptions {
39
+ crate struct GlobalTestOptions {
39
40
/// Whether to disable the default `extern crate my_crate;` when creating doctests.
40
41
crate no_crate_inject : bool ,
41
42
/// Additional crate-level attributes to add to doctests.
42
43
crate attrs : Vec < String > ,
43
44
}
44
45
45
- crate fn run ( options : Options ) -> Result < ( ) , ErrorReported > {
46
+ crate fn run ( options : RustdocOptions ) -> Result < ( ) , ErrorReported > {
46
47
let input = config:: Input :: File ( options. input . clone ( ) ) ;
47
48
48
49
let invalid_codeblock_attributes_name = crate :: lint:: INVALID_CODEBLOCK_ATTRIBUTES . name ;
@@ -214,10 +215,10 @@ crate fn run_tests(mut test_args: Vec<String>, nocapture: bool, tests: Vec<test:
214
215
}
215
216
216
217
// Look for `#![doc(test(no_crate_inject))]`, used by crates in the std facade.
217
- fn scrape_test_config ( attrs : & [ ast:: Attribute ] ) -> TestOptions {
218
+ fn scrape_test_config ( attrs : & [ ast:: Attribute ] ) -> GlobalTestOptions {
218
219
use rustc_ast_pretty:: pprust;
219
220
220
- let mut opts = TestOptions { no_crate_inject : false , attrs : Vec :: new ( ) } ;
221
+ let mut opts = GlobalTestOptions { no_crate_inject : false , attrs : Vec :: new ( ) } ;
221
222
222
223
let test_attrs: Vec < _ > = attrs
223
224
. iter ( )
@@ -292,66 +293,63 @@ fn run_test(
292
293
test : & str ,
293
294
crate_name : & str ,
294
295
line : usize ,
295
- options : Options ,
296
- should_panic : bool ,
296
+ rustdoc_options : RustdocOptions ,
297
+ mut lang_string : LangString ,
297
298
no_run : bool ,
298
- as_test_harness : bool ,
299
299
runtool : Option < String > ,
300
300
runtool_args : Vec < String > ,
301
301
target : TargetTriple ,
302
- compile_fail : bool ,
303
- mut error_codes : Vec < String > ,
304
- opts : & TestOptions ,
302
+ opts : & GlobalTestOptions ,
305
303
edition : Edition ,
306
304
outdir : DirState ,
307
305
path : PathBuf ,
308
306
test_id : & str ,
309
307
report_unused_externs : impl Fn ( UnusedExterns ) ,
310
308
) -> Result < ( ) , TestFailure > {
311
309
let ( test, line_offset, supports_color) =
312
- make_test ( test, Some ( crate_name) , as_test_harness , opts, edition, Some ( test_id) ) ;
310
+ make_test ( test, Some ( crate_name) , lang_string . test_harness , opts, edition, Some ( test_id) ) ;
313
311
314
312
let output_file = outdir. path ( ) . join ( "rust_out" ) ;
315
313
316
- let rustc_binary = options
314
+ let rustc_binary = rustdoc_options
317
315
. test_builder
318
316
. as_deref ( )
319
317
. unwrap_or_else ( || rustc_interface:: util:: rustc_path ( ) . expect ( "found rustc" ) ) ;
320
318
let mut compiler = Command :: new ( & rustc_binary) ;
321
319
compiler. arg ( "--crate-type" ) . arg ( "bin" ) ;
322
- for cfg in & options . cfgs {
320
+ for cfg in & rustdoc_options . cfgs {
323
321
compiler. arg ( "--cfg" ) . arg ( & cfg) ;
324
322
}
325
- if let Some ( sysroot) = options . maybe_sysroot {
323
+ if let Some ( sysroot) = rustdoc_options . maybe_sysroot {
326
324
compiler. arg ( "--sysroot" ) . arg ( sysroot) ;
327
325
}
328
326
compiler. arg ( "--edition" ) . arg ( & edition. to_string ( ) ) ;
329
327
compiler. env ( "UNSTABLE_RUSTDOC_TEST_PATH" , path) ;
330
328
compiler. env ( "UNSTABLE_RUSTDOC_TEST_LINE" , format ! ( "{}" , line as isize - line_offset as isize ) ) ;
331
329
compiler. arg ( "-o" ) . arg ( & output_file) ;
332
- if as_test_harness {
330
+ if lang_string . test_harness {
333
331
compiler. arg ( "--test" ) ;
334
332
}
335
- if options . json_unused_externs && !compile_fail {
333
+ if rustdoc_options . json_unused_externs && !lang_string . compile_fail {
336
334
compiler. arg ( "--error-format=json" ) ;
337
335
compiler. arg ( "--json" ) . arg ( "unused-externs" ) ;
338
336
compiler. arg ( "-Z" ) . arg ( "unstable-options" ) ;
339
337
compiler. arg ( "-W" ) . arg ( "unused_crate_dependencies" ) ;
340
338
}
341
- for lib_str in & options . lib_strs {
339
+ for lib_str in & rustdoc_options . lib_strs {
342
340
compiler. arg ( "-L" ) . arg ( & lib_str) ;
343
341
}
344
- for extern_str in & options . extern_strs {
342
+ for extern_str in & rustdoc_options . extern_strs {
345
343
compiler. arg ( "--extern" ) . arg ( & extern_str) ;
346
344
}
347
345
compiler. arg ( "-Ccodegen-units=1" ) ;
348
- for codegen_options_str in & options . codegen_options_strs {
346
+ for codegen_options_str in & rustdoc_options . codegen_options_strs {
349
347
compiler. arg ( "-C" ) . arg ( & codegen_options_str) ;
350
348
}
351
- for debugging_option_str in & options . debugging_opts_strs {
349
+ for debugging_option_str in & rustdoc_options . debugging_opts_strs {
352
350
compiler. arg ( "-Z" ) . arg ( & debugging_option_str) ;
353
351
}
354
- if no_run && !compile_fail && options . persist_doctests . is_none ( ) {
352
+ if no_run && !lang_string . compile_fail && rustdoc_options . persist_doctests . is_none ( ) {
355
353
compiler. arg ( "--emit=metadata" ) ;
356
354
}
357
355
compiler. arg ( "--target" ) . arg ( match target {
@@ -360,7 +358,7 @@ fn run_test(
360
358
path. to_str ( ) . expect ( "target path must be valid unicode" ) . to_string ( )
361
359
}
362
360
} ) ;
363
- if let ErrorOutputType :: HumanReadable ( kind) = options . error_format {
361
+ if let ErrorOutputType :: HumanReadable ( kind) = rustdoc_options . error_format {
364
362
let ( short, color_config) = kind. unzip ( ) ;
365
363
366
364
if short {
@@ -418,20 +416,20 @@ fn run_test(
418
416
419
417
let out = out_lines. join ( "\n " ) ;
420
418
let _bomb = Bomb ( & out) ;
421
- match ( output. status . success ( ) , compile_fail) {
419
+ match ( output. status . success ( ) , lang_string . compile_fail ) {
422
420
( true , true ) => {
423
421
return Err ( TestFailure :: UnexpectedCompilePass ) ;
424
422
}
425
423
( true , false ) => { }
426
424
( false , true ) => {
427
- if !error_codes. is_empty ( ) {
425
+ if !lang_string . error_codes . is_empty ( ) {
428
426
// We used to check if the output contained "error[{}]: " but since we added the
429
427
// colored output, we can't anymore because of the color escape characters before
430
428
// the ":".
431
- error_codes. retain ( |err| !out. contains ( & format ! ( "error[{}]" , err) ) ) ;
429
+ lang_string . error_codes . retain ( |err| !out. contains ( & format ! ( "error[{}]" , err) ) ) ;
432
430
433
- if !error_codes. is_empty ( ) {
434
- return Err ( TestFailure :: MissingErrorCodes ( error_codes) ) ;
431
+ if !lang_string . error_codes . is_empty ( ) {
432
+ return Err ( TestFailure :: MissingErrorCodes ( lang_string . error_codes ) ) ;
435
433
}
436
434
}
437
435
}
@@ -454,11 +452,11 @@ fn run_test(
454
452
} else {
455
453
cmd = Command :: new ( output_file) ;
456
454
}
457
- if let Some ( run_directory) = options . test_run_directory {
455
+ if let Some ( run_directory) = rustdoc_options . test_run_directory {
458
456
cmd. current_dir ( run_directory) ;
459
457
}
460
458
461
- let result = if options . nocapture {
459
+ let result = if rustdoc_options . nocapture {
462
460
cmd. status ( ) . map ( |status| process:: Output {
463
461
status,
464
462
stdout : Vec :: new ( ) ,
@@ -470,9 +468,9 @@ fn run_test(
470
468
match result {
471
469
Err ( e) => return Err ( TestFailure :: ExecutionError ( e) ) ,
472
470
Ok ( out) => {
473
- if should_panic && out. status . success ( ) {
471
+ if lang_string . should_panic && out. status . success ( ) {
474
472
return Err ( TestFailure :: UnexpectedRunPass ) ;
475
- } else if !should_panic && !out. status . success ( ) {
473
+ } else if !lang_string . should_panic && !out. status . success ( ) {
476
474
return Err ( TestFailure :: ExecutionFailure ( out) ) ;
477
475
}
478
476
}
@@ -487,7 +485,7 @@ crate fn make_test(
487
485
s : & str ,
488
486
crate_name : Option < & str > ,
489
487
dont_insert_main : bool ,
490
- opts : & TestOptions ,
488
+ opts : & GlobalTestOptions ,
491
489
edition : Edition ,
492
490
test_id : Option < & str > ,
493
491
) -> ( String , usize , bool ) {
@@ -804,11 +802,11 @@ crate struct Collector {
804
802
// the `names` vector of that test will be `["Title", "Subtitle"]`.
805
803
names : Vec < String > ,
806
804
807
- options : Options ,
805
+ rustdoc_options : RustdocOptions ,
808
806
use_headers : bool ,
809
807
enable_per_target_ignores : bool ,
810
808
crate_name : Symbol ,
811
- opts : TestOptions ,
809
+ opts : GlobalTestOptions ,
812
810
position : Span ,
813
811
source_map : Option < Lrc < SourceMap > > ,
814
812
filename : Option < PathBuf > ,
@@ -820,17 +818,17 @@ crate struct Collector {
820
818
impl Collector {
821
819
crate fn new (
822
820
crate_name : Symbol ,
823
- options : Options ,
821
+ rustdoc_options : RustdocOptions ,
824
822
use_headers : bool ,
825
- opts : TestOptions ,
823
+ opts : GlobalTestOptions ,
826
824
source_map : Option < Lrc < SourceMap > > ,
827
825
filename : Option < PathBuf > ,
828
826
enable_per_target_ignores : bool ,
829
827
) -> Collector {
830
828
Collector {
831
829
tests : Vec :: new ( ) ,
832
830
names : Vec :: new ( ) ,
833
- options ,
831
+ rustdoc_options ,
834
832
use_headers,
835
833
enable_per_target_ignores,
836
834
crate_name,
@@ -884,14 +882,14 @@ impl Tester for Collector {
884
882
let name = self . generate_name ( line, & filename) ;
885
883
let crate_name = self . crate_name . to_string ( ) ;
886
884
let opts = self . opts . clone ( ) ;
887
- let edition = config. edition . unwrap_or ( self . options . edition ) ;
888
- let options = self . options . clone ( ) ;
889
- let runtool = self . options . runtool . clone ( ) ;
890
- let runtool_args = self . options . runtool_args . clone ( ) ;
891
- let target = self . options . target . clone ( ) ;
885
+ let edition = config. edition . unwrap_or ( self . rustdoc_options . edition ) ;
886
+ let rustdoc_options = self . rustdoc_options . clone ( ) ;
887
+ let runtool = self . rustdoc_options . runtool . clone ( ) ;
888
+ let runtool_args = self . rustdoc_options . runtool_args . clone ( ) ;
889
+ let target = self . rustdoc_options . target . clone ( ) ;
892
890
let target_str = target. to_string ( ) ;
893
891
let unused_externs = self . unused_extern_reports . clone ( ) ;
894
- let no_run = config. no_run || options . no_run ;
892
+ let no_run = config. no_run || rustdoc_options . no_run ;
895
893
if !config. compile_fail {
896
894
self . compiling_test_count . fetch_add ( 1 , Ordering :: SeqCst ) ;
897
895
}
@@ -925,7 +923,7 @@ impl Tester for Collector {
925
923
self . visited_tests. entry( ( file. clone( ) , line) ) . and_modify( |v| * v += 1 ) . or_insert( 0 )
926
924
} ,
927
925
) ;
928
- let outdir = if let Some ( mut path) = options . persist_doctests . clone ( ) {
926
+ let outdir = if let Some ( mut path) = rustdoc_options . persist_doctests . clone ( ) {
929
927
path. push ( & test_id) ;
930
928
931
929
std:: fs:: create_dir_all ( & path)
@@ -965,15 +963,12 @@ impl Tester for Collector {
965
963
& test,
966
964
& crate_name,
967
965
line,
968
- options ,
969
- config. should_panic ,
966
+ rustdoc_options ,
967
+ config,
970
968
no_run,
971
- config. test_harness ,
972
969
runtool,
973
970
runtool_args,
974
971
target,
975
- config. compile_fail ,
976
- config. error_codes ,
977
972
& opts,
978
973
edition,
979
974
outdir,
0 commit comments