Skip to content

Commit

Permalink
Auto merge of #5810 - Eh2406:test-fix, r=alexcrichton
Browse files Browse the repository at this point in the history
now that we respect gitignore tests can be simplified

There are a lot of test that used a tempfile to avoid the fact that cargo would not init a git in the test folder. (or because they were copy/pasted from one that did.) Now that #5733 landed we can remove them all.
  • Loading branch information
bors committed Jul 26, 2018
2 parents d195f28 + 7fc0dff commit 3b14105
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 47 deletions.
4 changes: 1 addition & 3 deletions tests/testsuite/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use support::{execs, main_file, project};
use support::registry::Package;
use support::ChannelChanger;
use support::hamcrest::{assert_that, existing_dir, existing_file, is_not};
use tempfile;

#[test]
fn cargo_compile_simple() {
Expand Down Expand Up @@ -485,8 +484,7 @@ Caused by:

#[test]
fn cargo_compile_without_manifest() {
let tmpdir = tempfile::Builder::new().prefix("cargo").tempdir().unwrap();
let p = ProjectBuilder::new(tmpdir.path().to_path_buf()).no_manifest().build();
let p = project().no_manifest().build();

assert_that(
p.cargo("build"),
Expand Down
1 change: 0 additions & 1 deletion tests/testsuite/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,6 @@ fn shows_warnings() {
#[test]
fn warns_if_no_vcs_detected() {
let p = project()
.use_temp_dir()
.file("src/lib.rs", "pub fn foo() {}")
.build();

Expand Down
15 changes: 4 additions & 11 deletions tests/testsuite/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use std::env;
use cargo::util::ProcessBuilder;
use support::{cargo_exe, execs, paths};
use support::hamcrest::{assert_that, existing_dir, existing_file, is_not};
use tempfile;

fn cargo_process(s: &str) -> ProcessBuilder {
let mut p = support::process(&cargo_exe());
Expand Down Expand Up @@ -62,12 +61,10 @@ fn simple_bin() {

#[test]
fn both_lib_and_bin() {
let td = tempfile::Builder::new().prefix("cargo").tempdir().unwrap();
assert_that(
cargo_process("init")
.arg("--lib")
.arg("--bin")
.cwd(td.path())
.env("USER", "foo"),
execs()
.with_status(101)
Expand Down Expand Up @@ -304,21 +301,17 @@ fn simple_git() {

#[test]
fn auto_git() {
let td = tempfile::Builder::new().prefix("cargo").tempdir().unwrap();
let foo = &td.path().join("foo");
fs::create_dir_all(&foo).unwrap();
assert_that(
cargo_process("init")
.arg("--lib")
.cwd(foo.clone())
.env("USER", "foo"),
execs().with_status(0),
);

assert_that(&foo.join("Cargo.toml"), existing_file());
assert_that(&foo.join("src/lib.rs"), existing_file());
assert_that(&foo.join(".git"), existing_dir());
assert_that(&foo.join(".gitignore"), existing_file());
assert_that(&paths::root().join("Cargo.toml"), existing_file());
assert_that(&paths::root().join("src/lib.rs"), existing_file());
assert_that(&paths::root().join(".git"), existing_dir());
assert_that(&paths::root().join(".gitignore"), existing_file());
}

#[test]
Expand Down
1 change: 0 additions & 1 deletion tests/testsuite/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ extern crate serde_derive;
#[macro_use]
extern crate serde_json;
extern crate tar;
extern crate tempfile;
extern crate toml;
extern crate url;
#[cfg(windows)]
Expand Down
38 changes: 22 additions & 16 deletions tests/testsuite/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use cargo::util::ProcessBuilder;
use support::process;
use support::{execs, paths};
use support::hamcrest::{assert_that, existing_dir, existing_file, is_not};
use tempfile;

fn cargo_process(s: &str) -> ProcessBuilder {
let mut p = support::cargo_process();
Expand Down Expand Up @@ -110,27 +109,22 @@ fn both_lib_and_bin() {

#[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 = tempfile::Builder::new().prefix("cargo").tempdir().unwrap();
assert_that(
cargo_process("new")
.arg("--lib")
.arg("foo")
.cwd(td.path())
.env("USER", "foo"),
execs().with_status(0),
);

assert_that(td.path(), existing_dir());
assert_that(&td.path().join("foo/Cargo.toml"), existing_file());
assert_that(&td.path().join("foo/src/lib.rs"), existing_file());
assert_that(&td.path().join("foo/.git"), existing_dir());
assert_that(&td.path().join("foo/.gitignore"), existing_file());
assert_that(&paths::root(), existing_dir());
assert_that(&paths::root().join("foo/Cargo.toml"), existing_file());
assert_that(&paths::root().join("foo/src/lib.rs"), existing_file());
assert_that(&paths::root().join("foo/.git"), existing_dir());
assert_that(&paths::root().join("foo/.gitignore"), existing_file());

assert_that(
cargo_process("build").cwd(&td.path().join("foo")),
cargo_process("build").cwd(&paths::root().join("foo")),
execs().with_status(0),
);
}
Expand Down Expand Up @@ -476,6 +470,9 @@ fn subpackage_no_git() {
execs().with_status(0),
);

assert_that(&paths::root().join("foo/.git"), existing_dir());
assert_that(&paths::root().join("foo/.gitignore"), existing_file());

let subpackage = paths::root().join("foo").join("components");
fs::create_dir(&subpackage).unwrap();
assert_that(
Expand All @@ -502,10 +499,19 @@ fn subpackage_git_with_gitignore() {
execs().with_status(0),
);

let gitignore = paths::root().join("foo").join(".gitignore");
assert_that(
&paths::root().join("foo/.git"),
existing_dir(),
);
assert_that(
&paths::root().join("foo/.gitignore"),
existing_file(),
);

let gitignore = paths::root().join("foo/.gitignore");
fs::write(gitignore, b"components").unwrap();

let subpackage = paths::root().join("foo").join("components");
let subpackage = paths::root().join("foo/components");
fs::create_dir(&subpackage).unwrap();
assert_that(
cargo_process("new")
Expand All @@ -515,11 +521,11 @@ fn subpackage_git_with_gitignore() {
);

assert_that(
&paths::root().join("foo").join("components").join("subcomponent").join(".git"),
&paths::root().join("foo/components/subcomponent/.git"),
existing_dir(),
);
assert_that(
&paths::root().join("foo").join("components").join("subcomponent").join(".gitignore"),
&paths::root().join("foo/components/subcomponent/.gitignore"),
existing_file(),
);

Expand Down
20 changes: 5 additions & 15 deletions tests/testsuite/support/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ use std::usize;
use cargo::util::{ProcessBuilder, ProcessError, Rustc};
use cargo;
use serde_json::{self, Value};
use tempfile::TempDir;
use url::Url;

use self::hamcrest as ham;
Expand Down Expand Up @@ -180,9 +179,8 @@ impl SymlinkBuilder {
}
}

pub enum Project {
Rooted(PathBuf),
Temp(TempDir),
pub struct Project {
root: PathBuf
}

#[must_use]
Expand All @@ -206,15 +204,15 @@ impl ProjectBuilder {

pub fn new(root: PathBuf) -> ProjectBuilder {
ProjectBuilder {
root: Project::Rooted(root),
root: Project { root },
files: vec![],
symlinks: vec![],
no_manifest: false,
}
}

pub fn at<P: AsRef<Path>>(mut self, path: P) -> Self {
self.root = Project::Rooted(paths::root().join(path));
self.root = Project{root: paths::root().join(path)};
self
}

Expand All @@ -238,11 +236,6 @@ impl ProjectBuilder {
self
}

pub fn use_temp_dir(mut self) -> Self {
self.root = Project::Temp(TempDir::new().unwrap());
self
}

pub fn no_manifest(mut self) -> Self {
self.no_manifest = true;
self
Expand Down Expand Up @@ -286,10 +279,7 @@ impl ProjectBuilder {
impl Project {
/// Root of the project, ex: `/path/to/cargo/target/cit/t0/foo`
pub fn root(&self) -> PathBuf {
match self {
Project::Rooted(p) => p.clone(),
Project::Temp(t) => t.path().to_path_buf(),
}
self.root.clone()
}

/// Project's target dir, ex: `/path/to/cargo/target/cit/t0/foo/target`
Expand Down

0 comments on commit 3b14105

Please sign in to comment.