Skip to content

Commit 7466f45

Browse files
committed
Auto merge of #2713 - sbeckeriv:fix-name-ordering, r=alexcrichton
Correct author order Dearest Reviewer, PR #2696 added some new checks for environment variables for author and email. After the pull request was merged a comment was left about gits ordering for looking at the variables. This pull request closes #2705 by reordering the look up and also addeds CARGO_NAME and CARGO_EMAIL to the top of the order. Thanks Becker
2 parents 42bce5c + c29be69 commit 7466f45

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

src/cargo/ops/cargo_new.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -458,10 +458,11 @@ fn get_environment_variable(variables: &[&str] ) -> Option<String>{
458458
fn discover_author() -> CargoResult<(String, Option<String>)> {
459459
let git_config = GitConfig::open_default().ok();
460460
let git_config = git_config.as_ref();
461-
let name_variables = ["NAME", "GIT_AUTHOR_NAME", "GIT_COMMITTER_NAME",
462-
"USER", "USERNAME"];
463-
let name = git_config.and_then(|g| g.get_string("user.name").ok())
464-
.or_else(|| get_environment_variable(&name_variables));
461+
let name_variables = ["CARGO_NAME", "GIT_AUTHOR_NAME", "GIT_COMMITTER_NAME",
462+
"USER", "USERNAME", "NAME"];
463+
let name = get_environment_variable(&name_variables[0..3])
464+
.or_else(|| git_config.and_then(|g| g.get_string("user.name").ok()))
465+
.or_else(|| get_environment_variable(&name_variables[3..]));
465466

466467
let name = match name {
467468
Some(name) => name,
@@ -471,9 +472,11 @@ fn discover_author() -> CargoResult<(String, Option<String>)> {
471472
username_var)
472473
}
473474
};
474-
let email_variables = ["EMAIL", "GIT_AUTHOR_EMAIL", "GIT_COMMITTER_EMAIL"];
475-
let email = git_config.and_then(|g| g.get_string("user.email").ok())
476-
.or_else(|| get_environment_variable(&email_variables) );
475+
let email_variables = ["CARGO_EMAIL", "GIT_AUTHOR_EMAIL", "GIT_COMMITTER_EMAIL",
476+
"EMAIL"];
477+
let email = get_environment_variable(&email_variables[0..3])
478+
.or_else(|| git_config.and_then(|g| g.get_string("user.email").ok()))
479+
.or_else(|| get_environment_variable(&email_variables[3..]));
477480

478481
let name = name.trim().to_string();
479482
let email = email.map(|s| s.trim().to_string());

tests/test_cargo_new.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,24 @@ test!(finds_author_username {
181181
assert!(contents.contains(r#"authors = ["foo"]"#));
182182
});
183183

184+
test!(finds_author_priority {
185+
// Use a temp dir to make sure we don't pick up .cargo/config somewhere in
186+
// the hierarchy
187+
let td = TempDir::new("cargo").unwrap();
188+
assert_that(cargo_process("new").arg("foo")
189+
.env("USER", "bar2")
190+
.env("EMAIL", "baz2")
191+
.env("CARGO_NAME", "bar")
192+
.env("CARGO_EMAIL", "baz")
193+
.cwd(td.path().clone()),
194+
execs().with_status(0));
195+
196+
let toml = td.path().join("foo/Cargo.toml");
197+
let mut contents = String::new();
198+
File::open(&toml).unwrap().read_to_string(&mut contents).unwrap();
199+
assert!(contents.contains(r#"authors = ["bar <baz>"]"#));
200+
});
201+
184202
test!(finds_author_email {
185203
// Use a temp dir to make sure we don't pick up .cargo/config somewhere in
186204
// the hierarchy

0 commit comments

Comments
 (0)