2019-11-25 02:42:45 +00:00
|
|
|
//! Tests for custom cargo commands and other global command features.
|
|
|
|
|
2015-02-06 07:27:53 +00:00
|
|
|
use std::env;
|
2021-08-06 00:19:42 +00:00
|
|
|
use std::fs;
|
2020-05-12 15:03:35 +00:00
|
|
|
use std::io::Read;
|
2015-02-27 01:04:25 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
2020-05-12 15:03:35 +00:00
|
|
|
use std::process::Stdio;
|
2014-08-21 16:24:34 +00:00
|
|
|
use std::str;
|
|
|
|
|
2019-09-12 17:14:29 +00:00
|
|
|
use cargo_test_support::cargo_process;
|
2021-08-06 00:19:42 +00:00
|
|
|
use cargo_test_support::paths;
|
2019-09-12 17:14:29 +00:00
|
|
|
use cargo_test_support::registry::Package;
|
2021-08-06 00:19:42 +00:00
|
|
|
use cargo_test_support::{basic_bin_manifest, basic_manifest, cargo_exe, project};
|
2014-08-09 05:37:50 +00:00
|
|
|
|
2015-02-27 01:04:25 +00:00
|
|
|
fn path() -> Vec<PathBuf> {
|
2017-09-24 14:26:37 +00:00
|
|
|
env::split_paths(&env::var_os("PATH").unwrap_or_default()).collect()
|
2014-08-12 04:22:29 +00:00
|
|
|
}
|
2015-02-06 07:27:53 +00:00
|
|
|
|
2019-06-05 18:52:53 +00:00
|
|
|
#[cargo_test]
|
2018-07-26 11:08:39 +00:00
|
|
|
fn list_commands_with_descriptions() {
|
|
|
|
let p = project().build();
|
2018-08-28 22:11:30 +00:00
|
|
|
p.cargo("--list")
|
2018-12-08 11:19:47 +00:00
|
|
|
.with_stdout_contains(
|
|
|
|
" build Compile a local package and all of its dependencies",
|
|
|
|
)
|
2019-02-03 04:01:23 +00:00
|
|
|
// Assert that `read-manifest` prints the right one-line description followed by another
|
|
|
|
// command, indented.
|
2018-12-08 11:19:47 +00:00
|
|
|
.with_stdout_contains(
|
|
|
|
" read-manifest Print a JSON representation of a Cargo.toml manifest.",
|
|
|
|
)
|
2018-08-28 22:11:30 +00:00
|
|
|
.run();
|
2018-07-26 11:08:39 +00:00
|
|
|
}
|
|
|
|
|
2020-07-24 21:40:49 +00:00
|
|
|
#[cargo_test]
|
|
|
|
fn list_aliases_with_descriptions() {
|
|
|
|
let p = project().build();
|
|
|
|
p.cargo("--list")
|
|
|
|
.with_stdout_contains(" b alias: build")
|
|
|
|
.with_stdout_contains(" c alias: check")
|
|
|
|
.with_stdout_contains(" r alias: run")
|
|
|
|
.with_stdout_contains(" t alias: test")
|
|
|
|
.run();
|
|
|
|
}
|
|
|
|
|
2019-06-05 18:52:53 +00:00
|
|
|
#[cargo_test]
|
2016-05-25 20:55:42 +00:00
|
|
|
fn list_command_looks_at_path() {
|
2021-08-06 00:19:42 +00:00
|
|
|
let proj = project()
|
|
|
|
.executable(Path::new("path-test").join("cargo-1"), "")
|
|
|
|
.build();
|
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();
|
2018-12-08 11:19:47 +00:00
|
|
|
let output = cargo_process("-v --list")
|
|
|
|
.env("PATH", &path)
|
|
|
|
.exec_with_output()
|
|
|
|
.unwrap();
|
2018-08-29 00:04:28 +00:00
|
|
|
let output = str::from_utf8(&output.stdout).unwrap();
|
|
|
|
assert!(
|
|
|
|
output.contains("\n 1 "),
|
|
|
|
"missing 1: {}",
|
|
|
|
output
|
|
|
|
);
|
2016-05-25 20:55:42 +00:00
|
|
|
}
|
2014-11-06 02:05:01 +00:00
|
|
|
|
2019-06-05 18:52:53 +00:00
|
|
|
#[cargo_test]
|
2016-05-25 20:55:42 +00:00
|
|
|
fn list_command_resolves_symlinks() {
|
2021-08-06 00:19:42 +00:00
|
|
|
let proj = project()
|
|
|
|
.symlink(cargo_exe(), Path::new("path-test").join("cargo-2"))
|
|
|
|
.build();
|
2016-04-19 19:00:30 +00:00
|
|
|
|
|
|
|
let mut path = path();
|
|
|
|
path.push(proj.root().join("path-test"));
|
|
|
|
let path = env::join_paths(path.iter()).unwrap();
|
2018-12-08 11:19:47 +00:00
|
|
|
let output = cargo_process("-v --list")
|
|
|
|
.env("PATH", &path)
|
|
|
|
.exec_with_output()
|
|
|
|
.unwrap();
|
2018-08-29 00:04:28 +00:00
|
|
|
let output = str::from_utf8(&output.stdout).unwrap();
|
|
|
|
assert!(
|
|
|
|
output.contains("\n 2 "),
|
|
|
|
"missing 2: {}",
|
|
|
|
output
|
|
|
|
);
|
2016-05-25 20:55:42 +00:00
|
|
|
}
|
2016-04-19 19:00:30 +00:00
|
|
|
|
2019-06-05 18:52:53 +00:00
|
|
|
#[cargo_test]
|
2016-05-25 20:55:42 +00:00
|
|
|
fn find_closest_biuld_to_build() {
|
2018-08-28 09:20:03 +00:00
|
|
|
cargo_process("biuld")
|
|
|
|
.with_status(101)
|
|
|
|
.with_stderr_contains(
|
2018-03-14 15:17:44 +00:00
|
|
|
"\
|
2018-03-19 20:13:04 +00:00
|
|
|
error: no such subcommand: `biuld`
|
|
|
|
|
|
|
|
<tab>Did you mean `build`?
|
2018-03-14 15:17:44 +00:00
|
|
|
",
|
2018-12-08 11:19:47 +00:00
|
|
|
)
|
|
|
|
.run();
|
2018-03-19 20:13:04 +00:00
|
|
|
|
|
|
|
// But, if we actually have `biuld`, it must work!
|
|
|
|
// https://github.com/rust-lang/cargo/issues/5201
|
|
|
|
Package::new("cargo-biuld", "1.0.0")
|
|
|
|
.file(
|
|
|
|
"src/main.rs",
|
|
|
|
r#"
|
2020-09-27 00:59:58 +00:00
|
|
|
fn main() {
|
|
|
|
println!("Similar, but not identical to, build");
|
|
|
|
}
|
|
|
|
"#,
|
2018-12-08 11:19:47 +00:00
|
|
|
)
|
|
|
|
.publish();
|
2018-08-28 09:20:03 +00:00
|
|
|
|
|
|
|
cargo_process("install cargo-biuld").run();
|
|
|
|
cargo_process("biuld")
|
|
|
|
.with_stdout("Similar, but not identical to, build\n")
|
|
|
|
.run();
|
|
|
|
cargo_process("--list")
|
|
|
|
.with_stdout_contains(
|
|
|
|
" build Compile a local package and all of its dependencies\n",
|
2018-12-08 11:19:47 +00:00
|
|
|
)
|
|
|
|
.with_stdout_contains(" biuld\n")
|
2018-08-28 09:20:03 +00:00
|
|
|
.run();
|
2016-05-25 20:55:42 +00:00
|
|
|
}
|
2014-11-06 02:05:01 +00:00
|
|
|
|
2019-10-05 15:44:26 +00:00
|
|
|
#[cargo_test]
|
|
|
|
fn find_closest_alias() {
|
|
|
|
let root = paths::root();
|
|
|
|
let my_home = root.join("my_home");
|
|
|
|
fs::create_dir(&my_home).unwrap();
|
2020-04-17 04:10:11 +00:00
|
|
|
fs::write(
|
|
|
|
&my_home.join("config"),
|
|
|
|
r#"
|
|
|
|
[alias]
|
|
|
|
myalias = "build"
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
2019-10-05 15:44:26 +00:00
|
|
|
|
|
|
|
cargo_process("myalais")
|
|
|
|
.env("CARGO_HOME", &my_home)
|
|
|
|
.with_status(101)
|
|
|
|
.with_stderr_contains(
|
|
|
|
"\
|
|
|
|
error: no such subcommand: `myalais`
|
|
|
|
|
|
|
|
<tab>Did you mean `myalias`?
|
|
|
|
",
|
|
|
|
)
|
|
|
|
.run();
|
|
|
|
|
|
|
|
// But, if no alias is defined, it must not suggest one!
|
|
|
|
cargo_process("myalais")
|
|
|
|
.with_status(101)
|
|
|
|
.with_stderr_contains(
|
|
|
|
"\
|
|
|
|
error: no such subcommand: `myalais`
|
|
|
|
",
|
|
|
|
)
|
|
|
|
.with_stderr_does_not_contain(
|
|
|
|
"\
|
|
|
|
<tab>Did you mean `myalias`?
|
|
|
|
",
|
|
|
|
)
|
|
|
|
.run();
|
|
|
|
}
|
|
|
|
|
2019-02-03 04:01:23 +00:00
|
|
|
// If a subcommand is more than an edit distance of 3 away, we don't make a suggestion.
|
2019-06-05 18:52:53 +00:00
|
|
|
#[cargo_test]
|
2016-05-25 20:55:42 +00:00
|
|
|
fn find_closest_dont_correct_nonsense() {
|
2018-08-28 09:20:03 +00:00
|
|
|
cargo_process("there-is-no-way-that-there-is-a-command-close-to-this")
|
|
|
|
.cwd(&paths::root())
|
|
|
|
.with_status(101)
|
|
|
|
.with_stderr(
|
2018-03-14 15:17:44 +00:00
|
|
|
"[ERROR] no such subcommand: \
|
2016-07-05 12:49:01 +00:00
|
|
|
`there-is-no-way-that-there-is-a-command-close-to-this`
|
2018-03-14 15:17:44 +00:00
|
|
|
",
|
2018-12-08 11:19:47 +00:00
|
|
|
)
|
|
|
|
.run();
|
2016-07-05 12:09:11 +00:00
|
|
|
}
|
|
|
|
|
2019-06-05 18:52:53 +00:00
|
|
|
#[cargo_test]
|
2016-07-05 12:09:11 +00:00
|
|
|
fn displays_subcommand_on_error() {
|
2018-08-28 09:20:03 +00:00
|
|
|
cargo_process("invalid-command")
|
|
|
|
.with_status(101)
|
|
|
|
.with_stderr("[ERROR] no such subcommand: `invalid-command`\n")
|
|
|
|
.run();
|
2016-05-25 20:55:42 +00:00
|
|
|
}
|
2014-12-22 21:58:24 +00:00
|
|
|
|
2021-03-20 08:45:19 +00:00
|
|
|
#[cargo_test]
|
|
|
|
fn override_cargo_home() {
|
|
|
|
let root = paths::root();
|
|
|
|
let my_home = root.join("my_home");
|
|
|
|
fs::create_dir(&my_home).unwrap();
|
|
|
|
fs::write(
|
|
|
|
&my_home.join("config"),
|
|
|
|
r#"
|
|
|
|
[cargo-new]
|
|
|
|
vcs = "none"
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
cargo_process("new foo").env("CARGO_HOME", &my_home).run();
|
|
|
|
|
|
|
|
assert!(!paths::root().join("foo/.git").is_dir());
|
|
|
|
|
|
|
|
cargo_process("new foo2").run();
|
|
|
|
|
|
|
|
assert!(paths::root().join("foo2/.git").is_dir());
|
|
|
|
}
|
|
|
|
|
2019-06-05 18:52:53 +00:00
|
|
|
#[cargo_test]
|
2017-03-02 06:38:54 +00:00
|
|
|
fn cargo_subcommand_env() {
|
2018-03-14 15:17:44 +00:00
|
|
|
let src = format!(
|
|
|
|
r#"
|
2017-03-02 06:38:54 +00:00
|
|
|
use std::env;
|
|
|
|
|
|
|
|
fn main() {{
|
|
|
|
println!("{{}}", env::var("{}").unwrap());
|
|
|
|
}}
|
2018-03-14 15:17:44 +00:00
|
|
|
"#,
|
|
|
|
cargo::CARGO_ENV
|
|
|
|
);
|
2017-03-02 06:38:54 +00:00
|
|
|
|
2018-08-28 09:20:03 +00:00
|
|
|
let p = project()
|
|
|
|
.at("cargo-envtest")
|
2017-03-02 06:38:54 +00:00
|
|
|
.file("Cargo.toml", &basic_bin_manifest("cargo-envtest"))
|
2017-07-22 03:12:21 +00:00
|
|
|
.file("src/main.rs", &src)
|
|
|
|
.build();
|
2017-03-02 06:38:54 +00:00
|
|
|
|
|
|
|
let target_dir = p.target_debug_dir();
|
|
|
|
|
2018-08-28 09:20:03 +00:00
|
|
|
p.cargo("build").run();
|
2018-08-29 06:11:10 +00:00
|
|
|
assert!(p.bin("cargo-envtest").is_file());
|
2017-03-02 06:38:54 +00:00
|
|
|
|
|
|
|
let cargo = cargo_exe().canonicalize().unwrap();
|
|
|
|
let mut path = path();
|
|
|
|
path.push(target_dir);
|
|
|
|
let path = env::join_paths(path.iter()).unwrap();
|
|
|
|
|
2018-08-28 09:20:03 +00:00
|
|
|
cargo_process("envtest")
|
|
|
|
.env("PATH", &path)
|
|
|
|
.with_stdout(cargo.to_str().unwrap())
|
|
|
|
.run();
|
2017-03-02 06:38:54 +00:00
|
|
|
}
|
|
|
|
|
2019-06-05 18:52:53 +00:00
|
|
|
#[cargo_test]
|
2018-03-19 21:31:37 +00:00
|
|
|
fn cargo_subcommand_args() {
|
2018-08-28 09:20:03 +00:00
|
|
|
let p = project()
|
|
|
|
.at("cargo-foo")
|
2018-07-24 22:35:01 +00:00
|
|
|
.file("Cargo.toml", &basic_manifest("cargo-foo", "0.0.1"))
|
2018-03-19 21:31:37 +00:00
|
|
|
.file(
|
|
|
|
"src/main.rs",
|
|
|
|
r#"
|
2020-09-27 00:59:58 +00:00
|
|
|
fn main() {
|
|
|
|
let args: Vec<_> = ::std::env::args().collect();
|
2021-06-16 16:41:38 +00:00
|
|
|
println!("{}", args.join(" "));
|
2020-09-27 00:59:58 +00:00
|
|
|
}
|
|
|
|
"#,
|
2018-12-08 11:19:47 +00:00
|
|
|
)
|
|
|
|
.build();
|
2018-03-19 21:31:37 +00:00
|
|
|
|
2018-08-28 09:20:03 +00:00
|
|
|
p.cargo("build").run();
|
2018-03-19 21:31:37 +00:00
|
|
|
let cargo_foo_bin = p.bin("cargo-foo");
|
2018-08-29 06:11:10 +00:00
|
|
|
assert!(cargo_foo_bin.is_file());
|
2018-03-19 21:31:37 +00:00
|
|
|
|
|
|
|
let mut path = path();
|
|
|
|
path.push(p.target_debug_dir());
|
|
|
|
let path = env::join_paths(path.iter()).unwrap();
|
|
|
|
|
2018-08-28 09:20:03 +00:00
|
|
|
cargo_process("foo bar -v --help")
|
|
|
|
.env("PATH", &path)
|
2021-06-16 16:41:38 +00:00
|
|
|
.with_stdout("[CWD]/cargo-foo/target/debug/cargo-foo[EXE] foo bar -v --help")
|
2018-11-18 13:38:33 +00:00
|
|
|
.run();
|
2018-03-19 21:31:37 +00:00
|
|
|
}
|
|
|
|
|
2019-06-05 18:52:53 +00:00
|
|
|
#[cargo_test]
|
2016-05-25 20:55:42 +00:00
|
|
|
fn explain() {
|
2018-08-28 09:20:03 +00:00
|
|
|
cargo_process("--explain E0001")
|
|
|
|
.with_stdout_contains(
|
2018-03-14 15:43:41 +00:00
|
|
|
"This error suggests that the expression arm corresponding to the noted pattern",
|
2018-12-08 11:19:47 +00:00
|
|
|
)
|
|
|
|
.run();
|
2016-05-25 20:55:42 +00:00
|
|
|
}
|
2018-03-31 09:19:28 +00:00
|
|
|
|
2020-05-12 15:03:35 +00:00
|
|
|
#[cargo_test]
|
|
|
|
fn closed_output_ok() {
|
|
|
|
// Checks that closed output doesn't cause an error.
|
|
|
|
let mut p = cargo_process("--list").build_command();
|
|
|
|
p.stdout(Stdio::piped()).stderr(Stdio::piped());
|
|
|
|
let mut child = p.spawn().unwrap();
|
|
|
|
// Close stdout
|
|
|
|
drop(child.stdout.take());
|
|
|
|
// Read stderr
|
|
|
|
let mut s = String::new();
|
|
|
|
child
|
|
|
|
.stderr
|
|
|
|
.as_mut()
|
|
|
|
.unwrap()
|
|
|
|
.read_to_string(&mut s)
|
|
|
|
.unwrap();
|
|
|
|
let status = child.wait().unwrap();
|
|
|
|
assert!(status.success());
|
2021-02-06 17:40:42 +00:00
|
|
|
assert!(s.is_empty(), "{}", s);
|
2020-05-12 15:03:35 +00:00
|
|
|
}
|