Skip to content

Do not rename packages on cargo new. #5013

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 8 additions & 53 deletions src/cargo/ops/cargo_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,32 +91,18 @@ struct CargoNewConfig {
version_control: Option<VersionControl>,
}

fn get_name<'a>(path: &'a Path, opts: &'a NewOptions, config: &Config) -> CargoResult<&'a str> {
fn get_name<'a>(path: &'a Path, opts: &'a NewOptions) -> CargoResult<&'a str> {
if let Some(name) = opts.name {
return Ok(name);
}

if path.file_name().is_none() {
bail!("cannot auto-detect project name from path {:?} ; use --name to override",
path.as_os_str());
}

let dir_name = path.file_name().and_then(|s| s.to_str()).ok_or_else(|| {
format_err!("cannot create a project with a non-unicode name: {:?}",
path.file_name().unwrap())
let file_name = path.file_name().ok_or_else(|| {
format_err!("cannot auto-detect project name from path {:?} ; use --name to override", path.as_os_str())
})?;

if opts.bin {
Ok(dir_name)
} else {
let new_name = strip_rust_affixes(dir_name);
if new_name != dir_name {
writeln!(config.shell().err(),
"note: package will be named `{}`; use --name to override",
new_name)?;
}
Ok(new_name)
}
file_name.to_str().ok_or_else(|| {
format_err!("cannot create project with a non-unicode name: {:?}", file_name)
})
}

fn check_name(name: &str, opts: &NewOptions) -> CargoResult<()> {
Expand Down Expand Up @@ -287,7 +273,7 @@ pub fn new(opts: &NewOptions, config: &Config) -> CargoResult<()> {
bail!("can't specify both lib and binary outputs")
}

let name = get_name(&path, opts, config)?;
let name = get_name(&path, opts)?;
check_name(name, opts)?;

let mkopts = MkOptions {
Expand Down Expand Up @@ -317,7 +303,7 @@ pub fn init(opts: &NewOptions, config: &Config) -> CargoResult<()> {
bail!("can't specify both lib and binary outputs");
}

let name = get_name(&path, opts, config)?;
let name = get_name(&path, opts)?;
check_name(name, opts)?;

let mut src_paths_types = vec![];
Expand Down Expand Up @@ -381,20 +367,6 @@ pub fn init(opts: &NewOptions, config: &Config) -> CargoResult<()> {
Ok(())
}

fn strip_rust_affixes(name: &str) -> &str {
for &prefix in &["rust-", "rust_", "rs-", "rs_"] {
if name.starts_with(prefix) {
return &name[prefix.len()..];
}
}
for &suffix in &["-rust", "_rust", "-rs", "_rs"] {
if name.ends_with(suffix) {
return &name[..name.len()-suffix.len()];
}
}
name
}

fn existing_vcs_repo(path: &Path, cwd: &Path) -> bool {
GitRepo::discover(path, cwd).is_ok() || HgRepo::discover(path, cwd).is_ok()
}
Expand Down Expand Up @@ -605,20 +577,3 @@ fn global_config(config: &Config) -> CargoResult<CargoNewConfig> {
version_control: vcs,
})
}

#[cfg(test)]
mod tests {
use super::strip_rust_affixes;

#[test]
fn affixes_stripped() {
assert_eq!(strip_rust_affixes("rust-foo"), "foo");
assert_eq!(strip_rust_affixes("foo-rs"), "foo");
assert_eq!(strip_rust_affixes("rs_foo"), "foo");
// Only one affix is stripped
assert_eq!(strip_rust_affixes("rs-foo-rs"), "foo-rs");
assert_eq!(strip_rust_affixes("foo-rs-rs"), "foo-rs");
// It shouldn't touch the middle
assert_eq!(strip_rust_affixes("some-rust-crate"), "some-rust-crate");
}
}
33 changes: 1 addition & 32 deletions tests/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,37 +163,6 @@ fn keyword_name() {
use --name to override crate name"));
}

#[test]
fn rust_prefix_stripped() {
assert_that(cargo_process("new").arg("--lib").arg("rust-foo").env("USER", "foo"),
execs().with_status(0)
.with_stderr_contains("note: package will be named `foo`; use --name to override"));
let toml = paths::root().join("rust-foo/Cargo.toml");
let mut contents = String::new();
File::open(&toml).unwrap().read_to_string(&mut contents).unwrap();
assert!(contents.contains(r#"name = "foo""#));
}

#[test]
fn bin_disables_stripping() {
assert_that(cargo_process("new").arg("rust-foo").arg("--bin").env("USER", "foo"),
execs().with_status(0));
let toml = paths::root().join("rust-foo/Cargo.toml");
let mut contents = String::new();
File::open(&toml).unwrap().read_to_string(&mut contents).unwrap();
assert!(contents.contains(r#"name = "rust-foo""#));
}

#[test]
fn explicit_name_not_stripped() {
assert_that(cargo_process("new").arg("foo").arg("--name").arg("rust-bar").env("USER", "foo"),
execs().with_status(0));
let toml = paths::root().join("foo/Cargo.toml");
let mut contents = String::new();
File::open(&toml).unwrap().read_to_string(&mut contents).unwrap();
assert!(contents.contains(r#"name = "rust-bar""#));
}

#[test]
fn finds_author_user() {
create_empty_gitconfig();
Expand Down Expand Up @@ -423,4 +392,4 @@ fn explicit_invalid_name_not_suggested() {
execs().with_status(101)
.with_stderr("\
[ERROR] Package names starting with a digit cannot be used as a crate name"));
}
}