Skip to content

Commit

Permalink
Fix list-spells and add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
machaval committed Nov 24, 2021
1 parent 84457a6 commit d0f07c3
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 128 deletions.
29 changes: 12 additions & 17 deletions native-cli/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ sourceSets {
mainClassName = 'org.mule.weave.dwnative.cli.DataWeaveCLI'


dependencies{
dependencies {
compile group: 'org.mule.weave', name: 'runtime', version: weaveVersion
compile group: 'org.graalvm.sdk', name: 'graal-sdk', version: '1.0.0-rc12'
compile group: 'org.mule.weave', name: 'core-modules', version: weaveVersion
Expand All @@ -41,8 +41,8 @@ dependencies{

ext {
aws = [
accessKey: System.getenv("AWS_ACCESS_KEY_ID"),
secretKey: System.getenv("AWS_SECRET_KEY")
accessKey: System.getenv("AWS_ACCESS_KEY_ID"),
secretKey: System.getenv("AWS_SECRET_KEY")
]
}

Expand All @@ -58,17 +58,12 @@ task genVersions() {
parentFile.mkdirs()
}
final PrintWriter outputPrinter = new PrintWriter(new FileWriter(componentVersion))
def properties = new Properties()
def gradleProperties = new File(project.projectDir, "../gradle.properties")
println(gradleProperties)
properties.load(new FileInputStream(gradleProperties))

outputPrinter.println("package org.mule.weave.v2.version")
outputPrinter.println()
outputPrinter.println("object ComponentVersion {")
def entrySet = properties.entrySet()
for (Map.Entry entry : entrySet) {
outputPrinter.println("\tvar " + entry.getKey() + " = \"" + entry.getValue() + "\"")
}
outputPrinter.println("\tval weaveVersion = \"" + weaveVersion + "\"")
outputPrinter.println("\tval nativeVersion = \"" + nativeVersion + "\"")
outputPrinter.println("}")
outputPrinter.close()
}
Expand All @@ -91,7 +86,7 @@ import org.apache.tools.ant.taskdefs.condition.Os
task distro(dependsOn: nativeImage, type: Zip) {
classifier = "native-distro-" + getOsName()
into("libs") {
from { configurations.runtime.filter {it.getName().contains("ndjson-module")} }
from { configurations.runtime.filter { it.getName().contains("ndjson-module") } }
}
into("bin") {
from 'build/graal/'
Expand Down Expand Up @@ -122,9 +117,9 @@ graal {
option "--allow-incomplete-classpath"
option "-H:EnableURLProtocols=http,https"
option "--initialize-at-run-time=" +
"io.netty,"+
"org.asynchttpclient,"+
"org.mule.weave.v2.module.http.netty.HttpAsyncClientService,"+
"io.netty," +
"org.asynchttpclient," +
"org.mule.weave.v2.module.http.netty.HttpAsyncClientService," +
"scala.util.Random," +
"org.mule.weave.v2.sdk.SPIBasedModuleLoaderProvider\$"
option "--initialize-at-build-time=" +
Expand All @@ -138,7 +133,7 @@ graal {
"org.mule.weave.v2.ts.," +
"org.mule.weave.v2.scope.," +
"org.mule.weave.v2.scope.," +
"org.mule.weave.v2.grammar.,"+
"org.mule.weave.v2.grammar.," +
"org.mule.weave.v2.sdk.," +
"org.mule.weave.v2.utils.," +
"org.mule.weave.v2.versioncheck.," +
Expand All @@ -147,7 +142,7 @@ graal {
"org.parboiled2.," +
"shapeless.syntax."

//For Runtime execution
//For Runtime execution
// "org.mule.weave.v2.interpreted.,"+
// "org.mule.weave.v2.model.values.,"
// "org.mule.weave.v2.model.types.,"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ trait Console {

def error(message: String): Unit

def fatal(message: String): Unit

def warn(message: String): Unit

def clear(): Unit
Expand All @@ -49,7 +51,11 @@ object DefaultConsole extends Console {
}

override def error(message: String): Unit = {
System.err.println(AnsiColor.red(message))
System.err.println(AnsiColor.red("[ERROR] " + message))
}

override def fatal(message: String): Unit = {
System.err.println(AnsiColor.red("[FATAL] " + message))
}

override def clear(): Unit = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package org.mule.weave.dwnative.cli

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

import java.io.PrintWriter
import java.io.StringWriter


object DataWeaveCLI extends App {
{
Expand All @@ -23,7 +26,18 @@ class DataWeaveCLIRunner {
-1
}
case Left(weaveCommand) => {
weaveCommand.exec()
try {
weaveCommand.exec()
} catch {
case exception: Exception => {
val exceptionString = new StringWriter()
exception.printStackTrace(new PrintWriter(exceptionString))
console.error("Unexpected exception happened while executing command. " +
"Please report this as an issue in https://github.com/mulesoft-labs/data-weave-cli/issues with all the details to reproduce.\n" +
s"Stacktrace is: ${exceptionString}")
-1
}
}
}
case _ => {
0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.mule.weave.dwnative.cli.utils.SpellsUtils
import org.mule.weave.dwnative.cli.utils.SpellsUtils._

import java.io.File
import java.io.FileFilter
import scala.io.Source

class ListSpellsCommand(console: Console) extends WeaveCommand {
Expand All @@ -21,30 +22,40 @@ class ListSpellsCommand(console: Console) extends WeaveCommand {
val builder = new StringBuilder()
builder.append("Spells:\n")
val grimoires: File = utils.grimoiresFolders()
val grimoiresDirs: Array[File] = grimoires.listFiles()
var grimoiresDirs: Array[File] = listGrimoires(grimoires)
if (grimoiresDirs == null || !new File(grimoires, DATA_WEAVE_GRIMOIRE_FOLDER).exists()) {
new AddWizardCommand(CloneWizardConfig(null), console).exec()
grimoiresDirs = listGrimoires(grimoires)
}
if (grimoiresDirs != null) {
grimoiresDirs.foreach((g) => {
val name = if (g.getName.equals(DATA_WEAVE_GRIMOIRE_FOLDER)) "" else utils.wizardName(g.getName) + "/"
val spells = g.listFiles()
if (spells != null) {
spells.foreach((s) => {
if (s.isDirectory && !s.isHidden) {
builder.append(s" - ${name}${s.getName}:")
val readme = new File(s, "Readme.md")
if (readme.exists()) {
val source = Source.fromFile(readme, "UTF-8")
builder.append("\n ")
builder.append(source.mkString.replaceAllLiterally("\n", "\n ").slice(0, 450))
grimoiresDirs
.foreach((file) => {
val name = if (file.getName.equals(DATA_WEAVE_GRIMOIRE_FOLDER)) "" else utils.wizardName(file.getName) + "/"
val spells = file.listFiles()
if (spells != null) {
spells.foreach((s) => {
if (s.isDirectory && !s.isHidden) {
builder.append(s" - ${name}${s.getName}:")
val readme = new File(s, "Readme.md")
if (readme.exists()) {
val source = Source.fromFile(readme, "UTF-8")
builder.append("\n ")
builder.append(source.mkString.replaceAllLiterally("\n", "\n ").slice(0, 450))
builder.append("\n")
source.close()
}
builder.append("\n")
source.close()
}
builder.append("\n")
}
})
}
})
})
}
})
}
builder.toString()
}

private def listGrimoires(grimoires: File) = {
grimoires.listFiles(new FileFilter {
override def accept(pathname: File): Boolean = pathname.isDirectory && !pathname.isHidden
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,15 @@ class SpellsUtils(console: Console) {
def wizardName(grimoire: String): String = {
if (grimoire == null)
"DW"
else
grimoire.substring(0, grimoire.length - s"-${DATA_WEAVE_GRIMOIRE_FOLDER}".length)
else {
val length = grimoire.length - s"-${DATA_WEAVE_GRIMOIRE_FOLDER}".length
if (length <= 0) {
console.error("Invalid grimoire name: `" + grimoire + "`")
grimoire
} else {
grimoire.substring(0, length)
}
}
}

def hoursSinceLastUpdate(): Int = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ class DataWeaveCLITest extends FreeSpec with Matchers {


"should take into account the env variable for default output" in {
val console = new TestConsole(System.in, System.out, Map())
new DataWeaveCLIRunner().run(Array("--list-spells"), console)

console.fatalMessages.isEmpty shouldBe (true)
}

"should work when listing all the spells" in {
val stream = new ByteArrayOutputStream()
new DataWeaveCLIRunner().run(Array("root: 'Mariano'"), new TestConsole(System.in, stream, Map(DataWeaveUtils.DW_DEFAULT_OUTPUT_MIMETYPE_VAR -> "application/xml")))
val source = Source.fromBytes(stream.toByteArray, "UTF-8")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
package org.mule.weave.dwnative.cli

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 {

val infoMessages = new ArrayBuffer[String]()
val errorMessages = new ArrayBuffer[String]()
val fatalMessages = new ArrayBuffer[String]()
val warnMessages = new ArrayBuffer[String]()


class TestConsole(val in: InputStream,val out: OutputStream, val envVars: Map[String,String] = Map()) extends Console {
override def info(message: String): Unit = {
infoMessages +=(message)
}

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

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

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

override def clear(): Unit = DefaultConsole.clear()

Expand Down
84 changes: 0 additions & 84 deletions native-common/build.gradle

This file was deleted.

0 comments on commit d0f07c3

Please sign in to comment.