]>
Commit | Line | Data |
---|---|---|
ee5e24ff AC |
1 | use std::env; |
2 | use std::ffi::OsString; | |
a6dad622 AC |
3 | use std::fs::{self, File}; |
4 | use std::io::prelude::*; | |
5 | use std::path::{Path, PathBuf}; | |
8cce8996 | 6 | use std::str; |
8cce8996 | 7 | |
26a5eeff | 8 | use cargo_process; |
a3f6a404 | 9 | use support::paths; |
1c439875 | 10 | use support::{execs, project, mkdir_recursive, ProjectBuilder, ERROR}; |
12f5de8e | 11 | use hamcrest::{assert_that}; |
a3f6a404 | 12 | |
13 | fn 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. | |
18 | fn 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 | 39 | fn path() -> Vec<PathBuf> { |
1384050e | 40 | env::split_paths(&env::var_os("PATH").unwrap_or(OsString::new())).collect() |
db3823a8 | 41 | } |
ee5e24ff | 42 | |
f3cb4232 | 43 | test!(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"); |
26a5eeff | 46 | let mut pr = cargo_process(); |
a3f6a404 | 47 | |
5d0cb3f2 | 48 | let mut path = path(); |
db3823a8 | 49 | path.push(proj.root().join("path-test")); |
ee5e24ff AC |
50 | let path = env::join_paths(path.iter()).unwrap(); |
51 | let output = pr.arg("-v").arg("--list") | |
a6dad622 | 52 | .env("PATH", &path); |
9ed3a6ea | 53 | let output = output.exec_with_output().unwrap(); |
a6dad622 | 54 | let output = str::from_utf8(&output.stdout).unwrap(); |
8cce8996 | 55 | assert!(output.contains("\n 1\n"), "missing 1: {}", output); |
157d639a | 56 | }); |
12f5de8e PW |
57 | |
58 | test!(find_closest_biuld_to_build { | |
26a5eeff AC |
59 | let mut pr = cargo_process(); |
60 | pr.arg("biuld"); | |
12f5de8e PW |
61 | |
62 | assert_that(pr, | |
20b768e6 | 63 | execs().with_status(101) |
1c439875 | 64 | .with_stderr(&format!("{error} no such subcommand |
12f5de8e | 65 | |
1a1d8f8c | 66 | <tab>Did you mean `build`? |
12f5de8e | 67 | |
1c439875 SBI |
68 | ", |
69 | error = ERROR))); | |
157d639a | 70 | }); |
12f5de8e PW |
71 | |
72 | // if a subcommand is more than 3 edit distance away, we don't make a suggestion | |
73 | test!(find_closest_dont_correct_nonsense { | |
26a5eeff | 74 | let mut pr = cargo_process(); |
bc8e3322 AC |
75 | pr.arg("there-is-no-way-that-there-is-a-command-close-to-this") |
76 | .cwd(&paths::root()); | |
12f5de8e PW |
77 | |
78 | assert_that(pr, | |
20b768e6 | 79 | execs().with_status(101) |
1c439875 SBI |
80 | .with_stderr(&format!("{error} no such subcommand |
81 | ", | |
82 | error = ERROR))); | |
157d639a | 83 | }); |
2badab8c BA |
84 | |
85 | test!(override_cargo_home { | |
86 | let root = paths::root(); | |
87 | let my_home = root.join("my_home"); | |
a6dad622 AC |
88 | fs::create_dir(&my_home).unwrap(); |
89 | File::create(&my_home.join("config")).unwrap().write_all(br#" | |
2badab8c BA |
90 | [cargo-new] |
91 | name = "foo" | |
92 | email = "bar" | |
93 | git = false | |
9ed3a6ea | 94 | "#).unwrap(); |
2badab8c | 95 | |
26a5eeff | 96 | assert_that(cargo_process() |
a6dad622 | 97 | .arg("new").arg("foo") |
a6dad622 | 98 | .env("USER", "foo") |
a6dad622 | 99 | .env("CARGO_HOME", &my_home), |
2badab8c BA |
100 | execs().with_status(0)); |
101 | ||
102 | let toml = paths::root().join("foo/Cargo.toml"); | |
a6dad622 AC |
103 | let mut contents = String::new(); |
104 | File::open(&toml).unwrap().read_to_string(&mut contents).unwrap(); | |
105 | assert!(contents.contains(r#"authors = ["foo <bar>"]"#)); | |
2badab8c | 106 | }); |
2ff5f53b AC |
107 | |
108 | test!(cargo_help { | |
26a5eeff | 109 | assert_that(cargo_process(), |
2ff5f53b | 110 | execs().with_status(0)); |
26a5eeff | 111 | assert_that(cargo_process().arg("help"), |
2ff5f53b | 112 | execs().with_status(0)); |
26a5eeff | 113 | assert_that(cargo_process().arg("-h"), |
2ff5f53b | 114 | execs().with_status(0)); |
26a5eeff | 115 | assert_that(cargo_process().arg("help").arg("build"), |
2ff5f53b | 116 | execs().with_status(0)); |
26a5eeff | 117 | assert_that(cargo_process().arg("build").arg("-h"), |
2ff5f53b | 118 | execs().with_status(0)); |
26a5eeff | 119 | assert_that(cargo_process().arg("help").arg("-h"), |
2ff5f53b | 120 | execs().with_status(0)); |
26a5eeff | 121 | assert_that(cargo_process().arg("help").arg("help"), |
2ff5f53b AC |
122 | execs().with_status(0)); |
123 | }); |