]> git.proxmox.com Git - cargo.git/commitdiff
Blacklist some binary names in `cargo new`
authorAleksey Kladov <aleksey.kladov@gmail.com>
Thu, 4 May 2017 12:23:38 +0000 (15:23 +0300)
committerAleksey Kladov <aleksey.kladov@gmail.com>
Thu, 4 May 2017 12:23:38 +0000 (15:23 +0300)
src/cargo/ops/cargo_new.rs
src/cargo/ops/cargo_rustc/layout.rs
src/cargo/ops/cargo_rustc/mod.rs
src/cargo/ops/mod.rs
src/cargo/util/toml.rs
tests/bench.rs
tests/new.rs

index 195c6efaf753ccc18c99d167dbbf2f9c2058deca..5b4f4faaede9b779adbb072081cbbb5f4189c1e8 100644 (file)
@@ -10,6 +10,7 @@ use git2::Config as GitConfig;
 use term::color::BLACK;
 
 use core::Workspace;
+use ops::is_bad_artifact_name;
 use util::{GitRepo, HgRepo, PijulRepo, CargoResult, human, ChainError, internal};
 use util::{Config, paths};
 
@@ -115,7 +116,7 @@ fn get_name<'a>(path: &'a Path, opts: &'a NewOptions, config: &Config) -> CargoR
     }
 }
 
-fn check_name(name: &str) -> CargoResult<()> {
+fn check_name(name: &str, is_bin: bool) -> CargoResult<()> {
 
     // Ban keywords + test list found at
     // https://doc.rust-lang.org/grammar.html#keywords
@@ -130,7 +131,7 @@ fn check_name(name: &str) -> CargoResult<()> {
         "super", "test", "trait", "true", "type", "typeof",
         "unsafe", "unsized", "use", "virtual", "where",
         "while", "yield"];
-    if blacklist.contains(&name) {
+    if blacklist.contains(&name) || (is_bin && is_bad_artifact_name(name)) {
         bail!("The name `{}` cannot be used as a crate name\n\
                use --name to override crate name",
                name)
@@ -274,7 +275,7 @@ pub fn new(opts: NewOptions, config: &Config) -> CargoResult<()> {
     }
 
     let name = get_name(&path, &opts, config)?;
-    check_name(name)?;
+    check_name(name, opts.bin)?;
 
     let mkopts = MkOptions {
         version_control: opts.version_control,
@@ -303,7 +304,7 @@ pub fn init(opts: NewOptions, config: &Config) -> CargoResult<()> {
     }
 
     let name = get_name(&path, &opts, config)?;
-    check_name(name)?;
+    check_name(name, opts.bin)?;
 
     let mut src_paths_types = vec![];
 
index f090cbc726ab058f936cec05639e46db012fa971..66882e628a28405f632739b42b65ecb1f42c92dd 100644 (file)
@@ -67,6 +67,13 @@ pub struct Layout {
     _lock: FileLock,
 }
 
+pub fn is_bad_artifact_name(name: &str) -> bool {
+    ["deps", "examples", "build", "native", "incremental"]
+        .iter()
+        .find(|&&reserved| reserved == name)
+        .is_some()
+}
+
 impl Layout {
     pub fn new(ws: &Workspace,
                triple: Option<&str>,
index ae3af71a6a758dda358cd0d1b25e5b0c7e15fc88..f52f868605114e584a6354607c181f88f2aac878 100644 (file)
@@ -23,6 +23,7 @@ use self::output_depinfo::output_depinfo;
 pub use self::compilation::Compilation;
 pub use self::context::{Context, Unit};
 pub use self::custom_build::{BuildOutput, BuildMap, BuildScripts};
+pub use self::layout::is_bad_artifact_name;
 
 mod compilation;
 mod context;
index 9ec74f393f495bfeef0d63fc2b97e4790eb076ad..8d8908320134197b6df78d9016bb7ff408aea560 100644 (file)
@@ -3,7 +3,7 @@ pub use self::cargo_compile::{compile, compile_with_exec, compile_ws, CompileOpt
 pub use self::cargo_compile::{CompileFilter, CompileMode, MessageFormat, Packages};
 pub use self::cargo_read_manifest::{read_manifest,read_package,read_packages};
 pub use self::cargo_rustc::{compile_targets, Compilation, Kind, Unit};
-pub use self::cargo_rustc::Context;
+pub use self::cargo_rustc::{Context, is_bad_artifact_name};
 pub use self::cargo_rustc::{BuildOutput, BuildConfig, TargetConfig};
 pub use self::cargo_rustc::{Executor, DefaultExecutor};
 pub use self::cargo_run::run;
index 24006bf2268bd67dd7f3c7e98f185e14fb3e81be..771f48ddfda49e720db4cda8a7c52cfc516aef52 100644 (file)
@@ -15,6 +15,7 @@ use core::{Summary, Manifest, Target, Dependency, DependencyInner, PackageId};
 use core::{EitherManifest, VirtualManifest};
 use core::dependency::{Kind, Platform};
 use core::manifest::{LibKind, Profile, ManifestMetadata};
+use ops::is_bad_artifact_name;
 use sources::CRATES_IO;
 use util::{self, CargoResult, human, ToUrl, ToSemver, ChainError, Config};
 
@@ -613,10 +614,8 @@ impl TomlManifest {
             None => inferred_bin_targets(&project.name, layout)
         };
 
-        let blacklist = vec!["build", "deps", "examples", "native"];
-
         for bin in bins.iter() {
-            if blacklist.iter().find(|&x| *x == bin.name()) != None {
+            if is_bad_artifact_name(&bin.name()) {
                 bail!("the binary target name `{}` is forbidden",
                       bin.name())
             }
index 33a6529b7e3009fcce4024d64d7270b094cf0547..f22ec077c95509f10995ab08c5fc136e6c0d3fd9 100644 (file)
@@ -7,7 +7,6 @@ use std::str;
 use cargo::util::process;
 use cargotest::is_nightly;
 use cargotest::support::paths::CargoPathExt;
-use cargotest::support::registry::Package;
 use cargotest::support::{project, execs, basic_bin_manifest, basic_lib_manifest};
 use hamcrest::{assert_that, existing_file};
 
index a2a3af453561ba6dce65c0bc6bb3eb846ee6393c..0312d779534e86a57d326f99c635dfbb0f0ad9de 100644 (file)
@@ -134,6 +134,15 @@ fn reserved_name() {
 use --name to override crate name"));
 }
 
+#[test]
+fn reserved_binary_name() {
+    assert_that(cargo_process("new").arg("--bin").arg("incremental"),
+                execs().with_status(101)
+                       .with_stderr("\
+[ERROR] The name `incremental` cannot be used as a crate name\n\
+use --name to override crate name"));
+}
+
 #[test]
 fn keyword_name() {
     assert_that(cargo_process("new").arg("pub"),