Skip to content
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

Fixes #8783 , cargo new fails without a author name or email #8912

Merged
merged 11 commits into from
Dec 3, 2020
57 changes: 36 additions & 21 deletions src/cargo/ops/cargo_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -645,19 +645,37 @@ fn mk(config: &Config, opts: &MkOptions<'_>) -> CargoResult<()> {
init_vcs(path, vcs, config)?;
write_ignore_file(path, &ignore, vcs)?;

let (author_name, email) = discover_author(path)?;
let author = match (cfg.name, cfg.email, author_name, email) {
(Some(name), Some(email), _, _)
| (Some(name), None, _, Some(email))
| (None, Some(email), name, _)
| (None, None, name, Some(email)) => {
let (discovered_name, discovered_email) = discover_author(path);

// "Name <email>" or "Name" or "<email>" or None if neither name nor email is obtained
// cfg takes priority over the discovered ones
let author_name = match (cfg.name, discovered_name) {
(Some(name), _) | (_, Some(name)) => Some(name),
(None, None) => None,
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this and the below match can be expressed as cfg.name.or(discovered_name)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your feedback and guidance.


let author_email = match (cfg.email, discovered_email) {
(Some(email), _) | (_, Some(email)) => Some(email),
(None, None) => None,
};

let author = match (author_name, author_email) {
(Some(name), Some(email)) => {
if email.is_empty() {
name
Some(name)
} else {
Some(format!("{} <{}>", name, email))
}
}
(Some(name), None) => Some(name),
(None, Some(email)) => {
if email.is_empty() {
None
} else {
format!("{} <{}>", name, email)
Some(format!("<{}>", email))
}
}
(Some(name), None, _, None) | (None, None, name, None) => name,
(None, None) => None,
};

let mut cargotoml_path_specifier = String::new();
Expand Down Expand Up @@ -706,7 +724,10 @@ edition = {}
[dependencies]
{}"#,
name,
toml::Value::String(author),
match author {
Some(value) => format!("{}", toml::Value::String(value)),
None => format!(""),
},
match opts.edition {
Some(edition) => toml::Value::String(edition.to_string()),
None => toml::Value::String("2018".to_string()),
Expand Down Expand Up @@ -781,7 +802,7 @@ fn get_environment_variable(variables: &[&str]) -> Option<String> {
variables.iter().filter_map(|var| env::var(var).ok()).next()
}

fn discover_author(path: &Path) -> CargoResult<(String, Option<String>)> {
fn discover_author(path: &Path) -> (Option<String>, Option<String>) {
let git_config = find_git_config(path);
let git_config = git_config.as_ref();

Expand All @@ -798,15 +819,10 @@ fn discover_author(path: &Path) -> CargoResult<(String, Option<String>)> {
.or_else(|| get_environment_variable(&name_variables[3..]));

let name = match name {
Some(name) => name,
None => {
let username_var = if cfg!(windows) { "USERNAME" } else { "USER" };
anyhow::bail!(
"could not determine the current user, please set ${}",
username_var
)
}
Some(namestr) => Some(namestr.trim().to_string()),
None => None,
};

let email_variables = [
"CARGO_EMAIL",
"GIT_AUTHOR_EMAIL",
Expand All @@ -817,7 +833,6 @@ fn discover_author(path: &Path) -> CargoResult<(String, Option<String>)> {
.or_else(|| git_config.and_then(|g| g.get_string("user.email").ok()))
.or_else(|| get_environment_variable(&email_variables[3..]));

let name = name.trim().to_string();
let email = email.map(|s| {
let mut s = s.trim();

Expand All @@ -830,7 +845,7 @@ fn discover_author(path: &Path) -> CargoResult<(String, Option<String>)> {
s.to_string()
});

Ok((name, email))
(name, email)
}

fn find_git_config(path: &Path) -> Option<GitConfig> {
Expand Down
29 changes: 29 additions & 0 deletions tests/testsuite/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,39 @@ fn finds_author_user() {
cargo_process("new foo").env("USER", "foo").run();

let toml = paths::root().join("foo/Cargo.toml");
println!("{:?}", toml);
let contents = fs::read_to_string(&toml).unwrap();
assert!(contents.contains(r#"authors = ["foo"]"#));
}

#[cargo_test]
fn author_without_user_or_email() {
create_empty_gitconfig();
cargo_process("new foo")
.env_remove("USER")
.env_remove("USERNAME")
.env_remove("NAME")
.env_remove("EMAIL")
.run();

let toml = paths::root().join("foo/Cargo.toml");
let contents = fs::read_to_string(&toml).unwrap();
assert!(contents.contains(r#"authors = []"#));
}

#[cargo_test]
fn finds_author_email_only() {
create_empty_gitconfig();
cargo_process("new foo")
.env("EMAIL", "baz")
.run();

let toml = paths::root().join("foo/Cargo.toml");
println!("{:?}", toml);
let contents = fs::read_to_string(&toml).unwrap();
assert!(contents.contains(r#"authors = ["<baz>"]"#));
}

#[cargo_test]
fn finds_author_user_escaped() {
create_empty_gitconfig();
Expand Down