From 7d579483c6141b47dd619d09908941e7b2d1416c Mon Sep 17 00:00:00 2001 From: Brennan Ashton Date: Wed, 31 Oct 2018 13:07:02 -0700 Subject: [PATCH] Strip angle brackets from author email before passing to template. Some people already have angle brackets around their email in git settings or other author sources. Right now if you create a new project the Cargo.toml would render something like: authors = ["bar <>"] instead of authors = ["bar "] This detects the emails that start and end with <> and removes them. --- src/cargo/ops/cargo_new.rs | 12 +++++++++++- tests/testsuite/new.rs | 17 +++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/cargo/ops/cargo_new.rs b/src/cargo/ops/cargo_new.rs index bd2bc4073cc..4be3af40a05 100644 --- a/src/cargo/ops/cargo_new.rs +++ b/src/cargo/ops/cargo_new.rs @@ -660,7 +660,17 @@ fn discover_author() -> CargoResult<(String, Option)> { .or_else(|| get_environment_variable(&email_variables[3..])); let name = name.trim().to_string(); - let email = email.map(|s| s.trim().to_string()); + let email = email.map(|s| { + let mut s = s.trim(); + + // In some cases emails will already have <> remove them since they + // are already added when needed. + if s.starts_with("<") && s.ends_with(">") { + s = &s[1..s.len() - 1]; + } + + s.to_string() + }); Ok((name, email)) } diff --git a/tests/testsuite/new.rs b/tests/testsuite/new.rs index 1cdabf7b93d..52e327e36c2 100644 --- a/tests/testsuite/new.rs +++ b/tests/testsuite/new.rs @@ -337,6 +337,23 @@ fn author_prefers_cargo() { assert!(!root.join("foo/.gitignore").exists()); } +#[test] +fn strip_angle_bracket_author_email() { + create_empty_gitconfig(); + cargo_process("new foo") + .env("USER", "bar") + .env("EMAIL", "") + .run(); + + 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#"authors = ["bar "]"#)); +} + #[test] fn git_prefers_command_line() { let root = paths::root();