Skip to content

Commit d07439b

Browse files
committed
yank: Check for bad version & added error message for prefixed v in version
1 parent cd27a11 commit d07439b

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

src/bin/cargo/commands/yank.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::command_prelude::*;
22

3+
use anyhow::Context;
34
use cargo::ops;
45
use cargo_credential::Secret;
56

@@ -60,5 +61,19 @@ fn resolve_crate<'k>(
6061
krate = Some(k);
6162
version = Some(v);
6263
}
64+
65+
if let Some(version) = version {
66+
semver::Version::parse(version).with_context(|| {
67+
if let Some(stripped) = version.strip_prefix("v") {
68+
return format!(
69+
"the version provided, `{version}` is not a \
70+
valid SemVer version\n\n\
71+
help: try changing the version to `{stripped}`",
72+
);
73+
}
74+
format!("invalid version `{version}`")
75+
})?;
76+
}
77+
6378
Ok((krate, version))
6479
}

tests/testsuite/yank.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -237,12 +237,10 @@ fn bad_version() {
237237
.replace_crates_io(registry.index_url())
238238
.with_status(101)
239239
.with_stderr_data(str![[r#"
240-
[UPDATING] crates.io index
241-
[YANK] foo@bar
242-
[ERROR] failed to yank from the registry at [ROOTURL]/api
240+
[ERROR] invalid version `bar`
243241
244242
Caused by:
245-
[37] Couldn't read a file:// file (Couldn't open file [ROOT]/api/api/v1/crates/foo/bar/yank)
243+
unexpected character 'b' while parsing major version number
246244
247245
"#]])
248246
.run();
@@ -272,12 +270,12 @@ fn prefixed_v_in_version() {
272270
.replace_crates_io(registry.index_url())
273271
.with_status(101)
274272
.with_stderr_data(str![[r#"
275-
[UPDATING] crates.io index
276-
277-
[ERROR] failed to yank from the registry at [ROOTURL]/api
273+
[ERROR] the version provided, `v0.0.1` is not a valid SemVer version
274+
275+
[HELP] try changing the version to `0.0.1`
278276
279277
Caused by:
280-
[37] Couldn't read a file:// file (Couldn't open file [ROOT]/api/api/v1/crates/bar/v0.0.1/yank)
278+
unexpected character 'v' while parsing major version number
281279
282280
"#]])
283281
.run();

0 commit comments

Comments
 (0)