]> git.proxmox.com Git - cargo.git/blame - tests/cargo.rs
More lint cleaning
[cargo.git] / tests / cargo.rs
CommitLineData
015a08a0 1extern crate cargo;
763ba535
AC
2extern crate cargotest;
3extern crate hamcrest;
4
ee5e24ff 5use std::env;
a6dad622
AC
6use std::fs::{self, File};
7use std::io::prelude::*;
8use std::path::{Path, PathBuf};
8cce8996 9use std::str;
8cce8996 10
763ba535
AC
11use cargotest::cargo_process;
12use cargotest::support::paths::{self, CargoPathExt};
015a08a0
VK
13use cargotest::support::{execs, project, ProjectBuilder, basic_bin_manifest};
14use hamcrest::{assert_that, existing_file};
a3f6a404 15
e8ff2e00 16#[cfg_attr(windows,allow(dead_code))]
974d5834
JB
17enum FakeKind<'a> {
18 Executable,
19 Symlink{target:&'a Path},
20}
21
a3f6a404 22/// Add an empty file with executable flags (and platform-dependent suffix).
23/// TODO: move this to `ProjectBuilder` if other cases using this emerge.
23591fe5 24fn fake_file(proj: ProjectBuilder, dir: &Path, name: &str, kind: &FakeKind) -> ProjectBuilder {
a6dad622
AC
25 let path = proj.root().join(dir).join(&format!("{}{}", name,
26 env::consts::EXE_SUFFIX));
763ba535 27 path.parent().unwrap().mkdir_p();
23591fe5 28 match *kind {
974d5834
JB
29 FakeKind::Executable => {
30 File::create(&path).unwrap();
31 make_executable(&path);
32 },
33 FakeKind::Symlink{target} => {
34 make_symlink(&path,target);
35 }
36 }
a6dad622
AC
37 return proj;
38
39 #[cfg(unix)]
40 fn make_executable(p: &Path) {
41 use std::os::unix::prelude::*;
42
53cc3ce8 43 let mut perms = fs::metadata(p).unwrap().permissions();
a6dad622
AC
44 let mode = perms.mode();
45 perms.set_mode(mode | 0o111);
46 fs::set_permissions(p, perms).unwrap();
47 }
48 #[cfg(windows)]
49 fn make_executable(_: &Path) {}
974d5834
JB
50 #[cfg(unix)]
51 fn make_symlink(p: &Path, t: &Path) {
52 ::std::os::unix::fs::symlink(t,p).expect("Failed to create symlink");
53 }
54 #[cfg(windows)]
55 fn make_symlink(_: &Path, _: &Path) {
56 panic!("Not supported")
57 }
a3f6a404 58}
59
a6dad622 60fn path() -> Vec<PathBuf> {
23591fe5 61 env::split_paths(&env::var_os("PATH").unwrap_or_default()).collect()
db3823a8 62}
ee5e24ff 63
6950bbb0
AC
64#[test]
65fn list_command_looks_at_path() {
a3f6a404 66 let proj = project("list-non-overlapping");
23591fe5 67 let proj = fake_file(proj, Path::new("path-test"), "cargo-1", &FakeKind::Executable);
26a5eeff 68 let mut pr = cargo_process();
a3f6a404 69
5d0cb3f2 70 let mut path = path();
db3823a8 71 path.push(proj.root().join("path-test"));
ee5e24ff
AC
72 let path = env::join_paths(path.iter()).unwrap();
73 let output = pr.arg("-v").arg("--list")
a6dad622 74 .env("PATH", &path);
9ed3a6ea 75 let output = output.exec_with_output().unwrap();
a6dad622 76 let output = str::from_utf8(&output.stdout).unwrap();
8cce8996 77 assert!(output.contains("\n 1\n"), "missing 1: {}", output);
6950bbb0 78}
12f5de8e 79
974d5834
JB
80// windows and symlinks don't currently agree that well
81#[cfg(unix)]
6950bbb0
AC
82#[test]
83fn list_command_resolves_symlinks() {
015a08a0 84 use cargotest::support::cargo_exe;
e8ff2e00 85
974d5834 86 let proj = project("list-non-overlapping");
c5611a32 87 let proj = fake_file(proj, Path::new("path-test"), "cargo-2",
23591fe5 88 &FakeKind::Symlink{target:&cargo_exe()});
974d5834
JB
89 let mut pr = cargo_process();
90
91 let mut path = path();
92 path.push(proj.root().join("path-test"));
93 let path = env::join_paths(path.iter()).unwrap();
94 let output = pr.arg("-v").arg("--list")
95 .env("PATH", &path);
96 let output = output.exec_with_output().unwrap();
97 let output = str::from_utf8(&output.stdout).unwrap();
98 assert!(output.contains("\n 2\n"), "missing 2: {}", output);
6950bbb0 99}
974d5834 100
6950bbb0
AC
101#[test]
102fn find_closest_biuld_to_build() {
26a5eeff
AC
103 let mut pr = cargo_process();
104 pr.arg("biuld");
12f5de8e
PW
105
106 assert_that(pr,
20b768e6 107 execs().with_status(101)
79858995 108 .with_stderr("[ERROR] no such subcommand: `biuld`
12f5de8e 109
1a1d8f8c 110<tab>Did you mean `build`?
12f5de8e 111
ac3a8579 112"));
6950bbb0 113}
12f5de8e
PW
114
115// if a subcommand is more than 3 edit distance away, we don't make a suggestion
6950bbb0
AC
116#[test]
117fn find_closest_dont_correct_nonsense() {
26a5eeff 118 let mut pr = cargo_process();
bc8e3322
AC
119 pr.arg("there-is-no-way-that-there-is-a-command-close-to-this")
120 .cwd(&paths::root());
12f5de8e
PW
121
122 assert_that(pr,
20b768e6 123 execs().with_status(101)
1671630b
KA
124 .with_stderr("[ERROR] no such subcommand: \
125 `there-is-no-way-that-there-is-a-command-close-to-this`
79858995
KA
126"));
127}
128
129#[test]
130fn displays_subcommand_on_error() {
131 let mut pr = cargo_process();
132 pr.arg("invalid-command");
133
134 assert_that(pr,
135 execs().with_status(101)
136 .with_stderr("[ERROR] no such subcommand: `invalid-command`
ac3a8579 137"));
6950bbb0 138}
2badab8c 139
6950bbb0
AC
140#[test]
141fn override_cargo_home() {
2badab8c
BA
142 let root = paths::root();
143 let my_home = root.join("my_home");
a6dad622
AC
144 fs::create_dir(&my_home).unwrap();
145 File::create(&my_home.join("config")).unwrap().write_all(br#"
2badab8c
BA
146 [cargo-new]
147 name = "foo"
148 email = "bar"
149 git = false
9ed3a6ea 150 "#).unwrap();
2badab8c 151
26a5eeff 152 assert_that(cargo_process()
a6dad622 153 .arg("new").arg("foo")
a6dad622 154 .env("USER", "foo")
a6dad622 155 .env("CARGO_HOME", &my_home),
2badab8c
BA
156 execs().with_status(0));
157
158 let toml = paths::root().join("foo/Cargo.toml");
a6dad622
AC
159 let mut contents = String::new();
160 File::open(&toml).unwrap().read_to_string(&mut contents).unwrap();
161 assert!(contents.contains(r#"authors = ["foo <bar>"]"#));
6950bbb0 162}
2ff5f53b 163
015a08a0
VK
164#[test]
165fn cargo_subcommand_env() {
166 use cargotest::support::cargo_exe;
167
168 let src = format!(r#"
169 use std::env;
170
171 fn main() {{
172 println!("{{}}", env::var("{}").unwrap());
173 }}
174 "#, cargo::CARGO_ENV);
175
176 let p = project("cargo-envtest")
177 .file("Cargo.toml", &basic_bin_manifest("cargo-envtest"))
178 .file("src/main.rs", &src);
179
180 let target_dir = p.target_debug_dir();
181
182 assert_that(p.cargo_process("build"), execs().with_status(0));
183 assert_that(&p.bin("cargo-envtest"), existing_file());
184
185 let mut pr = cargo_process();
186 let cargo = cargo_exe().canonicalize().unwrap();
187 let mut path = path();
188 path.push(target_dir);
189 let path = env::join_paths(path.iter()).unwrap();
190
191 assert_that(pr.arg("envtest").env("PATH", &path),
192 execs().with_status(0).with_stdout(cargo.to_str().unwrap()));
193}
194
6950bbb0
AC
195#[test]
196fn cargo_help() {
26a5eeff 197 assert_that(cargo_process(),
2ff5f53b 198 execs().with_status(0));
26a5eeff 199 assert_that(cargo_process().arg("help"),
2ff5f53b 200 execs().with_status(0));
26a5eeff 201 assert_that(cargo_process().arg("-h"),
2ff5f53b 202 execs().with_status(0));
26a5eeff 203 assert_that(cargo_process().arg("help").arg("build"),
2ff5f53b 204 execs().with_status(0));
26a5eeff 205 assert_that(cargo_process().arg("build").arg("-h"),
2ff5f53b 206 execs().with_status(0));
26a5eeff 207 assert_that(cargo_process().arg("help").arg("-h"),
2ff5f53b 208 execs().with_status(0));
26a5eeff 209 assert_that(cargo_process().arg("help").arg("help"),
2ff5f53b 210 execs().with_status(0));
6950bbb0 211}
8dad57e8 212
6950bbb0
AC
213#[test]
214fn explain() {
8dad57e8
AC
215 assert_that(cargo_process().arg("--explain").arg("E0001"),
216 execs().with_status(0));
6950bbb0 217}