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