Skip to content

Commit 568b23f

Browse files
committed
Auto merge of #4209 - natboehm:add-index-flag, r=alexcrichton
Replace `cargo publish --host` with `cargo publish --index` This change makes the command consistent with other versions with the same flag. `cargo publish --host` is still supported, currently marked as deprecated, or for reuse as mentioned in issue #4208. Fixes #3797
2 parents ad1de97 + 78e64de commit 568b23f

File tree

3 files changed

+173
-15
lines changed

3 files changed

+173
-15
lines changed

src/bin/publish.rs

+29-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ use cargo::util::important_paths::find_root_manifest_for_wd;
55

66
#[derive(Deserialize)]
77
pub struct Options {
8-
flag_host: Option<String>,
8+
flag_index: Option<String>,
9+
flag_host: Option<String>, // TODO: Deprecated, remove
910
flag_token: Option<String>,
1011
flag_manifest_path: Option<String>,
1112
flag_verbose: u32,
@@ -27,7 +28,8 @@ Usage:
2728
2829
Options:
2930
-h, --help Print this message
30-
--host HOST Host to upload the package to
31+
--index INDEX Registry index to upload the package to
32+
--host HOST DEPRECATED, renamed to '--index'
3133
--token TOKEN Token to use when uploading
3234
--no-verify Don't verify package tarball before publish
3335
--allow-dirty Allow publishing with a dirty source directory
@@ -48,9 +50,13 @@ pub fn execute(options: Options, config: &Config) -> CliResult {
4850
&options.flag_color,
4951
options.flag_frozen,
5052
options.flag_locked)?;
53+
54+
55+
5156
let Options {
5257
flag_token: token,
53-
flag_host: host,
58+
flag_index: index,
59+
flag_host: host, // TODO: Deprecated, remove
5460
flag_manifest_path,
5561
flag_no_verify: no_verify,
5662
flag_allow_dirty: allow_dirty,
@@ -59,12 +65,31 @@ pub fn execute(options: Options, config: &Config) -> CliResult {
5965
..
6066
} = options;
6167

68+
69+
// TODO: Deprecated
70+
// remove once it has been decided --host can be removed
71+
// We may instead want to repurpose the host flag, as
72+
// mentioned in this issue
73+
// https://github.com/rust-lang/cargo/issues/4208
74+
let msg = "The flag '--host' is no longer valid.
75+
76+
Previous versions of Cargo accepted this flag, but it is being
77+
deprecated. The flag is being renamed to 'index', as the flag
78+
wants the location of the index to which to publish. Please
79+
use '--index' instead.
80+
81+
This will soon become a hard error, so it's either recommended
82+
to update to a fixed version or contact the upstream maintainer
83+
about this warning.";
84+
6285
let root = find_root_manifest_for_wd(flag_manifest_path.clone(), config.cwd())?;
6386
let ws = Workspace::new(&root, config)?;
6487
ops::publish(&ws, &ops::PublishOpts {
6588
config: config,
6689
token: token,
67-
index: host,
90+
index:
91+
if host.clone().is_none() || host.clone().unwrap().is_empty() { index }
92+
else { config.shell().warn(&msg)?; host }, // TODO: Deprecated, remove
6893
verify: !no_verify,
6994
allow_dirty: allow_dirty,
7095
jobs: jobs,

tests/publish.rs

+143-10
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,62 @@ fn setup() {
4444
fn simple() {
4545
setup();
4646

47+
let p = project("foo")
48+
.file("Cargo.toml", r#"
49+
[project]
50+
name = "foo"
51+
version = "0.0.1"
52+
authors = []
53+
license = "MIT"
54+
description = "foo"
55+
"#)
56+
.file("src/main.rs", "fn main() {}");
57+
58+
assert_that(p.cargo_process("publish").arg("--no-verify")
59+
.arg("--index").arg(registry().to_string()),
60+
execs().with_status(0).with_stderr(&format!("\
61+
[UPDATING] registry `{reg}`
62+
[WARNING] manifest has no documentation, [..]
63+
See [..]
64+
[PACKAGING] foo v0.0.1 ({dir})
65+
[UPLOADING] foo v0.0.1 ({dir})
66+
",
67+
dir = p.url(),
68+
reg = registry())));
69+
70+
let mut f = File::open(&upload_path().join("api/v1/crates/new")).unwrap();
71+
// Skip the metadata payload and the size of the tarball
72+
let mut sz = [0; 4];
73+
assert_eq!(f.read(&mut sz).unwrap(), 4);
74+
let sz = ((sz[0] as u32) << 0) |
75+
((sz[1] as u32) << 8) |
76+
((sz[2] as u32) << 16) |
77+
((sz[3] as u32) << 24);
78+
f.seek(SeekFrom::Current(sz as i64 + 4)).unwrap();
79+
80+
// Verify the tarball
81+
let mut rdr = GzDecoder::new(f).unwrap();
82+
assert_eq!(rdr.header().filename().unwrap(), "foo-0.0.1.crate".as_bytes());
83+
let mut contents = Vec::new();
84+
rdr.read_to_end(&mut contents).unwrap();
85+
let mut ar = Archive::new(&contents[..]);
86+
for file in ar.entries().unwrap() {
87+
let file = file.unwrap();
88+
let fname = file.header().path_bytes();
89+
let fname = &*fname;
90+
assert!(fname == b"foo-0.0.1/Cargo.toml" ||
91+
fname == b"foo-0.0.1/Cargo.toml.orig" ||
92+
fname == b"foo-0.0.1/src/main.rs",
93+
"unexpected filename: {:?}", file.header().path());
94+
}
95+
}
96+
97+
// TODO: Deprecated
98+
// remove once it has been decided --host can be removed
99+
#[test]
100+
fn simple_with_host() {
101+
setup();
102+
47103
let p = project("foo")
48104
.file("Cargo.toml", r#"
49105
[project]
@@ -58,6 +114,83 @@ fn simple() {
58114
assert_that(p.cargo_process("publish").arg("--no-verify")
59115
.arg("--host").arg(registry().to_string()),
60116
execs().with_status(0).with_stderr(&format!("\
117+
[WARNING] The flag '--host' is no longer valid.
118+
119+
Previous versions of Cargo accepted this flag, but it is being
120+
deprecated. The flag is being renamed to 'index', as the flag
121+
wants the location of the index to which to publish. Please
122+
use '--index' instead.
123+
124+
This will soon become a hard error, so it's either recommended
125+
to update to a fixed version or contact the upstream maintainer
126+
about this warning.
127+
[UPDATING] registry `{reg}`
128+
[WARNING] manifest has no documentation, [..]
129+
See [..]
130+
[PACKAGING] foo v0.0.1 ({dir})
131+
[UPLOADING] foo v0.0.1 ({dir})
132+
",
133+
dir = p.url(),
134+
reg = registry())));
135+
136+
let mut f = File::open(&upload_path().join("api/v1/crates/new")).unwrap();
137+
// Skip the metadata payload and the size of the tarball
138+
let mut sz = [0; 4];
139+
assert_eq!(f.read(&mut sz).unwrap(), 4);
140+
let sz = ((sz[0] as u32) << 0) |
141+
((sz[1] as u32) << 8) |
142+
((sz[2] as u32) << 16) |
143+
((sz[3] as u32) << 24);
144+
f.seek(SeekFrom::Current(sz as i64 + 4)).unwrap();
145+
146+
// Verify the tarball
147+
let mut rdr = GzDecoder::new(f).unwrap();
148+
assert_eq!(rdr.header().filename().unwrap(), "foo-0.0.1.crate".as_bytes());
149+
let mut contents = Vec::new();
150+
rdr.read_to_end(&mut contents).unwrap();
151+
let mut ar = Archive::new(&contents[..]);
152+
for file in ar.entries().unwrap() {
153+
let file = file.unwrap();
154+
let fname = file.header().path_bytes();
155+
let fname = &*fname;
156+
assert!(fname == b"foo-0.0.1/Cargo.toml" ||
157+
fname == b"foo-0.0.1/Cargo.toml.orig" ||
158+
fname == b"foo-0.0.1/src/main.rs",
159+
"unexpected filename: {:?}", file.header().path());
160+
}
161+
}
162+
163+
// TODO: Deprecated
164+
// remove once it has been decided --host can be removed
165+
#[test]
166+
fn simple_with_index_and_host() {
167+
setup();
168+
169+
let p = project("foo")
170+
.file("Cargo.toml", r#"
171+
[project]
172+
name = "foo"
173+
version = "0.0.1"
174+
authors = []
175+
license = "MIT"
176+
description = "foo"
177+
"#)
178+
.file("src/main.rs", "fn main() {}");
179+
180+
assert_that(p.cargo_process("publish").arg("--no-verify")
181+
.arg("--index").arg(registry().to_string())
182+
.arg("--host").arg(registry().to_string()),
183+
execs().with_status(0).with_stderr(&format!("\
184+
[WARNING] The flag '--host' is no longer valid.
185+
186+
Previous versions of Cargo accepted this flag, but it is being
187+
deprecated. The flag is being renamed to 'index', as the flag
188+
wants the location of the index to which to publish. Please
189+
use '--index' instead.
190+
191+
This will soon become a hard error, so it's either recommended
192+
to update to a fixed version or contact the upstream maintainer
193+
about this warning.
61194
[UPDATING] registry `{reg}`
62195
[WARNING] manifest has no documentation, [..]
63196
See [..]
@@ -113,7 +246,7 @@ fn git_deps() {
113246
.file("src/main.rs", "fn main() {}");
114247

115248
assert_that(p.cargo_process("publish").arg("-v").arg("--no-verify")
116-
.arg("--host").arg(registry().to_string()),
249+
.arg("--index").arg(registry().to_string()),
117250
execs().with_status(101).with_stderr("\
118251
[UPDATING] registry [..]
119252
[ERROR] crates cannot be published to crates.io with dependencies sourced from \
@@ -150,7 +283,7 @@ fn path_dependency_no_version() {
150283
.file("bar/src/lib.rs", "");
151284

152285
assert_that(p.cargo_process("publish")
153-
.arg("--host").arg(registry().to_string()),
286+
.arg("--index").arg(registry().to_string()),
154287
execs().with_status(101).with_stderr("\
155288
[UPDATING] registry [..]
156289
[ERROR] all path dependencies must have a version specified when publishing.
@@ -175,7 +308,7 @@ fn unpublishable_crate() {
175308
.file("src/main.rs", "fn main() {}");
176309

177310
assert_that(p.cargo_process("publish")
178-
.arg("--host").arg(registry().to_string()),
311+
.arg("--index").arg(registry().to_string()),
179312
execs().with_status(101).with_stderr("\
180313
[ERROR] some crates cannot be published.
181314
`foo` is marked as unpublishable
@@ -205,7 +338,7 @@ fn dont_publish_dirty() {
205338
.build();
206339

207340
assert_that(p.cargo("publish")
208-
.arg("--host").arg(registry().to_string()),
341+
.arg("--index").arg(registry().to_string()),
209342
execs().with_status(101).with_stderr("\
210343
[UPDATING] registry `[..]`
211344
error: 1 files in the working directory contain changes that were not yet \
@@ -240,7 +373,7 @@ fn publish_clean() {
240373
.build();
241374

242375
assert_that(p.cargo("publish")
243-
.arg("--host").arg(registry().to_string()),
376+
.arg("--index").arg(registry().to_string()),
244377
execs().with_status(0));
245378
}
246379

@@ -268,7 +401,7 @@ fn publish_in_sub_repo() {
268401
.build();
269402

270403
assert_that(p.cargo("publish").cwd(p.root().join("bar"))
271-
.arg("--host").arg(registry().to_string()),
404+
.arg("--index").arg(registry().to_string()),
272405
execs().with_status(0));
273406
}
274407

@@ -297,7 +430,7 @@ fn publish_when_ignored() {
297430
.build();
298431

299432
assert_that(p.cargo("publish")
300-
.arg("--host").arg(registry().to_string()),
433+
.arg("--index").arg(registry().to_string()),
301434
execs().with_status(0));
302435
}
303436

@@ -324,7 +457,7 @@ fn ignore_when_crate_ignored() {
324457
"#)
325458
.nocommit_file("bar/src/main.rs", "fn main() {}");
326459
assert_that(p.cargo("publish").cwd(p.root().join("bar"))
327-
.arg("--host").arg(registry().to_string()),
460+
.arg("--index").arg(registry().to_string()),
328461
execs().with_status(0));
329462
}
330463

@@ -350,7 +483,7 @@ fn new_crate_rejected() {
350483
"#)
351484
.nocommit_file("src/main.rs", "fn main() {}");
352485
assert_that(p.cargo("publish")
353-
.arg("--host").arg(registry().to_string()),
486+
.arg("--index").arg(registry().to_string()),
354487
execs().with_status(101));
355488
}
356489

@@ -370,7 +503,7 @@ fn dry_run() {
370503
.file("src/main.rs", "fn main() {}");
371504

372505
assert_that(p.cargo_process("publish").arg("--dry-run")
373-
.arg("--host").arg(registry().to_string()),
506+
.arg("--index").arg(registry().to_string()),
374507
execs().with_status(0).with_stderr(&format!("\
375508
[UPDATING] registry `[..]`
376509
[WARNING] manifest has no documentation, [..]

tests/registry.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ fn bad_license_file() {
616616
"#);
617617
assert_that(p.cargo_process("publish")
618618
.arg("-v")
619-
.arg("--host").arg(registry().to_string()),
619+
.arg("--index").arg(registry().to_string()),
620620
execs().with_status(101)
621621
.with_stderr_contains("\
622622
[ERROR] the license file `foo` does not exist"));

0 commit comments

Comments
 (0)