]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/issue-15149.rs
Auto merge of #21582 - FlaPer87:rollup, r=brson
[rust.git] / src / test / run-pass / issue-15149.rs
1 // Copyright 2014 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 use std::slice::SliceExt;
12 use std::io::{Command, fs, USER_RWX};
13 use std::os;
14 use std::path::BytesContainer;
15 use std::rand::random;
16
17 fn main() {
18     // If we're the child, make sure we were invoked correctly
19     let args = os::args();
20     if args.len() > 1 && args[1].as_slice() == "child" {
21         // FIXME: This should check the whole `args[0]` instead of just
22         // checking that it ends_with the executable name. This
23         // is needed because of Windows, which has a different behavior.
24         // See #15149 for more info.
25         return assert!(args[0].ends_with(&format!("mytest{}", os::consts::EXE_SUFFIX)[]));
26     }
27
28     test();
29 }
30
31 fn test() {
32     // If we're the parent, copy our own binary to a new directory.
33     let my_path = os::self_exe_name().unwrap();
34     let my_dir  = my_path.dir_path();
35
36     let random_u32: u32 = random();
37     let child_dir = Path::new(my_dir.join(format!("issue-15149-child-{}",
38                                                   random_u32)));
39     fs::mkdir(&child_dir, USER_RWX).unwrap();
40
41     let child_path = child_dir.join(format!("mytest{}",
42                                             os::consts::EXE_SUFFIX));
43     fs::copy(&my_path, &child_path).unwrap();
44
45     // Append the new directory to our own PATH.
46     let mut path = os::split_paths(os::getenv("PATH").unwrap_or(String::new()));
47     path.push(child_dir.clone());
48     let path = os::join_paths(path.as_slice()).unwrap();
49
50     let child_output = Command::new("mytest").env("PATH", path.as_slice())
51                                              .arg("child")
52                                              .output().unwrap();
53
54     assert!(child_output.status.success(),
55             format!("child assertion failed\n child stdout:\n {}\n child stderr:\n {}",
56                     child_output.output.container_as_str().unwrap(),
57                     child_output.error.container_as_str().unwrap()));
58
59     fs::rmdir_recursive(&child_dir).unwrap();
60
61 }