@@ -532,45 +532,165 @@ fn as_vec_u16(str: Option<&OsString>) -> io::Result<(*const u16, Vec<u16>)> {
532532
533533#[ cfg( test) ]
534534mod tests {
535- use ffi:: { OsStr , OsString } ;
536- use super :: make_command_line;
535+ use ffi:: OsString ;
536+ use path:: Path ;
537+ use fs:: canonicalize;
538+ use env:: join_paths;
539+
540+ fn gen_env ( ) -> HashMap < OsString , OsString > {
541+ let mut env: HashMap < OsString , OsString > = HashMap :: new ( ) ;
542+ env. insert ( OsString :: from ( "HOMEDRIVE" ) , OsString :: from ( "C:" ) ) ;
543+ let p1 = canonicalize ( "./src/test/run-pass/process_command/fixtures/bin" ) . unwrap ( ) ;
544+ let p2 = canonicalize ( "./src/test/run-pass/process_command/fixtures" ) . unwrap ( ) ;
545+ let p3 = canonicalize ( "./src/test/run-pass/process_command" ) . unwrap ( ) ;
546+ let paths = vec ! [ p1, p2, p3] ;
547+ let path = join_paths ( paths) . unwrap ( ) ;
548+ env. insert ( OsString :: from ( "PATH" ) , OsString :: from ( & path) ) ;
549+ env. insert ( OsString :: from ( "USERNAME" ) , OsString :: from ( "rust" ) ) ;
550+ env. insert ( OsString :: from ( "COMSPEC" ) , OsString :: from ( "C:\\ Windows\\ system32\\ cmd.exe" ) ) ;
551+ return env
552+ }
537553
538554 #[ test]
539- fn test_make_command_line_with_out_env ( ) {
540- fn test_wrapper ( prog : & str , args : & [ & str ] ) -> String {
541- let command_line = & make_command_line ( OsStr :: new ( prog) ,
555+ mod make_command_line_without_env {
556+ use ffi:: { OsStr , OsString } ;
557+ use super :: make_command_line;
558+
559+ fn fn_wrapper ( prog : & str , args : & [ & str ] ) -> ( String , String ) {
560+ let ( app_name, command_line) = & make_command_line ( OsStr :: new ( prog) ,
542561 & args. iter ( )
543562 . map ( |a| OsString :: from ( a) )
544563 . collect :: < Vec < OsString > > ( ) ,
545564 None )
546- . unwrap ( ) ;
547- String :: from_utf16 ( command_line) . unwrap ( )
548- }
549-
550- assert_eq ! (
551- test_wrapper( "prog" , & [ "aaa" , "bbb" , "ccc" ] ) ,
552- "prog aaa bbb ccc"
553- ) ;
554-
555- assert_eq ! (
556- test_wrapper( "C:\\ Program Files\\ blah\\ blah.exe" , & [ "aaa" ] ) ,
557- "\" C:\\ Program Files\\ blah\\ blah.exe\" aaa"
558- ) ;
559- assert_eq ! (
560- test_wrapper( "C:\\ Program Files\\ test" , & [ "aa\" bb" ] ) ,
561- "\" C:\\ Program Files\\ test\" aa\\ \" bb"
562- ) ;
563- assert_eq ! (
564- test_wrapper( "echo" , & [ "a b c" ] ) ,
565- "echo \" a b c\" "
566- ) ;
567- assert_eq ! (
568- test_wrapper( "echo" , & [ "\" \\ \" \\ " , "\\ " ] ) ,
569- "echo \" \\ \" \\ \\ \\ \" \\ \\ \" \\ "
570- ) ;
571- assert_eq ! (
572- test_wrapper( "\u{03c0} \u{042f} \u{97f3} \u{00e6} \u{221e} " , & [ ] ) ,
573- "\u{03c0} \u{042f} \u{97f3} \u{00e6} \u{221e} "
574- ) ;
565+ . unwrap ( ) ;
566+ (
567+ String :: from_utf16 ( app_name. unwrap_or ( & [ ] ) . unwrap ( ) ,
568+ String :: from_utf16 ( command_line) . unwrap ( )
569+ )
570+ }
571+ fn simple_prog_with_args ( ) {
572+ let ( app, cmd) = fn_wrapper ( "prog" , & [ "aaa" , "bbb" , "ccc" ] ) ;
573+ assert ! ( app. is_empty( ) ) ;
574+ assert_eq ! ( cmd, "prog aaa bbb ccc" ) ;
575+ }
576+ fn exe_with_args ( ) {
577+ let ( app, cmd) = fn_wrapper ( "C:\\ Program Files\\ blah\\ blah.exe" , & [ "aaa" ] ) ;
578+ assert ! ( app. is_empty( ) ) ;
579+ assert_eq ! ( cmd, "\" C:\\ Program Files\\ blah\\ blah.exe\" aaa" ) ;
580+ }
581+ fn prog_without_ext ( ) {
582+ let ( app, cmd) = fn_wrapper ( "C:\\ Program Files\\ test" , & [ "aa\" bb" ] ) ;
583+ assert ! ( app. is_empty( ) ) ;
584+ assert_eq ! ( cmd, "\" C:\\ Program Files\\ test\" aa\\ \" bb" ) ;
585+ }
586+ fn simple_echo ( ) {
587+ let ( app, cmd) = fn_wrapper ( "echo" , & [ "a b c" ] ) ;
588+ assert ! ( app. is_empty( ) ) ;
589+ assert_eq ! ( cmd, "echo \" a b c\" " ) ;
590+ }
591+ fn command_line_with_special_chars ( ) {
592+ let ( app, cmd) = fn_wrapper ( "echo" , & [ "\" \\ \" \\ " , "\\ " ] ) ;
593+ assert ! ( app. is_empty( ) ) ;
594+ assert_eq ! ( cmd, "echo \" \\ \" \\ \\ \\ \" \\ \\ \" \\ " ) ;
595+
596+ let ( app, cmd) = fn_wrapper ( "\u{03c0} \u{042f} \u{97f3} \u{00e6} \u{221e} " , & [ ] ) ;
597+ assert ! ( app. is_empty( ) ) ;
598+ assert_eq ! ( cmd, "\u{03c0} \u{042f} \u{97f3} \u{00e6} \u{221e} " ) ;
599+ }
600+ fn batch_file ( ) {
601+ let ( app, cmd) = fn_wrapper ( "C:\\ Program Files\\ blah\\ blah.bat" , & [ "bbb" ] ) ;
602+ assert ! ( !app. is_empty( ) ) ;
603+ assert_eq ! ( app, "cmd.exe" ) ;
604+ assert_eq ! ( cmd, "cmd /c \" C:\\ Program Files\\ blah\\ blah.exe\" bbb" ) ;
605+ }
606+ fn cmd_file ( ) {
607+ let ( app, cmd) = fn_wrapper ( "C:\\ Program Files\\ cmd program\\ cool.cmd" , & [ "install" ] ) ;
608+ assert ! ( !app. is_empty( ) ) ;
609+ assert_eq ! ( app, "cmd.exe" ) ;
610+ assert_eq ! ( cmd, "cmd /c \" C:\\ Program Files\\ cmd program\\ cool.cmd\" install" ) ;
611+ }
612+ fn vbs_file_is_not_a_batch_script ( ) {
613+ let ( app, cmd) = fn_wrapper (
614+ "C:\\ Users\\ afiune\\ automate.vbs" ,
615+ & [ "all" , "the" , "things" ]
616+ ) ;
617+ assert ! ( !app. is_empty( ) ) ;
618+ assert_eq ! ( app, "cmd.exe" ) ;
619+ assert_eq ! ( cmd, "cmd /c \" C:\\ Users\\ afiune\\ automate.vbs\" all the things" ) ;
620+ }
621+ }
622+
623+ #[ test]
624+ mod test_make_command_line_with_env( ) {
625+ use super :: make_command_line;
626+ use ffi:: { OsStr , OsString } ;
627+ use super :: gen_env;
628+
629+ fn fn_wrapper ( prog : & str , args : & [ & str ] ) -> ( String , String ) {
630+ let env = gen_env ( ) ;
631+ let ( app_name, command_line) = & make_command_line ( OsStr :: new ( prog) ,
632+ & args. iter ( )
633+ . map ( |a| OsString :: from ( a) )
634+ . collect :: < Vec < OsString > > ( ) ,
635+ Some ( & env) )
636+ . unwrap ( ) ;
637+ (
638+ String :: from_utf16 ( app_name. unwrap_or ( & [ ] ) . unwrap ( ) ,
639+ String :: from_utf16 ( command_line) . unwrap ( )
640+ )
641+ }
642+ fn simple_prog_with_args ( ) {
643+ let ( app, cmd) = fn_wrapper ( "prog.cmd" , & [ "aaa" , "bbb" , "ccc" ] ) ;
644+ assert_eq ! ( app, "C:\\ Windows\\ system32\\ cmd.exe" ) ;
645+ assert_eq ! ( cmd, "cmd /c \" prog.cmd\" aaa bbb ccc" ) ;
646+ }
647+ fn exe_with_args ( ) {
648+ let ( app, cmd) = fn_wrapper ( "C:\\ Program Files\\ blah\\ blah.exe" , & [ "aaa" ] ) ;
649+ assert ! ( app. is_empty( ) ) ;
650+ assert_eq ! ( cmd, "\" C:\\ Program Files\\ blah\\ blah.exe\" aaa" ) ;
651+ }
652+ fn prog_without_ext ( ) {
653+ let ( app, cmd) = fn_wrapper ( "C:\\ Program Files\\ test" , & [ "aa\" bb" ] ) ;
654+ assert ! ( app. is_empty( ) ) ;
655+ assert_eq ! ( cmd, "\" C:\\ Program Files\\ test\" aa\\ \" bb" ) ;
656+ }
657+ fn simple_echo ( ) {
658+ let ( app, cmd) = fn_wrapper ( "echo" , & [ "a b c" ] ) ;
659+ assert ! ( app. is_empty( ) ) ;
660+ assert_eq ! ( cmd, "echo \" a b c\" " ) ;
661+ }
662+ fn command_line_with_special_chars ( ) {
663+ let ( app, cmd) = fn_wrapper ( "echo" , & [ "\" \\ \" \\ " , "\\ " ] ) ;
664+ assert ! ( app. is_empty( ) ) ;
665+ assert_eq ! ( cmd, "echo \" \\ \" \\ \\ \\ \" \\ \\ \" \\ " ) ;
666+
667+ let ( app, cmd) = fn_wrapper ( "\u{03c0} \u{042f} \u{97f3} \u{00e6} \u{221e} " , & [ ] ) ;
668+ assert ! ( app. is_empty( ) ) ;
669+ assert_eq ! ( cmd, "\u{03c0} \u{042f} \u{97f3} \u{00e6} \u{221e} " ) ;
670+ }
671+ fn batch_file ( ) {
672+ let ( app, cmd) = fn_wrapper ( "C:\\ Program Files\\ blah\\ blah.bat" , & [ "bbb" ] ) ;
673+ assert ! ( !app. is_empty( ) ) ;
674+ assert_eq ! ( app, "C:\\ Windows\\ system32\\ cmd.exe" ) ;
675+ assert_eq ! ( cmd, "cmd /c \" C:\\ Program Files\\ blah\\ blah.exe\" bbb" ) ;
676+ }
677+ fn cmd_file ( ) {
678+ let ( app, cmd) = fn_wrapper (
679+ "C:\\ Program Files\\ cmd program\\ cool.cmd" ,
680+ & [ "install" ]
681+ ) ;
682+ assert ! ( !app. is_empty( ) ) ;
683+ assert_eq ! ( app, "C:\\ Windows\\ system32\\ cmd.exe" ) ;
684+ assert_eq ! ( cmd, "cmd /c \" C:\\ Program Files\\ cmd program\\ cool.cmd\" install" ) ;
685+ }
686+ fn vbs_file_is_not_a_batch_script ( ) {
687+ let ( app, cmd) = fn_wrapper (
688+ "C:\\ Users\\ afiune\\ automate.vbs" ,
689+ & [ "all" , "the" , "things" ]
690+ ) ;
691+ assert ! ( !app. is_empty( ) ) ;
692+ assert_eq ! ( app, "C:\\ Windows\\ system32\\ cmd.exe" ) ;
693+ assert_eq ! ( cmd, "cmd /c \" C:\\ Users\\ afiune\\ automate.vbs\" all the things" ) ;
694+ }
575695 }
576696}
0 commit comments