Skip to content

Commit

Permalink
Adding more tests to validate spells works correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
machaval committed Dec 21, 2021
1 parent 1fe636e commit 5ab915a
Show file tree
Hide file tree
Showing 14 changed files with 222 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ import java.io.OutputStream
import java.io.PrintWriter
import java.io.StringWriter
import java.nio.charset.Charset
import java.nio.charset.StandardCharsets
import java.util.concurrent.ExecutorService
import scala.concurrent.Await
import scala.concurrent.Future
Expand Down Expand Up @@ -199,12 +198,14 @@ class NativeRuntime(resourcesCacheDir: File, executor: ExecutorService, libDir:

class WeavePathProtocolHandler(path: PathBasedResourceResolver) extends ReadFunctionProtocolHandler {

private val CLASSPATH_PREFIX = "classpath://"

override def handles(url: String): Boolean = {
url.startsWith("classpath://")
url.startsWith(CLASSPATH_PREFIX)
}

override def createSourceProvider(url: String, locatable: LocationCapable, charset: Charset): SourceProvider = {
val uri = url.stripPrefix("classpath://")
val uri = url.stripPrefix(CLASSPATH_PREFIX)
val wellFormedUri = if (uri.startsWith("/")) {
uri.substring(1)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.mule.weave.dwnative.cli
import org.mule.weave.dwnative.NativeRuntime
import org.mule.weave.dwnative.cli.commands.AddWizardCommand
import org.mule.weave.dwnative.cli.commands.CloneWizardConfig
import org.mule.weave.dwnative.cli.commands.CreateSpellCommand
import org.mule.weave.dwnative.cli.commands.ListSpellsCommand
import org.mule.weave.dwnative.cli.commands.RunWeaveCommand
import org.mule.weave.dwnative.cli.commands.UpdateAllGrimoires
Expand All @@ -14,6 +15,7 @@ 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.utils.SpellsUtils
import org.mule.weave.dwnative.utils.FileUtils
import org.mule.weave.v2.io.FileHelper
import org.mule.weave.v2.parser.ast.variables.NameIdentifier
import org.mule.weave.v2.runtime.utils.AnsiColor.red
Expand Down Expand Up @@ -95,6 +97,15 @@ class CLIArgumentsParser(console: Console) {
case "--watch" => {
watch = true
}
case "--new-spell" => {
if (i + 1 < args.length) {
i = i + 1
val spellName = args(i)
return Left(new CreateSpellCommand(spellName, console))
} else {
return Right("Missing <spell-name>")
}
}
case "--list-spells" => {
return Left(new ListSpellsCommand(console))
}
Expand Down Expand Up @@ -168,6 +179,8 @@ class CLIArgumentsParser(console: Console) {
if (!mainFile.isFile) {
return Right(s"Unable find `Main.dwl` in the spell: `${spell}`.")
}
//Watch all files in the src folder
filesToWatch ++= FileUtils.tree(srcFolder)
if (path.isEmpty) {
path = srcFolder.getAbsolutePath
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.mule.weave.dwnative.cli.commands

import org.mule.weave.dwnative.cli.Console

import java.io.File
import java.io.FileWriter
import java.nio.charset.StandardCharsets

class CreateSpellCommand(val spellName: String, console: Console) extends WeaveCommand {

def exec(): Int = {
var statusCode = 0
val homeFolder = new File(spellName)
if (homeFolder.exists()) {
console.error(s"Spell `${spellName}` already exists.")
statusCode = -1
} else {
if (homeFolder.mkdirs()) {
val srcFolder = new File(homeFolder, "src")
srcFolder.mkdirs()
val mainFile = new File(srcFolder, "Main.dwl")
val writer = new FileWriter(mainFile, StandardCharsets.UTF_8)
writer.write("%dw 2.0\n---")
console.info(s"Write your magic spell in `${mainFile.getAbsolutePath}`.")
writer.close()
} else {
console.error(s"Unable to create folder `${spellName}`.")
statusCode = -1
}
}
statusCode
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import java.io.StringWriter
import java.util.concurrent.Executors
import scala.util.Try

class RunWeaveCommand(config: WeaveRunnerConfig, console: Console) extends WeaveCommand {
class RunWeaveCommand(val config: WeaveRunnerConfig, console: Console) extends WeaveCommand {
val weaveUtils = new DataWeaveUtils(console)

private val DEFAULT_MIME_TYPE: String = "application/json"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,24 @@ class UsageCommand(console: Console) extends WeaveCommand {
|Arguments Detail:
|
| --watch | Keep the cli up and watch the used files for modifications and re execute
| --path | Path of jars or directories where weave files are being searched.
| --prop or -p | Property to be passed.
| --input or -i | Declares a new input.
| --list-spells | List all the available spells
| --spell or -s | Runs a spell. Use the <spellName> or <wizard>/<spellName> for spells from a given wizard.
| --update-grimoires | Update all wizard grimoires
| --add-wizard | Downloads wizard grimoire so that its spell are accessible
| --path | Path of jars or directories where weave files are being searched.
| --prop or -p | Property to be passed.
| --input or -i | Declares a new input.
| --verbose or -v | Enable Verbose Mode.
| --local-spell | Executes a local folder spell
| --new-spell | Create a new spell
| --output or -o | Specifies output file for the transformation if not standard output will be used.
| --main or -m | The full qualified name of the mapping to be execute.
| --file or -f | Path to the file
| --eval | Evaluates the script instead of writing it
| --version | The version of the CLI and Runtime
| --clean-cache | Cleans the cache where all artifacts are being downloaded this force to download all artifacts every time
| --remote-debug | Enables remote debugging
| --verbose or -v | Enable Verbose Mode.
| --telemetry | Enables telemetry reporting
| --local-spell | Executes a local folder spell
|
| Example:
|
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.mule.weave.dwnative.utils

import java.io.File
import java.io.IOException
import java.nio.file.CopyOption
import java.nio.file.FileVisitResult
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.SimpleFileVisitor
import java.nio.file.attribute.BasicFileAttributes

object FileUtils {

def tree(file: File): Array[File] = {
if (file.isDirectory) {
file.listFiles().flatMap((f) => tree(f))
} else {
Array(file)
}
}

@throws[IOException]
def copyFolder(source: Path, target: Path, options: CopyOption*): Unit = {
Files.walkFileTree(source, new SimpleFileVisitor[Path]() {
@throws[IOException]
override def preVisitDirectory(dir: Path, attrs: BasicFileAttributes): FileVisitResult = {
Files.createDirectories(target.resolve(source.relativize(dir)))
FileVisitResult.CONTINUE
}

@throws[IOException]
override def visitFile(file: Path, attrs: BasicFileAttributes): FileVisitResult = {
Files.copy(file, target.resolve(source.relativize(file)), options: _*)
FileVisitResult.CONTINUE
}
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"DW Rules"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MyLib::test()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fun test() = "DW Rules"
1 change: 1 addition & 0 deletions native-cli/src/test/resources/spells/spells.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is just a marker file to find this folder with in the classloader
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package org.mule.weave.dwnative.cli

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

class ArgumentsParserTest extends FreeSpec {

"should parse correctly a local spell arg" in {
val parser = new CLIArgumentsParser(new TestConsole())
val lib = TestUtils.getMyLocalSpellWithLib
val value = parser.parse(Array("--local-spell", lib.getAbsolutePath))
assert(value.isLeft)
val commandToRun = value.left.get
assert(commandToRun.isInstanceOf[RunWeaveCommand])
val runWeaveCommand = commandToRun.asInstanceOf[RunWeaveCommand]
assert(runWeaveCommand.config.filesToWatch.size == 2)
assert(!runWeaveCommand.config.watch)
}

"should set the watch command correctly" in {
val parser = new CLIArgumentsParser(new TestConsole())
val lib = TestUtils.getMyLocalSpellWithLib
val value = parser.parse(Array("--watch","--local-spell", lib.getAbsolutePath))
assert(value.isLeft)
val commandToRun = value.left.get
assert(commandToRun.isInstanceOf[RunWeaveCommand])
val runWeaveCommand = commandToRun.asInstanceOf[RunWeaveCommand]
assert(runWeaveCommand.config.filesToWatch.size == 2)
assert(runWeaveCommand.config.watch)
}


"should parse correctly when using literal script" in {
val parser = new CLIArgumentsParser(new TestConsole())
val value = parser.parse(Array("'Test'"))
assert(value.isLeft)
val commandToRun = value.left.get
assert(commandToRun.isInstanceOf[RunWeaveCommand])
val runWeaveCommand = commandToRun.asInstanceOf[RunWeaveCommand]
assert(runWeaveCommand.config.filesToWatch.isEmpty)
assert(!runWeaveCommand.config.watch)
}

"should parse new-spell correctly" in {
val parser = new CLIArgumentsParser(new TestConsole())
val value = parser.parse(Array("--new-spell", "Test"))
assert(value.isLeft)
val commandToRun = value.left.get
assert(commandToRun.isInstanceOf[CreateSpellCommand])
val runWeaveCommand = commandToRun.asInstanceOf[CreateSpellCommand]
assert(runWeaveCommand.spellName == "Test")
}

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
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 Expand Up @@ -38,6 +42,27 @@ class DataWeaveCLITest extends FreeSpec with Matchers {
}



"should be able to run a local spell" in {
val stream = new ByteArrayOutputStream()
val localSpell: File = TestUtils.getMyLocalSpell
val i = new DataWeaveCLIRunner().run(Array("--local-spell", localSpell.getAbsolutePath), new TestConsole(System.in, stream))
val source = Source.fromBytes(stream.toByteArray, "UTF-8")
val result: String = source.mkString
result.trim shouldBe "\"DW Rules\""
}


"should be able to run a local spell with a library" in {
val stream = new ByteArrayOutputStream()
val localSpell: File = TestUtils.getMyLocalSpellWithLib
val i = new DataWeaveCLIRunner().run(Array("--local-spell", localSpell.getAbsolutePath), new TestConsole(System.in, stream))
val source = Source.fromBytes(stream.toByteArray, "UTF-8")
val result: String = source.mkString
result.trim shouldBe "\"DW Rules\""
}


"should work with simple script and not output" in {
val stream = new ByteArrayOutputStream()
new DataWeaveCLIRunner().run(Array("(1 to 3)[0]"), new TestConsole(System.in, stream))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,34 @@ import java.io.InputStream
import java.io.OutputStream
import scala.collection.mutable.ArrayBuffer

class TestConsole(val in: InputStream, val out: OutputStream, val envVars: Map[String, String] = Map()) extends Console {
class TestConsole(val in: InputStream = System.in, val out: OutputStream = System.out, val envVars: Map[String, String] = Map()) extends Console {

val infoMessages = new ArrayBuffer[String]()
val errorMessages = new ArrayBuffer[String]()
val fatalMessages = new ArrayBuffer[String]()
val warnMessages = new ArrayBuffer[String]()
var clearCount: Int = 0


override def info(message: String): Unit = {
infoMessages +=(message)
infoMessages += (message)
}

override def error(message: String): Unit = {
errorMessages +=(message)
errorMessages += (message)
}

override def fatal(message: String): Unit = {
fatalMessages +=(message)
fatalMessages += (message)
}

override def warn(message: String): Unit = {
warnMessages +=(message)
warnMessages += (message)
}

override def clear(): Unit = DefaultConsole.clear()
override def clear(): Unit = {
clearCount = clearCount + 1
}

override def envVar(name: String): Option[String] = envVars.get(name)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.mule.weave.dwnative.cli

import org.mule.weave.dwnative.utils.FileUtils

import java.io.File
import java.nio.file.Files
import java.nio.file.Path

object TestUtils {

def getMyLocalSpell = {
val file = getSpellsFolder()
val localSpell = new File(file, "MyLocalSpell")
localSpell
}

def getMyLocalSpellWithLib = {
val file = getSpellsFolder()
val localSpell = new File(file, "MyLocalSpellWithLib")
localSpell
}


def getSpellsFolder(): File = {
val spellsUrls = getClass.getClassLoader.getResource("spells/spells.txt")
val file = new File(spellsUrls.getFile)
val workingDirectory: Path = Files.createTempDirectory("spell")
FileUtils.copyFolder(file.getParentFile.toPath, workingDirectory)
workingDirectory.toFile
}

}

0 comments on commit 5ab915a

Please sign in to comment.