Skip to content

Commit

Permalink
Strip angle brackets from author email before passing to template.
Browse files Browse the repository at this point in the history
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 <<foo@baz>>"]

instead of

authors = ["bar <foo@baz>"]

This detects the emails that start and end with <> and removes them.
  • Loading branch information
btashton committed Nov 1, 2018
1 parent fd9cb98 commit 7d57948
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/cargo/ops/cargo_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,17 @@ fn discover_author() -> CargoResult<(String, Option<String>)> {
.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))
}
Expand Down
17 changes: 17 additions & 0 deletions tests/testsuite/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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", "<baz>")
.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 <baz>"]"#));
}

#[test]
fn git_prefers_command_line() {
let root = paths::root();
Expand Down

0 comments on commit 7d57948

Please sign in to comment.