From 05a89cdfd16489d2eb2edb668bee45b9db225b34 Mon Sep 17 00:00:00 2001 From: Iwo Pieniak <44971105+ivopieniak@users.noreply.github.com> Date: Fri, 4 Nov 2022 18:23:27 +0000 Subject: [PATCH] Add YAML assertions (#52) Co-authored-by: i.pieniak --- docs/assertions/files.md | 19 +++++++++++++++++-- pom.xml | 6 ++++++ .../askimed/nf/test/core/AbstractTest.java | 6 +++--- .../test/lang/extensions/PathExtension.java | 9 +++++++++ 4 files changed, 35 insertions(+), 5 deletions(-) diff --git a/docs/assertions/files.md b/docs/assertions/files.md index bb2d82d1..fc3a4427 100644 --- a/docs/assertions/files.md +++ b/docs/assertions/files.md @@ -9,12 +9,27 @@ assert path(process.out.out_ch.get(0)).md5 == "64debea5017a035ddc67c0b51fa84b16" ``` ## JSON Files +nf-test supports comparison of JSON files and keys within JSON files. +To assert that two JSON files contain the same keys and values: +```Groovy +assert path(process.out.out_ch.get(0)).json == path('./some.json').json +``` +Individual keys can also be asserted: -nf-test extends `path` by a `json` property that can be used to read json files: +```Groovy +assert path(process.out.out_ch.get(0)).json.key == "value" +``` +## YAML Files +nf-test supports comparison of YAML files and keys within YAML files. +To assert that two YAML files contain the same keys and values: +```Groovy +assert path(process.out.out_ch.get(0)).yaml == path('./some.yaml').yaml +``` +Individual keys can also be asserted: ```Groovy -assert path(process.out.out_ch.get(0)).json.key == "value" +assert path(process.out.out_ch.get(0)).yaml.key == "value" ``` ## GZip Files diff --git a/pom.xml b/pom.xml index ecb0a6ed..7dd34640 100644 --- a/pom.xml +++ b/pom.xml @@ -47,6 +47,12 @@ 3.0.9 + + org.codehaus.groovy + groovy-yaml + 3.0.9 + + org.apache.ivy ivy diff --git a/src/main/java/com/askimed/nf/test/core/AbstractTest.java b/src/main/java/com/askimed/nf/test/core/AbstractTest.java index 7fc75279..a0fef4cf 100644 --- a/src/main/java/com/askimed/nf/test/core/AbstractTest.java +++ b/src/main/java/com/askimed/nf/test/core/AbstractTest.java @@ -26,13 +26,13 @@ public abstract class AbstractTest implements ITest { public static String[] SHARED_DIRECTORIES = { "bin", "lib" }; protected File config = null; - + private boolean updateSnapshot = false; - + public AbstractTest() { } - + public void config(String config) { this.config = new File(config); } diff --git a/src/main/java/com/askimed/nf/test/lang/extensions/PathExtension.java b/src/main/java/com/askimed/nf/test/lang/extensions/PathExtension.java index 6706aa27..e171698e 100644 --- a/src/main/java/com/askimed/nf/test/lang/extensions/PathExtension.java +++ b/src/main/java/com/askimed/nf/test/lang/extensions/PathExtension.java @@ -13,6 +13,8 @@ import com.askimed.nf.test.util.FileUtil; import groovy.json.JsonSlurper; +import groovy.yaml.YamlSlurper; + public class PathExtension { @@ -45,6 +47,13 @@ public static Object getJson(Path self) throws FileNotFoundException, IOExceptio return readJSON(self); } + /* YAML */ + + public static Object getYaml(Path self) throws FileNotFoundException, IOException { + YamlSlurper YamlSlurper = new YamlSlurper(); + return YamlSlurper.parse(self); + } + /* File methods */ public static Path[] list(Path self) {