]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass-fulldeps/issue-15149.rs
Auto merge of #28922 - panicbit:trpl-missing-docs, r=steveklabnik
[rust.git] / src / test / run-pass-fulldeps / 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 // no-prefer-dynamic
12 // ignore-cross-compile
13
14 #![feature(rustc_private)]
15
16 extern crate rustc_back;
17
18 use std::env;
19 use std::fs;
20 use std::process;
21 use std::str;
22 use rustc_back::tempdir::TempDir;
23
24 fn main() {
25     // If we're the child, make sure we were invoked correctly
26     let args: Vec<String> = env::args().collect();
27     if args.len() > 1 && args[1] == "child" {
28         // FIXME: This should check the whole `args[0]` instead of just
29         // checking that it ends_with the executable name. This
30         // is needed because of Windows, which has a different behavior.
31         // See #15149 for more info.
32         return assert!(args[0].ends_with(&format!("mytest{}",
33                                                   env::consts::EXE_SUFFIX)));
34     }
35
36     test();
37 }
38
39 fn test() {
40     // If we're the parent, copy our own binary to a new directory.
41     let my_path = env::current_exe().unwrap();
42     let my_dir  = my_path.parent().unwrap();
43
44     let child_dir = TempDir::new_in(&my_dir, "issue-15140-child").unwrap();
45     let child_dir = child_dir.path();
46
47     let child_path = child_dir.join(&format!("mytest{}",
48                                              env::consts::EXE_SUFFIX));
49     fs::copy(&my_path, &child_path).unwrap();
50
51     // Append the new directory to our own PATH.
52     let path = {
53         let mut paths: Vec<_> = env::split_paths(&env::var_os("PATH").unwrap()).collect();
54         paths.push(child_dir.to_path_buf());
55         env::join_paths(paths).unwrap()
56     };
57
58     let child_output = process::Command::new("mytest").env("PATH", &path)
59                                                       .arg("child")
60                                                       .output().unwrap();
61
62     assert!(child_output.status.success(),
63             format!("child assertion failed\n child stdout:\n {}\n child stderr:\n {}",
64                     str::from_utf8(&child_output.stdout).unwrap(),
65                     str::from_utf8(&child_output.stderr).unwrap()));
66
67     let res = fs::remove_dir_all(&child_dir);
68     if res.is_err() {
69         // On Windows deleting just executed mytest.exe can fail because it's still locked
70         std::thread::sleep_ms(1000);
71         fs::remove_dir_all(&child_dir).unwrap();
72     }
73 }