]> git.proxmox.com Git - cargo.git/commitdiff
Added some checks for empty package and target names.
authorLee Jeffery <lee@leejeffery.co.uk>
Sun, 17 May 2015 12:03:23 +0000 (13:03 +0100)
committerLee Jeffery <lee@leejeffery.co.uk>
Sun, 17 May 2015 12:03:23 +0000 (13:03 +0100)
src/cargo/util/toml.rs
tests/test_cargo_compile.rs

index 35450030ebd04df48308bf52f2eccafeacc318d7..53e4525b647495294e0c75bc2015c6a033c6a055 100644 (file)
@@ -365,6 +365,10 @@ impl TomlManifest {
             human("No `package` or `project` section found.")
         }));
 
+        if project.name.trim().is_empty() {
+            return Err(human("package name cannot be an empty string."))
+        }
+
         let pkgid = try!(project.to_package_id(source_id));
         let metadata = pkgid.generate_metadata();
 
@@ -391,6 +395,10 @@ impl TomlManifest {
             Some(ref bins) => {
                 let bin = layout.main();
 
+                for target in bins {
+                    try!(validate_binary_name(target));
+                }
+
                 bins.iter().map(|t| {
                     if bin.is_some() && t.path.is_none() {
                         TomlTarget {
@@ -518,7 +526,9 @@ impl TomlManifest {
 }
 
 fn validate_library_name(target: &TomlTarget) -> CargoResult<()> {
-    if target.name.contains("-") {
+    if target.name.trim().is_empty() {
+        Err(human(format!("library target names cannot be empty.")))
+    } else if target.name.contains("-") {
         Err(human(format!("library target names cannot contain hyphens: {}",
                           target.name)))
     } else {
@@ -526,6 +536,14 @@ fn validate_library_name(target: &TomlTarget) -> CargoResult<()> {
     }
 }
 
+fn validate_binary_name(target: &TomlTarget) -> CargoResult<()> {
+    if target.name.trim().is_empty() {
+        Err(human(format!("binary target names cannot be empty.")))
+    } else {
+        Ok(())
+    }
+}
+
 fn process_dependencies<F>(cx: &mut Context,
                            new_deps: Option<&HashMap<String, TomlDependency>>,
                            mut f: F) -> CargoResult<()>
index 42e563855c5ba583dc60800f947c90eb65ea65c9..29ee66fba6850946920fbfe7cfa5604fc9f4a185 100644 (file)
@@ -116,6 +116,72 @@ Caused by:
 
 });
 
+test!(cargo_compile_with_invalid_package_name {
+    let p = project("foo")
+        .file("Cargo.toml", r#"
+            [package]
+            name = ""
+            authors = []
+            version = "0.0.0"
+        "#);
+
+    assert_that(p.cargo_process("build"),
+                execs()
+                .with_status(101)
+                .with_stderr("\
+failed to parse manifest at `[..]`
+
+Caused by:
+  package name cannot be an empty string.
+"))
+});
+
+test!(cargo_compile_with_invalid_bin_target_name {
+    let p = project("foo")
+        .file("Cargo.toml", r#"
+            [package]
+            name = "foo"
+            authors = []
+            version = "0.0.0"
+
+            [[bin]]
+            name = ""
+        "#);
+
+    assert_that(p.cargo_process("build"),
+                execs()
+                .with_status(101)
+                .with_stderr("\
+failed to parse manifest at `[..]`
+
+Caused by:
+  binary target names cannot be empty.
+"))
+});
+
+test!(cargo_compile_with_invalid_lib_target_name {
+    let p = project("foo")
+        .file("Cargo.toml", r#"
+            [package]
+            name = "foo"
+            authors = []
+            version = "0.0.0"
+
+            [lib]
+            name = ""
+        "#);
+
+    assert_that(p.cargo_process("build"),
+                execs()
+                .with_status(101)
+                .with_stderr("\
+failed to parse manifest at `[..]`
+
+Caused by:
+  library target names cannot be empty.
+"))
+});
+
 test!(cargo_compile_without_manifest {
     let tmpdir = TempDir::new("cargo").unwrap();
     let p = ProjectBuilder::new("foo", tmpdir.path().to_path_buf());