Skip to content

Commit

Permalink
Add Strategy API
Browse files Browse the repository at this point in the history
  • Loading branch information
tmandry committed Jul 16, 2020
1 parent 15fc92d commit 2e65ed8
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 48 deletions.
32 changes: 15 additions & 17 deletions src/cargo.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use serde::Deserialize;
use std::path::PathBuf;
use std::process::{Command, Output, Stdio};
use std::process::{Command, Stdio};

use crate::error::{Error, Result};
use crate::manifest::Name;
use crate::run::Project;
use crate::run::{CargoProject, StrategyResult};
use crate::rustflags;

#[derive(Deserialize)]
Expand All @@ -17,7 +17,7 @@ fn raw_cargo() -> Command {
Command::new(option_env!("CARGO").unwrap_or("cargo"))
}

fn cargo(project: &Project) -> Command {
fn cargo(project: &CargoProject) -> Command {
let mut cmd = raw_cargo();
cmd.current_dir(&project.dir);
cmd.env(
Expand All @@ -29,7 +29,7 @@ fn cargo(project: &Project) -> Command {
cmd
}

pub fn build_dependencies(project: &Project) -> Result<()> {
pub fn build_dependencies(project: &CargoProject) -> Result<()> {
let _ = cargo(project).arg("generate-lockfile").status();

let status = cargo(project)
Expand All @@ -47,7 +47,7 @@ pub fn build_dependencies(project: &Project) -> Result<()> {
}
}

pub fn build_test(project: &Project, name: &Name) -> Result<Output> {
pub fn build_test(project: &CargoProject, name: &Name) -> StrategyResult<Command> {
let _ = cargo(project)
.arg("clean")
.arg("--package")
Expand All @@ -57,27 +57,25 @@ pub fn build_test(project: &Project, name: &Name) -> Result<Output> {
.stderr(Stdio::null())
.status();

cargo(project)
.arg(if project.has_pass { "build" } else { "check" })
let mut cmd = cargo(project);
cmd.arg(if project.has_pass { "build" } else { "check" })
.arg("--bin")
.arg(name)
.args(features(project))
.arg("--quiet")
.arg("--color=never")
.output()
.map_err(Error::Cargo)
.arg("--color=never");
Ok(cmd)
}

pub fn run_test(project: &Project, name: &Name) -> Result<Output> {
cargo(project)
.arg("run")
pub fn run_test(project: &CargoProject, name: &Name) -> StrategyResult<Command> {
let mut cmd = cargo(project);
cmd.arg("run")
.arg("--bin")
.arg(name)
.args(features(project))
.arg("--quiet")
.arg("--color=never")
.output()
.map_err(Error::Cargo)
.arg("--color=never");
Ok(cmd)
}

pub fn metadata() -> Result<Metadata> {
Expand All @@ -93,7 +91,7 @@ pub fn metadata() -> Result<Metadata> {
})
}

fn features(project: &Project) -> Vec<String> {
fn features(project: &CargoProject) -> Vec<String> {
match &project.features {
Some(features) => vec![
"--no-default-features".to_owned(),
Expand Down
9 changes: 9 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@ impl Error {

impl StdError for Error {}

impl From<Box<dyn StdError + 'static>> for Error {
fn from(err: Box<dyn StdError + 'static>) -> Self {
match err.downcast::<Self>() {
Ok(e) => *e,
Err(e) => Error::Strategy(e),
}
}
}

impl From<GlobError> for Error {
fn from(err: GlobError) -> Self {
Error::Glob(err)
Expand Down
12 changes: 11 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@ use std::cell::RefCell;
use std::path::{Path, PathBuf};
use std::thread;

pub use run::{CargoStrategy, Project, Strategy, Test};

#[derive(Debug)]
pub struct TestCases {
runner: RefCell<Runner>,
Expand All @@ -238,6 +240,7 @@ pub struct TestCases {
#[derive(Debug)]
struct Runner {
tests: Vec<TestSpec>,
strategy: Box<dyn Strategy>,
}

#[derive(Clone, Debug)]
Expand All @@ -255,8 +258,15 @@ enum Expected {
impl TestCases {
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
Self::new_with_strategy(CargoStrategy::new())
}

pub fn new_with_strategy<S: Strategy + 'static>(strategy: S) -> Self {
TestCases {
runner: RefCell::new(Runner { tests: Vec::new() }),
runner: RefCell::new(Runner {
tests: Vec::new(),
strategy: Box::new(strategy),
}),
}
}

Expand Down
Loading

0 comments on commit 2e65ed8

Please sign in to comment.