Skip to content

Commit

Permalink
Fixes #2708
Browse files Browse the repository at this point in the history
Warns about not using snake_case or kebab-case format when creating new packages with `cargo new` command.
  • Loading branch information
Shankar Mathiah Nanjundan authored and Shankar Mathiah Nanjundan committed Oct 17, 2023
1 parent 87f4b1b commit 901018c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/cargo/ops/cargo_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,12 @@ fn check_name(
name
))?;
}
let name_in_lowercase = name.to_lowercase();
if name != name_in_lowercase {
shell.warn(format!(
"the name `{name}` is not snake_case or kebab-case which is recommended for package names, consider `{name_in_lowercase}`"
))?;
}

Ok(())
}
Expand Down
24 changes: 24 additions & 0 deletions tests/testsuite/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,7 @@ fn non_ascii_name() {
"\
[WARNING] the name `Привет` contains non-ASCII characters
Non-ASCII crate names are not supported by Rust.
[WARNING] the name `Привет` is not snake_case or kebab-case which is recommended for package names, consider `привет`
[CREATED] binary (application) `Привет` package
",
)
Expand Down Expand Up @@ -501,6 +502,29 @@ or change the name in Cargo.toml with:
.run();
}

#[cargo_test]
fn non_snake_case_name() {
cargo_process("new UPPERcase_name")
.with_stderr(
"\
[WARNING] the name `UPPERcase_name` is not snake_case or kebab-case which is recommended for package names, consider `uppercase_name`
[CREATED] binary (application) `UPPERcase_name` package
",
)
.run();
}

#[cargo_test]
fn kebab_case_name_is_accepted() {
cargo_process("new kebab-case-is-valid")
.with_stderr(
"\
[CREATED] binary (application) `kebab-case-is-valid` package
",
)
.run();
}

#[cargo_test]
fn git_default_branch() {
// Check for init.defaultBranch support.
Expand Down

0 comments on commit 901018c

Please sign in to comment.