Skip to content

Commit

Permalink
Removed eo compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
bugdea1er committed Mar 31, 2022
1 parent d853396 commit 950ddaa
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 153 deletions.
105 changes: 25 additions & 80 deletions checker/src/main/scala/org/polystat/py2eo/checker/Check.scala
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
package org.polystat.py2eo.checker

import org.polystat.py2eo.checker.CompilingResult.CompilingResult
import org.polystat.py2eo.checker.Mutate.Mutation
import org.polystat.py2eo.checker.Mutate.Mutation.Mutation
import org.polystat.py2eo.transpiler.Transpile
import org.yaml.snakeyaml.Yaml

import java.nio.file.Files
import java.nio.file.StandardCopyOption.REPLACE_EXISTING
import java.util.concurrent.TimeUnit
import scala.language.postfixOps
import scala.reflect.io.{Directory, File, Path}
import scala.reflect.io.{File, Path}
import scala.sys.error
import scala.sys.process.Process

object Check {

// TODO: get these files as resources
private val resourcesPath = Directory.Current.get / "checker/src/test/resources/org/polystat/py2eo/checker"
private val runEOPath = resourcesPath / "runEO"

/**
* Apply analysis to every yaml test in the input directory
*
Expand All @@ -40,106 +32,59 @@ object Check {

def diffName(test: Path, mutation: Mutation): String = s"${test.stripExtension}-$mutation-diff.txt"

def expected(mutation: Mutation): CompilingResult = mutation match {
case Mutation.nameMutation => CompilingResult.transpiled
case Mutation.literalMutation => CompilingResult.compiled
case Mutation.operatorMutation => CompilingResult.transpiled
case Mutation.reverseBoolMutation => CompilingResult.transpiled
case Mutation.breakToContinue => CompilingResult.transpiled
case Mutation.breakSyntax => CompilingResult.failed
case Mutation.literalToIdentifier => CompilingResult.transpiled
case Mutation.removeBrackets => CompilingResult.transpiled
case Mutation.addExcessParam => CompilingResult.transpiled
case Mutation.swapParam => CompilingResult.transpiled
}

private def check(inputPath: Path, outputPath: Path, mutations: Iterable[Mutation]): Iterator[TestResult] = {
if (inputPath isDirectory) {
inputPath.toDirectory.list flatMap (item => check(item, outputPath, mutations))
} else if (inputPath.extension == "yaml") {
Iterator(check(inputPath toFile, outputPath, mutations))
} else if (inputPath hasExtension "yaml") {
Iterator(check(inputPath.toFile, outputPath, mutations))
} else {
Iterator empty
}
}

private def check(test: File, outputPath: Path, mutations: Iterable[Mutation]): TestResult = {
Transpile(test.stripExtension, parseYaml(test)) match {
case None => TestResult(test.stripExtension, Left(CompilingResult.failed))
val module = test.stripExtension
println(s"checking $module")
Transpile(module, parseYaml(test)) match {
case None => TestResult(module, None)
case Some(transpiled) =>
val file = File(outputPath / test.changeExtension("eo").name)
file writeAll transpiled

val originalResult = if (!compile(file)) {
CompilingResult.transpiled
} else {
run(file)
}

if (originalResult == CompilingResult.passed) {
val resultList = for {mutation <- mutations} yield (mutation, check(test, outputPath, mutation))
TestResult(test.stripExtension, Right(resultList.toMap[Mutation, CompilingResult]))
} else {
TestResult(test.stripExtension, Left(originalResult))
}
val resultList = mutations map (mutation => (mutation, check(test, outputPath, mutation)))
TestResult(module, Some(resultList.toMap[Mutation, CompilingResult]))
}
}

private def check(test: File, outputPath: Path, mutation: Mutation): CompilingResult = {
val original = parseYaml(test)
val mutant = Mutate(original, mutation, 1)
println(s"checking ${test.stripExtension} with $mutation")

if (mutant equals original) {
CompilingResult.invalid
} else {
Transpile(test.stripExtension, mutant) match {
val module = test.stripExtension
Transpile(module, mutant) match {
case None => CompilingResult.failed
case Some(transpiled) =>
val originalFile = File(outputPath / test.changeExtension("eo").name)
val mutatedFile = File(outputPath / s"${test.stripExtension}-$mutation")
val diffFile = File(outputPath / diffName(test, mutation))
mutatedFile.writeAll(transpiled)

val diff = Process(s"diff $originalFile $mutatedFile", outputPath.jfile).lazyLines_!
diffFile.writeAll(diff.mkString("\n"))

// TODO: actually need to get rid of old eo-files in that directory
if (!compile(mutatedFile)) CompilingResult.transpiled else run(mutatedFile)
val mutant = File(outputPath / s"$module-$mutation").addExtension("eo")
mutant writeAll transpiled

val original = File(outputPath / test.changeExtension("eo").name)
val diff = Process(s"diff $original $mutant", outputPath.jfile).lazyLines_!

if (diff isEmpty) {
CompilingResult.nodiff
} else {
val diffFile = File(outputPath / diffName(test, mutation))
diffFile writeAll diff.mkString("\n")
CompilingResult.passed
}
}
}
}

private def compile(file: File): Boolean = {
val result = Files.copy(file.jfile.toPath, File(runEOPath / file.name).jfile.toPath, REPLACE_EXISTING)
val ret = Process("mvn clean test", runEOPath.jfile).! == 0

Files delete result

ret
}

private def run(file: File): CompilingResult = {
val test = Files.copy(file.jfile.toPath, File(runEOPath / "Test.eo").jfile.toPath, REPLACE_EXISTING)

// Dunno why but it doesn't work without this line
val dir = new java.io.File(runEOPath.jfile.getPath)

val process = new ProcessBuilder("mvn clean test").directory(dir).start
val ret = process.waitFor(40, TimeUnit.SECONDS)

Files delete test

if (ret) {
if (process.exitValue == 0) {
CompilingResult.passed
} else {
CompilingResult.compiled
}
} else {
CompilingResult.timeout
}
}

private def parseYaml(file: File): String = {
val input = file slurp
val map = new Yaml load[java.util.Map[String, String]] input
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ object CompilingResult extends Enumeration {
type CompilingResult = Value
val invalid: Value = Value("n/a")
val failed: Value = Value("failed")
val transpiled: Value = Value("transpiled")
val compiled: Value = Value("compiled")
val nodiff: Value = Value("no diff")
val passed: Value = Value("passed")
val timeout: Value = Value("timeout")
}

case class TestResult(name: String, results: Either[CompilingResult, Map[Mutation, CompilingResult]])
case class TestResult(name: String, results: Option[Map[Mutation, CompilingResult]])
29 changes: 13 additions & 16 deletions checker/src/main/scala/org/polystat/py2eo/checker/Write.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.polystat.py2eo.checker

import org.polystat.py2eo.checker.Check.{diffName, expected}
import org.polystat.py2eo.checker.CompilingResult.CompilingResult
import org.polystat.py2eo.checker.Mutate.Mutation.Mutation

Expand Down Expand Up @@ -41,33 +40,31 @@ object Write {
private def row(test: TestResult, mutations: Iterable[Mutation]): String = {
lazy val name = test.name
test.results match {
case Left(stage) =>
lazy val colspan = mutations.size
lazy val str = s"<td colspan=\"$colspan\" class=\"data\">Original test $stage</td>"
case None =>
lazy val colspan = mutations size
lazy val str = s"<td colspan=\"$colspan\" class=\"data\">Original test couldn't be transpiled</td>"
s"<tr>\n<th class=\"left\">$name</th>\n$str</tr>\n"

case Right(results) =>
case Some(results) =>
lazy val cells = mutations.toList map (mutation => cell(mutation, name, results(mutation)))
s"<tr>\n<th class=\"left\">$name</th>\n${cells mkString}</tr>\n"
}
}

/** Returns table cell with test result */
private def cell(mutation: Mutation, name: String, stage: CompilingResult): String = {
lazy val link = diffName(name, mutation)

lazy val kind = if (stage == CompilingResult.invalid) {
"data"
} else if (stage == expected(mutation)) {
"expected data"
} else {
"unexpected data"
lazy val kind = stage match {
case CompilingResult.invalid => "data"
case CompilingResult.failed => "unexpected data"
case CompilingResult.nodiff => "unexpected data"
case CompilingResult.passed => "expected data"
}

lazy val data = if (Set(CompilingResult.invalid, CompilingResult.failed) contains stage) {
stage toString
} else {
lazy val data = if (stage equals CompilingResult.passed) {
lazy val link = Check.diffName(name, mutation)
s"<a href=\"$link\">$stage</a>"
} else {
stage
}

s"<td class=\"$kind\">$data</td>\n"
Expand Down

This file was deleted.

0 comments on commit 950ddaa

Please sign in to comment.