Skip to content

Commit

Permalink
Auto merge of #4630 - ehuss:new-test-temp-dir, r=alexcrichton
Browse files Browse the repository at this point in the history
Fix `new` tests on Windows inadvertently picking up ~/.cargo/config.

Fixes #4601.  By relying on `__CARGO_TEST_ROOT`, using a temp directory is no
longer necessary.
  • Loading branch information
bors committed Oct 17, 2017
2 parents 361857e + eea294a commit 7e89f40
Showing 1 changed file with 19 additions and 45 deletions.
64 changes: 19 additions & 45 deletions tests/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,17 @@ fn simple_bin() {

#[test]
fn both_lib_and_bin() {
let td = TempDir::new("cargo").unwrap();
assert_that(cargo_process("new").arg("--lib").arg("--bin").arg("foo").cwd(td.path())
assert_that(cargo_process("new").arg("--lib").arg("--bin").arg("foo")
.env("USER", "foo"),
execs().with_status(101).with_stderr(
"[ERROR] can't specify both lib and binary outputs"));
}

#[test]
fn simple_git() {
// Run inside a temp directory so that cargo will initialize a git repo.
// If this ran inside paths::root() it would detect that we are already
// inside a git repo and skip the initialization.
let td = TempDir::new("cargo").unwrap();
assert_that(cargo_process("new").arg("--lib").arg("foo").cwd(td.path())
.env("USER", "foo"),
Expand Down Expand Up @@ -194,86 +196,66 @@ fn explicit_name_not_stripped() {

#[test]
fn finds_author_user() {
// Use a temp dir to make sure we don't pick up .cargo/config somewhere in
// the hierarchy
let td = TempDir::new("cargo").unwrap();
create_empty_gitconfig();
assert_that(cargo_process("new").arg("foo").env("USER", "foo")
.cwd(td.path()),
assert_that(cargo_process("new").arg("foo").env("USER", "foo"),
execs().with_status(0));

let toml = td.path().join("foo/Cargo.toml");
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 = ["foo"]"#));
}

#[test]
fn finds_author_user_escaped() {
// Use a temp dir to make sure we don't pick up .cargo/config somewhere in
// the hierarchy
let td = TempDir::new("cargo").unwrap();
create_empty_gitconfig();
assert_that(cargo_process("new").arg("foo").env("USER", "foo \"bar\"")
.cwd(td.path()),
assert_that(cargo_process("new").arg("foo").env("USER", "foo \"bar\""),
execs().with_status(0));

let toml = td.path().join("foo/Cargo.toml");
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 = ["foo \"bar\""]"#));
}

#[test]
fn finds_author_username() {
// Use a temp dir to make sure we don't pick up .cargo/config somewhere in
// the hierarchy
let td = TempDir::new("cargo").unwrap();
create_empty_gitconfig();
assert_that(cargo_process("new").arg("foo")
.env_remove("USER")
.env("USERNAME", "foo")
.cwd(td.path()),
.env("USERNAME", "foo"),
execs().with_status(0));

let toml = td.path().join("foo/Cargo.toml");
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 = ["foo"]"#));
}

#[test]
fn finds_author_priority() {
// Use a temp dir to make sure we don't pick up .cargo/config somewhere in
// the hierarchy
let td = TempDir::new("cargo").unwrap();
assert_that(cargo_process("new").arg("foo")
.env("USER", "bar2")
.env("EMAIL", "baz2")
.env("CARGO_NAME", "bar")
.env("CARGO_EMAIL", "baz")
.cwd(td.path()),
.env("CARGO_EMAIL", "baz"),
execs().with_status(0));

let toml = td.path().join("foo/Cargo.toml");
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 finds_author_email() {
// Use a temp dir to make sure we don't pick up .cargo/config somewhere in
// the hierarchy
let td = TempDir::new("cargo").unwrap();
create_empty_gitconfig();
assert_that(cargo_process("new").arg("foo")
.env("USER", "bar")
.env("EMAIL", "baz")
.cwd(td.path()),
.env("EMAIL", "baz"),
execs().with_status(0));

let toml = td.path().join("foo/Cargo.toml");
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>"]"#));
Expand Down Expand Up @@ -319,14 +301,12 @@ fn finds_local_author_git() {

#[test]
fn finds_git_email() {
let td = TempDir::new("cargo").unwrap();
assert_that(cargo_process("new").arg("foo")
.env("GIT_AUTHOR_NAME", "foo")
.env("GIT_AUTHOR_EMAIL", "gitfoo")
.cwd(td.path()),
.env("GIT_AUTHOR_EMAIL", "gitfoo"),
execs().with_status(0));

let toml = td.path().join("foo/Cargo.toml");
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 = ["foo <gitfoo>"]"#), contents);
Expand All @@ -335,17 +315,13 @@ fn finds_git_email() {

#[test]
fn finds_git_author() {
// Use a temp dir to make sure we don't pick up .cargo/config somewhere in
// the hierarchy
let td = TempDir::new("cargo").unwrap();
create_empty_gitconfig();
assert_that(cargo_process("new").arg("foo")
.env_remove("USER")
.env("GIT_COMMITTER_NAME", "gitfoo")
.cwd(td.path()),
.env("GIT_COMMITTER_NAME", "gitfoo"),
execs().with_status(0));

let toml = td.path().join("foo/Cargo.toml");
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 = ["gitfoo"]"#));
Expand Down Expand Up @@ -379,7 +355,6 @@ fn author_prefers_cargo() {
#[test]
fn git_prefers_command_line() {
let root = paths::root();
let td = TempDir::new("cargo").unwrap();
fs::create_dir(&root.join(".cargo")).unwrap();
File::create(&root.join(".cargo/config")).unwrap().write_all(br#"
[cargo-new]
Expand All @@ -389,10 +364,9 @@ fn git_prefers_command_line() {
"#).unwrap();

assert_that(cargo_process("new").arg("foo").arg("--vcs").arg("git")
.cwd(td.path())
.env("USER", "foo"),
execs().with_status(0));
assert!(td.path().join("foo/.gitignore").exists());
assert!(paths::root().join("foo/.gitignore").exists());
}

#[test]
Expand Down

0 comments on commit 7e89f40

Please sign in to comment.