2015-02-06 07:27:53 +00:00
|
|
|
use std::env;
|
|
|
|
use std::ffi::OsString;
|
2015-02-27 01:04:25 +00:00
|
|
|
use std::fs::{self, File};
|
|
|
|
use std::io::prelude::*;
|
|
|
|
use std::path::{Path, PathBuf};
|
2014-08-21 16:24:34 +00:00
|
|
|
use std::str;
|
|
|
|
|
2015-12-17 17:53:14 +00:00
|
|
|
use cargo_process;
|
2014-08-09 05:37:50 +00:00
|
|
|
use support::paths;
|
2016-02-27 18:53:30 +00:00
|
|
|
use support::{execs, project, mkdir_recursive, ProjectBuilder, ERROR};
|
2014-11-06 02:05:01 +00:00
|
|
|
use hamcrest::{assert_that};
|
2014-08-09 05:37:50 +00:00
|
|
|
|
|
|
|
fn setup() {
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Add an empty file with executable flags (and platform-dependent suffix).
|
|
|
|
/// TODO: move this to `ProjectBuilder` if other cases using this emerge.
|
|
|
|
fn fake_executable(proj: ProjectBuilder, dir: &Path, name: &str) -> ProjectBuilder {
|
2015-02-27 01:04:25 +00:00
|
|
|
let path = proj.root().join(dir).join(&format!("{}{}", name,
|
|
|
|
env::consts::EXE_SUFFIX));
|
|
|
|
mkdir_recursive(path.parent().unwrap()).unwrap();
|
|
|
|
File::create(&path).unwrap();
|
|
|
|
make_executable(&path);
|
|
|
|
return proj;
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
fn make_executable(p: &Path) {
|
|
|
|
use std::os::unix::prelude::*;
|
|
|
|
|
2016-04-19 18:59:55 +00:00
|
|
|
let mut perms = fs::metadata(p).unwrap().permissions();
|
2015-02-27 01:04:25 +00:00
|
|
|
let mode = perms.mode();
|
|
|
|
perms.set_mode(mode | 0o111);
|
|
|
|
fs::set_permissions(p, perms).unwrap();
|
|
|
|
}
|
|
|
|
#[cfg(windows)]
|
|
|
|
fn make_executable(_: &Path) {}
|
2014-08-09 05:37:50 +00:00
|
|
|
}
|
|
|
|
|
2015-02-27 01:04:25 +00:00
|
|
|
fn path() -> Vec<PathBuf> {
|
2015-02-13 04:10:07 +00:00
|
|
|
env::split_paths(&env::var_os("PATH").unwrap_or(OsString::new())).collect()
|
2014-08-12 04:22:29 +00:00
|
|
|
}
|
2015-02-06 07:27:53 +00:00
|
|
|
|
2016-04-19 18:59:55 +00:00
|
|
|
test!(list_command_looks_at_path {
|
2014-08-09 05:37:50 +00:00
|
|
|
let proj = project("list-non-overlapping");
|
|
|
|
let proj = fake_executable(proj, &Path::new("path-test"), "cargo-1");
|
2015-12-17 17:53:14 +00:00
|
|
|
let mut pr = cargo_process();
|
2014-08-09 05:37:50 +00:00
|
|
|
|
2015-01-14 08:19:27 +00:00
|
|
|
let mut path = path();
|
2014-08-12 04:22:29 +00:00
|
|
|
path.push(proj.root().join("path-test"));
|
2015-02-06 07:27:53 +00:00
|
|
|
let path = env::join_paths(path.iter()).unwrap();
|
|
|
|
let output = pr.arg("-v").arg("--list")
|
2015-02-27 01:04:25 +00:00
|
|
|
.env("PATH", &path);
|
2014-12-21 23:19:44 +00:00
|
|
|
let output = output.exec_with_output().unwrap();
|
2015-02-27 01:04:25 +00:00
|
|
|
let output = str::from_utf8(&output.stdout).unwrap();
|
2014-08-21 16:24:34 +00:00
|
|
|
assert!(output.contains("\n 1\n"), "missing 1: {}", output);
|
2014-12-19 18:38:32 +00:00
|
|
|
});
|
2014-11-06 02:05:01 +00:00
|
|
|
|
|
|
|
test!(find_closest_biuld_to_build {
|
2015-12-17 17:53:14 +00:00
|
|
|
let mut pr = cargo_process();
|
|
|
|
pr.arg("biuld");
|
2014-11-06 02:05:01 +00:00
|
|
|
|
|
|
|
assert_that(pr,
|
2015-12-03 23:32:30 +00:00
|
|
|
execs().with_status(101)
|
2016-02-27 18:53:30 +00:00
|
|
|
.with_stderr(&format!("{error} no such subcommand
|
2014-11-06 02:05:01 +00:00
|
|
|
|
2016-03-03 18:18:02 +00:00
|
|
|
<tab>Did you mean `build`?
|
2014-11-06 02:05:01 +00:00
|
|
|
|
2016-02-27 18:53:30 +00:00
|
|
|
",
|
|
|
|
error = ERROR)));
|
2014-12-19 18:38:32 +00:00
|
|
|
});
|
2014-11-06 02:05:01 +00:00
|
|
|
|
|
|
|
// if a subcommand is more than 3 edit distance away, we don't make a suggestion
|
|
|
|
test!(find_closest_dont_correct_nonsense {
|
2015-12-17 17:53:14 +00:00
|
|
|
let mut pr = cargo_process();
|
2016-04-08 20:30:45 +00:00
|
|
|
pr.arg("there-is-no-way-that-there-is-a-command-close-to-this")
|
|
|
|
.cwd(&paths::root());
|
2014-11-06 02:05:01 +00:00
|
|
|
|
|
|
|
assert_that(pr,
|
2015-12-03 23:32:30 +00:00
|
|
|
execs().with_status(101)
|
2016-02-27 18:53:30 +00:00
|
|
|
.with_stderr(&format!("{error} no such subcommand
|
|
|
|
",
|
|
|
|
error = ERROR)));
|
2014-12-19 18:38:32 +00:00
|
|
|
});
|
2014-12-22 21:58:24 +00:00
|
|
|
|
|
|
|
test!(override_cargo_home {
|
|
|
|
let root = paths::root();
|
|
|
|
let my_home = root.join("my_home");
|
2015-02-27 01:04:25 +00:00
|
|
|
fs::create_dir(&my_home).unwrap();
|
|
|
|
File::create(&my_home.join("config")).unwrap().write_all(br#"
|
2014-12-22 21:58:24 +00:00
|
|
|
[cargo-new]
|
|
|
|
name = "foo"
|
|
|
|
email = "bar"
|
|
|
|
git = false
|
2014-12-21 23:19:44 +00:00
|
|
|
"#).unwrap();
|
2014-12-22 21:58:24 +00:00
|
|
|
|
2015-12-17 17:53:14 +00:00
|
|
|
assert_that(cargo_process()
|
2015-02-27 01:04:25 +00:00
|
|
|
.arg("new").arg("foo")
|
|
|
|
.env("USER", "foo")
|
|
|
|
.env("CARGO_HOME", &my_home),
|
2014-12-22 21:58:24 +00:00
|
|
|
execs().with_status(0));
|
|
|
|
|
|
|
|
let toml = paths::root().join("foo/Cargo.toml");
|
2015-02-27 01:04:25 +00:00
|
|
|
let mut contents = String::new();
|
|
|
|
File::open(&toml).unwrap().read_to_string(&mut contents).unwrap();
|
|
|
|
assert!(contents.contains(r#"authors = ["foo <bar>"]"#));
|
2014-12-22 21:58:24 +00:00
|
|
|
});
|
2015-03-16 13:32:50 +00:00
|
|
|
|
|
|
|
test!(cargo_help {
|
2015-12-17 17:53:14 +00:00
|
|
|
assert_that(cargo_process(),
|
2015-03-16 13:32:50 +00:00
|
|
|
execs().with_status(0));
|
2015-12-17 17:53:14 +00:00
|
|
|
assert_that(cargo_process().arg("help"),
|
2015-03-16 13:32:50 +00:00
|
|
|
execs().with_status(0));
|
2015-12-17 17:53:14 +00:00
|
|
|
assert_that(cargo_process().arg("-h"),
|
2015-03-16 13:32:50 +00:00
|
|
|
execs().with_status(0));
|
2015-12-17 17:53:14 +00:00
|
|
|
assert_that(cargo_process().arg("help").arg("build"),
|
2015-03-16 13:32:50 +00:00
|
|
|
execs().with_status(0));
|
2015-12-17 17:53:14 +00:00
|
|
|
assert_that(cargo_process().arg("build").arg("-h"),
|
2015-03-16 13:32:50 +00:00
|
|
|
execs().with_status(0));
|
2015-12-17 17:53:14 +00:00
|
|
|
assert_that(cargo_process().arg("help").arg("-h"),
|
2015-03-16 13:32:50 +00:00
|
|
|
execs().with_status(0));
|
2015-12-17 17:53:14 +00:00
|
|
|
assert_that(cargo_process().arg("help").arg("help"),
|
2015-03-16 13:32:50 +00:00
|
|
|
execs().with_status(0));
|
|
|
|
});
|