Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 11 additions & 22 deletions src/main/scala/eu/neverblink/jelly/cli/JellyCommand.scala
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down