@@ -246,6 +246,13 @@ impl MDBook {
246246
247247 /// Run `rustdoc` tests on the book, linking against the provided libraries.
248248 pub fn test ( & mut self , library_paths : Vec < & str > ) -> Result < ( ) > {
249+ // test_chapter with chapter:None will run all tests.
250+ self . test_chapter ( library_paths, None )
251+ }
252+
253+ /// Run `rustdoc` tests on a specific chapter of the book, linking against the provided libraries.
254+ /// If `chapter` is `None`, all tests will be run.
255+ pub fn test_chapter ( & mut self , library_paths : Vec < & str > , chapter : Option < & str > ) -> Result < ( ) > {
249256 let library_args: Vec < & str > = ( 0 ..library_paths. len ( ) )
250257 . map ( |_| "-L" )
251258 . zip ( library_paths. into_iter ( ) )
@@ -254,6 +261,8 @@ impl MDBook {
254261
255262 let temp_dir = TempFileBuilder :: new ( ) . prefix ( "mdbook-" ) . tempdir ( ) ?;
256263
264+ let mut chapter_found = false ;
265+
257266 // FIXME: Is "test" the proper renderer name to use here?
258267 let preprocess_context =
259268 PreprocessorContext :: new ( self . root . clone ( ) , self . config . clone ( ) , "test" . to_string ( ) ) ;
@@ -270,8 +279,16 @@ impl MDBook {
270279 _ => continue ,
271280 } ;
272281
273- let path = self . source_dir ( ) . join ( & chapter_path) ;
274- info ! ( "Testing file: {:?}" , path) ;
282+ if let Some ( chapter) = chapter {
283+ if ch. name != chapter && chapter_path. to_str ( ) != Some ( chapter) {
284+ if chapter == "?" {
285+ info ! ( "Skipping chapter '{}'..." , ch. name) ;
286+ }
287+ continue ;
288+ }
289+ }
290+ chapter_found = true ;
291+ info ! ( "Testing chapter '{}': {:?}" , ch. name, chapter_path) ;
275292
276293 // write preprocessed file to tempdir
277294 let path = temp_dir. path ( ) . join ( & chapter_path) ;
@@ -311,6 +328,11 @@ impl MDBook {
311328 if failed {
312329 bail ! ( "One or more tests failed" ) ;
313330 }
331+ if let Some ( chapter) = chapter {
332+ if !chapter_found {
333+ bail ! ( "Chapter not found: {}" , chapter) ;
334+ }
335+ }
314336 Ok ( ( ) )
315337 }
316338
0 commit comments