]> git.proxmox.com Git - cargo.git/blame - tests/test_cargo.rs
Auto merge of #1402 - cmr:issue-1015, r=alexcrichton
[cargo.git] / tests / test_cargo.rs
CommitLineData
ee5e24ff
AC
1use std::env;
2use std::ffi::OsString;
a6dad622
AC
3use std::fs::{self, File};
4use std::io::prelude::*;
5use std::path::{Path, PathBuf};
8cce8996 6use std::str;
f3cb4232 7use cargo::util::process;
8cce8996 8
a3f6a404 9use support::paths;
9ed3a6ea 10use support::{execs, project, cargo_dir, mkdir_recursive, ProjectBuilder};
12f5de8e 11use hamcrest::{assert_that};
a3f6a404 12
13fn setup() {
14}
15
16/// Add an empty file with executable flags (and platform-dependent suffix).
17/// TODO: move this to `ProjectBuilder` if other cases using this emerge.
18fn fake_executable(proj: ProjectBuilder, dir: &Path, name: &str) -> ProjectBuilder {
a6dad622
AC
19 let path = proj.root().join(dir).join(&format!("{}{}", name,
20 env::consts::EXE_SUFFIX));
21 mkdir_recursive(path.parent().unwrap()).unwrap();
22 File::create(&path).unwrap();
23 make_executable(&path);
24 return proj;
25
26 #[cfg(unix)]
27 fn make_executable(p: &Path) {
28 use std::os::unix::prelude::*;
29
30 let mut perms = fs::metadata(p).unwrap().permissions();;
31 let mode = perms.mode();
32 perms.set_mode(mode | 0o111);
33 fs::set_permissions(p, perms).unwrap();
34 }
35 #[cfg(windows)]
36 fn make_executable(_: &Path) {}
a3f6a404 37}
38
a6dad622 39fn path() -> Vec<PathBuf> {
1384050e 40 env::split_paths(&env::var_os("PATH").unwrap_or(OsString::new())).collect()
db3823a8 41}
ee5e24ff 42
f3cb4232 43test!(list_commands_looks_at_path {
a3f6a404 44 let proj = project("list-non-overlapping");
a3f6a404 45 let proj = fake_executable(proj, &Path::new("path-test"), "cargo-1");
a6dad622
AC
46 let mut pr = process(&cargo_dir().join("cargo")).unwrap();
47 pr.cwd(&proj.root())
48 .env("HOME", &paths::home());
a3f6a404 49
5d0cb3f2 50 let mut path = path();
db3823a8 51 path.push(proj.root().join("path-test"));
ee5e24ff
AC
52 let path = env::join_paths(path.iter()).unwrap();
53 let output = pr.arg("-v").arg("--list")
a6dad622 54 .env("PATH", &path);
9ed3a6ea 55 let output = output.exec_with_output().unwrap();
a6dad622 56 let output = str::from_utf8(&output.stdout).unwrap();
8cce8996 57 assert!(output.contains("\n 1\n"), "missing 1: {}", output);
157d639a 58});
12f5de8e
PW
59
60test!(find_closest_biuld_to_build {
a6dad622
AC
61 let mut pr = process(&cargo_dir().join("cargo")).unwrap();
62 pr.arg("biuld").cwd(&paths::root()).env("HOME", &paths::home());
12f5de8e
PW
63
64 assert_that(pr,
65 execs().with_status(127)
66 .with_stderr("No such subcommand
67
21e94a46 68Did you mean `build`?
12f5de8e
PW
69
70"));
157d639a 71});
12f5de8e
PW
72
73// if a subcommand is more than 3 edit distance away, we don't make a suggestion
74test!(find_closest_dont_correct_nonsense {
a6dad622
AC
75 let mut pr = process(&cargo_dir().join("cargo")).unwrap();
76 pr.arg("asdf").cwd(&paths::root()).env("HOME", &paths::home());
12f5de8e
PW
77
78 assert_that(pr,
79 execs().with_status(127)
80 .with_stderr("No such subcommand
81"));
157d639a 82});
2badab8c
BA
83
84test!(override_cargo_home {
85 let root = paths::root();
86 let my_home = root.join("my_home");
a6dad622
AC
87 fs::create_dir(&my_home).unwrap();
88 File::create(&my_home.join("config")).unwrap().write_all(br#"
2badab8c
BA
89 [cargo-new]
90 name = "foo"
91 email = "bar"
92 git = false
9ed3a6ea 93 "#).unwrap();
2badab8c 94
a6dad622
AC
95 assert_that(process(&cargo_dir().join("cargo")).unwrap()
96 .arg("new").arg("foo")
97 .cwd(&paths::root())
98 .env("USER", "foo")
99 .env("HOME", &paths::home())
100 .env("CARGO_HOME", &my_home),
2badab8c
BA
101 execs().with_status(0));
102
103 let toml = paths::root().join("foo/Cargo.toml");
a6dad622
AC
104 let mut contents = String::new();
105 File::open(&toml).unwrap().read_to_string(&mut contents).unwrap();
106 assert!(contents.contains(r#"authors = ["foo <bar>"]"#));
2badab8c 107});