Skip to content

Commit

Permalink
Improved examples CI job (#215)
Browse files Browse the repository at this point in the history
  • Loading branch information
RCMartins committed Mar 26, 2021
1 parent cfeebd1 commit 0b0fec6
Show file tree
Hide file tree
Showing 14 changed files with 95 additions and 39 deletions.
4 changes: 2 additions & 2 deletions blinky-core/src/main/scala/blinky/internal/Blinky.scala
Expand Up @@ -36,13 +36,13 @@ class Blinky(config: BlinkyConfig) extends SemanticRule("Blinky") {
.map(new Blinky(_))

override def fix(implicit doc: SemanticDocument): Patch = {
val findMutations: FindMutations = new FindMutations(config.activeMutators, doc)

val VirtualFile(fileName, _) = doc.input

if (!fileShouldBeMutated(fileName))
Patch.empty
else {
val findMutations: FindMutations = new FindMutations(config.activeMutators, doc)

def createPatch(
mutantSeq: Seq[Mutant],
needsParens: Boolean
Expand Down
1 change: 1 addition & 0 deletions ci-tests/examples/example1/result.txt
@@ -0,0 +1 @@
[75.0% >= 75.0%]
1 change: 1 addition & 0 deletions ci-tests/examples/example2/result.txt
@@ -0,0 +1 @@
[66.6% >= 66.6%]
1 change: 1 addition & 0 deletions ci-tests/examples/example3/result.txt
@@ -0,0 +1 @@
[100.0% >= 100.0%]
Empty file.
1 change: 1 addition & 0 deletions ci-tests/examples/example4/result.txt
@@ -0,0 +1 @@
[100.0% >= 100.0%]
1 change: 1 addition & 0 deletions ci-tests/examples/example5/result.txt
@@ -0,0 +1 @@
[75.0% >= 75.0%]
1 change: 1 addition & 0 deletions ci-tests/examples/example6/result.txt
@@ -0,0 +1 @@
[100.0% >= 100.0%]
1 change: 1 addition & 0 deletions ci-tests/examples/example7/result.txt
@@ -0,0 +1 @@
[100.0% >= 100.0%]
1 change: 1 addition & 0 deletions ci-tests/examples/example8/result.txt
@@ -0,0 +1 @@
[100.0% >= 100.0%]
2 changes: 1 addition & 1 deletion ci-tests/examples/example9/.blinky.conf
@@ -1,5 +1,5 @@
projectPath = "."
projectName = "example9"
projectName = "example"
filesToMutate = "Example"
options = {
maxRunningTime = 1 minute
Expand Down
2 changes: 0 additions & 2 deletions ci-tests/examples/example9/build.sbt

This file was deleted.

1 change: 1 addition & 0 deletions ci-tests/examples/example9/result.txt
@@ -0,0 +1 @@
[75.0% >= 75.0%]
117 changes: 83 additions & 34 deletions project/RunExamples.scala
@@ -1,6 +1,5 @@
import scala.sys.process._

import ammonite.ops._
import os.copy

object RunExamples {

Expand All @@ -21,50 +20,100 @@ object RunExamples {
System.exit(1)
}

val examples: Seq[(Path, CommandResult)] =
val examplesResult: Seq[(Path, CommandResult, String)] =
examplesToRun.filterNot(_.baseName == "default").map { examplePath =>
println("\n")
val msg = s"Testing $examplePath:"
println("-" * msg.length)
println(msg)
println("-" * msg.length)

preProcessDirectory(defaultDirectory, examplePath)

val confPath = examplePath / ".blinky.conf"
val result = %%(
"cs",
"launch",
s"com.github.rcmartins:blinky-cli_2.12:$versionNumber",
"--",
confPath,
"--verbose",
"true"
)(examplePath)
println(result.out.string)
(examplePath, result)
runExample(versionNumber, defaultDirectory, examplePath)
}

val brokenExamples = examples.filter(_._2.exitCode != 0)
val brokenExamples = examplesResult.filter(_._2.exitCode != 0)

if (brokenExamples.nonEmpty) {
Console.err.println("There were broken tests:")
println(brokenExamples.map { case (path, _) => s"$path" }.mkString("\n"))
println(
brokenExamples.map { case (path, _, extraText) => s"$path\n$extraText" }.mkString("\n")
)
System.exit(1)
} else
println("All tests were successful!")
}

private def preProcessDirectory(defaultDirectory: Path, testDirectory: Path): Unit = {
Process(
command = Seq("bash", "-c", s"""cp -nr $defaultDirectory/* $testDirectory"""),
cwd = pwd.toNIO.toFile
).!
private def preProcessDirectory(
defaultDirectory: Path,
originalExamplePath: Path
): Path = {
val tempExamplePath: Path = tmp.dir()
copy.into(originalExamplePath, tempExamplePath)
val testDirectory: Path = tempExamplePath / originalExamplePath.baseName

def showIfError(result: CommandResult): Unit =
if (result.exitCode != 0)
println(result.err.string)

showIfError(%%("git", "init")(testDirectory))
showIfError(%%("git", "config", "--global", "user.email", "you@example.com")(testDirectory))
showIfError(%%("git", "config", "--global", "user.name", "Your Name")(testDirectory))
showIfError(%%("bash", "-c", s"""cp -nr $defaultDirectory/* $testDirectory""")(testDirectory))
showIfError(%%("git", "add", ".")(testDirectory))
showIfError(%%("git", "commit", "-m", "first commit!")(testDirectory))

val startupScriptName = "startup.sh"
if (exists(testDirectory / startupScriptName)) {
%("chmod", "+x", startupScriptName)(testDirectory)
%(s"./$startupScriptName")(testDirectory)
val startupScript = testDirectory / "startup.sh"
if (exists(startupScript)) {
println(s"Running $startupScript script...")
%("chmod", "+x", startupScript)(testDirectory)
%(startupScript)(testDirectory)
}

testDirectory
}

private def runExample(
versionNumber: String,
defaultDirectory: Path,
originalExamplePath: Path
): (Path, CommandResult, String) = {
val examplePath: Path =
preProcessDirectory(defaultDirectory, originalExamplePath)

println("\n")
val msg = s"Testing $examplePath:"
println("-" * msg.length)
println(msg)
println("-" * msg.length)

val confPath = examplePath / ".blinky.conf"
val result = %%(
"cs",
"launch",
s"com.github.rcmartins:blinky-cli_2.12:$versionNumber",
"--",
confPath,
"--verbose",
"true"
)(examplePath)
println(result.out.string)

val extraCheckText: String =
read(examplePath / "result.txt")
.split("\n")
.map { expectedResult =>
if (!result.out.string.contains(expectedResult))
s"""${"-" * 10}
|Example failed. Expected line does not appear:
|$expectedResult
|${"-" * 10}
|""".stripMargin
else
""
}
.mkString("\n")

val resultUpdated =
if (extraCheckText.nonEmpty)
result.copy(exitCode = 1)
else
result

(examplePath, resultUpdated, extraCheckText)
}

}

0 comments on commit 0b0fec6

Please sign in to comment.