Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
16 changes: 1 addition & 15 deletions src/main/scala/eu/neverblink/jelly/cli/JellyCommand.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package eu.neverblink.jelly.cli

import caseapp.*
import eu.neverblink.jelly.cli.JellyCommand.emptyRemainingArgs

import java.io.{ByteArrayOutputStream, OutputStream, PrintStream}
import scala.compiletime.uninitialized
Expand Down Expand Up @@ -51,23 +50,10 @@ 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) =
def setUpTest(): Unit =
if !isTest then testMode(true)
osOut.reset()
osErr.reset()
run(options, remainingArgs)
(getOut, getErr)

@throws[ExitError]
override def exit(code: Int): Nothing =
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,35 @@ 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,
)
TestAppRunner.runCommand(RdfFromJelly, 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) = TestAppRunner.runCommand(RdfFromJelly, 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) =
TestAppRunner.runCommand(
RdfFromJelly,
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 +50,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) =
TestAppRunner.runCommand(RdfFromJelly, 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
@@ -1,12 +1,13 @@
package eu.neverblink.jelly.cli.command

import eu.neverblink.jelly.cli.command.helpers.TestAppRunner
import org.scalatest.matchers.should.Matchers
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) = TestAppRunner.runCommand(Version, List("version"))
out should startWith("jelly-cli")
out should include("Jelly-JVM")
out should include("Apache Jena")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package eu.neverblink.jelly.cli.command.helpers
import eu.neverblink.jelly.cli.{App, JellyCommand}

object TestAppRunner:
/*
* Runs a command in test mode from the outside app parsing level
* @param command
* the command to run
* @param args
* the command line arguments
*/
def runCommand(command: JellyCommand[?], args: List[String]): (String, String) =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not put it in JellyCommand?

Then the call would be simpler. Instead of TestAppRunner.runCommand(RdfFromJelly, List(...))

You'd have RdfFromJelly.runTest(List(...))

Shorter.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can keep the command name as part of the input List(), this does make sense.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, makes sense. I think it makes the most sense to put as much of the input command in the argument lists in the test as possible - it will be easier to evaluate tests then.

command.setUpTest()
App.main(args.toArray)
(command.getOut, command.getErr)