Skip to content

Commit

Permalink
Check for unknown file check directives and fix a broken test. (FuelL…
Browse files Browse the repository at this point in the history
…abs#2185)

* Check for unknown file check directives and fix a broken test.

This commit makes sure we emit an error when tests contain an unknown
FileCheck directives, to prevent introducing broken tests to the tree.

Previously we just ignored unknown directives.

Instead of passing the entire test text to FileCheck, we now parse
the test for directives and add it to the checker one-by-one, making
sure to deal with errors.

For a broken test, this now emits:

```
thread 'main' panicked at 'Discovering tests
should_fail/match_expressions_rest/test.toml: Failed to parse: Unknown
FileCheck directive: nextnl', test/src/e2e_vm_tests/mod.rs:45:9
```

Also fixes a non-working test originally introduced in
FuelLabs@2c5564b,
due to a simple typo.

* Simplify the code.

* Tighten the regex with multiline mode.

* Fix clippy.

Co-authored-by: Emily Herbert <[email protected]>
Co-authored-by: Toby Hutton <[email protected]>
  • Loading branch information
3 people authored Jul 2, 2022
1 parent 02c010c commit 850e0de
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
39 changes: 24 additions & 15 deletions test/src/e2e_vm_tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod harness;

use forc_util::init_tracing_subscriber;
use fuel_vm::prelude::*;
use regex::Regex;

use std::{
collections::HashMap,
Expand Down Expand Up @@ -218,22 +219,30 @@ fn discover_test_configs() -> Result<Vec<TestDescription>, String> {
Ok(configs)
}

const DIRECTIVE_RX: &str = r"(?m)^\s*#\s*(\w+):\s+(.*)$";

fn build_file_checker(content: &str) -> Result<filecheck::Checker, String> {
let mut checker = filecheck::CheckerBuilder::new();

// Parse the file and check for unknown FileCheck directives.
let re = Regex::new(DIRECTIVE_RX).unwrap();
for cap in re.captures_iter(content) {
if let Ok(false) = checker.directive(&cap[0]) {
return Err(format!("Unknown FileCheck directive: {}", &cap[1]));
}
}

Ok(checker.finish())
}

fn parse_test_toml(path: &Path) -> Result<TestDescription, String> {
let (toml_content, checker) = std::fs::read_to_string(path)
.map_err(|e| e.to_string())
.and_then(|toml_content_str| {
// Parse the file for FileCheck directives and_then parse the file into TOML.
filecheck::CheckerBuilder::new()
.text(&toml_content_str)
.map_err(|e| e.to_string())
.and_then(|checker| {
toml_content_str
.parse::<toml::Value>()
.map_err(|e| e.to_string())
.map(|toml_content| (toml_content, checker.finish()))
})
})
.map_err(|e| format!("Failed to parse: {e}"))?;
let toml_content_str = std::fs::read_to_string(path).map_err(|e| e.to_string())?;

let checker = build_file_checker(&toml_content_str)?;

let toml_content = toml_content_str
.parse::<toml::Value>()
.map_err(|e| e.to_string())?;

if !toml_content.is_table() {
return Err("Malformed test description.".to_owned());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
category = "fail"

# check: Point { .., x } => { x },
# nextnl: $()Unexpected rest token, must be at the end of pattern.
# nextln: $()Unexpected rest token, must be at the end of pattern.

0 comments on commit 850e0de

Please sign in to comment.