@@ -63,8 +63,19 @@ fn default_configuration(session::session sess, str argv0, str input) ->
63
63
fn build_configuration ( session:: session sess, str argv0 ,
64
64
str input ) -> ast:: crate_cfg {
65
65
// Combine the configuration requested by the session (command line) with
66
- // some default configuration items
67
- ret sess. get_opts ( ) . cfg + default_configuration ( sess, argv0, input) ;
66
+ // some default and generated configuration items
67
+ auto default_cfg = default_configuration ( sess, argv0, input) ;
68
+ auto user_cfg = sess. get_opts ( ) . cfg ;
69
+ auto gen_cfg = {
70
+ // If the user wants a test runner, then add the test cfg
71
+ if ( sess. get_opts ( ) . test
72
+ && !attr:: contains_name ( user_cfg, "test" ) ) {
73
+ ~[ attr:: mk_word_item ( "test" ) ]
74
+ } else {
75
+ ~[ ]
76
+ }
77
+ } ;
78
+ ret user_cfg + gen_cfg + default_cfg;
68
79
}
69
80
70
81
// Convert strings provided as --cfg [cfgspec] into a crate_cfg
@@ -374,9 +385,8 @@ fn parse_pretty(session::session sess, &str name) -> pp_mode {
374
385
"`identified`" ) ;
375
386
}
376
387
377
- fn main ( vec[ str] args) {
378
- auto opts =
379
- [ optflag ( "h" ) , optflag ( "help" ) , optflag ( "v" ) , optflag ( "version" ) ,
388
+ fn opts ( ) -> vec[ getopts:: opt ] {
389
+ ret [ optflag ( "h" ) , optflag ( "help" ) , optflag ( "v" ) , optflag ( "version" ) ,
380
390
optflag ( "glue" ) , optflag ( "emit-llvm" ) , optflagopt ( "pretty" ) ,
381
391
optflag ( "ls" ) , optflag ( "parse-only" ) , optflag ( "O" ) ,
382
392
optopt ( "OptLevel" ) , optmulti ( "L" ) , optflag ( "S" ) ,
@@ -385,10 +395,13 @@ fn main(vec[str] args) {
385
395
optflag ( "time-llvm-passes" ) , optflag ( "no-typestate" ) ,
386
396
optflag ( "noverify" ) , optmulti ( "cfg" ) , optflag ( "test" ) ,
387
397
optflag ( "lib" ) , optflag ( "static" ) ] ;
398
+ }
399
+
400
+ fn main( vec[ str] args ) {
388
401
auto binary = vec:: shift[ str] ( args) ;
389
402
auto binary_dir = fs:: dirname ( binary) ;
390
403
auto match =
391
- alt ( getopts:: getopts ( args, opts) ) {
404
+ alt ( getopts:: getopts ( args, opts ( ) ) ) {
392
405
case ( getopts:: success ( ?m) ) { m }
393
406
case ( getopts:: failure ( ?f) ) {
394
407
log_err #fmt( "error: %s" , getopts:: fail_str ( f) ) ;
@@ -572,6 +585,40 @@ fn main(vec[str] args) {
572
585
run:: run_program ( "rm" , [ saved_out_filename + ".o" ] ) ;
573
586
}
574
587
}
588
+
589
+ #[ cfg( test) ]
590
+ mod test {
591
+
592
+ import std:: ivec;
593
+
594
+ // When the user supplies --test we should implicitly supply --cfg test
595
+ #[ test]
596
+ fn test_switch_implies_cfg_test ( ) {
597
+ auto match = alt ( getopts:: getopts ( [ "--test" ] , opts ( ) ) ) {
598
+ getopts:: success ( ?m) { m }
599
+ } ;
600
+ auto sessopts = build_session_options ( "whatever" , match , "whatever" ) ;
601
+ auto sess = build_session ( sessopts) ;
602
+ auto cfg = build_configuration ( sess, "whatever" , "whatever" ) ;
603
+ assert attr:: contains_name ( cfg, "test" ) ;
604
+ }
605
+
606
+ // When the user supplies --test and --cfg test, don't implicitly add
607
+ // another --cfg test
608
+ #[ test]
609
+ fn test_switch_implies_cfg_test_unless_cfg_test ( ) {
610
+ auto match = alt ( getopts:: getopts ( [ "--test" ,
611
+ "--cfg=test" ] , opts ( ) ) ) {
612
+ getopts:: success ( ?m) { m }
613
+ } ;
614
+ auto sessopts = build_session_options ( "whatever" , match , "whatever" ) ;
615
+ auto sess = build_session ( sessopts) ;
616
+ auto cfg = build_configuration ( sess, "whatever" , "whatever" ) ;
617
+ auto test_items = attr:: find_meta_items_by_name ( cfg, "test" ) ;
618
+ assert ivec:: len ( test_items) == 1 u;
619
+ }
620
+ }
621
+
575
622
// Local Variables:
576
623
// mode: rust
577
624
// fill-column: 78;
0 commit comments