]>
Commit | Line | Data |
---|---|---|
ba280047 | 1 | use std::io::fs::{self, PathExtensions}; |
8cce8996 | 2 | use std::io; |
2badab8c | 3 | use std::io::{USER_RWX, File}; |
a3f6a404 | 4 | use std::os; |
8cce8996 | 5 | use std::str; |
f3cb4232 | 6 | use cargo::util::process; |
8cce8996 | 7 | |
a3f6a404 | 8 | use support::paths; |
9ed3a6ea | 9 | use support::{execs, project, cargo_dir, mkdir_recursive, ProjectBuilder}; |
12f5de8e | 10 | use hamcrest::{assert_that}; |
a3f6a404 | 11 | |
12 | fn setup() { | |
13 | } | |
14 | ||
15 | /// Add an empty file with executable flags (and platform-dependent suffix). | |
16 | /// TODO: move this to `ProjectBuilder` if other cases using this emerge. | |
17 | fn fake_executable(proj: ProjectBuilder, dir: &Path, name: &str) -> ProjectBuilder { | |
18 | let path = proj.root().join(dir).join(format!("{}{}", name, os::consts::EXE_SUFFIX)); | |
9ed3a6ea AC |
19 | mkdir_recursive(&Path::new(path.dirname())).unwrap(); |
20 | fs::File::create(&path).unwrap(); | |
21 | let io::FileStat{perm, ..} = fs::stat(&path).unwrap(); | |
22 | fs::chmod(&path, io::OTHER_EXECUTE | perm).unwrap(); | |
a3f6a404 | 23 | proj |
24 | } | |
25 | ||
db3823a8 AC |
26 | // We can't entirely obliterate PATH because windows needs it for paths to |
27 | // things like libgcc, but we want to filter out everything which has a `cargo` | |
28 | // installation as we don't want it to muck with the --list tests | |
29 | fn new_path() -> Vec<Path> { | |
30 | let path = os::getenv_as_bytes("PATH").unwrap_or(Vec::new()); | |
dfae53c4 | 31 | os::split_paths(path).into_iter().filter(|p| { |
db3823a8 AC |
32 | !p.join(format!("cargo{}", os::consts::EXE_SUFFIX)).exists() |
33 | }).collect() | |
34 | } | |
f3cb4232 | 35 | test!(list_commands_looks_at_path { |
a3f6a404 | 36 | let proj = project("list-non-overlapping"); |
a3f6a404 | 37 | let proj = fake_executable(proj, &Path::new("path-test"), "cargo-1"); |
df9cf066 JB |
38 | let pr = process(cargo_dir().join("cargo")) |
39 | .unwrap() | |
40 | .cwd(proj.root()) | |
41 | .env("HOME", Some(paths::home())); | |
a3f6a404 | 42 | |
db3823a8 AC |
43 | let mut path = new_path(); |
44 | path.push(proj.root().join("path-test")); | |
45 | let path = os::join_paths(path.as_slice()).unwrap(); | |
8cce8996 | 46 | let output = pr.arg("-v").arg("--list").env("PATH", Some(path.as_slice())); |
9ed3a6ea AC |
47 | let output = output.exec_with_output().unwrap(); |
48 | let output = str::from_utf8(output.output.as_slice()).unwrap(); | |
8cce8996 | 49 | assert!(output.contains("\n 1\n"), "missing 1: {}", output); |
157d639a | 50 | }); |
12f5de8e PW |
51 | |
52 | test!(find_closest_biuld_to_build { | |
df9cf066 | 53 | let pr = process(cargo_dir().join("cargo")).unwrap() |
12f5de8e PW |
54 | .arg("biuld").cwd(paths::root()) |
55 | .env("HOME", Some(paths::home())); | |
56 | ||
57 | assert_that(pr, | |
58 | execs().with_status(127) | |
59 | .with_stderr("No such subcommand | |
60 | ||
21e94a46 | 61 | Did you mean `build`? |
12f5de8e PW |
62 | |
63 | ")); | |
157d639a | 64 | }); |
12f5de8e PW |
65 | |
66 | // if a subcommand is more than 3 edit distance away, we don't make a suggestion | |
67 | test!(find_closest_dont_correct_nonsense { | |
df9cf066 | 68 | let pr = process(cargo_dir().join("cargo")).unwrap() |
12f5de8e PW |
69 | .arg("asdf").cwd(paths::root()) |
70 | .env("HOME", Some(paths::home())); | |
71 | ||
72 | assert_that(pr, | |
73 | execs().with_status(127) | |
74 | .with_stderr("No such subcommand | |
75 | ")); | |
157d639a | 76 | }); |
2badab8c BA |
77 | |
78 | test!(override_cargo_home { | |
79 | let root = paths::root(); | |
80 | let my_home = root.join("my_home"); | |
9ed3a6ea | 81 | fs::mkdir(&my_home, USER_RWX).unwrap(); |
bef8095b | 82 | File::create(&my_home.join("config")).write_str(r#" |
2badab8c BA |
83 | [cargo-new] |
84 | name = "foo" | |
85 | email = "bar" | |
86 | git = false | |
9ed3a6ea | 87 | "#).unwrap(); |
2badab8c BA |
88 | |
89 | assert_that(process(cargo_dir().join("cargo")).unwrap() | |
90 | .arg("new").arg("foo") | |
91 | .cwd(paths::root()) | |
92 | .env("USER", Some("foo")) | |
93 | .env("HOME", Some(paths::home())) | |
94 | .env("CARGO_HOME", Some(my_home.clone())), | |
95 | execs().with_status(0)); | |
96 | ||
97 | let toml = paths::root().join("foo/Cargo.toml"); | |
9ed3a6ea | 98 | let toml = File::open(&toml).read_to_string().unwrap(); |
2badab8c BA |
99 | assert!(toml.as_slice().contains(r#"authors = ["foo <bar>"]"#)); |
100 | }); |