Skip to content

Commit

Permalink
Validate feature names are crates.io friendly.
Browse files Browse the repository at this point in the history
  • Loading branch information
MaulingMonkey committed Aug 16, 2019
1 parent 60b7bf0 commit 12f6fca
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/cargo/ops/cargo_package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ pub fn package(ws: &Workspace<'_>, opts: &PackageOpts<'_>) -> CargoResult<Option

if opts.check_metadata {
check_metadata(pkg, config)?;
verify_features(pkg)?;
}

verify_dependencies(pkg)?;
Expand Down Expand Up @@ -218,6 +219,31 @@ fn verify_dependencies(pkg: &Package) -> CargoResult<()> {
Ok(())
}

// Checks that the package features are rusty / can be published to crates.io.
// Intended to avoid this suprise after a successful cargo publish --dry-run:
// invalid upload request: invalid value: string "futures-0.1", expected a
// valid feature name containing only letters, numbers, hyphens, or underscores
fn verify_features(pkg: &Package) -> CargoResult<()> {
for (feature, _) in pkg.summary().features() {
if feature.is_empty() || feature.as_str() == "_" {
failure::bail!(
"expected a valid (not empty nor \"_\") feature name containing \
only letters, numbers, hyphens, or underscores.\n\
feature `{}` is not valid.",
feature
)
} else if !feature.chars().all(|ch| ch.is_ascii_alphanumeric() || ch == '-' || ch == '_') {
failure::bail!(
"expected a valid (not empty nor \"_\") feature name containing \
only letters, numbers, hyphens, or underscores.\n\
feature `{}` contains other characters.",
feature
)
}
}
Ok(())
}

// Checks if the package source is in a *git* DVCS repository. If *git*, and
// the source is *dirty* (e.g., has uncommitted changes) and not `allow_dirty`
// then `bail!` with an informative message. Otherwise return the sha1 hash of
Expand Down
30 changes: 30 additions & 0 deletions tests/testsuite/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,36 @@ fn publish_with_no_default_features() {
.run();
}

#[cargo_test]
fn publish_with_unrusty_features() {
registry::init();

let p = project()
.file(
"Cargo.toml",
r#"
[project]
name = "foo"
version = "0.0.1"
authors = []
license = "MIT"
description = "foo"
[features]
"invalid::characters" = []
"#,
)
.file("src/main.rs", "fn main() {}")
.build();

p.cargo("publish --no-default-features --dry-run --index")
.arg(registry_url().to_string())
.with_stderr_contains("error: expected a valid (not empty nor \"_\") feature name containing only letters, numbers, hyphens, or underscores.")
.with_stderr_contains("feature `invalid::characters` contains other characters.")
.with_status(101)
.run();
}

#[cargo_test]
fn publish_with_patch() {
Package::new("bar", "1.0.0").publish();
Expand Down

0 comments on commit 12f6fca

Please sign in to comment.