diff --git a/src/main/scala/eu/neverblink/jelly/cli/JellyCommand.scala b/src/main/scala/eu/neverblink/jelly/cli/JellyCommand.scala index 69d3e32..69da29a 100644 --- a/src/main/scala/eu/neverblink/jelly/cli/JellyCommand.scala +++ b/src/main/scala/eu/neverblink/jelly/cli/JellyCommand.scala @@ -1,14 +1,10 @@ package eu.neverblink.jelly.cli import caseapp.* -import eu.neverblink.jelly.cli.JellyCommand.emptyRemainingArgs import java.io.{ByteArrayOutputStream, OutputStream, PrintStream} import scala.compiletime.uninitialized -object JellyCommand: - val emptyRemainingArgs: RemainingArgs = RemainingArgs(Seq.empty, Seq.empty) - abstract class JellyCommand[T: {Parser, Help}] extends Command[T]: private var isTest = false private var out = System.out @@ -31,6 +27,17 @@ abstract class JellyCommand[T: {Parser, Help}] extends Command[T]: out = System.out err = System.err + /** Runs the command in test mode from the outside app parsing level + * @param args + * the command line arguments + */ + def runCommand(args: List[String]): (String, String) = + if !isTest then testMode(true) + osOut.reset() + osErr.reset() + App.main(args.toArray) + (osOut.toString, osErr.toString) + def getOut: String = if isTest then out.flush() @@ -51,24 +58,6 @@ abstract class JellyCommand[T: {Parser, Help}] extends Command[T]: s else throw new IllegalStateException("Not in test mode") - /** Run the command in test mode, capturing stdout and stderr. - * @param options - * the command options - * @param remainingArgs - * the remaining arguments - * @throws ExitError - * if the command exits - * @return - * (stdout, stderr) - */ - @throws[ExitError] - def runTest(options: T, remainingArgs: RemainingArgs = emptyRemainingArgs): (String, String) = - if !isTest then testMode(true) - osOut.reset() - osErr.reset() - run(options, remainingArgs) - (getOut, getErr) - @throws[ExitError] override def exit(code: Int): Nothing = if isTest then throw ExitError(code) diff --git a/src/test/scala/eu/neverblink/jelly/cli/command/RdfFromJellySpec.scala b/src/test/scala/eu/neverblink/jelly/cli/command/RdfFromJellySpec.scala index e7721bd..bda172a 100644 --- a/src/test/scala/eu/neverblink/jelly/cli/command/RdfFromJellySpec.scala +++ b/src/test/scala/eu/neverblink/jelly/cli/command/RdfFromJellySpec.scala @@ -1,6 +1,5 @@ package eu.neverblink.jelly.cli.command -import caseapp.core.{Indexed, RemainingArgs} import eu.neverblink.jelly.cli.command.helpers.* import eu.neverblink.jelly.cli.command.rdf.* import org.apache.jena.riot.RDFLanguages @@ -11,37 +10,34 @@ import scala.io.Source import scala.util.Using class RdfFromJellySpec extends AnyWordSpec with Matchers with CleanUpAfterTest: + "rdf from-jelly command" should { "be able to convert a Jelly file to NTriples output stream" in { val jellyFile = DataGenHelper.generateJellyFile(3) val nQuadString = DataGenHelper.generateNQuadString(3) - val options = RdfFromJellyOptions(outputFile = None) - val args = RemainingArgs(indexedRemaining = List(Indexed(jellyFile)), Seq.empty) val (out, err) = - RdfFromJelly.runTest( - options, - args, - ) + RdfFromJelly.runCommand(List("rdf", "from-jelly", jellyFile)) val sortedOut = out.split("\n").map(_.trim).sorted val sortedQuads = nQuadString.split("\n").map(_.trim).sorted sortedOut should contain theSameElementsAs sortedQuads } + "be able to convert a Jelly stream to NTriples output stream" in { DataGenHelper.generateJellyInputStream(3) val nQuadString = DataGenHelper.generateNQuadString(3) - val options = RdfFromJellyOptions(outputFile = None) - val (out, err) = RdfFromJelly.runTest(options) + val (out, err) = RdfFromJelly.runCommand(List("rdf", "from-jelly")) val sortedOut = out.split("\n").map(_.trim).sorted val sortedQuads = nQuadString.split("\n").map(_.trim).sorted sortedOut should contain theSameElementsAs sortedQuads } "be able to convert a Jelly file to NTriples file" in { val jellyFile = DataGenHelper.generateJellyFile(3) - val args = RemainingArgs(indexedRemaining = List(Indexed(jellyFile)), Seq.empty) val nQuadString = DataGenHelper.generateNQuadString(3) val outputFile = DataGenHelper.generateOutputFile(RDFLanguages.NQUADS) - val options = RdfFromJellyOptions(outputFile = Some(outputFile)) - val (out, err) = RdfFromJelly.runTest(options, args) + val (out, err) = + RdfFromJelly.runCommand( + List("rdf", "from-jelly", jellyFile, "--to", outputFile), + ) val sortedOut = Using.resource(Source.fromFile(outputFile)) { content => content.getLines().toList.map(_.trim).sorted } @@ -53,8 +49,8 @@ class RdfFromJellySpec extends AnyWordSpec with Matchers with CleanUpAfterTest: DataGenHelper.generateJellyInputStream(3) val outputFile = DataGenHelper.generateOutputFile(RDFLanguages.NQUADS) val nQuadString = DataGenHelper.generateNQuadString(3) - val options = RdfFromJellyOptions(outputFile = Some(outputFile)) - val (out, err) = RdfFromJelly.runTest(options) + val (out, err) = + RdfFromJelly.runCommand(List("rdf", "from-jelly", "--to", outputFile)) val sortedOut = Using.resource(Source.fromFile(outputFile)) { content => content.getLines().toList.map(_.trim).sorted } diff --git a/src/test/scala/eu/neverblink/jelly/cli/command/VersionSpec.scala b/src/test/scala/eu/neverblink/jelly/cli/command/VersionSpec.scala index 3059933..aa88d2f 100644 --- a/src/test/scala/eu/neverblink/jelly/cli/command/VersionSpec.scala +++ b/src/test/scala/eu/neverblink/jelly/cli/command/VersionSpec.scala @@ -6,7 +6,7 @@ import org.scalatest.wordspec.AnyWordSpec class VersionSpec extends AnyWordSpec, Matchers: "version command" should { "print something" in { - val (out, err) = Version.runTest(VersionOptions()) + val (out, err) = Version.runCommand(List("version")) out should startWith("jelly-cli") out should include("Jelly-JVM") out should include("Apache Jena")