Skip to content

Commit

Permalink
chore(remap): add integration testing crate
Browse files Browse the repository at this point in the history
Signed-off-by: Jean Mertz <[email protected]>
  • Loading branch information
JeanMertz committed Jan 14, 2021
1 parent 3a9f7e6 commit 1076a54
Show file tree
Hide file tree
Showing 6 changed files with 278 additions and 6 deletions.
100 changes: 96 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,10 @@ members = [
"lib/k8s-test-framework",
"lib/portpicker",
"lib/prometheus-parser",
"lib/vector-api-client",
"lib/remap-cli",
"lib/remap-lang",
"lib/remap-functions",
"lib/remap-lang",
"lib/remap-tests",
"lib/shared",
"lib/tracing-limit",
"lib/vector-api-client",
Expand Down
13 changes: 13 additions & 0 deletions lib/remap-tests/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "remap-tests"
version = "0.1.0"
authors = ["Vector Contributors <[email protected]>"]
edition = "2018"
publish = false

[dependencies]
ansi_term = "0.12"
prettydiff = "0.4"
remap = { package = "remap-lang", path = "../remap-lang" }
remap-functions = { path = "../remap-functions" }
serde_json = "1"
151 changes: 151 additions & 0 deletions lib/remap-tests/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
use ansi_term::Colour;
use remap::{prelude::*, Program, Runtime};
use std::fs;
use std::path::PathBuf;

fn main() {
let mut failed_count = 0;
let tests = fs::read_dir("tests").expect("dir exists");

for file in tests {
let mut test = Test::new(file.expect("file").path());

print!("{:.<30}", test.name);

if test.skip {
println!("{}", Colour::Yellow.bold().paint("SKIPPED"));
}

let state = state::Program::default();
let mut runtime = Runtime::new(state);
let program = Program::new(&test.source, &remap_functions::all(), None, true);
let want = test.result.to_string();

match program {
Ok(program) => {
let result = runtime.execute(&mut test.object, &program);

match result {
Ok(value) => {
let got = value.to_string();

if got == want {
println!("{}", Colour::Green.bold().paint("OK"));
} else {
println!("{} (expectation)", Colour::Red.bold().paint("FAILED"));

let diff =
prettydiff::diff_chars(&got, &want).set_highlight_whitespace(true);
println!(" {}", diff);

failed_count += 1;
}
}
Err(err) => {
let got = err.to_string();
if got == want {
println!("{}", Colour::Green.bold().paint("OK"));
} else {
println!("{} (runtime)", Colour::Red.bold().paint("FAILED"));
println!("{}", err);
}
}
}
}
Err(err) => {
let got = err.to_string().trim().to_owned();
let want = want.trim().to_owned();
if got == want {
println!("{}", Colour::Green.bold().paint("OK"));
} else {
println!("{} (compilation)", Colour::Red.bold().paint("FAILED"));

let diff = prettydiff::diff_chars(&got, &want).set_highlight_whitespace(true);
println!("{}", diff);
}
}
}
}

if failed_count > 0 {
std::process::exit(1)
}
}

#[derive(Debug)]
struct Test {
name: String,
source: String,
object: Value,
result: String,
skip: bool,
}

enum CaptureMode {
Result,
Object,
None,
}

impl Test {
fn new(path: PathBuf) -> Self {
let name = path
.to_string_lossy()
.strip_prefix("tests/")
.expect("test")
.to_owned();

let content = fs::read_to_string(path).expect("content");

let mut source = String::new();
let mut object = String::new();
let mut result = String::new();
let mut skip = false;

if content.starts_with("# SKIP") {
skip = true;
}

let mut capture_mode = CaptureMode::None;
for mut line in content.lines() {
if line.starts_with('#') {
line = line.strip_prefix('#').expect("prefix");
line = line.strip_prefix(' ').unwrap_or(line);

if line.starts_with("object:") {
capture_mode = CaptureMode::Object;
line = line.strip_prefix("object:").expect("object").trim_start();
} else if line.starts_with("result:") {
capture_mode = CaptureMode::Result;
line = line.strip_prefix("result:").expect("result").trim_start();
}

match capture_mode {
CaptureMode::None => continue,
CaptureMode::Result => {
result.push_str(line);
result.push('\n');
}
CaptureMode::Object => {
object.push_str(line);
}
}
} else {
source.push_str(line);
source.push('\n')
}
}

let object = serde_json::from_str::<'_, Value>(&object).expect("valid object");

result = result.trim_end().to_owned();

Self {
name,
source,
object,
result,
skip,
}
}
}
6 changes: 6 additions & 0 deletions lib/remap-tests/tests/example.vrl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# object: { "bar": "bar" }
# result: { "bar": "foo", "foo": "test" }

.foo = "test"
.bar = "foo"
.
11 changes: 11 additions & 0 deletions lib/remap-tests/tests/invalid_path.vrl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# object: { "bar": "bar" }
# result:
#
# remap error: parser error: --> 2:6
# |
# 2 | .foo.␊
# | ^---
# |
# = expected path segment

.foo.

0 comments on commit 1076a54

Please sign in to comment.