Skip to content

Commit 371ea93

Browse files
Modify the tests to run from the main app (#29)
* Modify the tests to run from the main app * Remove redundant line * Move the logic to JellyComand --------- Co-authored-by: Karolina Bogacka <karolina@neverblink.eu>
1 parent 4466c0f commit 371ea93

3 files changed

Lines changed: 22 additions & 37 deletions

File tree

‎src/main/scala/eu/neverblink/jelly/cli/JellyCommand.scala‎

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
package eu.neverblink.jelly.cli
22

33
import caseapp.*
4-
import eu.neverblink.jelly.cli.JellyCommand.emptyRemainingArgs
54

65
import java.io.{ByteArrayOutputStream, OutputStream, PrintStream}
76
import scala.compiletime.uninitialized
87

9-
object JellyCommand:
10-
val emptyRemainingArgs: RemainingArgs = RemainingArgs(Seq.empty, Seq.empty)
11-
128
abstract class JellyCommand[T: {Parser, Help}] extends Command[T]:
139
private var isTest = false
1410
private var out = System.out
@@ -31,6 +27,17 @@ abstract class JellyCommand[T: {Parser, Help}] extends Command[T]:
3127
out = System.out
3228
err = System.err
3329

30+
/** Runs the command in test mode from the outside app parsing level
31+
* @param args
32+
* the command line arguments
33+
*/
34+
def runCommand(args: List[String]): (String, String) =
35+
if !isTest then testMode(true)
36+
osOut.reset()
37+
osErr.reset()
38+
App.main(args.toArray)
39+
(osOut.toString, osErr.toString)
40+
3441
def getOut: String =
3542
if isTest then
3643
out.flush()
@@ -51,24 +58,6 @@ abstract class JellyCommand[T: {Parser, Help}] extends Command[T]:
5158
s
5259
else throw new IllegalStateException("Not in test mode")
5360

54-
/** Run the command in test mode, capturing stdout and stderr.
55-
* @param options
56-
* the command options
57-
* @param remainingArgs
58-
* the remaining arguments
59-
* @throws ExitError
60-
* if the command exits
61-
* @return
62-
* (stdout, stderr)
63-
*/
64-
@throws[ExitError]
65-
def runTest(options: T, remainingArgs: RemainingArgs = emptyRemainingArgs): (String, String) =
66-
if !isTest then testMode(true)
67-
osOut.reset()
68-
osErr.reset()
69-
run(options, remainingArgs)
70-
(getOut, getErr)
71-
7261
@throws[ExitError]
7362
override def exit(code: Int): Nothing =
7463
if isTest then throw ExitError(code)

‎src/test/scala/eu/neverblink/jelly/cli/command/RdfFromJellySpec.scala‎

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package eu.neverblink.jelly.cli.command
22

3-
import caseapp.core.{Indexed, RemainingArgs}
43
import eu.neverblink.jelly.cli.command.helpers.*
54
import eu.neverblink.jelly.cli.command.rdf.*
65
import org.apache.jena.riot.RDFLanguages
@@ -11,37 +10,34 @@ import scala.io.Source
1110
import scala.util.Using
1211

1312
class RdfFromJellySpec extends AnyWordSpec with Matchers with CleanUpAfterTest:
13+
1414
"rdf from-jelly command" should {
1515
"be able to convert a Jelly file to NTriples output stream" in {
1616
val jellyFile = DataGenHelper.generateJellyFile(3)
1717
val nQuadString = DataGenHelper.generateNQuadString(3)
18-
val options = RdfFromJellyOptions(outputFile = None)
19-
val args = RemainingArgs(indexedRemaining = List(Indexed(jellyFile)), Seq.empty)
2018
val (out, err) =
21-
RdfFromJelly.runTest(
22-
options,
23-
args,
24-
)
19+
RdfFromJelly.runCommand(List("rdf", "from-jelly", jellyFile))
2520
val sortedOut = out.split("\n").map(_.trim).sorted
2621
val sortedQuads = nQuadString.split("\n").map(_.trim).sorted
2722
sortedOut should contain theSameElementsAs sortedQuads
2823
}
24+
2925
"be able to convert a Jelly stream to NTriples output stream" in {
3026
DataGenHelper.generateJellyInputStream(3)
3127
val nQuadString = DataGenHelper.generateNQuadString(3)
32-
val options = RdfFromJellyOptions(outputFile = None)
33-
val (out, err) = RdfFromJelly.runTest(options)
28+
val (out, err) = RdfFromJelly.runCommand(List("rdf", "from-jelly"))
3429
val sortedOut = out.split("\n").map(_.trim).sorted
3530
val sortedQuads = nQuadString.split("\n").map(_.trim).sorted
3631
sortedOut should contain theSameElementsAs sortedQuads
3732
}
3833
"be able to convert a Jelly file to NTriples file" in {
3934
val jellyFile = DataGenHelper.generateJellyFile(3)
40-
val args = RemainingArgs(indexedRemaining = List(Indexed(jellyFile)), Seq.empty)
4135
val nQuadString = DataGenHelper.generateNQuadString(3)
4236
val outputFile = DataGenHelper.generateOutputFile(RDFLanguages.NQUADS)
43-
val options = RdfFromJellyOptions(outputFile = Some(outputFile))
44-
val (out, err) = RdfFromJelly.runTest(options, args)
37+
val (out, err) =
38+
RdfFromJelly.runCommand(
39+
List("rdf", "from-jelly", jellyFile, "--to", outputFile),
40+
)
4541
val sortedOut = Using.resource(Source.fromFile(outputFile)) { content =>
4642
content.getLines().toList.map(_.trim).sorted
4743
}
@@ -53,8 +49,8 @@ class RdfFromJellySpec extends AnyWordSpec with Matchers with CleanUpAfterTest:
5349
DataGenHelper.generateJellyInputStream(3)
5450
val outputFile = DataGenHelper.generateOutputFile(RDFLanguages.NQUADS)
5551
val nQuadString = DataGenHelper.generateNQuadString(3)
56-
val options = RdfFromJellyOptions(outputFile = Some(outputFile))
57-
val (out, err) = RdfFromJelly.runTest(options)
52+
val (out, err) =
53+
RdfFromJelly.runCommand(List("rdf", "from-jelly", "--to", outputFile))
5854
val sortedOut = Using.resource(Source.fromFile(outputFile)) { content =>
5955
content.getLines().toList.map(_.trim).sorted
6056
}

‎src/test/scala/eu/neverblink/jelly/cli/command/VersionSpec.scala‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import org.scalatest.wordspec.AnyWordSpec
66
class VersionSpec extends AnyWordSpec, Matchers:
77
"version command" should {
88
"print something" in {
9-
val (out, err) = Version.runTest(VersionOptions())
9+
val (out, err) = Version.runCommand(List("version"))
1010
out should startWith("jelly-cli")
1111
out should include("Jelly-JVM")
1212
out should include("Apache Jena")

0 commit comments

Comments
 (0)