-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding more tests to validate spells works correctly
- Loading branch information
Showing
14 changed files
with
222 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
native-cli/src/main/scala/org/mule/weave/dwnative/cli/commands/CreateSpellCommand.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
native-cli/src/main/scala/org/mule/weave/dwnative/utils/FileUtils.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"DW Rules" |
1 change: 1 addition & 0 deletions
1
native-cli/src/test/resources/spells/MyLocalSpellWithLib/src/Main.dwl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
MyLib::test() |
1 change: 1 addition & 0 deletions
1
native-cli/src/test/resources/spells/MyLocalSpellWithLib/src/MyLib.dwl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
fun test() = "DW Rules" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
55 changes: 55 additions & 0 deletions
55
native-cli/src/test/scala/org/mule/weave/dwnative/cli/ArgumentsParserTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
native-cli/src/test/scala/org/mule/weave/dwnative/cli/TestUtils.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
} |