Skip to content

Commit

Permalink
Create mock.nf in project folder instead of meta folder (#198)
Browse files Browse the repository at this point in the history
* Add mock file in project folder to preserve `projectDir`

* Add missing testcase

* Add missing params file for regex testcase

* Small refactoring
  • Loading branch information
lukfor authored May 14, 2024
1 parent c2734d9 commit 8172c5d
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 80 deletions.
73 changes: 13 additions & 60 deletions src/main/java/com/askimed/nf/test/core/AbstractTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@

import java.io.File;
import java.io.IOException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Vector;

import com.askimed.nf.test.util.HashUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.askimed.nf.test.config.Config;
import com.askimed.nf.test.config.FileStaging;
import com.askimed.nf.test.util.FileUtil;

public abstract class AbstractTest implements ITest {
Expand Down Expand Up @@ -57,6 +54,8 @@ public abstract class AbstractTest implements ITest {

public File projectDir = new File(System.getProperty("user.dir"));

public File mockFile;

public boolean skipped = false;

protected File config = null;
Expand Down Expand Up @@ -106,33 +105,16 @@ public void defineDirectories(File testDirectory) throws IOException {
outputDir = constructDirectory(launchDir, DIRECTORY_OUTPUT);
workDir = constructDirectory(launchDir, DIRECTORY_WORK);

mockFile = new File( ".nf-test-" + getHash() + ".nf");
mockFile.deleteOnExit();
}

@Override
public void setup(Config config) throws IOException {

initDirectory("Launch Directory", launchDir);
initDirectory("Meta Directory", metaDir);
initDirectory("Output Directory", outputDir);
initDirectory("Working Directory", workDir);
FileStaging[] sharedDirectories = new FileStaging[]{
new FileStaging("bin", config != null ? config.getStageMode() : FileStaging.MODE_COPY),
new FileStaging("lib", config != null ? config.getStageMode() : FileStaging.MODE_COPY),
new FileStaging("assets", config != null ? config.getStageMode() : FileStaging.MODE_COPY)
};
try {
// copy bin, assets and lib to metaDir
shareDirectories(sharedDirectories, metaDir);
if (config != null) {
// copy user defined staging directories
log.debug("Stage {} user provided files...", config.getStageBuilder().getPaths().size());
shareDirectories(config.getStageBuilder().getPaths(), metaDir);
}
shareDirectories(parent.getStageBuilder().getPaths(), metaDir);
} catch (Exception e) {
throw new IOException("Testcase setup failed: Directories could not be shared:\n" + e);
}

setupDirectory("Launch Directory", launchDir);
setupDirectory("Meta Directory", metaDir);
setupDirectory("Output Directory", outputDir);
setupDirectory("Working Directory", workDir);
}

@Override
Expand All @@ -145,13 +127,13 @@ public void execute() throws Throwable {
}
}

public File constructDirectory(File root, String... childs) {
private File constructDirectory(File root, String... childs) {
String path = FileUtil.path(root.getAbsolutePath(), FileUtil.path(childs));
File directory = new File(path).getAbsoluteFile();
return directory;
}

public void initDirectory(String name, File directory) throws IOException {
private void setupDirectory(String name, File directory) throws IOException {

try {
FileUtil.deleteDirectory(directory);
Expand All @@ -164,7 +146,7 @@ public void initDirectory(String name, File directory) throws IOException {

@Override
public void cleanup() {
// FileUtil.deleteDirectory(metaDir);

}

@Override
Expand All @@ -191,25 +173,10 @@ public String getHash() {
throw new RuntimeException("Error generating hash");
}

return hash(parent.getFilename() + getName());
return HashUtil.getMd5(parent.getFilename() + getName());

}

private String hash(String value) {

MessageDigest md;
try {
md = MessageDigest.getInstance("MD5");
md.update(value.getBytes());
byte[] md5sum = md.digest();
BigInteger bigInt = new BigInteger(1, md5sum);
return bigInt.toString(16);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return "??";
}
}

public void tag(String tag) {
tags.add(tag);
}
Expand Down Expand Up @@ -268,20 +235,6 @@ public boolean isWithTrace() {
return withTrace;
}

protected void shareDirectories(List<FileStaging> directories, File stageDir) throws IOException {
for (FileStaging directory : directories) {
String metaDirectory = FileUtil.path(stageDir.getAbsolutePath(), directory.getPath());
directory.stage(metaDirectory);
}
}

protected void shareDirectories(FileStaging[] directories, File stageDir) throws IOException {
for (FileStaging directory : directories) {
String metaDirectory = FileUtil.path(stageDir.getAbsolutePath(), directory.getPath());
directory.stage(metaDirectory);
}
}

@Override
public void setUpdateSnapshot(boolean updateSnapshot) {
this.updateSnapshot = updateSnapshot;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,10 @@ public void execute() throws Throwable {
}

// Create workflow mock
File workflow = new File(metaDir, FILE_MOCK);
writeWorkflowMock(workflow);
writeWorkflowMock(mockFile);

// Copy mock file in meta folder for debugging
FileUtil.copy(mockFile, new File(metaDir, FILE_MOCK));

context.getParams().put("nf_test_output", metaDir.getAbsolutePath());

Expand All @@ -121,7 +123,7 @@ public void execute() throws Throwable {
File paramsFile = new File(metaDir, FILE_PARAMS);

NextflowCommand nextflow = new NextflowCommand();
nextflow.setScript(workflow.getAbsolutePath());
nextflow.setScript(mockFile.getAbsolutePath());
nextflow.setParams(context.getParams());
for (String profile: parent.getProfiles()) {
nextflow.addProfile(profile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,10 @@ public void execute() throws Throwable {
context.evaluateProcessClosure();

// Create workflow mock
File workflow = new File(metaDir, FILE_MOCK);
writeWorkflowMock(workflow);
writeWorkflowMock(mockFile);

// Copy mock file in meta folder for debugging
FileUtil.copy(mockFile, new File(metaDir, FILE_MOCK));

context.getParams().put("nf_test_output", metaDir.getAbsolutePath());

Expand All @@ -124,7 +126,7 @@ public void execute() throws Throwable {
File paramsFile = new File(metaDir, FILE_PARAMS);

NextflowCommand nextflow = new NextflowCommand();
nextflow.setScript(workflow.getAbsolutePath());
nextflow.setScript(mockFile.getAbsolutePath());
nextflow.setParams(context.getParams());
for (String profile: parent.getProfiles()) {
nextflow.addProfile(profile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,10 @@ public void execute() throws Throwable {
context.evaluateWorkflowClosure();

// Create workflow mock
File workflow = new File(metaDir, FILE_MOCK);
writeWorkflowMock(workflow);
writeWorkflowMock(mockFile);

// Copy mock file in meta folder for debugging
FileUtil.copy(mockFile, new File(metaDir, FILE_MOCK));

context.getParams().put("nf_test_output", metaDir.getAbsolutePath());
if (isDebug()) {
Expand All @@ -136,7 +138,7 @@ public void execute() throws Throwable {
File paramsFile = new File(metaDir, FILE_PARAMS);

NextflowCommand nextflow = new NextflowCommand();
nextflow.setScript(workflow.getAbsolutePath());
nextflow.setScript(mockFile.getAbsolutePath());
nextflow.setParams(context.getParams());
for (String profile: parent.getProfiles()) {
nextflow.addProfile(profile);
Expand Down
23 changes: 14 additions & 9 deletions src/main/java/com/askimed/nf/test/util/FileUtil.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
package com.askimed.nf.test.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.PushbackInputStream;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand Down Expand Up @@ -189,4 +181,17 @@ public static String encodeBase64(byte[] bytes) {
return encodedContent;
}

public static void copy(File source, File dest) throws IOException {
try (InputStream is = new FileInputStream(source);
OutputStream os = new FileOutputStream(dest)) {

byte[] buffer = new byte[1024];
int length;

// Read from the input stream and write to the output stream
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
}
}
}
24 changes: 24 additions & 0 deletions src/main/java/com/askimed/nf/test/util/HashUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.askimed.nf.test.util;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class HashUtil {

public static String getMd5(String value) {

MessageDigest md;
try {
md = MessageDigest.getInstance("MD5");
md.update(value.getBytes());
byte[] md5sum = md.digest();
BigInteger bigInt = new BigInteger(1, md5sum);
return bigInt.toString(16);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return "??";
}
}

}
14 changes: 12 additions & 2 deletions src/test/java/com/askimed/nf/test/lang/WorkflowTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,9 @@ public void testIssue125() throws Exception {
public void testStagingWithoutMapping() throws Exception {

App app = new App();
int exitCode = app.run(new String[] { "test", "test-data/workflow/staging/hello.nf.test" });
assertEquals(1, exitCode);
// TODO: remove this test. no staging needed.
//int exitCode = app.run(new String[] { "test", "test-data/workflow/staging/hello.nf.test" });
//assertEquals(1, exitCode);

}

Expand Down Expand Up @@ -183,4 +184,13 @@ public void testStagingInTestsuite() throws Exception {

}

@Test
public void testRegex() throws Exception {

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

}

}
2 changes: 2 additions & 0 deletions test-data/workflow/regex/params.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
localOutputDir: "example_output"
someRegex: '^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}\$'
36 changes: 36 additions & 0 deletions test-data/workflow/regex/workflow.nf
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
process exampleProc {

storeDir "${outputDir}/exampleProc"
input:
tuple(
val(outputDir),
val(localOutputDir),
val(someRegexString)
)

output:
tuple(
val(outputDir),
path("example_output/example*.txt")
)

shell:
"""
mkdir -p !{localOutputDir}
echo !{someRegexString} > "!{localOutputDir}/example1.txt"
"""
}


workflow PipeWf {
take:
inputCh

main:
inputCh
| exampleProc
| set { outputCh }

emit:
outputCh
}
27 changes: 27 additions & 0 deletions test-data/workflow/regex/workflow.nf.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
nextflow_workflow {

name "Test workflow"
script "./workflow.nf"
workflow "PipeWf"

test("Output will exist in default outputDir") {

when {

params {
outDir = "$outputDir"
load("test-data/workflow/regex/params.yaml")
}
workflow {
"""
input[0] = Channel.of([params.outDir,params.localOutputDir])
"""
}
}

then {
assert workflow.success
assert path("${outputDir}/exampleProc/example_output").exists()
}
}
}

0 comments on commit 8172c5d

Please sign in to comment.