Skip to content

Commit 1615ceb

Browse files
authored
W-12136893: add tests for language level validation (#68)
* W-12136893: add tests for language level validation
1 parent 784b046 commit 1615ceb

4 files changed

Lines changed: 47 additions & 9 deletions

File tree

native-cli-integration-tests/src/test/scala/org/mule/weave/native/NativeCliITTestRunner.scala

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,27 @@ class NativeCliITTestRunner(args: Array[String])
2828
dwPath.getAbsolutePath
2929
}
3030

31-
def execute(): (Int, String) = {
31+
def execute(): (Int, String, String) = {
3232
execute(5, TimeUnit.SECONDS)
3333
}
3434

35-
def execute(timeout: Long, unit: TimeUnit): (Int, String) = {
35+
def execute(timeout: Long, unit: TimeUnit): (Int, String, String) = {
3636
val command = DW_CLI_EXECUTABLE +: args
3737
println(s"Executing command: ${command.mkString(" ")}")
3838
val proc = Runtime.getRuntime.exec(command)
3939
proc.waitFor(timeout, unit)
40-
proc.exitValue()
4140
val source = Source.fromInputStream(proc.getInputStream)
41+
val errorStream = Source.fromInputStream(proc.getErrorStream)
4242
var out = ""
43+
var error = ""
4344
try {
4445
out = source.mkString.trim
46+
error = errorStream.mkString.trim
4547
} finally {
4648
source.close()
49+
errorStream.close()
4750
}
48-
(proc.exitValue(), out)
51+
(proc.exitValue(), out, error)
4952
}
5053
}
5154

native-cli-integration-tests/src/test/scala/org/mule/weave/native/NativeCliRuntimeIT.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ class NativeCliRuntimeIT extends FunSpec
212212
args = args :+ "-f"
213213
args = args :+ cliTransform.getAbsolutePath
214214

215-
val (exitCode, _) = NativeCliITTestRunner(args).execute(TIMEOUT._1, TIMEOUT._2)
215+
val (exitCode, _, _) = NativeCliITTestRunner(args).execute(TIMEOUT._1, TIMEOUT._2)
216216

217217
exitCode shouldBe 0
218218
doAssert(outputPath.toFile, scenario.output, maybeEncoding)

native-cli-integration-tests/src/test/scala/org/mule/weave/native/NativeCliTest.scala

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.mule.weave.native
22

3+
import org.mule.weave.v2.utils.DataWeaveVersion
34
import org.scalatest.BeforeAndAfterAll
45
import org.scalatest.FreeSpec
56
import org.scalatest.Matchers
@@ -26,7 +27,7 @@ class NativeCliTest extends FreeSpec
2627
"it should execute simple migration correctly" in {
2728
val stream: URL = getClass.getClassLoader.getResource("dw1/SimpleFile.dw1")
2829
val file = new File(stream.toURI)
29-
val (_, output) = NativeCliITTestRunner(Array("--migrate", file.getAbsolutePath)).execute()
30+
val (_, output, _) = NativeCliITTestRunner(Array("--migrate", file.getAbsolutePath)).execute()
3031
output.trim shouldBe
3132
"""
3233
|%dw 2.0
@@ -41,20 +42,34 @@ class NativeCliTest extends FreeSpec
4142
}
4243

4344
"it should execute simple case correctly" in {
44-
val (_, output) = NativeCliITTestRunner("1 to 10").execute()
45+
val (_, output, _) = NativeCliITTestRunner("1 to 10").execute()
4546
output shouldBe "[\n 1,\n 2,\n 3,\n 4,\n 5,\n 6,\n 7,\n 8,\n 9,\n 10\n]"
4647
}
4748

4849
"it should execute with input" in {
4950
val path = getResourcePath("inputs/payload.json")
50-
val (_, output) = NativeCliITTestRunner(Array("-i", "payload", path, "payload.name")).execute()
51+
val (_, output, _) = NativeCliITTestRunner(Array("-i", "payload", path, "payload.name")).execute()
5152
output shouldBe "\"Tomo\""
5253
}
5354

5455
"it should execute with input and script" in {
5556
val inputPath = getResourcePath("inputs/payload.json")
5657
val transformationPath = getResourcePath("scripts/GetName.dwl")
57-
val (_, output) = NativeCliITTestRunner(Array("-i", "payload", inputPath, "-f", transformationPath)).execute()
58+
val (_, output, _) = NativeCliITTestRunner(Array("-i", "payload", inputPath, "-f", transformationPath)).execute()
5859
output shouldBe "\"Tomo\""
5960
}
61+
62+
"it should fail if language level is set incorrectly" in {
63+
val (exitCode, _, errorMsg) = NativeCliITTestRunner(Array("-language-level", "payload")).execute()
64+
exitCode should not be 0
65+
errorMsg should include("Unrecognized language level")
66+
}
67+
68+
"should fail if language level is greater than runtime" in {
69+
val runtimeLL = DataWeaveVersion()
70+
val badLL = s"${runtimeLL.major}.${runtimeLL.minor + 1}"
71+
val (exitCode, _, errorMsg) = NativeCliITTestRunner(Array("-language-level", badLL)).execute()
72+
exitCode should not be 0
73+
errorMsg should include(s"Invalid language level, cannot be higher than ${runtimeLL.toString()}")
74+
}
6075
}

native-cli/src/test/scala/org/mule/weave/dwnative/cli/ArgumentsParserTest.scala

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package org.mule.weave.dwnative.cli
33
import org.mule.weave.dwnative.cli.commands.CreateSpellCommand
44
import org.mule.weave.dwnative.cli.commands.HelpCommand
55
import org.mule.weave.dwnative.cli.commands.RunWeaveCommand
6+
import org.mule.weave.v2.utils.DataWeaveVersion
7+
68
import org.scalatest.FreeSpec
79
import org.scalatest.Matchers
810

@@ -105,6 +107,24 @@ class ArgumentsParserTest extends FreeSpec with Matchers {
105107
assert(commandToRun.isInstanceOf[RunWeaveCommand])
106108
}
107109

110+
"should fail if language level is badly written" in {
111+
val parser = new CLIArgumentsParser(new TestConsole())
112+
val value = parser.parse(Array("'Test'", "--language-level", "-2.4"))
113+
assert(value.isRight)
114+
val message = value.right.get
115+
message shouldBe "Unrecognized language level"
116+
}
117+
118+
"should fail if language level is greater than runtime" in {
119+
val parser = new CLIArgumentsParser(new TestConsole())
120+
val runtimeLL = DataWeaveVersion()
121+
val badLL = s"${runtimeLL.major}.${runtimeLL.minor + 1}"
122+
val value = parser.parse(Array("'Test'", "--language-level", badLL))
123+
assert(value.isRight)
124+
val message = value.right.get
125+
message shouldBe s"Invalid language level, cannot be higher than ${runtimeLL.toString()}"
126+
}
127+
108128
"should fail parsing unrecognized argument" in {
109129
val parser = new CLIArgumentsParser(new TestConsole())
110130
val value = parser.parse(Array("-o", "/tmp/out.json", "--parameter", "p1", "p2", "p3", "1 to 10" ))

0 commit comments

Comments
 (0)