scrut 0.4.3

A simple and powerful test framework for CLI applications
Documentation
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

use std::borrow::Cow;
use std::fmt::Display;
#[cfg(test)]
use std::time::Duration;

use serde::Serialize;
use serde::Serializer;
use serde::ser::SerializeMap;
use serde_json::Value;
use serde_json::json;

use crate::config::OutputStreamControl;
use crate::config::TestCaseConfig;
use crate::diff::Diff;
use crate::diff::DiffTool;
use crate::escaping::strip_colors_bytes;
use crate::expectation::Expectation;
use crate::newline::replace_crlf;
use crate::output::ExitStatus;
use crate::output::Output;

pub type Result<T> = anyhow::Result<T, TestCaseError>;

/// An aggregate that unifies all ingredients for a test: a title
/// of the expected and intended state of the world; what a specific
/// command line should output and why
#[derive(Clone, Debug, Default, PartialEq, Serialize)]
pub struct TestCase {
    /// A human readable description that clarifies the intention
    pub title: String,

    /// The valid shell expression that is to be executed
    pub shell_expression: String,

    /// The expectations that describe the output of the execution
    pub expectations: Vec<Expectation>,

    /// The expected exit code of the execution
    #[serde(serialize_with = "serialize_always_as_value")]
    pub exit_code: Option<i32>,

    /// The line number of this test in the original file (starting at 1)
    pub line_number: usize,

    /// Configuration that influences the behavior of this test-case
    #[serde(skip_serializing_if = "TestCaseConfig::is_empty")]
    pub config: TestCaseConfig,
}

impl TestCase {
    /// Validate that the outcome of an execution matches with the assumed
    /// outcome in regards to exit code and (STDOUT) output, or return an
    /// [`TestCaseError`]
    pub fn validate(&self, output: &Output) -> Result<()> {
        if let ExitStatus::Code(exit_code) = output.exit_code {
            let expected = self.exit_code.unwrap_or(0);
            if exit_code != expected {
                return Err(TestCaseError::InvalidExitCode {
                    actual: exit_code,
                    expected,
                });
            }
        }
        let diff_tool = DiffTool::new(self.expectations.clone());
        let stream = if self.config.output_stream == Some(OutputStreamControl::Stderr) {
            &output.stderr
        } else {
            &output.stdout
        };
        let diff = diff_tool
            .diff(stream.into())
            .map_err(TestCaseError::InternalError)?;
        if diff.has_differences() {
            Err(TestCaseError::MalformedOutput(diff))
        } else {
            Ok(())
        }
    }

    /// Returns output with configured transformations applied:
    /// - Remove CRLF?
    /// - Strip ANSI escaping?
    pub fn render_output<'a>(&self, output: &'a [u8]) -> anyhow::Result<Cow<'a, [u8]>> {
        let processed_output = if self.config.keep_crlf != Some(true) {
            replace_crlf(output)
        } else {
            Cow::Borrowed(output)
        };

        if self.config.strip_ansi_escaping == Some(true) {
            Ok(Cow::Owned(strip_colors_bytes(&processed_output)?))
        } else {
            Ok(processed_output)
        }
    }

    #[cfg(test)]
    pub fn from_expression(expression: &str) -> Self {
        Self {
            title: "Test".into(),
            shell_expression: expression.into(),
            ..Default::default()
        }
    }

    #[cfg(test)]
    pub fn from_expression_timed(expression: &str, timeout: Option<Duration>) -> Self {
        Self {
            title: "Test".into(),
            shell_expression: expression.into(),
            config: TestCaseConfig {
                timeout,
                ..Default::default()
            },
            ..Default::default()
        }
    }

    pub(crate) fn shell_expression_lines(&self) -> usize {
        self.shell_expression.matches('\n').count() + 1
    }

    pub(crate) fn expectations_lines(&self) -> usize {
        self.expectations.len()
    }
}

impl Display for TestCase {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let map = json!({
            "title": self.title.clone(),
            "shell_expression": self.shell_expression.clone(),
            "expectations": self.expectations
                .iter()
                .map(|e| Value::String(e.to_expression_string(&Default::default())))
                .collect::<Vec<_>>(),
            "exit_code": self.exit_code.unwrap_or(0),
            "line_number": self.line_number,
            "config": &self.config,
        });
        let out = serde_json::to_string(&map).map_err(|_| std::fmt::Error)?;
        write!(f, "{}", out)
    }
}

fn serialize_always_as_value<S>(x: &Option<i32>, s: S) -> std::result::Result<S::Ok, S::Error>
where
    S: Serializer,
{
    s.serialize_i32(x.unwrap_or(0))
}

/// An error that occurs when the actual output of an execution does not
/// match with the expectations.
///
/// There are four causes why an error can be raised:
/// 1) MalformedOutput: A line of output does not match the expected content or form
/// 2) UnexpectedOutput: There are more lines of output than there are
///    expectations to validate the output. Hence the additional output is
///    unexpected.
/// 3) InsufficientOutput: There are more expectation than there is output. That
///    means some of the expectations could never be applied and must be
///    considered failed (assuming they are non-optional)
/// 4) InternalError: An error occurred during processing, e.g. invalid UTF8
#[derive(Debug)]
pub enum TestCaseError {
    /// The validation of the expectation for the given line failed (invalid input)
    MalformedOutput(Diff),

    /// An execution ends in an unexpected exit code
    InvalidExitCode { actual: i32, expected: i32 },

    /// Delegated internal errors, e.g. relating to decoding
    InternalError(anyhow::Error),

    /// Test case timed out
    Timeout,

    /// Whether this test was skipped intentionally
    Skipped,
}

impl PartialEq for TestCaseError {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (Self::MalformedOutput(l0), Self::MalformedOutput(r0)) => l0 == r0,
            (
                Self::InvalidExitCode {
                    actual: l_actual,
                    expected: l_expected,
                },
                Self::InvalidExitCode {
                    actual: r_actual,
                    expected: r_expected,
                },
            ) => l_actual == r_actual && l_expected == r_expected,
            (Self::InternalError(l0), Self::InternalError(r0)) => l0.to_string() == r0.to_string(),
            (_, _) => false,
        }
    }
}

impl Serialize for TestCaseError {
    fn serialize<S>(&self, serializer: S) -> anyhow::Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        match self {
            Self::MalformedOutput(diff) => {
                let mut variant = serializer.serialize_map(Some(2))?;
                variant.serialize_entry("kind", "malformed_output")?;
                variant.serialize_entry("diff", &diff.lines)?;
                variant.end()
            }
            Self::InvalidExitCode { actual, expected } => {
                let mut variant = serializer.serialize_map(Some(3))?;
                variant.serialize_entry("kind", "invalid_exit_code")?;
                variant.serialize_entry("actual", actual)?;
                variant.serialize_entry("expected", expected)?;
                variant.end()
            }
            Self::InternalError(err) => {
                let mut variant = serializer.serialize_map(Some(2))?;
                variant.serialize_entry("kind", "internal_error")?;
                variant.serialize_entry("error", &format!("{}", err))?;
                variant.end()
            }
            Self::Timeout => {
                let mut variant = serializer.serialize_map(Some(1))?;
                variant.serialize_entry("kind", "timeout")?;
                variant.end()
            }
            Self::Skipped => {
                let mut variant = serializer.serialize_map(Some(1))?;
                variant.serialize_entry("kind", "skipped")?;
                variant.end()
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::TestCase;
    use super::TestCaseError;
    use crate::config::TestCaseConfig;
    use crate::diff::Diff;
    use crate::diff::DiffLine;
    use crate::lossy_string;
    use crate::test_expectation;

    #[test]
    fn test_validate_succeeds_on_valid() {
        let testcase = TestCase {
            title: "an testcase".to_string(),
            shell_expression: "a command".to_string(),
            expectations: vec![test_expectation!("no-eol", "the stdout")],
            exit_code: Some(123),
            line_number: 234,
            ..Default::default()
        };
        testcase
            .validate(&("the stdout", "the stderr", Some(123)).into())
            .expect("no error");
    }

    #[test]
    fn test_validate_fails_on_invalid_exit_code() {
        let testcase = TestCase {
            title: "an testcase".to_string(),
            shell_expression: "a command".to_string(),
            expectations: vec![test_expectation!("no-eol", "the stdout", false, false)],
            exit_code: Some(234),
            line_number: 123,
            ..Default::default()
        };
        let asserted_output = ("the stdout", "the stderr", Some(123)).into();
        let result = testcase.validate(&asserted_output);
        match result {
            Ok(_) => panic!("assertion should have failed"),
            Err(err) => match err {
                TestCaseError::InvalidExitCode { actual, expected } => {
                    assert_eq!(
                        asserted_output.exit_code.as_code(),
                        actual,
                        "asserted output is delegated"
                    );
                    assert_eq!(234, expected, "expected exit code is delegated");
                }
                _ => panic!("unexpected error: {:?}", err),
            },
        }
    }

    #[test]
    fn test_validate_fails_on_malformed_output() {
        let testcase = TestCase {
            title: "an testcase".to_string(),
            shell_expression: "a command".to_string(),
            expectations: vec![test_expectation!(
                "no-eol",
                "something not matching",
                false,
                false
            )],
            exit_code: Some(123),
            line_number: 234,
            ..Default::default()
        };
        let asserted_output = ("the stdout", "the stderr", Some(123)).into();
        let result = testcase.validate(&asserted_output);
        match result {
            Ok(_) => panic!("assertion should have failed"),
            Err(err) => {
                assert_eq!(
                    TestCaseError::MalformedOutput(Diff::new(vec![
                        DiffLine::UnmatchedExpectation {
                            index: 0,
                            expectation: testcase.expectations[0].clone()
                        },
                        DiffLine::UnexpectedLines {
                            lines: vec![(0, b"the stdout".to_vec())]
                        },
                    ])),
                    err,
                    "expected exit code is delegated"
                );
            }
        }
    }

    #[test]
    fn test_render_output_crlf_support() {
        let tests = &[
            (false, "foo", "foo"),
            (true, "foo", "foo"),
            (false, "foo\nbar\nbaz", "foo\nbar\nbaz"),
            (true, "foo\nbar\nbaz", "foo\nbar\nbaz"),
            (false, "foo\r\nbar\r\nbaz", "foo\nbar\nbaz"),
            (true, "foo\r\nbar\r\nbaz", "foo\r\nbar\r\nbaz"),
        ];
        for (crlf_support, from, expect) in tests {
            let tc = TestCase {
                title: "an testcase".to_string(),
                shell_expression: "a command".to_string(),
                expectations: vec![test_expectation!("no-eol", "the stdout")],
                exit_code: Some(123),
                line_number: 234,
                config: TestCaseConfig {
                    keep_crlf: Some(*crlf_support),
                    ..Default::default()
                },
            };
            let output = tc
                .render_output(from.as_bytes())
                .expect("rendering should succeed");
            assert_eq!(
                *expect,
                lossy_string!(&output),
                "from {} (crlf = {})",
                *from,
                *crlf_support
            );
        }
    }

    #[test]
    fn test_render_output_strip_ansi_escaping() {
        let tests = &[
            (false, "foo", "foo"),
            (true, "foo", "foo"),
            (false, "foo\nbar\nbaz", "foo\nbar\nbaz"),
            (true, "foo\nbar\nbaz", "foo\nbar\nbaz"),
            (
                false,
                "foo\n\x1b[1mbar\x1b[0m\nbaz",
                "foo\n\x1b[1mbar\x1b[0m\nbaz",
            ),
            (true, "foo\n\x1b[1mbar\x1b[0m\nbaz", "foo\nbar\nbaz"),
        ];
        for (strip_ansi_escaping, from, expect) in tests {
            let tc = TestCase {
                title: "an testcase".to_string(),
                shell_expression: "a command".to_string(),
                expectations: vec![test_expectation!("no-eol", "the stdout")],
                exit_code: Some(123),
                line_number: 234,
                config: TestCaseConfig {
                    strip_ansi_escaping: Some(*strip_ansi_escaping),
                    ..Default::default()
                },
            };
            let output = tc
                .render_output(from.as_bytes())
                .expect("rendering should succeed");
            assert_eq!(
                *expect,
                lossy_string!(&output),
                "from {} (strip = {})",
                *from,
                *strip_ansi_escaping
            );
        }
    }
}