From 09d6c79278b2c603bc494db2dadd2536919638dd Mon Sep 17 00:00:00 2001 From: Ed Page Date: Tue, 11 Jul 2023 14:27:36 -0500 Subject: [PATCH 1/3] test(script): Verify existing leading number behavior --- tests/testsuite/script.rs | 40 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/tests/testsuite/script.rs b/tests/testsuite/script.rs index fcf58de6981..e6a8ac018bc 100644 --- a/tests/testsuite/script.rs +++ b/tests/testsuite/script.rs @@ -543,6 +543,46 @@ 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_status(101) + .with_stderr( + r#"[WARNING] `package.edition` is unspecifiead, defaulting to `2021` +[ERROR] failed to parse manifest at `[ROOT]/foo/42answer.rs` + +Caused by: + invalid character `-` in package name: `-42answer`, the first character must be a Unicode XID start character (most letters or `_`) +"#, + ) + .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_status(101) + .with_stderr( + r#"[WARNING] `package.edition` is unspecifiead, defaulting to `2021` +[ERROR] failed to parse manifest at `[ROOT]/foo/42.rs` + +Caused by: + invalid character `-` in package name: `-42`, the first character must be a Unicode XID start character (most letters or `_`) +"#, + ) + .run(); +} + #[cargo_test] fn script_like_dir() { let p = cargo_test_support::project() From 87be661989788e296c90c4ae835cb92822a1617d Mon Sep 17 00:00:00 2001 From: Ed Page Date: Tue, 11 Jul 2023 14:35:50 -0500 Subject: [PATCH 2/3] fix(embedded): Don't generate package names with invalid leading digit --- src/cargo/util/restricted_names.rs | 12 ++++-------- tests/testsuite/script.rs | 15 +++++++++------ 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/src/cargo/util/restricted_names.rs b/src/cargo/util/restricted_names.rs index be1811a8878..8f83674dd65 100644 --- a/src/cargo/util/restricted_names.rs +++ b/src/cargo/util/restricted_names.rs @@ -87,17 +87,13 @@ 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 { diff --git a/tests/testsuite/script.rs b/tests/testsuite/script.rs index e6a8ac018bc..bb672fc0def 100644 --- a/tests/testsuite/script.rs +++ b/tests/testsuite/script.rs @@ -552,13 +552,16 @@ fn test_name_has_leading_number() { p.cargo("-Zscript -v 42answer.rs") .masquerade_as_nightly_cargo(&["script"]) - .with_status(101) + .with_stdout( + r#"bin: [..]/debug/answer[EXE] +args: [] +"#, + ) .with_stderr( r#"[WARNING] `package.edition` is unspecifiead, defaulting to `2021` -[ERROR] failed to parse manifest at `[ROOT]/foo/42answer.rs` - -Caused by: - invalid character `-` in package name: `-42answer`, the first character must be a Unicode XID start character (most letters or `_`) +[COMPILING] answer v0.0.0 ([ROOT]/foo) +[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]s +[RUNNING] `[..]/debug/answer[EXE]` "#, ) .run(); @@ -577,7 +580,7 @@ fn test_name_is_number() { [ERROR] failed to parse manifest at `[ROOT]/foo/42.rs` Caused by: - invalid character `-` in package name: `-42`, the first character must be a Unicode XID start character (most letters or `_`) + package name cannot be an empty string "#, ) .run(); From 7d4e39c4f58391421a905d257c11cc85bbf9458c Mon Sep 17 00:00:00 2001 From: Ed Page Date: Tue, 11 Jul 2023 14:37:44 -0500 Subject: [PATCH 3/3] fix(embedded): Don't generate empty package names --- src/cargo/util/restricted_names.rs | 3 +++ tests/testsuite/script.rs | 13 ++++++++----- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/cargo/util/restricted_names.rs b/src/cargo/util/restricted_names.rs index 8f83674dd65..2c3eaa9e1f8 100644 --- a/src/cargo/util/restricted_names.rs +++ b/src/cargo/util/restricted_names.rs @@ -100,6 +100,9 @@ pub fn sanitize_package_name(name: &str, placeholder: char) -> String { slug.push(placeholder); } } + if slug.is_empty() { + slug.push_str("package"); + } slug } diff --git a/tests/testsuite/script.rs b/tests/testsuite/script.rs index bb672fc0def..ab758498b02 100644 --- a/tests/testsuite/script.rs +++ b/tests/testsuite/script.rs @@ -574,13 +574,16 @@ fn test_name_is_number() { p.cargo("-Zscript -v 42.rs") .masquerade_as_nightly_cargo(&["script"]) - .with_status(101) + .with_stdout( + r#"bin: [..]/debug/package[EXE] +args: [] +"#, + ) .with_stderr( r#"[WARNING] `package.edition` is unspecifiead, defaulting to `2021` -[ERROR] failed to parse manifest at `[ROOT]/foo/42.rs` - -Caused by: - package name cannot be an empty string +[COMPILING] package v0.0.0 ([ROOT]/foo) +[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]s +[RUNNING] `[..]/debug/package[EXE]` "#, ) .run();