|
| 1 | +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +#[cfg(test)] |
| 12 | +mod test_find_program { |
| 13 | + use std::ffi::{OsStr, OsString}; |
| 14 | + use std::process::Command; |
| 15 | + use std::collections::HashMap; |
| 16 | + use std::path::Path; |
| 17 | + use std::fs::canonicalize; |
| 18 | + use std::env::join_paths; |
| 19 | + |
| 20 | + fn gen_env() -> HashMap<OsString, OsString> { |
| 21 | + let env: HashMap<OsString, OsString> = HashMap::new(); |
| 22 | + env.insert(OsString::from("HOMEDRIVE"), OsString::from("C:")); |
| 23 | + let p1 = canonicalize("./src/test/run-pass/process_command/fixtures/bin").unwrap(); |
| 24 | + let p2 = canonicalize("./src/test/run-pass/process_command/fixtures").unwrap(); |
| 25 | + let p3 = canonicalize("./src/test/run-pass/process_command").unwrap(); |
| 26 | + let paths = vec![p1, p2, p3]; |
| 27 | + let path = join_paths(paths).unwrap(); |
| 28 | + env.insert(OsString::from("PATH"), OsString::from(&path)); |
| 29 | + env.insert(OsString::from("USERNAME"), OsString::from("rust")); |
| 30 | + env |
| 31 | + } |
| 32 | + |
| 33 | + fn command_with_pathext(cmd: &str) -> Command { |
| 34 | + let mut env = gen_env(); |
| 35 | + env.insert( |
| 36 | + OsString::from("PATHEXT"), |
| 37 | + OsString::from(".COM;.EXE;.BAT;.CMD;.VBS;.VBE;.MSC") |
| 38 | + ); |
| 39 | + let mut cmd = Command::new(cmd); |
| 40 | + cmd.with_env(&env); |
| 41 | + cmd |
| 42 | + } |
| 43 | + |
| 44 | + fn command_without_pathext(cmd: &str) -> Command { |
| 45 | + let env = gen_env(); |
| 46 | + let mut cmd = Command::new(cmd); |
| 47 | + cmd.with_env(&env); |
| 48 | + cmd |
| 49 | + } |
| 50 | + |
| 51 | + mod with_pathext_set { |
| 52 | + use super::command_with_pathext; |
| 53 | + |
| 54 | + fn command_on_path_found() { |
| 55 | + let c = command_with_pathext("bin"); |
| 56 | + let bat = canonicalize("./src/test/run-pass/process_command/fixtures/bin/bin.bat"); |
| 57 | + assert_eq!(bat.ok(), c.find_program()); |
| 58 | + } |
| 59 | + |
| 60 | + fn command_not_found() { |
| 61 | + let c = command_with_pathext("missing"); |
| 62 | + assert_eq!(None, c.find_program()); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + mod without_pathext_set { |
| 67 | + use super::command_without_pathext; |
| 68 | + |
| 69 | + fn bat_command_not_found() { |
| 70 | + let c = command_without_pathext("bin"); |
| 71 | + assert_eq!(None, c.find_program()); |
| 72 | + } |
| 73 | + |
| 74 | + fn exe_command_found() { |
| 75 | + let c = command_without_pathext("exec"); |
| 76 | + let exe = canonicalize("./src/test/run-pass/process_command/fixtures/bin/exec.exe"); |
| 77 | + assert_eq!(exe.ok(), c.find_program()); |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments