@@ -257,7 +257,26 @@ pub struct TestOpts {
257
257
pub ratchet_noise_percent : Option < f64 > ,
258
258
pub save_metrics : Option < Path > ,
259
259
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
+ }
261
280
}
262
281
263
282
/// Result of parsing the options.
@@ -280,7 +299,9 @@ fn optgroups() -> Vec<getopts::OptGroup> {
280
299
getopts:: optopt( "" , "logfile" , "Write logs to the specified file instead \
281
300
of stdout", "PATH" ) ,
282
301
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") )
284
305
}
285
306
286
307
fn usage ( binary : & str , helpstr : & str ) {
@@ -295,6 +316,10 @@ have a substring match, only those tests are run.
295
316
By default, all tests are run in parallel. This can be altered with the
296
317
RUST_TEST_TASKS environment variable when running tests (set it to 1).
297
318
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
+
298
323
Test Attributes:
299
324
300
325
#[test] - Indicates a function is a test to be run. This function
@@ -351,6 +376,11 @@ pub fn parse_opts(args: &[~str]) -> Option<OptRes> {
351
376
let test_shard = matches. opt_str ( "test-shard" ) ;
352
377
let test_shard = opt_shard ( test_shard) ;
353
378
379
+ let mut nocapture = matches. opt_present ( "nocapture" ) ;
380
+ if !nocapture {
381
+ nocapture = os:: getenv ( "RUST_TEST_NOCAPTURE" ) . is_some ( ) ;
382
+ }
383
+
354
384
let test_opts = TestOpts {
355
385
filter : filter,
356
386
run_ignored : run_ignored,
@@ -360,7 +390,8 @@ pub fn parse_opts(args: &[~str]) -> Option<OptRes> {
360
390
ratchet_noise_percent : ratchet_noise_percent,
361
391
save_metrics : save_metrics,
362
392
test_shard : test_shard,
363
- logfile : logfile
393
+ logfile : logfile,
394
+ nocapture : nocapture,
364
395
} ;
365
396
366
397
Some ( Ok ( test_opts) )
@@ -843,7 +874,7 @@ fn run_tests(opts: &TestOpts,
843
874
// that hang forever.
844
875
try!( callback ( TeWait ( test. desc . clone ( ) , test. testfn . padding ( ) ) ) ) ;
845
876
}
846
- run_test ( !opts. run_tests , test, tx. clone ( ) ) ;
877
+ run_test ( opts , !opts. run_tests , test, tx. clone ( ) ) ;
847
878
pending += 1 ;
848
879
}
849
880
@@ -859,7 +890,7 @@ fn run_tests(opts: &TestOpts,
859
890
// (this includes metric fns)
860
891
for b in filtered_benchs_and_metrics. move_iter ( ) {
861
892
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 ( ) ) ;
863
894
let ( test, result, stdout) = rx. recv ( ) ;
864
895
try!( callback ( TeResult ( test, result, stdout) ) ) ;
865
896
}
@@ -941,7 +972,8 @@ pub fn filter_tests(
941
972
}
942
973
}
943
974
944
- pub fn run_test ( force_ignore : bool ,
975
+ pub fn run_test ( opts : & TestOpts ,
976
+ force_ignore : bool ,
945
977
test : TestDescAndFn ,
946
978
monitor_ch : Sender < MonitorMsg > ) {
947
979
@@ -955,6 +987,7 @@ pub fn run_test(force_ignore: bool,
955
987
#[ allow( deprecated_owned_vector) ]
956
988
fn run_test_inner ( desc : TestDesc ,
957
989
monitor_ch : Sender < MonitorMsg > ,
990
+ nocapture : bool ,
958
991
testfn : proc ( ) : Send ) {
959
992
spawn ( proc ( ) {
960
993
let ( tx, rx) = channel ( ) ;
@@ -965,8 +998,12 @@ pub fn run_test(force_ignore: bool,
965
998
DynTestName ( ref name) => name. clone ( ) . into_maybe_owned ( ) ,
966
999
StaticTestName ( name) => name. into_maybe_owned ( ) ,
967
1000
} ) ;
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
+ }
970
1007
let result_future = task. future_result ( ) ;
971
1008
task. spawn ( testfn) ;
972
1009
@@ -1000,8 +1037,9 @@ pub fn run_test(force_ignore: bool,
1000
1037
monitor_ch. send ( ( desc, TrMetrics ( mm) , Vec :: new ( ) ) ) ;
1001
1038
return ;
1002
1039
}
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( ) )
1005
1043
}
1006
1044
}
1007
1045
@@ -1320,7 +1358,7 @@ mod tests {
1320
1358
testfn : DynTestFn ( proc ( ) f( ) ) ,
1321
1359
} ;
1322
1360
let ( tx, rx) = channel ( ) ;
1323
- run_test ( false , desc, tx) ;
1361
+ run_test ( & TestOpts :: new ( ) , false , desc, tx) ;
1324
1362
let ( _, res, _) = rx. recv ( ) ;
1325
1363
assert ! ( res != TrOk ) ;
1326
1364
}
@@ -1337,7 +1375,7 @@ mod tests {
1337
1375
testfn : DynTestFn ( proc ( ) f( ) ) ,
1338
1376
} ;
1339
1377
let ( tx, rx) = channel ( ) ;
1340
- run_test ( false , desc, tx) ;
1378
+ run_test ( & TestOpts :: new ( ) , false , desc, tx) ;
1341
1379
let ( _, res, _) = rx. recv ( ) ;
1342
1380
assert ! ( res == TrIgnored ) ;
1343
1381
}
@@ -1354,7 +1392,7 @@ mod tests {
1354
1392
testfn : DynTestFn ( proc ( ) f( ) ) ,
1355
1393
} ;
1356
1394
let ( tx, rx) = channel ( ) ;
1357
- run_test ( false , desc, tx) ;
1395
+ run_test ( & TestOpts :: new ( ) , false , desc, tx) ;
1358
1396
let ( _, res, _) = rx. recv ( ) ;
1359
1397
assert ! ( res == TrOk ) ;
1360
1398
}
@@ -1371,7 +1409,7 @@ mod tests {
1371
1409
testfn : DynTestFn ( proc ( ) f( ) ) ,
1372
1410
} ;
1373
1411
let ( tx, rx) = channel ( ) ;
1374
- run_test ( false , desc, tx) ;
1412
+ run_test ( & TestOpts :: new ( ) , false , desc, tx) ;
1375
1413
let ( _, res, _) = rx. recv ( ) ;
1376
1414
assert ! ( res == TrFailed ) ;
1377
1415
}
@@ -1401,17 +1439,9 @@ mod tests {
1401
1439
// When we run ignored tests the test filter should filter out all the
1402
1440
// unignored tests and flip the ignore flag on the rest to false
1403
1441
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 ;
1415
1445
1416
1446
let tests = vec ! (
1417
1447
TestDescAndFn {
@@ -1439,17 +1469,8 @@ mod tests {
1439
1469
1440
1470
#[ test]
1441
1471
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 ;
1453
1474
1454
1475
let names =
1455
1476
vec ! ( "sha1::test" . to_owned( ) , "int::test_to_str" . to_owned( ) , "int::test_pow" . to_owned( ) ,
0 commit comments