Skip to content

Commit 018b758

Browse files
committed
test(pkgid): Focus tests on use cases, rather than success/failure
I assume the reason these aren't all individual tests is test-time but if we divide by success/failure, I'll need to duplicate things to handle partial versions. Overall, I feel like this makes the tests make more sense.
1 parent 3138f91 commit 018b758

File tree

1 file changed

+75
-41
lines changed

1 file changed

+75
-41
lines changed

tests/testsuite/pkgid.rs

Lines changed: 75 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,31 @@ use cargo_test_support::project;
44
use cargo_test_support::registry::Package;
55

66
#[cargo_test]
7-
fn simple() {
8-
Package::new("bar", "0.1.0").publish();
7+
fn local() {
98
let p = project()
109
.file(
1110
"Cargo.toml",
1211
r#"
12+
[workspace]
13+
members = ["bar"]
14+
1315
[package]
1416
name = "foo"
1517
version = "0.1.0"
1618
edition = "2018"
17-
18-
[dependencies]
19-
bar = "0.1.0"
2019
"#,
2120
)
2221
.file("src/main.rs", "fn main() {}")
22+
.file(
23+
"bar/Cargo.toml",
24+
r#"
25+
[package]
26+
name = "bar"
27+
version = "0.1.0"
28+
edition = "2018"
29+
"#,
30+
)
31+
.file("bar/src/main.rs", "fn main() {}")
2332
.build();
2433

2534
p.cargo("generate-lockfile").run();
@@ -28,16 +37,38 @@ fn simple() {
2837
.with_stdout(format!("file://[..]{}#0.1.0", p.root().to_str().unwrap()))
2938
.run();
3039

31-
p.cargo("pkgid bar")
32-
.with_stdout("https://github.com/rust-lang/crates.io-index#bar@0.1.0")
40+
// Bad file URL.
41+
p.cargo("pkgid ./Cargo.toml")
42+
.with_status(101)
43+
.with_stderr(
44+
"\
45+
error: invalid package ID specification: `./Cargo.toml`
46+
47+
Caused by:
48+
package ID specification `./Cargo.toml` looks like a file path, maybe try file://[..]/Cargo.toml
49+
",
50+
)
51+
.run();
52+
53+
// Bad file URL with similar name.
54+
p.cargo("pkgid './bar'")
55+
.with_status(101)
56+
.with_stderr(
57+
"\
58+
error: invalid package ID specification: `./bar`
59+
60+
<tab>Did you mean `bar`?
61+
62+
Caused by:
63+
package ID specification `./bar` looks like a file path, maybe try file://[..]/bar
64+
",
65+
)
3366
.run();
3467
}
3568

3669
#[cargo_test]
37-
fn suggestion_bad_pkgid() {
70+
fn registry() {
3871
Package::new("crates-io", "0.1.0").publish();
39-
Package::new("two-ver", "0.1.0").publish();
40-
Package::new("two-ver", "0.2.0").publish();
4172
let p = project()
4273
.file(
4374
"Cargo.toml",
@@ -49,16 +80,18 @@ fn suggestion_bad_pkgid() {
4980
5081
[dependencies]
5182
crates-io = "0.1.0"
52-
two-ver = "0.1.0"
53-
two-ver2 = { package = "two-ver", version = "0.2.0" }
5483
"#,
5584
)
56-
.file("src/lib.rs", "")
85+
.file("src/main.rs", "fn main() {}")
5786
.file("cratesio", "")
5887
.build();
5988

6089
p.cargo("generate-lockfile").run();
6190

91+
p.cargo("pkgid crates-io")
92+
.with_stdout("https://github.com/rust-lang/crates.io-index#crates-io@0.1.0")
93+
.run();
94+
6295
// Bad URL.
6396
p.cargo("pkgid https://example.com/crates-io")
6497
.with_status(101)
@@ -83,6 +116,35 @@ error: package ID specification `crates_io` did not match any packages
83116
",
84117
)
85118
.run();
119+
}
120+
121+
#[cargo_test]
122+
fn multiple_versions() {
123+
Package::new("two-ver", "0.1.0").publish();
124+
Package::new("two-ver", "0.2.0").publish();
125+
let p = project()
126+
.file(
127+
"Cargo.toml",
128+
r#"
129+
[package]
130+
name = "foo"
131+
version = "0.1.0"
132+
edition = "2018"
133+
134+
[dependencies]
135+
two-ver = "0.1.0"
136+
two-ver2 = { package = "two-ver", version = "0.2.0" }
137+
"#,
138+
)
139+
.file("src/lib.rs", "")
140+
.file("cratesio", "")
141+
.build();
142+
143+
p.cargo("generate-lockfile").run();
144+
145+
p.cargo("pkgid two-ver:0.2.0")
146+
.with_stdout("https://github.com/rust-lang/crates.io-index#two-ver@0.2.0")
147+
.run();
86148

87149
// Ambiguous.
88150
p.cargo("pkgid two-ver")
@@ -107,34 +169,6 @@ Did you mean one of these?
107169
108170
two-ver@0.1.0
109171
two-ver@0.2.0
110-
",
111-
)
112-
.run();
113-
114-
// Bad file URL.
115-
p.cargo("pkgid ./Cargo.toml")
116-
.with_status(101)
117-
.with_stderr(
118-
"\
119-
error: invalid package ID specification: `./Cargo.toml`
120-
121-
Caused by:
122-
package ID specification `./Cargo.toml` looks like a file path, maybe try file://[..]/Cargo.toml
123-
",
124-
)
125-
.run();
126-
127-
// Bad file URL with similar name.
128-
p.cargo("pkgid './cratesio'")
129-
.with_status(101)
130-
.with_stderr(
131-
"\
132-
error: invalid package ID specification: `./cratesio`
133-
134-
<tab>Did you mean `crates-io`?
135-
136-
Caused by:
137-
package ID specification `./cratesio` looks like a file path, maybe try file://[..]/cratesio
138172
",
139173
)
140174
.run();

0 commit comments

Comments
 (0)