]> git.lizzy.rs Git - rust.git/blob - library/std/src/sys/windows/process/tests.rs
3b65856dcaca6d4f3e017a2ba97fccf2cf8f339c
[rust.git] / library / std / src / sys / windows / process / tests.rs
1 use super::make_command_line;
2 use super::Arg;
3 use crate::env;
4 use crate::ffi::{OsStr, OsString};
5 use crate::process::Command;
6
7 #[test]
8 fn test_raw_args() {
9     let command_line = &make_command_line(
10         OsStr::new("quoted exe"),
11         &[
12             Arg::Regular(OsString::from("quote me")),
13             Arg::Raw(OsString::from("quote me *not*")),
14             Arg::Raw(OsString::from("\t\\")),
15             Arg::Raw(OsString::from("internal \\\"backslash-\"quote")),
16             Arg::Regular(OsString::from("optional-quotes")),
17         ],
18         false,
19     )
20     .unwrap();
21     assert_eq!(
22         String::from_utf16(command_line).unwrap(),
23         "\"quoted exe\" \"quote me\" quote me *not* \t\\ internal \\\"backslash-\"quote optional-quotes"
24     );
25 }
26
27 #[test]
28 fn test_make_command_line() {
29     fn test_wrapper(prog: &str, args: &[&str], force_quotes: bool) -> String {
30         let command_line = &make_command_line(
31             OsStr::new(prog),
32             &args.iter().map(|a| Arg::Regular(OsString::from(a))).collect::<Vec<_>>(),
33             force_quotes,
34         )
35         .unwrap();
36         String::from_utf16(command_line).unwrap()
37     }
38
39     assert_eq!(test_wrapper("prog", &["aaa", "bbb", "ccc"], false), "\"prog\" aaa bbb ccc");
40
41     assert_eq!(test_wrapper("prog", &[r"C:\"], false), r#""prog" C:\"#);
42     assert_eq!(test_wrapper("prog", &[r"2slashes\\"], false), r#""prog" 2slashes\\"#);
43     assert_eq!(test_wrapper("prog", &[r" C:\"], false), r#""prog" " C:\\""#);
44     assert_eq!(test_wrapper("prog", &[r" 2slashes\\"], false), r#""prog" " 2slashes\\\\""#);
45
46     assert_eq!(
47         test_wrapper("C:\\Program Files\\blah\\blah.exe", &["aaa"], false),
48         "\"C:\\Program Files\\blah\\blah.exe\" aaa"
49     );
50     assert_eq!(
51         test_wrapper("C:\\Program Files\\blah\\blah.exe", &["aaa", "v*"], false),
52         "\"C:\\Program Files\\blah\\blah.exe\" aaa v*"
53     );
54     assert_eq!(
55         test_wrapper("C:\\Program Files\\blah\\blah.exe", &["aaa", "v*"], true),
56         "\"C:\\Program Files\\blah\\blah.exe\" \"aaa\" \"v*\""
57     );
58     assert_eq!(
59         test_wrapper("C:\\Program Files\\test", &["aa\"bb"], false),
60         "\"C:\\Program Files\\test\" aa\\\"bb"
61     );
62     assert_eq!(test_wrapper("echo", &["a b c"], false), "\"echo\" \"a b c\"");
63     assert_eq!(
64         test_wrapper("echo", &["\" \\\" \\", "\\"], false),
65         "\"echo\" \"\\\" \\\\\\\" \\\\\" \\"
66     );
67     assert_eq!(
68         test_wrapper("\u{03c0}\u{042f}\u{97f3}\u{00e6}\u{221e}", &[], false),
69         "\"\u{03c0}\u{042f}\u{97f3}\u{00e6}\u{221e}\""
70     );
71 }
72
73 // On Windows, environment args are case preserving but comparisons are case-insensitive.
74 // See: #85242
75 #[test]
76 fn windows_env_unicode_case() {
77     let test_cases = [
78         ("ä", "Ä"),
79         ("ß", "SS"),
80         ("Ä", "Ö"),
81         ("Ä", "Ö"),
82         ("I", "İ"),
83         ("I", "i"),
84         ("I", "ı"),
85         ("i", "I"),
86         ("i", "İ"),
87         ("i", "ı"),
88         ("İ", "I"),
89         ("İ", "i"),
90         ("İ", "ı"),
91         ("ı", "I"),
92         ("ı", "i"),
93         ("ı", "İ"),
94         ("ä", "Ä"),
95         ("ß", "SS"),
96         ("Ä", "Ö"),
97         ("Ä", "Ö"),
98         ("I", "İ"),
99         ("I", "i"),
100         ("I", "ı"),
101         ("i", "I"),
102         ("i", "İ"),
103         ("i", "ı"),
104         ("İ", "I"),
105         ("İ", "i"),
106         ("İ", "ı"),
107         ("ı", "I"),
108         ("ı", "i"),
109         ("ı", "İ"),
110     ];
111     // Test that `cmd.env` matches `env::set_var` when setting two strings that
112     // may (or may not) be case-folded when compared.
113     for (a, b) in test_cases.iter() {
114         let mut cmd = Command::new("cmd");
115         cmd.env(a, "1");
116         cmd.env(b, "2");
117         env::set_var(a, "1");
118         env::set_var(b, "2");
119
120         for (key, value) in cmd.get_envs() {
121             assert_eq!(
122                 env::var(key).ok(),
123                 value.map(|s| s.to_string_lossy().into_owned()),
124                 "command environment mismatch: {} {}",
125                 a,
126                 b
127             );
128         }
129     }
130 }