@@ -120,7 +120,8 @@ pub fn run(mut options: Options) -> isize {
120120 Some ( source_map) ,
121121 None ,
122122 options. linker ,
123- options. edition
123+ options. edition ,
124+ options. persist_doctests ,
124125 ) ;
125126
126127 {
@@ -184,7 +185,8 @@ fn run_test(test: &str, cratename: &str, filename: &FileName, line: usize,
184185 cg : CodegenOptions , externs : Externs ,
185186 should_panic : bool , no_run : bool , as_test_harness : bool ,
186187 compile_fail : bool , mut error_codes : Vec < String > , opts : & TestOptions ,
187- maybe_sysroot : Option < PathBuf > , linker : Option < PathBuf > , edition : Edition ) {
188+ maybe_sysroot : Option < PathBuf > , linker : Option < PathBuf > , edition : Edition ,
189+ persist_doctests : Option < PathBuf > ) {
188190 // The test harness wants its own `main` and top-level functions, so
189191 // never wrap the test in `fn main() { ... }`.
190192 let ( test, line_offset) = make_test ( test, Some ( cratename) , as_test_harness, opts) ;
@@ -249,6 +251,20 @@ fn run_test(test: &str, cratename: &str, filename: &FileName, line: usize,
249251 let old = io:: set_panic ( Some ( box Sink ( data. clone ( ) ) ) ) ;
250252 let _bomb = Bomb ( data. clone ( ) , old. unwrap_or ( box io:: stdout ( ) ) ) ;
251253
254+ enum DirState {
255+ Temp ( tempfile:: TempDir ) ,
256+ Perm ( PathBuf ) ,
257+ }
258+
259+ impl DirState {
260+ fn path ( & self ) -> & std:: path:: Path {
261+ match self {
262+ DirState :: Temp ( t) => t. path ( ) ,
263+ DirState :: Perm ( p) => p. as_path ( ) ,
264+ }
265+ }
266+ }
267+
252268 let ( libdir, outdir, compile_result) = driver:: spawn_thread_pool ( sessopts, |sessopts| {
253269 let source_map = Lrc :: new ( SourceMap :: new ( sessopts. file_path_mapping ( ) ) ) ;
254270 let emitter = errors:: emitter:: EmitterWriter :: new ( box Sink ( data. clone ( ) ) ,
@@ -267,7 +283,17 @@ fn run_test(test: &str, cratename: &str, filename: &FileName, line: usize,
267283 rustc_lint:: register_builtins ( & mut sess. lint_store . borrow_mut ( ) , Some ( & sess) ) ;
268284
269285 let outdir = Mutex :: new (
270- TempFileBuilder :: new ( ) . prefix ( "rustdoctest" ) . tempdir ( ) . expect ( "rustdoc needs a tempdir" )
286+ if let Some ( mut path) = persist_doctests {
287+ path. push ( format ! ( "{}_{}" , filename. to_string( ) . rsplit( '/' ) . next( ) . unwrap( ) . replace( "." , "_" ) , line) ) ;
288+ std:: fs:: create_dir_all ( & path) . expect ( "Couldn't create directory for doctest executables" ) ;
289+
290+ DirState :: Perm ( path)
291+ } else {
292+ DirState :: Temp ( TempFileBuilder :: new ( )
293+ . prefix ( "rustdoctest" )
294+ . tempdir ( )
295+ . expect ( "rustdoc needs a tempdir" ) )
296+ }
271297 ) ;
272298 let libdir = sess. target_filesearch ( PathKind :: All ) . get_lib_path ( ) ;
273299 let mut control = driver:: CompileController :: basic ( ) ;
@@ -629,13 +655,15 @@ pub struct Collector {
629655 filename : Option < PathBuf > ,
630656 linker : Option < PathBuf > ,
631657 edition : Edition ,
658+ persist_doctests : Option < PathBuf > ,
632659}
633660
634661impl Collector {
635662 pub fn new ( cratename : String , cfgs : Vec < String > , libs : Vec < SearchPath > , cg : CodegenOptions ,
636663 externs : Externs , use_headers : bool , opts : TestOptions ,
637664 maybe_sysroot : Option < PathBuf > , source_map : Option < Lrc < SourceMap > > ,
638- filename : Option < PathBuf > , linker : Option < PathBuf > , edition : Edition ) -> Collector {
665+ filename : Option < PathBuf > , linker : Option < PathBuf > , edition : Edition ,
666+ persist_doctests : Option < PathBuf > ) -> Collector {
639667 Collector {
640668 tests : Vec :: new ( ) ,
641669 names : Vec :: new ( ) ,
@@ -652,6 +680,7 @@ impl Collector {
652680 filename,
653681 linker,
654682 edition,
683+ persist_doctests,
655684 }
656685 }
657686
@@ -695,6 +724,8 @@ impl Tester for Collector {
695724 let maybe_sysroot = self . maybe_sysroot . clone ( ) ;
696725 let linker = self . linker . clone ( ) ;
697726 let edition = config. edition . unwrap_or ( self . edition ) ;
727+ let persist_doctests = self . persist_doctests . clone ( ) ;
728+
698729 debug ! ( "Creating test {}: {}" , name, test) ;
699730 self . tests . push ( testing:: TestDescAndFn {
700731 desc : testing:: TestDesc {
@@ -727,7 +758,8 @@ impl Tester for Collector {
727758 & opts,
728759 maybe_sysroot,
729760 linker,
730- edition)
761+ edition,
762+ persist_doctests)
731763 } ) )
732764 } {
733765 Ok ( ( ) ) => ( ) ,
0 commit comments