]> git.lizzy.rs Git - rust.git/blob - src/test/ui-fulldeps/issue-15149.rs
Avoid possible filename collision in coverage tests
[rust.git] / src / test / ui-fulldeps / issue-15149.rs
1 // run-pass
2
3 #![allow(unused_variables)]
4 // no-prefer-dynamic
5 // ignore-cross-compile
6
7 use std::env;
8 use std::fs;
9 use std::process;
10 use std::str;
11 use std::path::PathBuf;
12
13 fn main() {
14     // If we're the child, make sure we were invoked correctly
15     let args: Vec<String> = env::args().collect();
16     if args.len() > 1 && args[1] == "child" {
17         // FIXME: This should check the whole `args[0]` instead of just
18         // checking that it ends_with the executable name. This
19         // is needed because of Windows, which has a different behavior.
20         // See #15149 for more info.
21         return assert!(args[0].ends_with(&format!("mytest{}",
22                                                   env::consts::EXE_SUFFIX)));
23     }
24
25     test();
26 }
27
28 fn test() {
29     // If we're the parent, copy our own binary to a new directory.
30     let my_path = env::current_exe().unwrap();
31     let my_dir  = my_path.parent().unwrap();
32
33     let child_dir = PathBuf::from(env::var_os("RUST_TEST_TMPDIR").unwrap());
34     let child_dir = child_dir.join("issue-15140-child");
35     fs::create_dir_all(&child_dir).unwrap();
36
37     let child_path = child_dir.join(&format!("mytest{}",
38                                              env::consts::EXE_SUFFIX));
39     fs::copy(&my_path, &child_path).unwrap();
40
41     // Append the new directory to our own PATH.
42     let path = {
43         let mut paths: Vec<_> = env::split_paths(&env::var_os("PATH").unwrap()).collect();
44         paths.push(child_dir.to_path_buf());
45         env::join_paths(paths).unwrap()
46     };
47
48     let child_output = process::Command::new("mytest").env("PATH", &path)
49                                                       .arg("child")
50                                                       .output().unwrap();
51
52     assert!(child_output.status.success(),
53             "child assertion failed\n child stdout:\n {}\n child stderr:\n {}",
54             str::from_utf8(&child_output.stdout).unwrap(),
55             str::from_utf8(&child_output.stderr).unwrap());
56 }