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

Support cargo test #24

Merged
merged 6 commits into from
Mar 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ script:
- cargo check --verbose
- cargo test --verbose
- cargo test --verbose --features "print"
- cargo test --verbose --features "test_unstable"
# Audit `format` for new features.
- cargo test --verbose --features "strict_unstable"
- cargo test --verbose --features "strict_unstable,cargo_unstable,test_unstable"

branches:
only:
Expand Down
10 changes: 10 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,20 @@ serde_json = "1.0"
lazy_static = "1.1.0"
log = "0.4"

[dev-dependencies]
assert_fs = "0.11"

[features]
# Upcoming features in cargo.
cargo_unstable = []
# Programmatic use of `cargo test` (relies on an unstable CLI API).
test_unstable = []
# This mostly exists for testing, to catch new fields and enum variants being
# added (for the cases we actually cover).
strict_unstable = []
# This is for when using `escargot` in tests and you want logged output to
# instead be printed because no logger is configured.
print = []

[package.metadata.docs.rs]
features = [ "cargo_unstable", "test_unstable" ]
4 changes: 4 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ install:
test_script:
- cargo check --verbose
- cargo test --verbose
- cargo test --verbose --features "print"
- cargo test --verbose --features "test_unstable"
# Audit `format` for new features.
- cargo test --verbose --features "strict_unstable,cargo_unstable,test_unstable"

branches:
only:
Expand Down
124 changes: 115 additions & 9 deletions src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,24 @@ use cargo::CURRENT_TARGET;
use error::*;
use msg::*;
use run::CargoRun;
#[cfg(feature = "test_unstable")]
use test::CargoTest;

/// The `build` subcommand.
///
/// # Example
///
/// ```rust
/// extern crate escargot;
/// extern crate assert_fs;
///
/// let temp = assert_fs::TempDir::new().unwrap();
/// escargot::CargoBuild::new()
/// .bin("bin_fixture")
/// .bin("bin")
/// .current_release()
/// .current_target()
/// .manifest_path("tests/fixtures/bin/Cargo.toml")
/// .target_dir(temp.path())
/// .exec()
/// .unwrap();
/// ```
Expand All @@ -33,7 +41,14 @@ impl CargoBuild {
/// # Example
///
/// ```rust
/// extern crate escargot;
/// extern crate assert_fs;
///
/// let temp = assert_fs::TempDir::new().unwrap();
/// escargot::CargoBuild::new()
/// .bin("bin")
/// .manifest_path("tests/fixtures/bin/Cargo.toml")
/// .target_dir(temp.path())
/// .exec()
/// .unwrap();
/// ```
Expand All @@ -56,9 +71,15 @@ impl CargoBuild {
/// # Example
///
/// ```rust
/// extern crate escargot;
/// extern crate assert_fs;
///
/// let temp = assert_fs::TempDir::new().unwrap();
/// escargot::CargoBuild::new()
/// .package("escargot")
/// .bin("bin_fixture")
/// .package("bin")
/// .bin("bin")
/// .manifest_path("tests/fixtures/bin/Cargo.toml")
/// .target_dir(temp.path())
/// .exec()
/// .unwrap();
/// ```
Expand All @@ -70,8 +91,14 @@ impl CargoBuild {
/// # Example
///
/// ```rust
/// extern crate escargot;
/// extern crate assert_fs;
///
/// let temp = assert_fs::TempDir::new().unwrap();
/// escargot::CargoBuild::new()
/// .bin("bin_fixture")
/// .bin("bin")
/// .manifest_path("tests/fixtures/bin/Cargo.toml")
/// .target_dir(temp.path())
/// .exec()
/// .unwrap();
/// ```
Expand All @@ -85,8 +112,14 @@ impl CargoBuild {
/// # Example
///
/// ```rust
/// extern crate escargot;
/// extern crate assert_fs;
///
/// let temp = assert_fs::TempDir::new().unwrap();
/// escargot::CargoBuild::new()
/// .example("example_fixture")
/// .manifest_path("tests/fixtures/example/Cargo.toml")
/// .target_dir(temp.path())
/// .exec()
/// .unwrap();
/// ```
Expand All @@ -95,6 +128,46 @@ impl CargoBuild {
self.arg("--example").arg(name)
}

/// Build all tests
///
/// # Example
///
/// ```rust
/// extern crate escargot;
/// extern crate assert_fs;
///
/// let temp = assert_fs::TempDir::new().unwrap();
/// escargot::CargoBuild::new()
/// .tests()
/// .manifest_path("tests/fixtures/test/Cargo.toml")
/// .target_dir(temp.path())
/// .exec()
/// .unwrap();
/// ```
pub fn tests(self) -> Self {
self.arg("--tests")
}

/// Build only `name` test.
///
/// # Example
///
/// ```rust
/// extern crate escargot;
/// extern crate assert_fs;
///
/// let temp = assert_fs::TempDir::new().unwrap();
/// escargot::CargoBuild::new()
/// .test("test")
/// .manifest_path("tests/fixtures/test/Cargo.toml")
/// .target_dir(temp.path())
/// .exec()
/// .unwrap();
/// ```
pub fn test<S: AsRef<ffi::OsStr>>(self, name: S) -> Self {
self.arg("--test").arg(name)
}

/// Path to Cargo.toml
pub fn manifest_path<S: AsRef<ffi::OsStr>>(self, path: S) -> Self {
self.arg("--manifest-path").arg(path)
Expand Down Expand Up @@ -156,26 +229,59 @@ impl CargoBuild {
}

/// Build the configured target, returning compiler messages.
pub fn exec(self) -> CargoResult<MessageIter> {
MessageIter::from_command(self.cmd)
pub fn exec(self) -> CargoResult<CommandMessages> {
CommandMessages::with_command(self.cmd)
}

/// Provide a proxy for running the built target.
///
/// # Example
///
/// ```rust
/// extern crate escargot;
/// extern crate assert_fs;
///
/// let temp = assert_fs::TempDir::new().unwrap();
/// let run = escargot::CargoBuild::new()
/// .bin("bin_fixture")
/// .bin("bin")
/// .current_release()
/// .current_target()
/// .manifest_path("tests/fixtures/bin/Cargo.toml")
/// .target_dir(temp.path())
/// .run()
/// .unwrap();
/// println!("artifact={}", run.path().display());
/// ```
pub fn run(self) -> CargoResult<CargoRun> {
let msgs = MessageIter::from_command(self.cmd)?;
CargoRun::with_messages(msgs, self.bin, self.example)
let msgs = CommandMessages::with_command(self.cmd)?;
CargoRun::from_message(msgs, self.bin, self.example)
}

/// Provide a proxy for running the built target.
///
/// Required feature: `test_unstable` since the format parsed is unstable.
///
/// # Example
///
/// ```rust
/// extern crate escargot;
/// extern crate assert_fs;
///
/// let temp = assert_fs::TempDir::new().unwrap();
/// let run = escargot::CargoBuild::new()
/// .test("test")
/// .current_release()
/// .current_target()
/// .manifest_path("tests/fixtures/test/Cargo.toml")
/// .target_dir(temp.path())
/// .run_tests().unwrap()
/// .next().unwrap().unwrap();
/// println!("artifact={}", run.path().display());
/// ```
#[cfg(feature = "test_unstable")]
pub fn run_tests(self) -> CargoResult<impl Iterator<Item = Result<CargoTest, CargoError>>> {
let msgs = CommandMessages::with_command(self.cmd)?;
Ok(CargoTest::with_messages(msgs))
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ use std::path;

pub mod diagnostic;

#[cfg(feature = "test_unstable")]
pub mod test;

type CowPath<'a> = borrow::Cow<'a, path::Path>;
type CowStr<'a> = borrow::Cow<'a, str>;

Expand Down Expand Up @@ -46,6 +49,10 @@ pub struct Artifact<'a> {
/// The full paths to the generated artifacts
#[serde(borrow)]
pub filenames: Vec<CowPath<'a>>,
/// The full paths to the generated artifacts
#[cfg(feature = "cargo_unstable")]
#[serde(borrow)]
pub executable: Option<CowPath<'a>>,
/// If true, then the files were already generated
pub fresh: bool,
#[doc(hidden)]
Expand Down
Loading