From 77cad4218774c31d64cdf7f8d9bd4ceaedaffa59 Mon Sep 17 00:00:00 2001 From: Lukas Forer Date: Wed, 12 Jun 2024 17:24:58 +0200 Subject: [PATCH] Fix issues with paths in snapshots --- .../extensions/util/PathConverterRaw.java | 44 +++++++++++++++++++ .../com/askimed/nf/test/util/ObjectUtil.java | 5 ++- .../snapshot_transformer.nf.test.snap | 4 +- 3 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/askimed/nf/test/lang/extensions/util/PathConverterRaw.java diff --git a/src/main/java/com/askimed/nf/test/lang/extensions/util/PathConverterRaw.java b/src/main/java/com/askimed/nf/test/lang/extensions/util/PathConverterRaw.java new file mode 100644 index 00000000..84e7fbb4 --- /dev/null +++ b/src/main/java/com/askimed/nf/test/lang/extensions/util/PathConverterRaw.java @@ -0,0 +1,44 @@ +package com.askimed.nf.test.lang.extensions.util; + +import com.askimed.nf.test.lang.extensions.PathExtension; +import groovy.json.JsonGenerator.Converter; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Path; +import java.security.NoSuchAlgorithmException; +import java.util.List; +import java.util.Vector; + +public class PathConverterRaw implements Converter { + + @Override + public boolean handles(Class type) { + return true; + } + + @Override + public Object convert(Object value, String key) { + Path path = null; + if (value instanceof Path) { + path = (Path) value; + if (!path.toFile().exists()) { + throw new RuntimeException("Path " + path.toString() + " not found."); + } + } else if (value instanceof File) { + path = ((File) value).toPath(); + if (!path.toFile().exists()) { + throw new RuntimeException("Path " + path.toString() + " not found."); + } + } else { + path = new File(value.toString()).toPath(); + + } + + if (path.toFile().exists()) { + return path.toFile().getAbsolutePath(); + } + return value; + } + +} \ No newline at end of file diff --git a/src/main/java/com/askimed/nf/test/util/ObjectUtil.java b/src/main/java/com/askimed/nf/test/util/ObjectUtil.java index fc516f52..c64bf644 100644 --- a/src/main/java/com/askimed/nf/test/util/ObjectUtil.java +++ b/src/main/java/com/askimed/nf/test/util/ObjectUtil.java @@ -2,6 +2,7 @@ import com.askimed.nf.test.lang.extensions.SnapshotFile; import com.askimed.nf.test.lang.extensions.util.PathConverter; +import com.askimed.nf.test.lang.extensions.util.PathConverterRaw; import groovy.json.JsonGenerator; import groovy.json.JsonOutput; import groovy.json.JsonSlurper; @@ -60,10 +61,10 @@ public static JsonGenerator createJsonGenerator() { } public static JsonGenerator createJsonGeneratorRaw() { - return new JsonGenerator.Options().excludeFieldsByName("mapping").build(); + return new JsonGenerator.Options().excludeFieldsByName("mapping") + .addConverter(new PathConverterRaw()).build(); } - public static Object toMap(Object object) { JsonGenerator jsonGenerator = createJsonGeneratorRaw(); String json = jsonGenerator.toJson(object); diff --git a/test-data/snapshots/snapshot_transformer.nf.test.snap b/test-data/snapshots/snapshot_transformer.nf.test.snap index 2eb0f3da..aec87cc7 100644 --- a/test-data/snapshots/snapshot_transformer.nf.test.snap +++ b/test-data/snapshots/snapshot_transformer.nf.test.snap @@ -16,7 +16,7 @@ 8, 9 ], - "value": "1,36", + "value": "1.36", "files": [ [ "ID,Name,Quantity,Price", @@ -49,6 +49,6 @@ "nf-test": "0.9.0-rc2", "nextflow": "23.04.3" }, - "timestamp": "2024-06-11T16:48:22.553889" + "timestamp": "2024-06-12T17:08:37.459381" } } \ No newline at end of file