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
// Copyright © 2018-19 Qtrac Ltd. All rights reserved.

use crate::plan::DiffKind;
use crate::util::{PathBufExt, Which};
use approx::abs_diff_eq;
use std::fmt;
use std::path::{Path, PathBuf};

static EXPECTED_PATH: &str = "$EXPECTED_PATH";
static ACTUAL_PATH: &str = "$ACTUAL_PATH";
static OUT_PATH: &str = "$OUT_PATH";

/// Represents a single test which can be generated or retested.
///
/// Create a new test using [`new()`](struct.Test.html#method.new).
/// Override its defaults using the builder methods, and finally call
/// [`build()`](struct.Test.html#method.build).
///
/// New `Test`s should be passed to the
/// [`Plan::push()`](struct.Plan.html#method.push) method.
///
/// See also the [builders example](index.html#builders-example).
#[derive(Clone, Debug)]
pub struct Test {
    pub(crate) name: String,
    pub(crate) exit_code: i32,
    pub(crate) wait: f64,
    pub(crate) app: PathBuf,
    pub(crate) app_args: Vec<String>,
    pub(crate) stdin: Vec<u8>,
    pub(crate) stdin_filename: Option<PathBuf>,
    pub(crate) stdout: Option<PathBuf>,
    pub(crate) diff: Option<DiffKind>,
    pub(crate) diff_args: Vec<String>,
}

impl Test {
    pub(crate) fn default() -> Test {
        Test {
            name: "".to_string(),
            exit_code: 0,
            wait: 0.0,
            app: PathBuf::new(),
            app_args: vec![],
            stdin: vec![],
            stdin_filename: None,
            stdout: None,
            diff: None,
            diff_args: vec![],
        }
    }

    pub(crate) fn with_paths(
        &self,
        expected_path: &Path,
        actual_path: &Path,
        which: &Which,
    ) -> Test {
        let out_path = if which == &Which::Expected {
            expected_path
        } else {
            actual_path
        };
        let mut app_args = Vec::with_capacity(self.app_args.len());
        if !self.app_args.is_empty() {
            let out = out_path.to_string_lossy();
            for arg in &self.app_args {
                app_args.push(arg.replace(OUT_PATH, &out));
            }
        }
        let mut diff_args = Vec::with_capacity(self.diff_args.len());
        if !self.diff_args.is_empty() {
            let exp_path = &expected_path.to_string_lossy();
            let act_path = &actual_path.to_string_lossy();
            for arg in &self.diff_args {
                diff_args.push(
                    arg.replace(EXPECTED_PATH, &exp_path)
                        .replace(ACTUAL_PATH, &act_path),
                );
            }
        }
        let stdout = self.maybe_stdout_with_path(&out_path);
        Test {
            name: self.name.clone(),
            exit_code: self.exit_code,
            wait: 0.0,
            app: self.app.clone(),
            app_args,
            stdin: self.stdin.clone(),
            stdin_filename: self.stdin_filename.clone(),
            stdout,
            diff: self.diff.clone(),
            diff_args,
        }
    }

    pub(crate) fn output_filename(
        &self,
        out_path: &Path,
        which: &Which,
    ) -> PathBuf {
        let out = out_path.to_string_lossy().into_owned();
        for arg in &self.app_args {
            if arg.contains(&out) {
                return Path::new(arg).to_path_buf();
            } else if arg.contains(OUT_PATH) {
                return Path::new(&arg.replace(OUT_PATH, &out))
                    .to_path_buf();
            }
        }
        if let Some(path) = self.maybe_stdout_with_path(&out_path) {
            return path;
        }
        for arg in &self.diff_args {
            if which == &Which::Expected && arg.contains(EXPECTED_PATH) {
                return Path::new(&arg.replace(EXPECTED_PATH, &out))
                    .to_path_buf();
            } else if which == &Which::Actual && arg.contains(ACTUAL_PATH)
            {
                return Path::new(&arg.replace(ACTUAL_PATH, &out))
                    .to_path_buf();
            }
        }
        PathBuf::new()
    }

    fn maybe_stdout_with_path(&self, out_path: &Path) -> Option<PathBuf> {
        match &self.stdout {
            Some(stdout) => {
                if !stdout.is_empty() {
                    if stdout.starts_with(OUT_PATH) {
                        return Some(
                            stdout
                                .replace(OUT_PATH, out_path)
                                .to_path_buf(),
                        );
                    } else {
                        return Some(out_path.join(stdout).to_path_buf());
                    }
                }
            }
            None => return None,
        }
        None
    }

    pub(crate) fn is_ok(&self) -> bool {
        if self.app.is_empty() {
            return false;
        }
        if !self.app_args.is_empty() {
            return true;
        }
        match &self.stdout {
            Some(stdout) => !stdout.is_empty(),
            None => false,
        }
    }

    pub(crate) fn will_diff(&self) -> bool {
        self.diff != Some(DiffKind::No)
    }

    pub(crate) fn rt(&self, u: u32) -> String {
        let mut test = format!("[{}]\n", u);
        if !self.name.is_empty() {
            test.push_str(&format!("NAME: {}\n", &self.name));
        }
        if self.exit_code != 0 {
            test.push_str(&format!("EXITCODE: {}\n", self.exit_code));
        }
        if let Some(filename) = &self.stdin_filename {
            if !filename.is_empty() {
                test.push_str(&format!(
                    "STDIN: {}\n",
                    &filename.display()
                ));
            }
        }
        if let Some(stdout) = &self.stdout {
            if !stdout.is_empty() {
                let mut stdout = stdout.to_string_lossy().into_owned();
                if stdout.starts_with(OUT_PATH) {
                    // Allow for / or \
                    stdout = stdout[OUT_PATH.len() + 1..].to_string();
                }
                test.push_str(&format!("STDOUT: {}\n", &stdout));
            }
        }
        if !abs_diff_eq!(self.wait, 0.0) {
            test.push_str(&format!("WAIT: {:.3}\n", self.wait));
        }
        test.push_str(&format!("APP: {}\n", &self.app.display()));
        for arg in &self.app_args {
            test.push_str(&format!("     {}\n", &arg));
        }
        if let Some(diff) = &self.diff {
            let text = match diff {
                DiffKind::No => "no".to_string(),
                DiffKind::Binary => "rt-binary".to_string(),
                DiffKind::Image => "rt-image".to_string(),
                DiffKind::Json => "rt-json".to_string(),
                DiffKind::Text => "rt-text".to_string(),
                DiffKind::Custom(name) => format!("{}", name.display()),
            };
            test.push_str(&format!("DIFF: {}\n", text));
            for arg in &self.diff_args {
                test.push_str(&format!("      {}\n", &arg));
            }
        }
        test
    }
}

impl fmt::Display for Test {
    fn fmt(&self, out: &mut fmt::Formatter) -> fmt::Result {
        let mut app = self.app.to_string_lossy().into_owned();
        if app.contains(' ') {
            app.insert(0, '"');
            app.push('"');
        }
        let mut args = Vec::with_capacity(self.app_args.len());
        for arg in &self.app_args {
            args.push(if arg.contains(' ') {
                format!("\"{}\"", arg)
            } else {
                arg.to_string()
            });
        }
        let name = if self.name.is_empty() {
            "".to_string()
        } else {
            format!("{}: ", &self.name)
        };
        write!(
            out,
            "{}{} {} -> {}",
            name,
            app,
            args.join(" "),
            self.exit_code
        )
    }
}