Skip to content

Commit

Permalink
Merge branch 'main' into features/dependencies-alias
Browse files Browse the repository at this point in the history
  • Loading branch information
lukfor authored Jan 28, 2024
2 parents 6c692d0 + ab4f138 commit edcd626
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 3 deletions.
8 changes: 8 additions & 0 deletions docs/docs/assertions/snapshots.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ You can also use helper methods to add objects to snapshots. For example, you ca
assert snapshot(workflow, path(params.outdir).list()).match()
```

## Compressed Snapshots

If you add complex objects to snapshots with large content, you could use the `md5()` function to store the hashsum instead of the content in the snapshot file:

```Groovy
assert snapshot(hugeObject).md5().match()
```

## File Paths

If nf-test detects a path in the snapshot it automatically replace it by a unique *fingerprint* of the file that ensures the file content is the same. The fingerprint is default the md5 sum.
Expand Down
11 changes: 8 additions & 3 deletions src/main/java/com/askimed/nf/test/lang/extensions/Snapshot.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.askimed.nf.test.lang.extensions;

import java.io.IOException;

import com.askimed.nf.test.core.ITest;
import com.askimed.nf.test.util.ObjectUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.askimed.nf.test.core.ITest;
import java.io.IOException;

public class Snapshot {

Expand All @@ -27,6 +27,11 @@ public boolean match() throws IOException {
return match(test.getName());
}

public Snapshot md5() {
actual = ObjectUtil.getMd5(actual);
return this;
}

public boolean match(String id) throws IOException {

//check if match with this id was already called. --> duplicate snapshots.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,5 @@ public String toString() {
return prettyJson;
}


}
41 changes: 41 additions & 0 deletions src/main/java/com/askimed/nf/test/util/ObjectUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.askimed.nf.test.util;

import com.askimed.nf.test.lang.extensions.SnapshotFile;
import groovy.json.JsonGenerator;
import groovy.json.JsonOutput;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class ObjectUtil {

public static String getMd5(Object object) {
JsonGenerator jsonGenerator = SnapshotFile.createJsonGenerator();
String json = jsonGenerator.toJson(object);
try {
return calculateMD5(JsonOutput.prettyPrint(json));
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}


public static String calculateMD5(String input) throws NoSuchAlgorithmException {
// Get an instance of the MD5 message digest algorithm
MessageDigest md = MessageDigest.getInstance("MD5");

// Update the digest with the input string's bytes
md.update(input.getBytes());

// Get the hash value as an array of bytes
byte[] digest = md.digest();

// Convert the byte array to a hexadecimal string
StringBuilder result = new StringBuilder();
for (byte b : digest) {
result.append(String.format("%02x", b));
}

return result.toString();
}
}
9 changes: 9 additions & 0 deletions src/test/java/com/askimed/nf/test/lang/ProcessTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -306,4 +306,13 @@ public void testNotUniquenessOfSnapshots() throws Exception {

}

@Test
public void testMd5Snapshots() throws Exception {

App app = new App();
int exitCode = app.run(new String[] { "test", "test-data/process/snapshots/md5.nf.test" });
assertEquals(0, exitCode);

}

}
36 changes: 36 additions & 0 deletions test-data/process/snapshots/md5.nf.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
nextflow_process {

name "Test process xy"

script "./process.nf"
process "TEST_PROCESS"

test("Should succeed because two unique snapshots") {

when {
process {
"""
input[0] = "Lukas"
"""
}
}

then {

def content = [
object1: "lukas",
object2: 27,
object3: [
a: "lll",
b: [1,2,3,4,5,6]
]
]

assert process.success
assert snapshot(content).match()
assert snapshot(content).md5().match("lukas")
}

}

}
26 changes: 26 additions & 0 deletions test-data/process/snapshots/md5.nf.test.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"Should succeed because two unique snapshots": {
"content": [
{
"object1": "lukas",
"object2": 27,
"object3": {
"a": "lll",
"b": [
1,
2,
3,
4,
5,
6
]
}
}
],
"timestamp": "2024-01-28T11:41:29.018819"
},
"lukas": {
"content": "a54d97026c5ed6e6a57ccd3a3f5a3cbc",
"timestamp": "2024-01-28T11:41:29.025981"
}
}

0 comments on commit edcd626

Please sign in to comment.