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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/*
 * 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::sync::Arc;

use anyhow::Context;
use anyhow::Result;
use anyhow::bail;
use regex::Regex;

use crate::config::TestCaseConfig;
use crate::expectation::Expectation;
use crate::expectation::ExpectationMaker;
use crate::testcase::TestCase;

lazy_static! {
    /// Exit code expression matches an output line of the form:
    ///
    /// ```bnf
    /// <exit-code-expression> ::= "[" <integer> "]"
    /// ```
    static ref EXIT_CODE_EXPRESSION: Regex =
        Regex::new("^\\[([0-9]+)\\]$").expect("exit code expression must compile");
}

pub(super) enum CodeType {
    CommandStart,
    CommandContinue,
    Expectation,
    ExitCode,
}

/// A meta parser engine, that can be used for any line-by-line test file format
/// which uses the standard Cram-like encoding of testcase bodies like:
///
/// ```txt
/// $ a-command \
/// > potential more lines
/// other lines are
/// all output
/// ```
pub(super) struct LineParser {
    pub(super) testcases: Vec<TestCase>,
    expectation_maker: Arc<ExpectationMaker>,
    title: Option<String>,
    command: Vec<String>,
    exit_code: Option<i32>,
    expectations: Vec<Expectation>,
    in_command: bool,
    allow_multiple_commands: bool,
    output_start_index: Option<usize>,
    config: Option<TestCaseConfig>,
}

impl LineParser {
    pub(super) fn new(
        expectation_maker: Arc<ExpectationMaker>,
        allow_multiple_commands: bool,
    ) -> Self {
        Self {
            expectation_maker,
            title: None,
            command: vec![],
            expectations: vec![],
            exit_code: None,
            testcases: vec![],
            in_command: false,
            allow_multiple_commands,
            output_start_index: None,
            config: None,
        }
    }

    /// Add a line that is either a command or an expectation
    pub(super) fn add_testcase_body(&mut self, line: &str, index: usize) -> Result<CodeType> {
        // start of command
        if self.allow_multiple_commands || self.command.is_empty() {
            if let Some(line) = line.strip_prefix("$ ") {
                self.in_command = true;
                if !self.command.is_empty() {
                    self.end_testcase(index)?;
                }
                if self.output_start_index.is_none() {
                    self.output_start_index = Some(index);
                }
                self.command.push(line.into());
                return Ok(CodeType::CommandStart);
            }
        }

        // continuation of command
        if self.in_command && (line == ">" || line.starts_with("> ")) {
            if self.command.is_empty() {
                bail!(
                    "line {}: command extender '>' requires previous command start '$' which is not given",
                    index + 1
                );
            }
            self.command
                .push(line.strip_prefix("> ").unwrap_or_default().into());
            return Ok(CodeType::CommandContinue);
        }

        self.in_command = false;
        if let Some(exit_code) = extract_exit_code(line) {
            if self.exit_code.is_some() {
                bail!("line {}: exit code provided multiple times", index + 1)
            }
            self.exit_code = Some(exit_code);
            return Ok(CodeType::ExitCode);
        }

        self.expectations.push(
            self.expectation_maker
                .parse(line)
                .with_context(|| format!("parsing line {}", index + 1))?,
        );
        Ok(CodeType::Expectation)
    }

    /// Add a line of title
    pub(super) fn set_testcase_title(&mut self, line: &str) {
        self.title = Some(line.to_string())
    }

    /// Add a line of title
    pub(super) fn set_testcase_config(&mut self, config: TestCaseConfig) {
        self.config = Some(config)
    }

    /// Signify end of currently processed testcase, which will test the
    /// validity of the testcase, add it to the stack and flush the state
    /// so that the next testcase(s) can be processed.
    pub(super) fn end_testcase(&mut self, line_index: usize) -> Result<()> {
        let (has_commands, has_expectations) =
            (!self.command.is_empty(), !self.expectations.is_empty());
        if !has_commands {
            if has_expectations {
                bail!(
                    "line {}: testcase output expectation(s) given, but no shell expression specified. Did you forget to prefix the command with '$'?",
                    line_index + 1
                )
            }
            return Ok(());
        }
        self.testcases.push(TestCase {
            title: self.title.to_owned().unwrap_or_default(),
            shell_expression: self.command.join("\n"),
            exit_code: self.exit_code,
            expectations: self.expectations.clone(),
            line_number: self.output_start_index.unwrap_or(line_index) + 1,
            config: self.config.clone().unwrap_or_default(),
        });
        self.flush();
        Ok(())
    }

    // whether shell expression(s) or expectation(s) are given
    pub(super) fn has_testcase_body(&self) -> bool {
        !self.command.is_empty() || !self.expectations.is_empty()
    }

    fn flush(&mut self) {
        self.title = None;
        self.command = vec![];
        self.expectations = vec![];
        self.exit_code = None;
        self.output_start_index = None;
        self.config = None;
    }
}

/// Parse a line of output for whether it contains an exit code of
/// the form `[<numeric code>]` and return the numeric value if it does
pub(super) fn extract_exit_code(line: &str) -> Option<i32> {
    // map. and then? map! and then?? map!!1!1!!!1 and ... then? ERRRR
    EXIT_CODE_EXPRESSION
        .captures(line)
        .and_then(|captures| {
            captures
                .iter()
                .nth(1)
                .and_then(|matching| matching.map(|matching| matching.as_str()))
        })
        .and_then(|s| s.parse::<i32>().ok())
}

/// Lines starting with "#" are considered comments
pub(super) fn is_comment(line: &str) -> bool {
    line.starts_with('#')
}

#[cfg(test)]
mod tests {
    use std::sync::Arc;

    use super::LineParser;
    use super::extract_exit_code;
    use crate::expectation::tests::expectation_maker;
    use crate::test_expectation;
    use crate::testcase::TestCase;

    fn engine(allow_multiple_commands: bool) -> LineParser {
        let maker = expectation_maker();
        LineParser::new(Arc::new(maker), allow_multiple_commands)
    }

    #[test]
    fn test_testcase_is_combined() {
        let mut engine = engine(false);
        engine.set_testcase_title("foo");
        engine.add_testcase_body("$ bar", 1).expect("add command");
        engine.add_testcase_body("baz", 2).expect("add expectation");
        engine.add_testcase_body("[5]", 3).expect("add expectation");
        engine.end_testcase(4).expect("testcase ending");
        assert_eq!(
            vec![TestCase {
                title: "foo".to_string(),
                exit_code: Some(5),
                expectations: vec![test_expectation!("equal", "baz"),],
                shell_expression: "bar".to_string(),
                line_number: 2,
                ..Default::default()
            },],
            engine.testcases,
        )
    }

    #[test]
    fn test_last_title_is_used() {
        let mut engine = engine(false);
        engine.set_testcase_title("foo1");
        engine.set_testcase_title("foo2");
        engine.set_testcase_title("foo3");
        engine.add_testcase_body("$ bar", 1).expect("add command");
        engine.add_testcase_body("baz", 2).expect("add expectation");
        engine.add_testcase_body("[5]", 3).expect("add expectation");
        engine.end_testcase(4).expect("testcase ending");
        assert_eq!(
            vec![TestCase {
                title: "foo3".to_string(),
                exit_code: Some(5),
                expectations: vec![test_expectation!("equal", "baz"),],
                shell_expression: "bar".to_string(),
                line_number: 2,
                ..Default::default()
            },],
            engine.testcases,
        )
    }

    #[test]
    fn test_command_is_combined() {
        let mut engine = engine(false);
        engine.set_testcase_title("foo");
        engine.add_testcase_body("$ bar1", 1).expect("add command");
        engine.add_testcase_body(">", 1).expect("add command");
        engine.add_testcase_body("> bar2", 1).expect("add command");
        engine.add_testcase_body("> bar3", 1).expect("add command");
        engine.add_testcase_body("baz", 2).expect("add expectation");
        engine.add_testcase_body("[5]", 3).expect("add expectation");
        engine.end_testcase(4).expect("testcase ending");
        assert_eq!(
            vec![TestCase {
                title: "foo".to_string(),
                exit_code: Some(5),
                expectations: vec![test_expectation!("equal", "baz"),],
                shell_expression: "bar1\n\nbar2\nbar3".to_string(),
                line_number: 2,
                ..Default::default()
            },],
            engine.testcases,
        )
    }

    #[test]
    fn test_expectations_are_stacked() {
        let mut engine = engine(false);
        engine.set_testcase_title("foo");
        engine.add_testcase_body("$ bar", 1).expect("add command");
        engine
            .add_testcase_body("baz1", 2)
            .expect("add expectation");
        engine
            .add_testcase_body("baz2", 3)
            .expect("add expectation");
        engine
            .add_testcase_body("baz3", 4)
            .expect("add expectation");
        engine.add_testcase_body("[5]", 5).expect("add expectation");
        engine.end_testcase(6).expect("testcase ending");
        assert_eq!(
            vec![TestCase {
                title: "foo".to_string(),
                exit_code: Some(5),
                expectations: vec![
                    test_expectation!("equal", "baz1"),
                    test_expectation!("equal", "baz2"),
                    test_expectation!("equal", "baz3"),
                ],
                shell_expression: "bar".to_string(),
                line_number: 2,
                ..Default::default()
            },],
            engine.testcases,
        )
    }

    #[test]
    fn test_multiple_commands_in_block() {
        let mut engine = engine(true);
        engine
            .add_testcase_body("$ foo1", 1)
            .expect("add 1st command");
        engine
            .add_testcase_body("$ foo2", 2)
            .expect("add 2nd command");
        engine
            .add_testcase_body("$ foo3", 3)
            .expect("add 3rd command");
        engine.end_testcase(4).expect("testcase ending");
        assert_eq!(
            vec![
                TestCase {
                    title: "".to_string(),
                    exit_code: None,
                    expectations: vec![],
                    shell_expression: "foo1".to_string(),
                    line_number: 2,
                    ..Default::default()
                },
                TestCase {
                    title: "".to_string(),
                    exit_code: None,
                    expectations: vec![],
                    shell_expression: "foo2".to_string(),
                    line_number: 3,
                    ..Default::default()
                },
                TestCase {
                    title: "".to_string(),
                    exit_code: None,
                    expectations: vec![],
                    shell_expression: "foo3".to_string(),
                    line_number: 4,
                    ..Default::default()
                }
            ],
            engine.testcases,
        )
    }

    #[test]
    fn test_single_command_in_block() {
        let mut engine = engine(false);
        engine
            .add_testcase_body("$ foo1", 1)
            .expect("add 1st command");
        engine
            .add_testcase_body("$ foo2", 2)
            .expect("add 2nd command");
        engine
            .add_testcase_body("$ foo3", 3)
            .expect("add 3rd command");
        engine.end_testcase(4).expect("testcase ending");
        assert_eq!(
            vec![TestCase {
                title: "".to_string(),
                exit_code: None,
                expectations: vec![
                    test_expectation!("equal", "$ foo2"),
                    test_expectation!("equal", "$ foo3"),
                ],
                shell_expression: "foo1".to_string(),
                line_number: 2,
                ..Default::default()
            },],
            engine.testcases,
        )
    }

    #[test]
    fn test_testcases_stack() {
        let mut engine = engine(false);
        engine.set_testcase_title("foo1");
        engine.add_testcase_body("$ bar1", 1).expect("add command1");
        engine
            .add_testcase_body("baz1", 2)
            .expect("add expectation1");
        engine.add_testcase_body("[1]", 3).expect("add exit code1");
        engine.end_testcase(10).expect("testcase ending1");
        engine.set_testcase_title("foo2");
        engine.add_testcase_body("$ bar2", 4).expect("add command2");
        engine
            .add_testcase_body("baz2", 5)
            .expect("add expectation2");
        engine.add_testcase_body("[2]", 6).expect("add exit code2");
        engine.end_testcase(10).expect("testcase ending2");
        engine.set_testcase_title("foo3");
        engine.add_testcase_body("$ bar3", 7).expect("add command3");
        engine
            .add_testcase_body("baz3", 8)
            .expect("add expectation3");
        engine.add_testcase_body("[3]", 9).expect("add exit code3");
        engine.end_testcase(10).expect("testcase ending3");
        assert_eq!(
            vec![
                TestCase {
                    title: "foo1".to_string(),
                    exit_code: Some(1),
                    expectations: vec![test_expectation!("equal", "baz1"),],
                    shell_expression: "bar1".to_string(),
                    line_number: 2,
                    ..Default::default()
                },
                TestCase {
                    title: "foo2".to_string(),
                    exit_code: Some(2),
                    expectations: vec![test_expectation!("equal", "baz2"),],
                    shell_expression: "bar2".to_string(),
                    line_number: 5,
                    ..Default::default()
                },
                TestCase {
                    title: "foo3".to_string(),
                    exit_code: Some(3),
                    expectations: vec![test_expectation!("equal", "baz3"),],
                    shell_expression: "bar3".to_string(),
                    line_number: 8,
                    ..Default::default()
                }
            ],
            engine.testcases,
        )
    }

    #[test]
    fn test_exit_code_provided_is_remembered() {
        for provided in [true, false] {
            let mut engine = engine(false);
            engine.set_testcase_title("foo1");
            engine.add_testcase_body("$ bar", 1).expect("add command");
            if provided {
                engine.add_testcase_body("[0]", 2).expect("add exit code");
            }
            engine.end_testcase(3).expect("testcase ending");
            assert_eq!(
                vec![TestCase {
                    title: "foo1".to_string(),
                    exit_code: if provided { Some(0) } else { None },
                    expectations: vec![],
                    shell_expression: "bar".to_string(),
                    line_number: 2,
                    ..Default::default()
                },],
                engine.testcases,
                "provided exit code {provided}",
            )
        }
    }

    #[test]
    fn test_extract_exit_code() {
        let tests: Vec<(&str, Option<i32>)> = vec![
            ("foo", None),
            ("[]", None),
            ("[0]", Some(0)),
            ("[1]", Some(1)),
            ("[99]", Some(99)),
            ("[a]", None),
        ];
        tests.iter().for_each(|(line, expect)| {
            let result = extract_exit_code(line);
            assert_eq!(*expect, result, "parsed '{}'", line);
        });
    }
}