Skip to content

Commit

Permalink
fix(embedded): Extend sanitization rules to cover cargo-new validation
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Jun 16, 2023
1 parent 8186282 commit 8ba6073
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/cargo/ops/cargo_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ fn get_name<'a>(path: &'a Path, opts: &'a NewOptions) -> CargoResult<&'a str> {
})
}

/// See also `util::toml::embedded::sanitize_name`
fn check_name(
name: &str,
show_name_help: bool,
Expand Down
35 changes: 27 additions & 8 deletions src/cargo/util/toml/embedded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ fn write(
// more common convention for CLIs
'-'
};
let name = sanitize_package_name(file_name.as_ref(), separator);
let name = sanitize_name(file_name.as_ref(), separator);

let mut workspace_root = target_dir.to_owned();
workspace_root.push("eval");
Expand Down Expand Up @@ -154,7 +154,7 @@ fn expand_manifest_(script: &RawScript, config: &Config) -> CargoResult<toml::Ta
// more common convention for CLIs
'-'
};
let name = sanitize_package_name(file_name.as_ref(), separator);
let name = sanitize_name(file_name.as_ref(), separator);
let bin_name = name.clone();
package
.entry("name".to_owned())
Expand Down Expand Up @@ -206,8 +206,27 @@ fn expand_manifest_(script: &RawScript, config: &Config) -> CargoResult<toml::Ta
Ok(manifest)
}

fn sanitize_package_name(name: &str, placeholder: char) -> String {
restricted_names::sanitize_package_name(name, placeholder)
/// Ensure the package name matches the validation from `opt::cargo_new::check_name`
fn sanitize_name(name: &str, placeholder: char) -> String {
let mut name = restricted_names::sanitize_package_name(name, placeholder);

loop {
if restricted_names::is_keyword(&name) {
name.push(placeholder);
} else if restricted_names::is_conflicting_artifact_name(&name) {
// Being an embedded manifest, we always assume it is a `[[bin]]`
name.push(placeholder);
} else if name == "test" {
name.push(placeholder);
} else if restricted_names::is_windows_reserved(&name) {
// Go ahead and be consistent across platforms
name.push(placeholder);
} else {
break;
}
}

name
}

fn hash(script: &RawScript) -> blake3::Hash {
Expand Down Expand Up @@ -442,12 +461,12 @@ mod test_expand {
fn test_default() {
snapbox::assert_eq(
r#"[[bin]]
name = "test"
name = "test-"
path = "/home/me/test.rs"
[package]
edition = "2021"
name = "test"
name = "test-"
publish = false
version = "0.0.0"
Expand All @@ -464,15 +483,15 @@ strip = true
fn test_dependencies() {
snapbox::assert_eq(
r#"[[bin]]
name = "test"
name = "test-"
path = "/home/me/test.rs"
[dependencies]
time = "0.1.25"
[package]
edition = "2021"
name = "test"
name = "test-"
publish = false
version = "0.0.0"
Expand Down

0 comments on commit 8ba6073

Please sign in to comment.