crate_compile_test/
plan.rs1use std::path::{Path, PathBuf};
2use tempfile::tempdir;
3use walkdir::WalkDir;
4
5use config::{Config, Mode};
6use error::Result;
7use steps::{build::BuildStepFactory, check_errors::CheckErrorsStepFactory, TestStepFactory};
8
9pub struct TestPlan {
10 config: Config,
11 steps: Vec<Box<TestStepFactory>>,
12 crates: Vec<PathBuf>,
13}
14
15impl TestPlan {
16 pub fn new(mut config: Config) -> Self {
17 let crates = WalkDir::new(&config.base_dir)
18 .max_depth(1)
19 .min_depth(1)
20 .into_iter()
21 .filter_map(|entry| entry.ok())
22 .filter(|entry| entry.path().is_dir())
23 .map(|entry| entry.path().into())
24 .collect();
25
26 let mut steps: Vec<Box<TestStepFactory>> = match config.mode {
27 Mode::BuildFail => vec![Box::new(CheckErrorsStepFactory::new())],
28 Mode::BuildSuccess => vec![Box::new(BuildStepFactory::new())],
29 Mode::Expand => vec![Box::new(BuildStepFactory::new())],
30 };
31
32 if config.additional_steps.len() > 0 {
33 steps.append(&mut config.additional_steps);
34 }
35
36 TestPlan {
37 config,
38 crates,
39 steps,
40 }
41 }
42
43 pub fn crates(&self) -> &[PathBuf] {
44 &self.crates
45 }
46
47 pub fn is_crate_filtered_out(&self, crate_path: &Path) -> bool {
48 (self.config.crates_filter)(crate_path) == false
49 }
50
51 pub fn execute_steps(&self, crate_path: &Path) -> Result<()> {
52 let build_path = tempdir()?;
53
54 let local_steps: Vec<_> = self.steps
55 .iter()
56 .map(|factory| factory.initialize(&self.config, crate_path))
57 .collect();
58
59 for step in local_steps {
60 step?.execute(&self.config, build_path.as_ref())?;
61 }
62
63 Ok(())
64 }
65}