Skip to content

Commit

Permalink
Auto merge of #8675 - weihanglo:fix/name-help, r=Eh2406
Browse files Browse the repository at this point in the history
Add --name suggestion for cargo new

Resolves #8613

Since `check_name` have already got a parameter to show name help, I reuse the logic and sync the behavior between `cargo init` and `cargo new`. The divergence seems to be intentionally made in #7959:

_...Only print the --name suggestion for `cargo init`._

Feel free to discuss.
  • Loading branch information
bors committed Sep 10, 2020
2 parents f110fd9 + c6fcb0e commit 2c10f26
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 20 deletions.
29 changes: 20 additions & 9 deletions src/cargo/ops/cargo_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,19 @@ fn get_name<'a>(path: &'a Path, opts: &'a NewOptions) -> CargoResult<&'a str> {
})
}

fn check_name(name: &str, name_help: &str, has_bin: bool, shell: &mut Shell) -> CargoResult<()> {
fn check_name(
name: &str,
show_name_help: bool,
has_bin: bool,
shell: &mut Shell,
) -> CargoResult<()> {
// If --name is already used to override, no point in suggesting it
// again as a fix.
let name_help = if show_name_help {
"\nIf you need a crate name to not match the directory name, consider using --name flag."
} else {
""
};
restricted_names::validate_package_name(name, "crate name", name_help)?;

if restricted_names::is_keyword(name) {
Expand Down Expand Up @@ -363,7 +375,12 @@ pub fn new(opts: &NewOptions, config: &Config) -> CargoResult<()> {
}

let name = get_name(path, opts)?;
check_name(name, "", opts.kind.is_bin(), &mut config.shell())?;
check_name(
name,
opts.name.is_none(),
opts.kind.is_bin(),
&mut config.shell(),
)?;

let mkopts = MkOptions {
version_control: opts.version_control,
Expand Down Expand Up @@ -411,13 +428,7 @@ pub fn init(opts: &NewOptions, config: &Config) -> CargoResult<()> {
// user may mean "initialize for library, but also add binary target"
}
let has_bin = src_paths_types.iter().any(|x| x.bin);
// If --name is already used to override, no point in suggesting it
// again as a fix.
let name_help = match opts.name {
Some(_) => "",
None => "\nuse --name to override crate name",
};
check_name(name, name_help, has_bin, &mut config.shell())?;
check_name(name, opts.name.is_none(), has_bin, &mut config.shell())?;

let mut version_control = opts.version_control;

Expand Down
5 changes: 3 additions & 2 deletions tests/testsuite/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,8 @@ fn invalid_dir_name() {
.with_stderr(
"\
[ERROR] invalid character `.` in crate name: `foo.bar`, [..]
use --name to override crate name",
If you need a crate name to not match the directory name, consider using --name flag.
",
)
.run();

Expand All @@ -323,7 +324,7 @@ fn reserved_name() {
.with_stderr(
"\
[ERROR] the name `test` cannot be used as a crate name, it conflicts [..]\n\
use --name to override crate name
If you need a crate name to not match the directory name, consider using --name flag.
",
)
.run();
Expand Down
47 changes: 38 additions & 9 deletions tests/testsuite/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,25 @@ fn existing() {
fn invalid_characters() {
cargo_process("new foo.rs")
.with_status(101)
.with_stderr("[ERROR] invalid character `.` in crate name: `foo.rs`, [..]")
.with_stderr(
"\
[ERROR] invalid character `.` in crate name: `foo.rs`, [..]
If you need a crate name to not match the directory name, consider using --name flag.
",
)
.run();
}

#[cargo_test]
fn reserved_name() {
cargo_process("new test")
.with_status(101)
.with_stderr("[ERROR] the name `test` cannot be used as a crate name, it conflicts [..]")
.with_stderr(
"\
[ERROR] the name `test` cannot be used as a crate name, it conflicts [..]
If you need a crate name to not match the directory name, consider using --name flag.
",
)
.run();
}

Expand All @@ -133,7 +143,10 @@ fn reserved_binary_name() {
cargo_process("new --bin incremental")
.with_status(101)
.with_stderr(
"[ERROR] the name `incremental` cannot be used as a crate name, it conflicts [..]",
"\
[ERROR] the name `incremental` cannot be used as a crate name, it conflicts [..]
If you need a crate name to not match the directory name, consider using --name flag.
",
)
.run();

Expand All @@ -153,7 +166,12 @@ it conflicts with cargo's build directory names
fn keyword_name() {
cargo_process("new pub")
.with_status(101)
.with_stderr("[ERROR] the name `pub` cannot be used as a crate name, it is a Rust keyword")
.with_stderr(
"\
[ERROR] the name `pub` cannot be used as a crate name, it is a Rust keyword
If you need a crate name to not match the directory name, consider using --name flag.
",
)
.run();
}

Expand Down Expand Up @@ -522,7 +540,12 @@ fn restricted_windows_name() {
cargo_process("new nul")
.env("USER", "foo")
.with_status(101)
.with_stderr("[ERROR] cannot use name `nul`, it is a reserved Windows filename")
.with_stderr(
"\
[ERROR] cannot use name `nul`, it is a reserved Windows filename
If you need a crate name to not match the directory name, consider using --name flag.
",
)
.run();
} else {
cargo_process("new nul")
Expand Down Expand Up @@ -559,17 +582,23 @@ fn non_ascii_name_invalid() {
.env("USER", "foo")
.with_status(101)
.with_stderr(
"[ERROR] invalid character `Ⓐ` in crate name: `ⒶⒷⒸ`, \
the first character must be a Unicode XID start character (most letters or `_`)",
"\
[ERROR] invalid character `Ⓐ` in crate name: `ⒶⒷⒸ`, \
the first character must be a Unicode XID start character (most letters or `_`)
If you need a crate name to not match the directory name, consider using --name flag.
",
)
.run();

cargo_process("new a¼")
.env("USER", "foo")
.with_status(101)
.with_stderr(
"[ERROR] invalid character `¼` in crate name: `a¼`, \
characters must be Unicode XID characters (numbers, `-`, `_`, or most letters)",
"\
[ERROR] invalid character `¼` in crate name: `a¼`, \
characters must be Unicode XID characters (numbers, `-`, `_`, or most letters)
If you need a crate name to not match the directory name, consider using --name flag.
",
)
.run();
}

0 comments on commit 2c10f26

Please sign in to comment.