Skip to content

Commit f9aaf74

Browse files
committed
add json parser test suite
1 parent 506ab7b commit f9aaf74

1 file changed

Lines changed: 146 additions & 0 deletions

File tree

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
package io.viash.helpers
2+
3+
import org.scalatest.BeforeAndAfterAll
4+
import org.scalatest.funsuite.AnyFunSuite
5+
import java.nio.file.{Files, Paths, Path, StandardCopyOption}
6+
7+
/**
8+
* Test suite for language-specific JSON parsers.
9+
* Tests the ViashParseJson functions for Bash, Python, R, JavaScript, C#, and Scala.
10+
*/
11+
class JsonParserTest extends AnyFunSuite with BeforeAndAfterAll {
12+
Logger.UseColorOverride.value = Some(false)
13+
14+
private val projectRoot = Paths.get(System.getProperty("user.dir"))
15+
16+
// Helper to set up temp dir with correct structure for all language parsers
17+
private def setupTempDirWithParser(
18+
language: String,
19+
parserFileName: String
20+
): (Path, Path) = {
21+
val tempDir = Files.createTempDirectory("viash_json_parser_test_")
22+
23+
val testScriptSrc = projectRoot.resolve(s"src/test/resources/io/viash/helpers/languages/$language/test_ViashParseJson.$parserFileName")
24+
val parserScriptSrc = projectRoot.resolve(s"src/main/resources/io/viash/languages/$language/ViashParseJson.$parserFileName")
25+
26+
val testSubPath = s"src/test/resources/io/viash/helpers/languages/$language/test_ViashParseJson.$parserFileName"
27+
val parserSubPath = s"src/main/resources/io/viash/languages/$language/ViashParseJson.$parserFileName"
28+
29+
// Create directory structure for test file
30+
val testDir = tempDir.resolve(testSubPath).getParent
31+
Files.createDirectories(testDir)
32+
val testScript = tempDir.resolve(testSubPath)
33+
Files.copy(testScriptSrc, testScript, StandardCopyOption.REPLACE_EXISTING)
34+
35+
// Create directory structure for parser file
36+
val parserDir = tempDir.resolve(parserSubPath).getParent
37+
Files.createDirectories(parserDir)
38+
val parserScript = tempDir.resolve(parserSubPath)
39+
Files.copy(parserScriptSrc, parserScript, StandardCopyOption.REPLACE_EXISTING)
40+
41+
(tempDir, testScript)
42+
}
43+
44+
private def cleanupTempDir(tempDir: Path): Unit = {
45+
import scala.jdk.CollectionConverters._
46+
Files.walk(tempDir).iterator().asScala.toList.reverse.foreach(Files.delete)
47+
}
48+
49+
test("Bash JSON parser") {
50+
val (tempDir, testScript) = setupTempDirWithParser("bash", "sh")
51+
52+
try {
53+
val result = Exec.runCatchPath(
54+
List("bash", testScript.toString),
55+
cwd = Some(tempDir)
56+
)
57+
58+
assert(result.exitValue == 0, s"Bash JSON parser test failed:\n${result.output}")
59+
} finally {
60+
cleanupTempDir(tempDir)
61+
}
62+
}
63+
64+
test("Python JSON parser") {
65+
val (tempDir, testScript) = setupTempDirWithParser("python", "py")
66+
67+
try {
68+
val result = Exec.runCatchPath(
69+
List("python3", testScript.toString),
70+
cwd = Some(tempDir)
71+
)
72+
73+
assert(result.exitValue == 0, s"Python JSON parser test failed:\n${result.output}")
74+
} finally {
75+
cleanupTempDir(tempDir)
76+
}
77+
}
78+
79+
test("R JSON parser") {
80+
val (tempDir, testScript) = setupTempDirWithParser("r", "R")
81+
82+
try {
83+
val result = Exec.runCatchPath(
84+
List("Rscript", testScript.toString),
85+
cwd = Some(tempDir)
86+
)
87+
88+
assert(result.exitValue == 0, s"R JSON parser test failed:\n${result.output}")
89+
} finally {
90+
cleanupTempDir(tempDir)
91+
}
92+
}
93+
94+
test("JavaScript JSON parser") {
95+
val (tempDir, testScript) = setupTempDirWithParser("javascript", "js")
96+
97+
try {
98+
val result = Exec.runCatchPath(
99+
List("node", testScript.toString),
100+
cwd = Some(tempDir)
101+
)
102+
103+
assert(result.exitValue == 0, s"JavaScript JSON parser test failed:\n${result.output}")
104+
} finally {
105+
cleanupTempDir(tempDir)
106+
}
107+
}
108+
109+
test("C# JSON parser") {
110+
// Check if dotnet script is available
111+
val dotnetCheck = Exec.runCatch(List("dotnet", "script", "--version"))
112+
assume(dotnetCheck.exitValue == 0, "dotnet script not available, skipping C# test")
113+
114+
val (tempDir, testScript) = setupTempDirWithParser("csharp", "csx")
115+
116+
try {
117+
val result = Exec.runCatchPath(
118+
List("dotnet", "script", testScript.toString),
119+
cwd = Some(tempDir)
120+
)
121+
122+
assert(result.exitValue == 0, s"C# JSON parser test failed:\n${result.output}")
123+
} finally {
124+
cleanupTempDir(tempDir)
125+
}
126+
}
127+
128+
test("Scala JSON parser") {
129+
// Check if scala is available
130+
val scalaCheck = Exec.runCatch(List("scala", "-version"))
131+
assume(scalaCheck.exitValue == 0, "scala not available, skipping Scala test")
132+
133+
val (tempDir, testScript) = setupTempDirWithParser("scala", "scala")
134+
135+
try {
136+
val result = Exec.runCatchPath(
137+
List("scala", testScript.toString),
138+
cwd = Some(tempDir)
139+
)
140+
141+
assert(result.exitValue == 0, s"Scala JSON parser test failed:\n${result.output}")
142+
} finally {
143+
cleanupTempDir(tempDir)
144+
}
145+
}
146+
}

0 commit comments

Comments
 (0)