From d30e963ce67d3702f53455f55585ffa1583814fe Mon Sep 17 00:00:00 2001 From: Tim Erwich Date: Fri, 4 Mar 2016 19:32:22 +0000 Subject: [PATCH] Prevent the use of "test" as a crate name. Fixes #2432. --- src/cargo/ops/cargo_new.rs | 6 ++++++ tests/test_cargo_init.rs | 14 ++++++++++++++ tests/test_cargo_new.rs | 9 +++++++++ 3 files changed, 29 insertions(+) diff --git a/src/cargo/ops/cargo_new.rs b/src/cargo/ops/cargo_new.rs index 41fa01fc11f..0f5d8b3518f 100644 --- a/src/cargo/ops/cargo_new.rs +++ b/src/cargo/ops/cargo_new.rs @@ -89,6 +89,12 @@ fn get_name<'a>(path: &'a Path, opts: &'a NewOptions, config: &Config) -> CargoR } fn check_name(name: &str) -> CargoResult<()> { + if name == "test" { + bail!("The name `{}` cannot be used as a crate name\n\ + use --name to override crate name", + name) + } + for c in name.chars() { if c.is_alphanumeric() { continue } if c == '_' || c == '-' { continue } diff --git a/tests/test_cargo_init.rs b/tests/test_cargo_init.rs index 5a708dad4c4..9bbae87e7dc 100644 --- a/tests/test_cargo_init.rs +++ b/tests/test_cargo_init.rs @@ -239,6 +239,20 @@ error = ERROR))); assert_that(&foo.join("Cargo.toml"), is_not(existing_file())); }); +test!(reserved_name { + let test = &paths::root().join("test"); + fs::create_dir_all(&test).unwrap(); + assert_that(cargo_process("init").cwd(test.clone()) + .env("USER", "foo"), + execs().with_status(101).with_stderr(&format!("\ +{error} The name `test` cannot be used as a crate name\n\ +use --name to override crate name +", +error = ERROR))); + + assert_that(&test.join("Cargo.toml"), is_not(existing_file())); +}); + test!(git_autodetect { fs::create_dir(&paths::root().join(".git")).unwrap(); diff --git a/tests/test_cargo_new.rs b/tests/test_cargo_new.rs index c3e5db359b4..9b323fa0469 100644 --- a/tests/test_cargo_new.rs +++ b/tests/test_cargo_new.rs @@ -95,6 +95,15 @@ use --name to override crate name", error = ERROR))); }); +test!(reserved_name { + assert_that(cargo_process("new").arg("test"), + execs().with_status(101) + .with_stderr(&format!("\ +{error} The name `test` cannot be used as a crate name\n\ +use --name to override crate name", +error = ERROR))); +}); + test!(rust_prefix_stripped { assert_that(cargo_process("new").arg("rust-foo").env("USER", "foo"), execs().with_status(0)