Skip to content

Commit

Permalink
Fix: Update command keeps detached tests
Browse files Browse the repository at this point in the history
Summary:
This CL delegates the detached exit status to enable the `scrut update` command to update test files that contain detached tests.

# What?

- The detached error status is forwarded
- Update generators receive one output per testcase (whether detached or not)
- Update creates all testcases (including detached)

# Why?

- Enable updating of tests files that have `detached: true` tests

Reviewed By: abesto

Differential Revision: D59751010

fbshipit-source-id: 8366f3e00f52d5309fe680d5879d06ac444c6f4c
  • Loading branch information
ukautz authored and facebook-github-bot committed Jul 15, 2024
1 parent f18d672 commit 61801f3
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 24 deletions.
26 changes: 26 additions & 0 deletions selftest/complex/update-detached/test.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Update tests that use detached

Tests in this file validate that `update` commands on test files that contain `detached: true` tests leaves them unchanged.

```scrut
$ alias scrut_update='$SCRUT_BIN update --match-markdown="*.mdtest"'
```

## Create backup to compar against later

```scrut
$ cp "$TESTDIR"/test.mdtest ./test-copy.mdtest
```

## Run update

```scrut
$ scrut_update --replace --assume-yes "$TESTDIR"/test.mdtest
Result: 1 file(s) of which 0 updated, 0 skipped and 1 unchanged
```

## File ought to be unchanged

```scrut
$ diff "$TESTDIR"/test.mdtest ./test-copy.mdtest
```
27 changes: 27 additions & 0 deletions selftest/complex/update-detached/test.mdtest
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# This is an outdated test

## A detached test

```scrut {detached: true}
$ echo foo1
```

## A normal test

```scrut
$ echo foo2
foo2
```

## Another detached test

```scrut {detached: true}
$ echo foo3
```

## Another normal test

```scrut
$ echo foo4
foo4
```
26 changes: 7 additions & 19 deletions src/bin/commands/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use scrut::config::TestCaseConfig;
use scrut::executors::context::ContextBuilder;
use scrut::executors::error::ExecutionError;
use scrut::outcome::Outcome;
use scrut::output::ExitStatus;
use scrut::parsers::markdown::DEFAULT_MARKDOWN_LANGUAGES;
use scrut::parsers::parser::ParserType;
use scrut::renderers::diff::DiffRenderer;
Expand Down Expand Up @@ -272,27 +273,14 @@ impl Args {
debug_testcases(&test.testcases, &test.path, &outputs);
}

let testcase_count = testcases.len();
let expected_testcases = testcases
.into_iter()
.filter(|t| !t.config.detached.unwrap_or(false))
.collect::<Vec<_>>();
count_detached += testcase_count - expected_testcases.len();

// this should not happen: different amount of outputs than executed testcases
if outputs.len() != expected_testcases.len() {
bail!(
"expected {} outputs from execution, but got {}",
expected_testcases.len(),
outputs.len()
)
}

// .. to compare the outputs with testcases and gather that
// outcome for later rendering
for (testcase, output) in
expected_testcases.into_iter().zip(outputs.into_iter())
{
for (testcase, output) in testcases.into_iter().zip(outputs.into_iter()) {
if output.exit_code == ExitStatus::Detached {
count_detached += 1;
continue;
}

let result = testcase.validate(&output);
if result.is_err() {
count_failed += 1;
Expand Down
10 changes: 5 additions & 5 deletions src/executors/stateful_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,17 +166,17 @@ impl Executor for StatefulExecutor {
}

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

// undefined: things are hairy, better end
ExitStatus::Unknown => {
outputs.push(output);
outputs.extend((0..(testcases.len() - outputs.len())).map(|_| Output {
stderr: vec![].into(),
stdout: vec![].into(),
exit_code: ExitStatus::Unknown,
..Default::default()
}));
break;
}
Expand Down

0 comments on commit 61801f3

Please sign in to comment.