Skip to content

Commit 440cfad

Browse files
committed
cargo-run-deps: Testcases for cargo run -p <dev-dependency-pkgid>
TDD testcases for allowing `cargo run` to be invoked on dependencies of the workspace members via their corresponding pkgid. Relates to rust-lang#2267, rust-lang#872.
1 parent 46fa867 commit 440cfad

File tree

1 file changed

+108
-7
lines changed

1 file changed

+108
-7
lines changed

tests/testsuite/run.rs

Lines changed: 108 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Tests for the `cargo run` command.
22
3+
use cargo_test_support::registry::Package;
34
use cargo_test_support::{basic_bin_manifest, basic_lib_manifest, project, Project};
45
use cargo_util::paths::dylib_path_envvar;
56

@@ -1157,7 +1158,7 @@ fn run_multiple_packages() {
11571158
.file("foo/d2/Cargo.toml", &basic_bin_manifest("d2"))
11581159
.file("foo/d2/src/main.rs", "fn main() { println!(\"d2\"); }")
11591160
.file("d3/Cargo.toml", &basic_bin_manifest("d3"))
1160-
.file("d3/src/main.rs", "fn main() { println!(\"d2\"); }")
1161+
.file("d3/src/main.rs", "fn main() { println!(\"d3\"); }")
11611162
.build();
11621163

11631164
let cargo = || {
@@ -1182,12 +1183,7 @@ fn run_multiple_packages() {
11821183
.with_status(1)
11831184
.with_stderr_contains("error: The argument '--package <SPEC>' was provided more than once, but cannot be used multiple times").run();
11841185

1185-
cargo()
1186-
.arg("-p")
1187-
.arg("d3")
1188-
.with_status(101)
1189-
.with_stderr_contains("[ERROR] package(s) `d3` not found in workspace [..]")
1190-
.run();
1186+
cargo().arg("-p").arg("d3").with_stdout("d3").run();
11911187

11921188
cargo()
11931189
.arg("-p")
@@ -1199,6 +1195,111 @@ fn run_multiple_packages() {
11991195
.run();
12001196
}
12011197

1198+
#[cargo_test]
1199+
fn run_dependency_package() {
1200+
Package::new("bdep", "0.1.0")
1201+
.file(
1202+
"Cargo.toml",
1203+
r#"
1204+
[package]
1205+
name = "bdep"
1206+
version = "0.1.0"
1207+
authors = ["wycats@example.com"]
1208+
1209+
[[bin]]
1210+
name = "bdep"
1211+
"#,
1212+
)
1213+
.file("src/main.rs", "fn main() { println!(\"bdep 0.1.0\"); }")
1214+
.publish();
1215+
Package::new("bdep", "0.1.1")
1216+
.file(
1217+
"Cargo.toml",
1218+
r#"
1219+
[package]
1220+
name = "bdep"
1221+
version = "0.1.1"
1222+
authors = ["wycats@example.com"]
1223+
1224+
[[bin]]
1225+
name = "bdep"
1226+
"#,
1227+
)
1228+
.file("src/main.rs", "fn main() { println!(\"bdep 0.1.1\"); }")
1229+
.publish();
1230+
Package::new("libdep", "0.1.0")
1231+
.file(
1232+
"Cargo.toml",
1233+
r#"
1234+
[package]
1235+
name = "libdep"
1236+
version = "0.1.0"
1237+
authors = ["wycats@example.com"]
1238+
1239+
[lib]
1240+
"#,
1241+
)
1242+
.file("src/lib.rs", "pub fn f() {}")
1243+
.publish();
1244+
1245+
let p = project()
1246+
.file(
1247+
"Cargo.toml",
1248+
r#"
1249+
[package]
1250+
name = "foo"
1251+
version = "0.1.0"
1252+
edition = "2018"
1253+
1254+
[build-dependencies]
1255+
bdep = "0.1"
1256+
libdep = "0.1"
1257+
"#,
1258+
)
1259+
.file("src/main.rs", "fn main() { println!(\"foo\"); }")
1260+
.build();
1261+
1262+
let testcases = [
1263+
("bdep", "bdep 0.1.1"),
1264+
("bdep:0.1.1", "bdep 0.1.1"),
1265+
(
1266+
"https://github.com/rust-lang/crates.io-index#bdep",
1267+
"bdep 0.1.1",
1268+
),
1269+
];
1270+
for (pkgid, expected_output) in testcases {
1271+
p.cargo("run")
1272+
.arg("-p")
1273+
.arg(pkgid)
1274+
.with_stdout(expected_output)
1275+
.run();
1276+
}
1277+
1278+
p.cargo("run")
1279+
.arg("-p")
1280+
.arg("libdep")
1281+
.with_status(101)
1282+
.with_stderr_contains("[ERROR] a bin target must be available for `cargo run`")
1283+
.run();
1284+
1285+
let invalid_pkgids = [
1286+
"bdep:0.1.0",
1287+
"https://github.com/rust-lang/crates.io-index#bdep:0.1.0",
1288+
"bdep:0.2.0",
1289+
"https://github.com/rust-lang/crates.io-index#bdep:0.2.0",
1290+
];
1291+
for pkgid in invalid_pkgids {
1292+
p.cargo("run")
1293+
.arg("-p")
1294+
.arg(pkgid)
1295+
.with_status(101)
1296+
.with_stderr_contains(
1297+
"[ERROR] `cargo run` cannot find pkgid either in the workspace or among the workspace dependencies",
1298+
)
1299+
.run();
1300+
}
1301+
}
1302+
12021303
#[cargo_test]
12031304
fn explicit_bin_with_args() {
12041305
let p = project()

0 commit comments

Comments
 (0)