|
1 | 1 | use std::env::*;
|
2 | 2 | use std::ffi::{OsStr, OsString};
|
| 3 | +use std::path::Path; |
3 | 4 |
|
4 | 5 | use rand::distributions::{Alphanumeric, DistString};
|
5 | 6 |
|
@@ -161,3 +162,122 @@ fn test_env_get_set_multithreaded() {
|
161 | 162 | let _ = getter.join();
|
162 | 163 | let _ = setter.join();
|
163 | 164 | }
|
| 165 | + |
| 166 | +#[test] |
| 167 | +#[cfg_attr(any(target_os = "emscripten", target_os = "wasi", target_env = "sgx"), ignore)] |
| 168 | +fn test_self_exe_path() { |
| 169 | + let path = current_exe(); |
| 170 | + assert!(path.is_ok()); |
| 171 | + let path = path.unwrap(); |
| 172 | + |
| 173 | + // Hard to test this function |
| 174 | + assert!(path.is_absolute()); |
| 175 | +} |
| 176 | + |
| 177 | +#[test] |
| 178 | +fn test() { |
| 179 | + assert!((!Path::new("test-path").is_absolute())); |
| 180 | + |
| 181 | + #[cfg(not(target_env = "sgx"))] |
| 182 | + current_dir().unwrap(); |
| 183 | +} |
| 184 | + |
| 185 | +#[test] |
| 186 | +#[cfg(windows)] |
| 187 | +fn split_paths_windows() { |
| 188 | + use std::path::PathBuf; |
| 189 | + |
| 190 | + fn check_parse(unparsed: &str, parsed: &[&str]) -> bool { |
| 191 | + split_paths(unparsed).collect::<Vec<_>>() |
| 192 | + == parsed.iter().map(|s| PathBuf::from(*s)).collect::<Vec<_>>() |
| 193 | + } |
| 194 | + |
| 195 | + assert!(check_parse("", &mut [""])); |
| 196 | + assert!(check_parse(r#""""#, &mut [""])); |
| 197 | + assert!(check_parse(";;", &mut ["", "", ""])); |
| 198 | + assert!(check_parse(r"c:\", &mut [r"c:\"])); |
| 199 | + assert!(check_parse(r"c:\;", &mut [r"c:\", ""])); |
| 200 | + assert!(check_parse(r"c:\;c:\Program Files\", &mut [r"c:\", r"c:\Program Files\"])); |
| 201 | + assert!(check_parse(r#"c:\;c:\"foo"\"#, &mut [r"c:\", r"c:\foo\"])); |
| 202 | + assert!(check_parse(r#"c:\;c:\"foo;bar"\;c:\baz"#, &mut [r"c:\", r"c:\foo;bar\", r"c:\baz"])); |
| 203 | +} |
| 204 | + |
| 205 | +#[test] |
| 206 | +#[cfg(unix)] |
| 207 | +fn split_paths_unix() { |
| 208 | + use std::path::PathBuf; |
| 209 | + |
| 210 | + fn check_parse(unparsed: &str, parsed: &[&str]) -> bool { |
| 211 | + split_paths(unparsed).collect::<Vec<_>>() |
| 212 | + == parsed.iter().map(|s| PathBuf::from(*s)).collect::<Vec<_>>() |
| 213 | + } |
| 214 | + |
| 215 | + assert!(check_parse("", &mut [""])); |
| 216 | + assert!(check_parse("::", &mut ["", "", ""])); |
| 217 | + assert!(check_parse("/", &mut ["/"])); |
| 218 | + assert!(check_parse("/:", &mut ["/", ""])); |
| 219 | + assert!(check_parse("/:/usr/local", &mut ["/", "/usr/local"])); |
| 220 | +} |
| 221 | + |
| 222 | +#[test] |
| 223 | +#[cfg(unix)] |
| 224 | +fn join_paths_unix() { |
| 225 | + use std::ffi::OsStr; |
| 226 | + |
| 227 | + fn test_eq(input: &[&str], output: &str) -> bool { |
| 228 | + &*join_paths(input.iter().cloned()).unwrap() == OsStr::new(output) |
| 229 | + } |
| 230 | + |
| 231 | + assert!(test_eq(&[], "")); |
| 232 | + assert!(test_eq(&["/bin", "/usr/bin", "/usr/local/bin"], "/bin:/usr/bin:/usr/local/bin")); |
| 233 | + assert!(test_eq(&["", "/bin", "", "", "/usr/bin", ""], ":/bin:::/usr/bin:")); |
| 234 | + assert!(join_paths(["/te:st"].iter().cloned()).is_err()); |
| 235 | +} |
| 236 | + |
| 237 | +#[test] |
| 238 | +#[cfg(windows)] |
| 239 | +fn join_paths_windows() { |
| 240 | + use std::ffi::OsStr; |
| 241 | + |
| 242 | + fn test_eq(input: &[&str], output: &str) -> bool { |
| 243 | + &*join_paths(input.iter().cloned()).unwrap() == OsStr::new(output) |
| 244 | + } |
| 245 | + |
| 246 | + assert!(test_eq(&[], "")); |
| 247 | + assert!(test_eq(&[r"c:\windows", r"c:\"], r"c:\windows;c:\")); |
| 248 | + assert!(test_eq(&["", r"c:\windows", "", "", r"c:\", ""], r";c:\windows;;;c:\;")); |
| 249 | + assert!(test_eq(&[r"c:\te;st", r"c:\"], r#""c:\te;st";c:\"#)); |
| 250 | + assert!(join_paths([r#"c:\te"st"#].iter().cloned()).is_err()); |
| 251 | +} |
| 252 | + |
| 253 | +#[test] |
| 254 | +fn args_debug() { |
| 255 | + assert_eq!( |
| 256 | + format!("Args {{ inner: {:?} }}", args().collect::<Vec<_>>()), |
| 257 | + format!("{:?}", args()) |
| 258 | + ); |
| 259 | +} |
| 260 | + |
| 261 | +#[test] |
| 262 | +fn args_os_debug() { |
| 263 | + assert_eq!( |
| 264 | + format!("ArgsOs {{ inner: {:?} }}", args_os().collect::<Vec<_>>()), |
| 265 | + format!("{:?}", args_os()) |
| 266 | + ); |
| 267 | +} |
| 268 | + |
| 269 | +#[test] |
| 270 | +fn vars_debug() { |
| 271 | + assert_eq!( |
| 272 | + format!("Vars {{ inner: {:?} }}", vars().collect::<Vec<_>>()), |
| 273 | + format!("{:?}", vars()) |
| 274 | + ); |
| 275 | +} |
| 276 | + |
| 277 | +#[test] |
| 278 | +fn vars_os_debug() { |
| 279 | + assert_eq!( |
| 280 | + format!("VarsOs {{ inner: {:?} }}", vars_os().collect::<Vec<_>>()), |
| 281 | + format!("{:?}", vars_os()) |
| 282 | + ); |
| 283 | +} |
0 commit comments