Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(embedded): Always generate valid package names #12349

Merged
merged 3 commits into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions src/cargo/util/restricted_names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,23 +87,22 @@ pub fn validate_package_name(name: &str, what: &str, help: &str) -> CargoResult<
pub fn sanitize_package_name(name: &str, placeholder: char) -> String {
let mut slug = String::new();
let mut chars = name.chars();
if let Some(ch) = chars.next() {
if ch.is_digit(10) {
slug.push(placeholder);
slug.push(ch);
} else if unicode_xid::UnicodeXID::is_xid_start(ch) || ch == '_' {
while let Some(ch) = chars.next() {
if (unicode_xid::UnicodeXID::is_xid_start(ch) || ch == '_') && !ch.is_digit(10) {
slug.push(ch);
} else {
slug.push(placeholder);
break;
}
}
for ch in chars {
while let Some(ch) = chars.next() {
if unicode_xid::UnicodeXID::is_xid_continue(ch) || ch == '-' {
slug.push(ch);
} else {
slug.push(placeholder);
}
}
if slug.is_empty() {
slug.push_str("package");
weihanglo marked this conversation as resolved.
Show resolved Hide resolved
}
slug
}

Expand Down
46 changes: 46 additions & 0 deletions tests/testsuite/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,52 @@ args: []
.run();
}

#[cargo_test]
fn test_name_has_leading_number() {
let script = ECHO_SCRIPT;
let p = cargo_test_support::project()
.file("42answer.rs", script)
.build();

p.cargo("-Zscript -v 42answer.rs")
.masquerade_as_nightly_cargo(&["script"])
.with_stdout(
r#"bin: [..]/debug/answer[EXE]
args: []
"#,
)
.with_stderr(
r#"[WARNING] `package.edition` is unspecifiead, defaulting to `2021`
[COMPILING] answer v0.0.0 ([ROOT]/foo)
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]s
[RUNNING] `[..]/debug/answer[EXE]`
"#,
)
.run();
}

#[cargo_test]
fn test_name_is_number() {
let script = ECHO_SCRIPT;
let p = cargo_test_support::project().file("42.rs", script).build();

p.cargo("-Zscript -v 42.rs")
.masquerade_as_nightly_cargo(&["script"])
.with_stdout(
r#"bin: [..]/debug/package[EXE]
args: []
"#,
)
.with_stderr(
r#"[WARNING] `package.edition` is unspecifiead, defaulting to `2021`
[COMPILING] package v0.0.0 ([ROOT]/foo)
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]s
[RUNNING] `[..]/debug/package[EXE]`
"#,
)
.run();
}

#[cargo_test]
fn script_like_dir() {
let p = cargo_test_support::project()
Expand Down