Skip to content

Commit

Permalink
Fixing #25 . Avoid index out of bound exception. Also fixing commnad …
Browse files Browse the repository at this point in the history
…is --main and the exception should be clear
  • Loading branch information
machaval committed Mar 7, 2022
1 parent 8ba438f commit b3d95ec
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 9 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ weaveVersion=2.5.0-SNAPSHOT
nativeVersion=100.100.100
scalaVersion=2.12.11
ioVersion=0.4.2
graalvmVersion=21.3.0
graalvmVersion=22.0.0.2
#Libaries
scalaTestVersion=3.0.1
scalaTestPluginVersion=0.25
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import org.mule.weave.dwnative.cli.commands.VersionCommand
import org.mule.weave.dwnative.cli.commands.WeaveCommand
import org.mule.weave.dwnative.cli.commands.WeaveModule
import org.mule.weave.dwnative.cli.commands.WeaveRunnerConfig
import org.mule.weave.dwnative.cli.exceptions.ResourceNotFoundException
import org.mule.weave.dwnative.cli.utils.SpellsUtils
import org.mule.weave.dwnative.utils.FileUtils
import org.mule.weave.v2.io.FileHelper
Expand Down Expand Up @@ -219,17 +220,16 @@ class CLIArgumentsParser(console: Console) {
case "--telemetry" => {
telemetry = true
}
case "-main" | "-m" => {
case "--main" | "-m" => {
if (i + 1 < args.length) {
i = i + 1
val mainScriptName: String = args(i)
scriptToRun = Some((nativeRuntime) => {
val mainScriptName = args(i)
val maybeString = nativeRuntime.getResourceContent(NameIdentifier(mainScriptName))
if (maybeString.isDefined) {
WeaveModule(maybeString.get, args(i))
WeaveModule(maybeString.get, mainScriptName)
} else {
console.error(s"Unable to resolve `${mainScriptName}` in the specified classpath.")
WeaveModule("", args(i))
throw new ResourceNotFoundException(mainScriptName)
}
})
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.mule.weave.dwnative.cli

import org.mule.weave.dwnative.cli.commands.UsageCommand
import org.mule.weave.dwnative.cli.exceptions.CLIException

import java.io.PrintWriter
import java.io.StringWriter
Expand Down Expand Up @@ -29,6 +30,10 @@ class DataWeaveCLIRunner {
try {
weaveCommand.exec()
} catch {
case exception: CLIException => {
console.error(exception.getMessage)
-1
}
case exception: Exception => {
val exceptionString = new StringWriter()
exception.printStackTrace(new PrintWriter(exceptionString))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.mule.weave.dwnative.cli.exceptions

/**
* Marker trait for any exception that is user facing
*/
trait CLIException extends Exception {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.mule.weave.dwnative.cli.exceptions

class ResourceNotFoundException(name: String) extends CLIException {
override def getMessage: String = s"Resource: `${name}` was not found in the classpath. Please verify the name and the specified classpath."
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package org.mule.weave.dwnative.cli

import org.mule.weave.dwnative.NativeRuntime
import org.mule.weave.dwnative.cli.commands.CreateSpellCommand
import org.mule.weave.dwnative.cli.commands.RunWeaveCommand
import org.mule.weave.dwnative.cli.commands.WeaveCommand
import org.mule.weave.dwnative.cli.commands.WeaveModule
import org.scalatest.FreeSpec

class ArgumentsParserTest extends FreeSpec {
Expand Down Expand Up @@ -52,4 +55,12 @@ class ArgumentsParserTest extends FreeSpec {
assert(runWeaveCommand.spellName == "Test")
}

"should parse correctly a main " in {
val parser: CLIArgumentsParser = new CLIArgumentsParser(new TestConsole())
val value: Either[WeaveCommand, String] = parser.parse(Array("--main", "Test.dwl"))
assert(value.isLeft)
val commandToRun = value.left.get
assert(commandToRun.isInstanceOf[RunWeaveCommand])
}

}
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package org.mule.weave.dwnative.cli

import org.mule.weave.dwnative.utils.DataWeaveUtils
import org.mule.weave.dwnative.utils.FileUtils
import org.scalatest.FreeSpec
import org.scalatest.Matchers

import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.File
import java.nio.file.Files
import java.nio.file.Path
import scala.io.Source

class DataWeaveCLITest extends FreeSpec with Matchers {
Expand Down

0 comments on commit b3d95ec

Please sign in to comment.