@@ -257,7 +257,26 @@ pub struct TestOpts {
257257 pub ratchet_noise_percent : Option < f64 > ,
258258 pub save_metrics : Option < Path > ,
259259 pub test_shard : Option < ( uint , uint ) > ,
260- pub logfile : Option < Path >
260+ pub logfile : Option < Path > ,
261+ pub nocapture : bool ,
262+ }
263+
264+ impl TestOpts {
265+ #[ cfg( test) ]
266+ fn new ( ) -> TestOpts {
267+ TestOpts {
268+ filter : None ,
269+ run_ignored : false ,
270+ run_tests : false ,
271+ run_benchmarks : false ,
272+ ratchet_metrics : None ,
273+ ratchet_noise_percent : None ,
274+ save_metrics : None ,
275+ test_shard : None ,
276+ logfile : None ,
277+ nocapture : false ,
278+ }
279+ }
261280}
262281
263282/// Result of parsing the options.
@@ -280,7 +299,9 @@ fn optgroups() -> Vec<getopts::OptGroup> {
280299 getopts:: optopt( "" , "logfile" , "Write logs to the specified file instead \
281300 of stdout", "PATH" ) ,
282301 getopts:: optopt( "" , "test-shard" , "run shard A, of B shards, worth of the testsuite" ,
283- "A.B" ) )
302+ "A.B" ) ,
303+ getopts:: optflag( "" , "nocapture" , "don't capture stdout/stderr of each \
304+ task, allow printing directly") )
284305}
285306
286307fn usage ( binary : & str , helpstr : & str ) {
@@ -295,6 +316,10 @@ have a substring match, only those tests are run.
295316By default, all tests are run in parallel. This can be altered with the
296317RUST_TEST_TASKS environment variable when running tests (set it to 1).
297318
319+ All tests have their standard output and standard error captured by default.
320+ This can be overridden with the --nocapture flag or the RUST_TEST_NOCAPTURE=1
321+ environment variable. Logging is not captured by default.
322+
298323Test Attributes:
299324
300325 #[test] - Indicates a function is a test to be run. This function
@@ -351,6 +376,11 @@ pub fn parse_opts(args: &[~str]) -> Option<OptRes> {
351376 let test_shard = matches. opt_str ( "test-shard" ) ;
352377 let test_shard = opt_shard ( test_shard) ;
353378
379+ let mut nocapture = matches. opt_present ( "nocapture" ) ;
380+ if !nocapture {
381+ nocapture = os:: getenv ( "RUST_TEST_NOCAPTURE" ) . is_some ( ) ;
382+ }
383+
354384 let test_opts = TestOpts {
355385 filter : filter,
356386 run_ignored : run_ignored,
@@ -360,7 +390,8 @@ pub fn parse_opts(args: &[~str]) -> Option<OptRes> {
360390 ratchet_noise_percent : ratchet_noise_percent,
361391 save_metrics : save_metrics,
362392 test_shard : test_shard,
363- logfile : logfile
393+ logfile : logfile,
394+ nocapture : nocapture,
364395 } ;
365396
366397 Some ( Ok ( test_opts) )
@@ -843,7 +874,7 @@ fn run_tests(opts: &TestOpts,
843874 // that hang forever.
844875 try!( callback ( TeWait ( test. desc . clone ( ) , test. testfn . padding ( ) ) ) ) ;
845876 }
846- run_test ( !opts. run_tests , test, tx. clone ( ) ) ;
877+ run_test ( opts , !opts. run_tests , test, tx. clone ( ) ) ;
847878 pending += 1 ;
848879 }
849880
@@ -859,7 +890,7 @@ fn run_tests(opts: &TestOpts,
859890 // (this includes metric fns)
860891 for b in filtered_benchs_and_metrics. move_iter ( ) {
861892 try!( callback ( TeWait ( b. desc . clone ( ) , b. testfn . padding ( ) ) ) ) ;
862- run_test ( !opts. run_benchmarks , b, tx. clone ( ) ) ;
893+ run_test ( opts , !opts. run_benchmarks , b, tx. clone ( ) ) ;
863894 let ( test, result, stdout) = rx. recv ( ) ;
864895 try!( callback ( TeResult ( test, result, stdout) ) ) ;
865896 }
@@ -941,7 +972,8 @@ pub fn filter_tests(
941972 }
942973}
943974
944- pub fn run_test ( force_ignore : bool ,
975+ pub fn run_test ( opts : & TestOpts ,
976+ force_ignore : bool ,
945977 test : TestDescAndFn ,
946978 monitor_ch : Sender < MonitorMsg > ) {
947979
@@ -955,6 +987,7 @@ pub fn run_test(force_ignore: bool,
955987 #[ allow( deprecated_owned_vector) ]
956988 fn run_test_inner ( desc : TestDesc ,
957989 monitor_ch : Sender < MonitorMsg > ,
990+ nocapture : bool ,
958991 testfn : proc ( ) : Send ) {
959992 spawn ( proc ( ) {
960993 let ( tx, rx) = channel ( ) ;
@@ -965,8 +998,12 @@ pub fn run_test(force_ignore: bool,
965998 DynTestName ( ref name) => name. clone ( ) . into_maybe_owned ( ) ,
966999 StaticTestName ( name) => name. into_maybe_owned ( ) ,
9671000 } ) ;
968- task. opts . stdout = Some ( ~stdout as ~Writer : Send ) ;
969- task. opts . stderr = Some ( ~stderr as ~Writer : Send ) ;
1001+ if nocapture {
1002+ drop ( ( stdout, stderr) ) ;
1003+ } else {
1004+ task. opts . stdout = Some ( ~stdout as ~Writer : Send ) ;
1005+ task. opts . stderr = Some ( ~stderr as ~Writer : Send ) ;
1006+ }
9701007 let result_future = task. future_result ( ) ;
9711008 task. spawn ( testfn) ;
9721009
@@ -1000,8 +1037,9 @@ pub fn run_test(force_ignore: bool,
10001037 monitor_ch. send ( ( desc, TrMetrics ( mm) , Vec :: new ( ) ) ) ;
10011038 return ;
10021039 }
1003- DynTestFn ( f) => run_test_inner ( desc, monitor_ch, f) ,
1004- StaticTestFn ( f) => run_test_inner ( desc, monitor_ch, proc ( ) f( ) )
1040+ DynTestFn ( f) => run_test_inner ( desc, monitor_ch, opts. nocapture , f) ,
1041+ StaticTestFn ( f) => run_test_inner ( desc, monitor_ch, opts. nocapture ,
1042+ proc ( ) f( ) )
10051043 }
10061044}
10071045
@@ -1320,7 +1358,7 @@ mod tests {
13201358 testfn : DynTestFn ( proc ( ) f( ) ) ,
13211359 } ;
13221360 let ( tx, rx) = channel ( ) ;
1323- run_test ( false , desc, tx) ;
1361+ run_test ( & TestOpts :: new ( ) , false , desc, tx) ;
13241362 let ( _, res, _) = rx. recv ( ) ;
13251363 assert ! ( res != TrOk ) ;
13261364 }
@@ -1337,7 +1375,7 @@ mod tests {
13371375 testfn : DynTestFn ( proc ( ) f( ) ) ,
13381376 } ;
13391377 let ( tx, rx) = channel ( ) ;
1340- run_test ( false , desc, tx) ;
1378+ run_test ( & TestOpts :: new ( ) , false , desc, tx) ;
13411379 let ( _, res, _) = rx. recv ( ) ;
13421380 assert ! ( res == TrIgnored ) ;
13431381 }
@@ -1354,7 +1392,7 @@ mod tests {
13541392 testfn : DynTestFn ( proc ( ) f( ) ) ,
13551393 } ;
13561394 let ( tx, rx) = channel ( ) ;
1357- run_test ( false , desc, tx) ;
1395+ run_test ( & TestOpts :: new ( ) , false , desc, tx) ;
13581396 let ( _, res, _) = rx. recv ( ) ;
13591397 assert ! ( res == TrOk ) ;
13601398 }
@@ -1371,7 +1409,7 @@ mod tests {
13711409 testfn : DynTestFn ( proc ( ) f( ) ) ,
13721410 } ;
13731411 let ( tx, rx) = channel ( ) ;
1374- run_test ( false , desc, tx) ;
1412+ run_test ( & TestOpts :: new ( ) , false , desc, tx) ;
13751413 let ( _, res, _) = rx. recv ( ) ;
13761414 assert ! ( res == TrFailed ) ;
13771415 }
@@ -1401,17 +1439,9 @@ mod tests {
14011439 // When we run ignored tests the test filter should filter out all the
14021440 // unignored tests and flip the ignore flag on the rest to false
14031441
1404- let opts = TestOpts {
1405- filter : None ,
1406- run_ignored : true ,
1407- logfile : None ,
1408- run_tests : true ,
1409- run_benchmarks : false ,
1410- ratchet_noise_percent : None ,
1411- ratchet_metrics : None ,
1412- save_metrics : None ,
1413- test_shard : None
1414- } ;
1442+ let mut opts = TestOpts :: new ( ) ;
1443+ opts. run_tests = true ;
1444+ opts. run_ignored = true ;
14151445
14161446 let tests = vec ! (
14171447 TestDescAndFn {
@@ -1439,17 +1469,8 @@ mod tests {
14391469
14401470 #[ test]
14411471 pub fn sort_tests ( ) {
1442- let opts = TestOpts {
1443- filter : None ,
1444- run_ignored : false ,
1445- logfile : None ,
1446- run_tests : true ,
1447- run_benchmarks : false ,
1448- ratchet_noise_percent : None ,
1449- ratchet_metrics : None ,
1450- save_metrics : None ,
1451- test_shard : None
1452- } ;
1472+ let mut opts = TestOpts :: new ( ) ;
1473+ opts. run_tests = true ;
14531474
14541475 let names =
14551476 vec ! ( "sha1::test" . to_owned( ) , "int::test_to_str" . to_owned( ) , "int::test_pow" . to_owned( ) ,
0 commit comments