1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
use std::path::PathBuf;
use crate::junit::TestSuite;
use crate::errors::SuityError;
use std::process::{Command, Output};
use std::io;
use std::fs;


use crate::results;

pub enum RunspecResult {
    Ok,
    Errors(u64),
}

impl RunspecResult {
    pub fn as_exit_code(&self) -> i32 {
        match self {
            RunspecResult::Ok => 0,
            RunspecResult::Errors(n) => *n as i32
        }
    }
}
/// Desired output format. Right not only JUnit is supported and it's the default format.
#[derive(Debug, Copy, Clone,Deserialize)]
pub enum OutputFormat {
    JUnit
}

impl Default for OutputFormat {
    fn default() -> OutputFormat {
        OutputFormat::JUnit
    }
}

#[derive(Debug,Deserialize)]
pub struct Runspec {
    /// How to name this spec.
    #[serde(default = "default::name")]
    pub name: String,
    /// List of features to pass to cargo.
    #[serde(default = "default::features")]
    pub features: Vec<String>,
    /// Report format.
    #[serde(default = "default::format")]
    pub format: OutputFormat,
    /// Output directory. Default `./test-results/`
    #[serde(default = "default::output")]
    pub output: PathBuf,
    /// Run Doc-Tests or not. Default true.
    #[serde(default = "default::doc")]
    pub doc: bool,
    /// Run Unit-Tests or not. Default true.
    #[serde(default = "default::lib")]
    pub lib: bool,
    /// List of integration tests to run. Default all of them.
    #[serde(default = "default::integration")]
    pub integration: Vec<String>
}

impl Default for Runspec {
    fn default() -> Runspec {
        Runspec {
            name: String::from("default"),
            features: Vec::new(),
            format: OutputFormat::default(),
            output: PathBuf::from("test-results/"),
            doc: true,
            lib: true,
            integration: vec![String::from("*")],
        }
    }
}


impl Runspec {
    fn features_to_string(&self) -> String {
        itertools::join(self.features.iter(), " ")
    }

    pub fn execute<W: io::Write>(&mut self, output: W) -> Result<Vec<TestSuite>, SuityError> {
        let mut results: Vec<TestSuite> = Vec::with_capacity(5);
        let shared_args = self.get_shared_args();

        let mut args = shared_args.clone();
        args.push(String::from("--no-run"));
        let status = Command::new("cargo").args(args).status()?;
        if ! status.success() {
            return Err(SuityError::FailedToCompile{workflow: self.name.clone()})
        }
        if self.lib {
            let mut args = shared_args.clone();
            args.push(String::from("--lib"));
            args.push(String::from("--"));
            add_common_args(&mut args);

            let test_suite_name = format!("[{}] Lib-tests", self.name).to_string();

            if let Some(suite) = Runspec::run_cargo(&mut args, test_suite_name)? {
                results.push(suite);
            }
        }
        if self.doc {
            let mut args = shared_args.clone();
            args.push(String::from("--doc"));
            args.push(String::from("--"));
            add_common_args(&mut args);
            let test_suite_name = format!("[{}] Doc-tests", self.name).to_string();

            if let Some(suite) = Runspec::run_cargo(&mut args, test_suite_name)? {
                results.push(suite);
            }
        }

        if !self.integration.is_empty() {
            let tests = {
                if self.integration == vec!["*"] {
                    get_integration_tests()
                } else {
                    self.integration.clone()
                }
            };
            for name in tests {
                if name != "*" {
                    if let Some(suite) = self.run_integration_test(&name)? {
                        results.push(suite);
                    }
                }
            }
        }
        crate::junit::write_as_xml(&results, output)?;
        Ok(results)
    }

    fn run_integration_test(&mut self, test: &String) -> Result<Option<TestSuite>, SuityError> {
        let test_suite_name = format!("[{}] {}", self.name, &test).to_string();
        if let Some(path) = map_to_binary(&test) {
            let mut args = Vec::with_capacity(3);
            add_common_args(&mut args);
            let out = Command::new(path).args(&args).output()?;
            Runspec::parse_test_output(test_suite_name, &out)
        } else {
            Err(SuityError::TestBinaryNotFound{name: test.clone(), workflow: self.name.clone()})
        }
    }

    fn run_cargo(args: &Vec<String>, test_suite_name: String) -> Result<Option<TestSuite>, SuityError> {
        let out = Command::new("cargo").args(args).output()?;
        Runspec::parse_test_output(test_suite_name, &out)
    }

    fn parse_test_output(test_suite_name: String, out: &Output) -> Result<Option<TestSuite>, SuityError> {
        let stdout: String = String::from_utf8_lossy(&out.stdout).into();
        let events = results::parse_test_results(&stdout);
        let suite = TestSuite::new(events, test_suite_name)?;
        if suite.tests > 0 {
            Ok(Some(suite))
        } else {
            Ok(None)
        }
    }

    fn get_shared_args(&mut self) -> Vec<String> {
        let mut args: Vec<String> = vec![String::from("test")];
        if !self.features.is_empty() {
            args.push(String::from("--features"));
            args.push(self.features_to_string());
        }
        args
    }

    pub fn get_output_file_path(&self) -> PathBuf {
        let mut output_path = PathBuf::new();
        output_path.push(&self.output);
        output_path.push(&self.name);
        output_path.set_extension("xml");
        output_path
    }
}

fn get_integration_tests() -> Vec<String> {
    if let Ok(entries) = fs::read_dir("tests/") {
        entries
            .filter_map(Result::ok)
            .filter(filters::file_with_content)
            .map(filters::to_path)
            .filter(|path| filters::extension_is(path, "rs"))
            .filter_map(|path| path.clone().file_stem().map(|osstr| String::from(osstr.to_string_lossy())))
            .collect()
    } else {
        Vec::new()
    }
}

fn add_common_args(args: &mut Vec<String>) {
    args.push(String::from("-Z"));
    args.push(String::from("unstable-options"));
    args.push(String::from("--format=json"));
}

fn map_to_binary(name: &String) -> Option<PathBuf> {
    if let Ok(entries) = fs::read_dir("target/debug") {
        let mut executables: Vec<PathBuf> = entries
            .filter_map(Result::ok)
            .filter(filters::file_with_content)
            .map(filters::to_path)
            .filter(filters::is_executable)
            .filter(|f| filters::filename_starts_with(&f, &name))
            .collect();
        executables.sort_by(filters::sort_by_modify_date);
        executables.last().cloned()
    } else {
        None
    }
}

mod default {
    pub fn name() -> String {
        super::Runspec::default().name.clone()
    }
    pub fn features() -> Vec<String> {
        super::Runspec::default().features.clone()
    }
    pub fn format() -> super::OutputFormat {
        super::Runspec::default().format.clone()
    }
    pub fn output() -> std::path::PathBuf {
        super::Runspec::default().output.clone()
    }
    pub fn doc() -> bool {
        super::Runspec::default().doc.clone()
    }
    pub fn lib() -> bool {
        super::Runspec::default().lib.clone()
    }
    pub fn integration() -> Vec<String> {
        super::Runspec::default().integration.clone()
    }
}

mod filters {
    use std::cmp::Ordering;
    use std::fs;
    use std::path;
    use std::ffi::OsStr;
    use is_executable::IsExecutable;

    pub fn file_with_content(f: &fs::DirEntry) -> bool {
        if let Ok(meta) = f.metadata() {
            meta.is_file() && meta.len() > 1
        } else {
            false
        }
    }

    pub fn to_path(f: fs::DirEntry) -> path::PathBuf {
        f.path()
    }

    pub fn is_executable(f: &path::PathBuf) -> bool {
        f.is_executable()
    }

    pub fn extension_is(f: &path::PathBuf, suffix: &str) -> bool {
        let rust_ext = OsStr::new(suffix);
        f.extension() == Some(&rust_ext)
    }

    pub fn filename_starts_with(f: &path::PathBuf, prefix: &str) -> bool {
        if let Some(filename) = f.file_name() {
            let filename = filename.to_str().unwrap_or("");
            filename.starts_with(prefix)
        } else {
            false
        }
    }
    pub fn sort_by_modify_date(a: &path::PathBuf, b: &path::PathBuf) -> Ordering {
        let a_meta = a.metadata().map(|a| a.modified().unwrap()).unwrap();
        let b_meta = b.metadata().map(|b| b.modified().unwrap()).unwrap();
        a_meta.cmp(&b_meta)
    }
}