Skip to content

Commit 0905a91

Browse files
Modify the tests to run from the main app
1 parent 8b4504d commit 0905a91

File tree

4 files changed

+29
-28
lines changed

4 files changed

+29
-28
lines changed

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

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
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
@@ -51,23 +50,10 @@ abstract class JellyCommand[T: {Parser, Help}] extends Command[T]:
5150
s
5251
else throw new IllegalStateException("Not in test mode")
5352

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) =
53+
def setUpTest(): Unit =
6654
if !isTest then testMode(true)
6755
osOut.reset()
6856
osErr.reset()
69-
run(options, remainingArgs)
70-
(getOut, getErr)
7157

7258
@throws[ExitError]
7359
override def exit(code: Int): Nothing =

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

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,22 @@ import scala.io.Source
1111
import scala.util.Using
1212

1313
class RdfFromJellySpec extends AnyWordSpec with Matchers with CleanUpAfterTest:
14+
1415
"rdf from-jelly command" should {
1516
"be able to convert a Jelly file to NTriples output stream" in {
1617
val jellyFile = DataGenHelper.generateJellyFile(3)
1718
val nQuadString = DataGenHelper.generateNQuadString(3)
18-
val options = RdfFromJellyOptions(outputFile = None)
19-
val args = RemainingArgs(indexedRemaining = List(Indexed(jellyFile)), Seq.empty)
2019
val (out, err) =
21-
RdfFromJelly.runTest(
22-
options,
23-
args,
24-
)
20+
TestAppRunner.runCommand(RdfFromJelly, List("rdf", "from-jelly", jellyFile))
2521
val sortedOut = out.split("\n").map(_.trim).sorted
2622
val sortedQuads = nQuadString.split("\n").map(_.trim).sorted
2723
sortedOut should contain theSameElementsAs sortedQuads
2824
}
25+
2926
"be able to convert a Jelly stream to NTriples output stream" in {
3027
DataGenHelper.generateJellyInputStream(3)
3128
val nQuadString = DataGenHelper.generateNQuadString(3)
32-
val options = RdfFromJellyOptions(outputFile = None)
33-
val (out, err) = RdfFromJelly.runTest(options)
29+
val (out, err) = TestAppRunner.runCommand(RdfFromJelly, List("rdf", "from-jelly"))
3430
val sortedOut = out.split("\n").map(_.trim).sorted
3531
val sortedQuads = nQuadString.split("\n").map(_.trim).sorted
3632
sortedOut should contain theSameElementsAs sortedQuads
@@ -40,8 +36,11 @@ class RdfFromJellySpec extends AnyWordSpec with Matchers with CleanUpAfterTest:
4036
val args = RemainingArgs(indexedRemaining = List(Indexed(jellyFile)), Seq.empty)
4137
val nQuadString = DataGenHelper.generateNQuadString(3)
4238
val outputFile = DataGenHelper.generateOutputFile(RDFLanguages.NQUADS)
43-
val options = RdfFromJellyOptions(outputFile = Some(outputFile))
44-
val (out, err) = RdfFromJelly.runTest(options, args)
39+
val (out, err) =
40+
TestAppRunner.runCommand(
41+
RdfFromJelly,
42+
List("rdf", "from-jelly", jellyFile, "--to", outputFile),
43+
)
4544
val sortedOut = Using.resource(Source.fromFile(outputFile)) { content =>
4645
content.getLines().toList.map(_.trim).sorted
4746
}
@@ -53,8 +52,8 @@ class RdfFromJellySpec extends AnyWordSpec with Matchers with CleanUpAfterTest:
5352
DataGenHelper.generateJellyInputStream(3)
5453
val outputFile = DataGenHelper.generateOutputFile(RDFLanguages.NQUADS)
5554
val nQuadString = DataGenHelper.generateNQuadString(3)
56-
val options = RdfFromJellyOptions(outputFile = Some(outputFile))
57-
val (out, err) = RdfFromJelly.runTest(options)
55+
val (out, err) =
56+
TestAppRunner.runCommand(RdfFromJelly, List("rdf", "from-jelly", "--to", outputFile))
5857
val sortedOut = Using.resource(Source.fromFile(outputFile)) { content =>
5958
content.getLines().toList.map(_.trim).sorted
6059
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package eu.neverblink.jelly.cli.command
22

3+
import eu.neverblink.jelly.cli.command.helpers.TestAppRunner
34
import org.scalatest.matchers.should.Matchers
45
import org.scalatest.wordspec.AnyWordSpec
56

67
class VersionSpec extends AnyWordSpec, Matchers:
78
"version command" should {
89
"print something" in {
9-
val (out, err) = Version.runTest(VersionOptions())
10+
val (out, err) = TestAppRunner.runCommand(Version, List("version"))
1011
out should startWith("jelly-cli")
1112
out should include("Jelly-JVM")
1213
out should include("Apache Jena")
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package eu.neverblink.jelly.cli.command.helpers
2+
import eu.neverblink.jelly.cli.{App, JellyCommand}
3+
4+
object TestAppRunner:
5+
/*
6+
* Runs a command in test mode from the outside app parsing level
7+
* @param command
8+
* the command to run
9+
* @param args
10+
* the command line arguments
11+
*/
12+
def runCommand(command: JellyCommand[?], args: List[String]): (String, String) =
13+
command.setUpTest()
14+
App.main(args.toArray)
15+
(command.getOut, command.getErr)

0 commit comments

Comments
 (0)