From dd531f8a46c69ed6e601361ee55ff396c24badbf Mon Sep 17 00:00:00 2001 From: Lukas Forer Date: Wed, 23 Aug 2023 16:49:13 +0200 Subject: [PATCH 1/3] Add option to list all available tags --- src/main/java/com/askimed/nf/test/App.java | 12 ------ .../nf/test/commands/AbstractCommand.java | 40 +++++++++++++++++++ .../nf/test/commands/CleanCommand.java | 5 +-- .../askimed/nf/test/commands/InitCommand.java | 5 +-- .../nf/test/commands/ListTestsCommand.java | 17 +++++--- .../nf/test/commands/RunTestsCommand.java | 6 +-- .../test/commands/UpdatePluginsCommand.java | 5 +-- .../nf/test/commands/VersionCommand.java | 6 +-- .../nf/test/core/TestExecutionEngine.java | 36 ++++++++++++++++- 9 files changed, 98 insertions(+), 34 deletions(-) create mode 100644 src/main/java/com/askimed/nf/test/commands/AbstractCommand.java diff --git a/src/main/java/com/askimed/nf/test/App.java b/src/main/java/com/askimed/nf/test/App.java index c33a00f5..6d385504 100644 --- a/src/main/java/com/askimed/nf/test/App.java +++ b/src/main/java/com/askimed/nf/test/App.java @@ -22,8 +22,6 @@ public class App { public int run(String[] args) { - printHeader(); - CommandLine commandLine = new CommandLine(new App()); commandLine.addSubcommand("clean", new CleanCommand()); commandLine.addSubcommand("init", new InitCommand()); @@ -38,16 +36,6 @@ public int run(String[] args) { } - private void printHeader() { - - System.out.println(); - System.out.println(Emoji.ROCKET + AnsiText.bold(" " + App.NAME + " " + App.VERSION)); - System.out.println("https://code.askimed.com/nf-test"); - System.out.println("(c) 2021 - 2023 Lukas Forer and Sebastian Schoenherr"); - System.out.println(); - - } - public static void main(String[] args) throws Exception { App app = new App(); diff --git a/src/main/java/com/askimed/nf/test/commands/AbstractCommand.java b/src/main/java/com/askimed/nf/test/commands/AbstractCommand.java new file mode 100644 index 00000000..abf29b05 --- /dev/null +++ b/src/main/java/com/askimed/nf/test/commands/AbstractCommand.java @@ -0,0 +1,40 @@ +package com.askimed.nf.test.commands; + +import java.util.concurrent.Callable; + +import com.askimed.nf.test.App; +import com.askimed.nf.test.util.AnsiText; +import com.askimed.nf.test.util.Emoji; + +import picocli.CommandLine.Option; +import picocli.CommandLine.Help.Visibility; + +public abstract class AbstractCommand implements Callable { + + @Option(names = { + "--silent" }, description = "Hide header and program version", required = false, showDefaultValue = Visibility.ALWAYS) + private boolean silent = false; + + @Override + public Integer call() throws Exception { + + if (!silent) { + printHeader(); + } + + return execute(); + } + + public abstract Integer execute() throws Exception; + + private void printHeader() { + + System.out.println(); + System.out.println(Emoji.ROCKET + AnsiText.bold(" " + App.NAME + " " + App.VERSION)); + System.out.println("https://code.askimed.com/nf-test"); + System.out.println("(c) 2021 - 2023 Lukas Forer and Sebastian Schoenherr"); + System.out.println(); + + } + +} diff --git a/src/main/java/com/askimed/nf/test/commands/CleanCommand.java b/src/main/java/com/askimed/nf/test/commands/CleanCommand.java index f180b83e..fc43cc5f 100644 --- a/src/main/java/com/askimed/nf/test/commands/CleanCommand.java +++ b/src/main/java/com/askimed/nf/test/commands/CleanCommand.java @@ -1,7 +1,6 @@ package com.askimed.nf.test.commands; import java.io.File; -import java.util.concurrent.Callable; import com.askimed.nf.test.config.Config; import com.askimed.nf.test.util.AnsiColors; @@ -10,12 +9,12 @@ import picocli.CommandLine.Command; @Command(name = "clean") -public class CleanCommand implements Callable { +public class CleanCommand extends AbstractCommand { public static String NF_DIRECTORY = ".nf-test"; @Override - public Integer call() throws Exception { + public Integer execute() throws Exception { File workDir = new File(NF_DIRECTORY); diff --git a/src/main/java/com/askimed/nf/test/commands/InitCommand.java b/src/main/java/com/askimed/nf/test/commands/InitCommand.java index 43925ade..021fe408 100644 --- a/src/main/java/com/askimed/nf/test/commands/InitCommand.java +++ b/src/main/java/com/askimed/nf/test/commands/InitCommand.java @@ -1,7 +1,6 @@ package com.askimed.nf.test.commands; import java.io.File; -import java.util.concurrent.Callable; import com.askimed.nf.test.App; import com.askimed.nf.test.commands.init.InitTemplates; @@ -11,10 +10,10 @@ import picocli.CommandLine.Command; @Command(name = "init") -public class InitCommand implements Callable { +public class InitCommand extends AbstractCommand { @Override - public Integer call() throws Exception { + public Integer execute() throws Exception { try { diff --git a/src/main/java/com/askimed/nf/test/commands/ListTestsCommand.java b/src/main/java/com/askimed/nf/test/commands/ListTestsCommand.java index a0d7841e..a4337912 100644 --- a/src/main/java/com/askimed/nf/test/commands/ListTestsCommand.java +++ b/src/main/java/com/askimed/nf/test/commands/ListTestsCommand.java @@ -3,19 +3,18 @@ import java.io.File; import java.util.ArrayList; import java.util.List; -import java.util.concurrent.Callable; import com.askimed.nf.test.config.Config; import com.askimed.nf.test.core.TestExecutionEngine; import com.askimed.nf.test.util.AnsiColors; import picocli.CommandLine.Command; +import picocli.CommandLine.Help.Visibility; import picocli.CommandLine.Option; import picocli.CommandLine.Parameters; -import picocli.CommandLine.Help.Visibility; @Command(name = "list") -public class ListTestsCommand implements Callable { +public class ListTestsCommand extends AbstractCommand { @Parameters(description = "list all tests") private List testPaths = new ArrayList(); @@ -24,8 +23,12 @@ public class ListTestsCommand implements Callable { "--debug" }, description = "Show debugging infos", required = false, showDefaultValue = Visibility.ALWAYS) private boolean debug = false; + @Option(names = { + "--tags" }, description = "Show all available tags", required = false, showDefaultValue = Visibility.ALWAYS) + private boolean tags = false; + @Override - public Integer call() throws Exception { + public Integer execute() throws Exception { try { @@ -65,7 +68,11 @@ public Integer call() throws Exception { TestExecutionEngine engine = new TestExecutionEngine(); engine.setScripts(scripts); - return engine.listTests(); + if (tags) { + return engine.listTags(); + } else { + return engine.listTests(); + } } catch (Throwable e) { diff --git a/src/main/java/com/askimed/nf/test/commands/RunTestsCommand.java b/src/main/java/com/askimed/nf/test/commands/RunTestsCommand.java index 854364a1..2d456e63 100644 --- a/src/main/java/com/askimed/nf/test/commands/RunTestsCommand.java +++ b/src/main/java/com/askimed/nf/test/commands/RunTestsCommand.java @@ -8,7 +8,6 @@ import java.util.ArrayList; import java.util.List; import java.util.Vector; -import java.util.concurrent.Callable; import java.util.function.Consumer; import com.askimed.nf.test.config.Config; @@ -28,7 +27,7 @@ import picocli.CommandLine.Parameters; @Command(name = "test") -public class RunTestsCommand implements Callable { +public class RunTestsCommand extends AbstractCommand { @Parameters(description = "test files") private List testPaths = new ArrayList(); @@ -69,7 +68,8 @@ public class RunTestsCommand implements Callable { private List tags = new Vector(); @Override - public Integer call() throws Exception { + public Integer execute() throws Exception { + List scripts = new ArrayList(); PluginManager manager = new PluginManager(false); diff --git a/src/main/java/com/askimed/nf/test/commands/UpdatePluginsCommand.java b/src/main/java/com/askimed/nf/test/commands/UpdatePluginsCommand.java index 759d76eb..a1c8b0d1 100644 --- a/src/main/java/com/askimed/nf/test/commands/UpdatePluginsCommand.java +++ b/src/main/java/com/askimed/nf/test/commands/UpdatePluginsCommand.java @@ -1,7 +1,6 @@ package com.askimed.nf.test.commands; import java.io.File; -import java.util.concurrent.Callable; import com.askimed.nf.test.config.Config; import com.askimed.nf.test.plugins.PluginManager; @@ -10,10 +9,10 @@ import picocli.CommandLine.Command; @Command(name = "update-plugins") -public class UpdatePluginsCommand implements Callable { +public class UpdatePluginsCommand extends AbstractCommand { @Override - public Integer call() throws Exception { + public Integer execute() throws Exception { File configFile = new File(Config.FILENAME); diff --git a/src/main/java/com/askimed/nf/test/commands/VersionCommand.java b/src/main/java/com/askimed/nf/test/commands/VersionCommand.java index a90f6972..b36d6a3a 100644 --- a/src/main/java/com/askimed/nf/test/commands/VersionCommand.java +++ b/src/main/java/com/askimed/nf/test/commands/VersionCommand.java @@ -1,17 +1,15 @@ package com.askimed.nf.test.commands; -import java.util.concurrent.Callable; - import com.askimed.nf.test.nextflow.NextflowCommand; import com.askimed.nf.test.util.AnsiColors; import picocli.CommandLine.Command; @Command(name = "version") -public class VersionCommand implements Callable { +public class VersionCommand extends AbstractCommand { @Override - public Integer call() throws Exception { + public Integer execute() throws Exception { try { diff --git a/src/main/java/com/askimed/nf/test/core/TestExecutionEngine.java b/src/main/java/com/askimed/nf/test/core/TestExecutionEngine.java index ca6b12c9..623164bf 100644 --- a/src/main/java/com/askimed/nf/test/core/TestExecutionEngine.java +++ b/src/main/java/com/askimed/nf/test/core/TestExecutionEngine.java @@ -2,7 +2,7 @@ import java.io.File; import java.util.List; -import java.util.Vector; +import java.util.*; import com.askimed.nf.test.lang.TestSuiteBuilder; import com.askimed.nf.test.plugins.PluginManager; @@ -258,4 +258,38 @@ public int listTests() throws Throwable { } + public int listTags() throws Throwable { + + if (configFile != null) { + if (!configFile.exists()) { + System.out.println( + AnsiColors.red("Error: Test config file '" + configFile.getAbsolutePath() + "'not found")); + System.out.println(); + return 1; + } + } + + List testSuits = parse(tagQuery); + + if (testSuits.size() == 0) { + System.out.println(AnsiColors.red("Error: no valid tests found.")); + System.out.println(); + return 1; + } + + Set tags = new HashSet(); + for (ITestSuite testSuite : testSuits) { + tags.addAll(testSuite.getTags()); + for (ITest test : testSuite.getTests()) { + tags.addAll(test.getTags()); + } + } + + for (String tag : tags) { + System.out.println(tag); + } + return 0; + + } + } From 832cb5d274e9a41c48d5b772dee09c34f2817442 Mon Sep 17 00:00:00 2001 From: Lukas Forer Date: Thu, 24 Aug 2023 17:31:18 +0200 Subject: [PATCH 2/3] Add json output format to list command --- .../nf/test/commands/ListTestsCommand.java | 9 +- .../nf/test/core/TestExecutionEngine.java | 111 ++++++++++++++---- .../askimed/nf/test/util/OutputFormat.java | 7 ++ 3 files changed, 99 insertions(+), 28 deletions(-) create mode 100644 src/main/java/com/askimed/nf/test/util/OutputFormat.java diff --git a/src/main/java/com/askimed/nf/test/commands/ListTestsCommand.java b/src/main/java/com/askimed/nf/test/commands/ListTestsCommand.java index a4337912..16cd44cc 100644 --- a/src/main/java/com/askimed/nf/test/commands/ListTestsCommand.java +++ b/src/main/java/com/askimed/nf/test/commands/ListTestsCommand.java @@ -7,6 +7,7 @@ import com.askimed.nf.test.config.Config; import com.askimed.nf.test.core.TestExecutionEngine; import com.askimed.nf.test.util.AnsiColors; +import com.askimed.nf.test.util.OutputFormat; import picocli.CommandLine.Command; import picocli.CommandLine.Help.Visibility; @@ -27,6 +28,10 @@ public class ListTestsCommand extends AbstractCommand { "--tags" }, description = "Show all available tags", required = false, showDefaultValue = Visibility.ALWAYS) private boolean tags = false; + @Option(names = { + "--format" }, description = "Output format", required = false, showDefaultValue = Visibility.ALWAYS) + private OutputFormat format = OutputFormat.PRETTY; + @Override public Integer execute() throws Exception { @@ -69,9 +74,9 @@ public Integer execute() throws Exception { TestExecutionEngine engine = new TestExecutionEngine(); engine.setScripts(scripts); if (tags) { - return engine.listTags(); + return engine.listTags(format); } else { - return engine.listTests(); + return engine.listTests(format); } } catch (Throwable e) { diff --git a/src/main/java/com/askimed/nf/test/core/TestExecutionEngine.java b/src/main/java/com/askimed/nf/test/core/TestExecutionEngine.java index 623164bf..d2f0a3c0 100644 --- a/src/main/java/com/askimed/nf/test/core/TestExecutionEngine.java +++ b/src/main/java/com/askimed/nf/test/core/TestExecutionEngine.java @@ -1,14 +1,19 @@ package com.askimed.nf.test.core; import java.io.File; +import java.util.HashSet; import java.util.List; -import java.util.*; +import java.util.Set; +import java.util.Vector; import com.askimed.nf.test.lang.TestSuiteBuilder; import com.askimed.nf.test.plugins.PluginManager; import com.askimed.nf.test.util.AnsiColors; import com.askimed.nf.test.util.AnsiText; import com.askimed.nf.test.util.FileUtil; +import com.askimed.nf.test.util.OutputFormat; + +import groovy.json.JsonOutput; public class TestExecutionEngine { @@ -212,9 +217,7 @@ public int execute() throws Throwable { } - public int listTests() throws Throwable { - - int count = 0; + public int listTests(OutputFormat format) throws Throwable { if (configFile != null) { if (!configFile.exists()) { @@ -233,32 +236,25 @@ public int listTests() throws Throwable { return 1; } - int index = 0; - for (ITestSuite testSuite : testSuits) { - - System.out.println(); - System.out.println("[" + FileUtil.makeRelative(baseDir, scripts.get(index)) + "] " - + AnsiText.bold(testSuite.getName())); - System.out.println(); - - for (ITest test : testSuite.getTests()) { - System.out.println(AnsiText.padding("[" + FileUtil.makeRelative(baseDir, scripts.get(index)) + "@" - + test.getHash().substring(0, 8) + "] " + AnsiText.bold(test.getName()), 2)); - count++; - - } - index++; + switch (format) { + case JSON: + case json: + printTestsAsJson(testSuits); + break; + case RAW: + case raw: + printTestsAsList(testSuits); + break; + default: + printTestsPretty(testSuits); + break; } - System.out.println(); - System.out.println("Found " + count + " tests."); - System.out.println(); - return 0; } - public int listTags() throws Throwable { + public int listTags(OutputFormat format) throws Throwable { if (configFile != null) { if (!configFile.exists()) { @@ -285,11 +281,74 @@ public int listTags() throws Throwable { } } - for (String tag : tags) { - System.out.println(tag); + switch (format) { + case JSON: + case json: + printTagsAsJson(tags); + break; + default: + printTagsPretty(tags); + break; } + return 0; } + private void printTestsAsJson(List testSuits) { + List tests = new Vector(); + int index = 0; + for (ITestSuite testSuite : testSuits) { + for (ITest test : testSuite.getTests()) { + tests.add(scripts.get(index).getAbsolutePath() + "@" + test.getHash().substring(0, 8)); + } + index++; + } + System.out.println(JsonOutput.toJson(tests)); + } + + private void printTestsAsList(List testSuits) { + int index = 0; + for (ITestSuite testSuite : testSuits) { + for (ITest test : testSuite.getTests()) { + System.out.println(scripts.get(index).getAbsolutePath() + "@" + test.getHash().substring(0, 8)); + } + index++; + } + } + + private void printTestsPretty(List testSuits) { + int index = 0; + int count = 0; + for (ITestSuite testSuite : testSuits) { + + System.out.println(); + System.out.println("[" + FileUtil.makeRelative(baseDir, scripts.get(index)) + "] " + + AnsiText.bold(testSuite.getName())); + System.out.println(); + + for (ITest test : testSuite.getTests()) { + System.out.println(AnsiText.padding("[" + FileUtil.makeRelative(baseDir, scripts.get(index)) + "@" + + test.getHash().substring(0, 8) + "] " + AnsiText.bold(test.getName()), 2)); + count++; + + } + index++; + } + + System.out.println(); + System.out.println("Found " + count + " tests."); + System.out.println(); + } + + private void printTagsAsJson(Set tags) { + System.out.println(JsonOutput.toJson(tags)); + } + + private void printTagsPretty(Set tags) { + for (String tag : tags) { + System.out.println(tag); + } + } + } diff --git a/src/main/java/com/askimed/nf/test/util/OutputFormat.java b/src/main/java/com/askimed/nf/test/util/OutputFormat.java new file mode 100644 index 00000000..b752c221 --- /dev/null +++ b/src/main/java/com/askimed/nf/test/util/OutputFormat.java @@ -0,0 +1,7 @@ +package com.askimed.nf.test.util; + +public enum OutputFormat { + + PRETTY, RAW, JSON, pretty, raw, json + +} From 6d5a70d74ac7066dbd8f3e43defe767818925347 Mon Sep 17 00:00:00 2001 From: Lukas Forer Date: Wed, 30 Aug 2023 11:27:00 +0200 Subject: [PATCH 3/3] Add basic documentation for `list` command --- docs/docs/cli/list.md | 45 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/docs/docs/cli/list.md b/docs/docs/cli/list.md index 066cca98..12d4bd85 100644 --- a/docs/docs/cli/list.md +++ b/docs/docs/cli/list.md @@ -10,6 +10,18 @@ nf-test list [|] ### Optional Arguments +#### `--tags` +Print a list of all used tags. + +#### `--format json` +Print the list of tests or tags as json object. + +#### `--format raw` +Print the list of tests or tags as simple list without formatting. + +#### `--silent` +Hide program version and header infos. + #### `--debug` Show debugging infos. @@ -28,3 +40,36 @@ Show debugging infos. nf-test list tests/modules tests/modules/bwa_index.nf.test ``` + +* List of all testcases as json: + +``` +nf-test list --format json --silent +["/Users/lukfor/Development/git/nf-gwas/tests/main.nf.test@69b98c67","/Users/lukfor/Development/git/nf-gwas/tests/main.nf.test@fdb6c1cc","/Users/lukfor/Development/git/nf-gwas/tests/main.nf.test@d1c219eb","/Users/lukfor/Development/git/nf-gwas/tests/main.nf.test@3c54e3cb",...] +``` + +* List of all testcases as unformatted ist: + +``` +nf-test list --format raw --silent +/Users/lukfor/Development/git/nf-gwas/tests/main.nf.test@69b98c67 +/Users/lukfor/Development/git/nf-gwas/tests/main.nf.test@fdb6c1cc +/Users/lukfor/Development/git/nf-gwas/tests/main.nf.test@d1c219eb +/Users/lukfor/Development/git/nf-gwas/tests/main.nf.test@3c54e3cb +... +``` + +* List of all tags as json: + +``` +nf-test list --tags --format json --silent +["fastqc","snakemake"] +``` + +* List of all tags as unformatted list: + +``` +nf-test list --tags --format raw --silent +fastqc +snakemake +``` \ No newline at end of file