Auto merge of #1620 - Eljay:empty-package-name, r=alexcrichton

Some simple validation for package/target names. Currently failure for these cases happens later in rustc (e.g. "crate name must not be empty") or at the os level (e.g. "Is a directory" error when package name is empty on linux). Better to handle this earlier so it's consistent across platforms.

Still could do with a lot of validation for invalid characters etc. though.
This commit is contained in:
bors 2015-05-17 20:06:53 +00:00
commit c60f58568d
2 changed files with 85 additions and 1 deletions

View 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<()>

View 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());