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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
/*
 * 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::ops::Add;
use std::path::Path;
use std::thread::sleep;
use std::time::Duration;
use std::time::Instant;

use anyhow::Context;
use tempfile::TempDir;
use tracing::debug;
use tracing::trace;
use tracing::trace_span;

use super::context::Context as ExecutionContext;
use super::error::ExecutionError;
use super::executor::DEFAULT_TOTAL_TIMEOUT;
use super::executor::Executor;
use super::executor::Result;
use super::runner::Runner;
use crate::executors::error::ExecutionTimeout;
use crate::output::ExitStatus;
use crate::output::Output;
use crate::testcase::TestCase;

/// A generator that creates a new instance of a [`super::runner::Runner`] that is provided with a
/// shared directory.
pub type StatefulExecutorRunnerGenerator = Box<dyn Fn(&Path) -> Box<dyn Runner>>;

/// An executor that maintains state between the executions by providing the [`Runner`] with a
/// shared directory, which it then can use to store and restore state (e.g.
/// [`super::bash_runner::BashRunner`]).
///
/// Each execution runs in a new shell instance and is provided a shared directory that can be used
/// to share state between the executions.
///
/// The executors supports both timeouts per executions and timeouts over all executions.
pub struct StatefulExecutor(StatefulExecutorRunnerGenerator);

/// A dataset to differentiate between occurance of global and per-execution timeout
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
struct Timeout {
    is_global: bool,
    timeout: Duration,
}

impl StatefulExecutor {
    pub fn new(generator: StatefulExecutorRunnerGenerator) -> Self {
        Self(generator)
    }
}

impl Executor for StatefulExecutor {
    /// Run all Executions in given order. Timeout over all Executions is supported. Timeout per
    /// Execution is not.
    fn execute_all(
        &self,
        testcases: &[&TestCase],
        context: &ExecutionContext,
    ) -> Result<Vec<Output>> {
        // a temporary directory, that will be used to copy state in between the executions
        let state_directory = TempDir::with_prefix_in(".state.", &context.temp_directory)
            .context("generate temporary output directory")
            .map_err(|err| ExecutionError::aborted(err, None))?;

        // prepare "global" timeout, if there is any
        let timeout_duration = context
            .config
            .total_timeout
            .unwrap_or(*DEFAULT_TOTAL_TIMEOUT);
        let timeout_at = if timeout_duration.is_zero() {
            None
        } else {
            Some(Instant::now().add(timeout_duration))
        };
        let timeout_left = || timeout_at.map(|at| at.duration_since(Instant::now()));
        let runner_gen = &self.0;

        // iterate all executions and run them in a bash process, then run
        // the next execution using the state of the previous
        let mut outputs = vec![];
        for (index, testcase) in testcases.iter().enumerate() {
            let name = format!("exec{}", index + 1);
            let mut testcase = (*testcase).clone();

            // apply document-wide testcase defaults
            testcase.config = testcase.config.with_defaults_from(&context.config.defaults);

            // timeout is whatever the lowest provided value of:
            // - global (over all executions) timeout
            // - local (per execution) timeout
            let (is_global_timeout, timeout) = vec![
                testcase.config.timeout.map(|d| Timeout {
                    is_global: false,
                    timeout: d,
                }),
                timeout_left().map(|d| Timeout {
                    is_global: true,
                    timeout: d,
                }),
            ]
            .into_iter()
            .filter(|item| item.is_some())
            .min()
            .unwrap_or_default()
            .map_or((false, None), |t| (t.is_global, Some(t.timeout)));
            let span = trace_span!("execution", expression = &testcase.shell_expression, timeout = ?&timeout);
            let _enter = span.enter();

            // waiting on previous execution
            if let Some(ref wait) = testcase.config.wait {
                debug!("waiting {}", wait);
                if let Some(ref path) = wait.path {
                    wait_until_path_or_time(&context.temp_directory.join(path), wait.timeout)
                } else {
                    sleep(wait.timeout);
                }
            }

            // set timeout and identifying environment variable
            testcase.config.timeout = timeout;
            testcase.config.environment.insert(
                "SCRUT_TEST".into(),
                format!(
                    "{}:{}",
                    context.file.to_string_lossy(),
                    testcase.line_number
                ),
            );

            // run the execution, using the shared state directory
            let context = context.to_owned();

            trace!("effective testcase configuration: {}", &testcase.config);
            let mut output = runner_gen(state_directory.path())
                .run(&name, &testcase, context)
                .map_err(|err| ExecutionError::failed(index, err))?;
            trace!("{output:?}");

            // handle exit code
            let skip_document_code = testcase.config.get_skip_document_code();
            match output.exit_code {
                // having an actual numeric exit code ..
                ExitStatus::Code(code) => {
                    // .. ends collecting if user signals to skip
                    if code == skip_document_code {
                        return Err(ExecutionError::Skipped(index));
                    }

                    // .. otherwise keep collecting output
                    outputs.push(output);

                    // check if fail_fast is enabled and validation fails
                    if testcase.config.get_fail_fast() {
                        if testcase.validate(outputs.last().unwrap()).is_err() {
                            return Err(ExecutionError::Failed(index, outputs));
                        }
                    }
                }

                // running into a timeout ends all execution ..
                ExitStatus::Timeout(_) => {
                    // .. of the whole context? (global, timeout over all executions)
                    if is_global_timeout {
                        // ensure the original timeout is set here, because the global timeout is
                        // per all tests in one document and the per-testcase-set timeout is the
                        // remaining timeout, not the total.
                        output.exit_code = ExitStatus::Timeout(timeout_duration);
                        outputs.push(output);
                        return Err(ExecutionError::Timeout(ExecutionTimeout::Total, outputs));
                    }

                    // .. or of only this particular execution
                    outputs.push(output);
                    return Err(ExecutionError::Timeout(
                        ExecutionTimeout::Index(index),
                        outputs,
                    ));
                }

                // user triggered skip ends all execution
                ExitStatus::Skipped => {
                    return Err(ExecutionError::Skipped(index));
                }

                // user says the process is running detached and we should ignore it
                ExitStatus::Detached => outputs.push(Output {
                    exit_code: ExitStatus::Detached,
                    detached_process: output.detached_process,
                    ..Default::default()
                }),

                // undefined: things are hairy, better end
                ExitStatus::Unknown => {
                    outputs.push(output);
                    outputs.extend((0..(testcases.len() - outputs.len())).map(|_| Output {
                        exit_code: ExitStatus::Unknown,
                        ..Default::default()
                    }));
                    break;
                }
            }
        }

        Ok(outputs)
    }
}

fn wait_until_path_or_time(path: &Path, timeout: Duration) {
    let end = Instant::now().add(timeout);
    while end > Instant::now() {
        if path.exists() {
            return;
        }
        sleep(Duration::from_millis(50));
    }
}

#[cfg(test)]
mod tests {
    use std::time::Duration;

    use regex::Regex;

    use super::StatefulExecutor;
    use crate::executors::DEFAULT_SHELL;
    use crate::executors::bash_runner::BashRunner;
    use crate::executors::error::ExecutionError;
    use crate::executors::error::ExecutionTimeout;
    use crate::executors::executor::tests::combined_output_test_suite;
    use crate::executors::executor::tests::run_executor_tests;
    use crate::executors::executor::tests::standard_output_test_suite;
    use crate::output::ExitStatus;
    use crate::output::Output;
    use crate::testcase::TestCase;

    #[test]
    fn test_standard_test_suite() {
        standard_output_test_suite(StatefulExecutor(BashRunner::stateful_generator(
            *DEFAULT_SHELL,
        )));
    }

    #[test]
    fn test_combined_output_test_suite() {
        combined_output_test_suite(StatefulExecutor(BashRunner::stateful_generator(
            *DEFAULT_SHELL,
        )));
    }

    #[test]
    fn test_executor_respects_timeout() {
        // CAREFUL: Do not reduce sleep and timeout values! Windows execution
        // comes with a significatn overhead in startup time. These values
        // are chosen to ensure the tests do not flake in Windows.
        let tests = vec![
            (
                "Execution aborted when single test-case execution time exceeds per-document timeout",
                vec![
                    TestCase::from_expression("sleep 1.0 && echo OK1"),
                    TestCase::from_expression("sleep 1.0 && echo OK2"),
                    TestCase::from_expression("sleep 1.0 && echo OK3"),
                ],
                Some(Duration::from_millis(150)),
                Err(ExecutionError::Timeout(
                    ExecutionTimeout::Total,
                    vec![Output {
                        exit_code: ExitStatus::Timeout(Duration::from_millis(150)),
                        ..Default::default()
                    }],
                )),
            ),
            (
                "Execution aborted when test-case execution time exceeds per-testcase timeout",
                vec![
                    TestCase::from_expression("sleep 0.5 && echo OK1"),
                    TestCase::from_expression_timed(
                        "sleep 0.5 && echo OK2",
                        Some(Duration::from_millis(300)),
                    ),
                    TestCase::from_expression("sleep 0.5 && echo OK3"),
                ],
                Some(Duration::from_millis(1200)),
                Err(ExecutionError::Timeout(
                    ExecutionTimeout::Index(1),
                    vec![
                        Output {
                            exit_code: ExitStatus::SUCCESS,
                            stdout: "OK1\n".into(),
                            ..Default::default()
                        },
                        Output {
                            exit_code: ExitStatus::Timeout(Duration::from_millis(300)),
                            ..Default::default()
                        },
                    ],
                )),
            ),
            (
                "Execution aborted when cumulative test-case execution time exceeds per-document timeout",
                vec![
                    TestCase::from_expression("sleep 1 && echo OK1"),
                    TestCase::from_expression("sleep 1 && echo OK2"),
                    TestCase::from_expression("sleep 1 && echo OK3"),
                ],
                Some(Duration::from_millis(1500)),
                Err(ExecutionError::Timeout(
                    ExecutionTimeout::Total,
                    vec![
                        Output {
                            exit_code: ExitStatus::SUCCESS,
                            stdout: "OK1\n".into(),
                            ..Default::default()
                        },
                        Output {
                            exit_code: ExitStatus::Timeout(Duration::from_millis(1500)),
                            ..Default::default()
                        },
                    ],
                )),
            ),
            (
                "Execution not aborted when no timeout is triggered",
                vec![
                    TestCase::from_expression("sleep 0.1 && echo OK1"),
                    TestCase::from_expression("sleep 0.1 && echo OK2"),
                    TestCase::from_expression("sleep 0.1 && echo OK3"),
                ],
                Some(Duration::from_secs(2)),
                Ok(vec![
                    ("OK1\n", "").into(),
                    ("OK2\n", "").into(),
                    ("OK3\n", "").into(),
                ]),
            ),
        ];

        run_executor_tests(
            StatefulExecutor(BashRunner::stateful_generator(*DEFAULT_SHELL)),
            tests,
        );
    }

    #[test]
    fn test_supports_timeout_per_execution() {
        let tests = vec![
            (
                "Sufficient timeout has no effect",
                vec![TestCase::from_expression_timed(
                    "sleep 0.1 && echo OK1",
                    Some(Duration::from_millis(2000)),
                )],
                None,
                Ok(vec![("OK1\n", "").into()]),
            ),
            (
                "Insufficient timeout aborts execution",
                vec![TestCase::from_expression_timed(
                    "sleep 0.2 && echo OK1",
                    Some(Duration::from_millis(50)),
                )],
                None,
                Err(ExecutionError::Timeout(
                    ExecutionTimeout::Index(0),
                    vec![Output {
                        exit_code: ExitStatus::Timeout(Duration::from_millis(50)),
                        ..Default::default()
                    }],
                )),
            ),
            (
                "Timeout affects execution in isolation",
                vec![
                    TestCase::from_expression_timed(
                        "sleep 0.1 && echo OK1",
                        Some(Duration::from_millis(2000)),
                    ),
                    TestCase::from_expression_timed(
                        "sleep 0.1 && echo OK2",
                        Some(Duration::from_millis(10)),
                    ),
                    TestCase::from_expression_timed(
                        "sleep 0.1 && echo OK3",
                        Some(Duration::from_millis(10)),
                    ),
                    TestCase::from_expression_timed(
                        "sleep 0.1 && echo OK4",
                        Some(Duration::from_millis(2000)),
                    ),
                ],
                None,
                Err(ExecutionError::Timeout(
                    ExecutionTimeout::Index(1),
                    vec![
                        Output {
                            exit_code: ExitStatus::SUCCESS,
                            stdout: "OK1\n".into(),
                            ..Default::default()
                        },
                        Output {
                            exit_code: ExitStatus::Timeout(Duration::from_millis(10)),
                            ..Default::default()
                        },
                    ],
                )),
            ),
        ];

        run_executor_tests(
            StatefulExecutor(BashRunner::stateful_generator(*DEFAULT_SHELL)),
            tests,
        );
    }

    #[test]
    fn test_skipped_test_returns_skipped_error() {
        let tests = vec![(
            "Skip ends execution",
            vec![
                TestCase::from_expression("echo OK1"),
                TestCase::from_expression("exit 80"),
                TestCase::from_expression("echo OK2"),
            ],
            None,
            Err(ExecutionError::Skipped(1)),
        )];

        run_executor_tests(
            StatefulExecutor(BashRunner::stateful_generator(*DEFAULT_SHELL)),
            tests,
        );
    }

    #[test]
    fn test_executor_keeps_state() {
        let tests = vec![
            (
                "Environment variable persists",
                vec![
                    TestCase::from_expression("export FOO=bar"),
                    TestCase::from_expression("echo FOO=${FOO:-undefined}"),
                    TestCase::from_expression("unset FOO"),
                    TestCase::from_expression("echo FOO=${FOO:-undefined}"),
                ],
                None,
                Ok(vec![
                    ("", "").into(),
                    ("FOO=bar\n", "").into(),
                    ("", "").into(),
                    ("FOO=undefined\n", "").into(),
                ]),
            ),
            (
                "Shell variable persists",
                vec![
                    TestCase::from_expression("BAR=foo"),
                    TestCase::from_expression("echo BAR=${BAR:-undefined}"),
                    TestCase::from_expression("unset BAR"),
                    TestCase::from_expression("echo BAR=${BAR:-undefined}"),
                ],
                None,
                Ok(vec![
                    ("", "").into(),
                    ("BAR=foo\n", "").into(),
                    ("", "").into(),
                    ("BAR=undefined\n", "").into(),
                ]),
            ),
            (
                "Alias persists",
                vec![
                    TestCase::from_expression("alias foo='echo BAR'"),
                    TestCase::from_expression("foo"),
                    TestCase::from_expression("unalias foo"),
                    TestCase::from_expression("foo"),
                ],
                None,
                Ok(vec![
                    ("", "").into(),
                    ("BAR\n", "").into(),
                    ("", "").into(),
                    (
                        None,
                        Some(
                            Regex::new(": line \\d+: foo: command not found")
                                .expect("compile command not found regex"),
                        ),
                        Some(ExitStatus::Code(127)),
                    )
                        .into(),
                ]),
            ),
        ];

        run_executor_tests(
            StatefulExecutor(BashRunner::stateful_generator(*DEFAULT_SHELL)),
            tests,
        );
    }

    #[test]
    fn test_non_printable_ascii_in_output() {
        let tests = vec![(
            "Skip ends execution",
            vec![
                TestCase::from_expression("echo \"😊🦀\""),
                TestCase::from_expression("echo -e \"A\r\nB\""),
                TestCase::from_expression("echo \"🦀😊\" >&2"),
            ],
            None,
            Ok(vec![
                ("😊🦀\n", "").into(),
                ("A\nB\n", "").into(),
                ("", "🦀😊\n").into(),
            ]),
        )];

        run_executor_tests(
            StatefulExecutor(BashRunner::stateful_generator(*DEFAULT_SHELL)),
            tests,
        );
    }
}