Skip to content

Add VersionCheck tool. Refs #370, #331 #464

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions testgen/src/main/resources/version-check-ignore.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
binary
hexadecimal
octal
trinary
meetup
72 changes: 72 additions & 0 deletions testgen/src/main/scala/VersionCheck.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import java.io.File

import testgen.{CanonicalDataParser, Exercise}

import scala.io.Source

/**
* Tool to verify implemented version of exercises vs the canonical version.
* The scala project and the problem-specifications project should be
* pulled before running this tool.
*/
object VersionCheck {
private def getImplementedVersion(file: File): String = {
val prefix = "/** @version "
val suffix1 = " */"
val suffix2 = " **/"
val versionLine = Source.fromFile(file).getLines
.find(l => l.startsWith(prefix))
versionLine match {
case Some(l) =>
l.stripPrefix(prefix)
.stripSuffix(suffix1)
.stripSuffix(suffix2)
case None => "No version in file"
}
}

private def getCanonicalVersion(file: File): String = {
val exercise @ Exercise(name, version, cases, comments) =
CanonicalDataParser.parse(file)
version
}

private def getCanonicaDataPath(exerciseName: String,
problemSpecDir: String): File =
new File(new File(problemSpecDir, exerciseName), "canonical-data.json")

private val ignoreExercises: Set[String] =
Source.fromFile(new File("./src/main/resources/version-check-ignore.txt")).getLines.toSet

def main(args: Array[String]): Unit = {
if (args.length != 1) {
println("Usage VersionCheck /path/ where path is the filesystem path to the problem-specifications exercises directory.")
System.exit(-1)
}

val problemSpecDir = args.head

val exerciseDirs = new File("../exercises").listFiles()
.toList
.filter(f => f.isDirectory)
.filter(f => !ignoreExercises.contains(f.getName))
.sorted

exerciseDirs.foreach(f => {
new File(f, "src/test/scala").listFiles()
.toList
.filter(testFile => testFile.getName.endsWith("Test.scala")) match {
case x::_ =>
val implementedVersion = getImplementedVersion(x)
val canonicalDataPath = getCanonicaDataPath(f.getName, problemSpecDir)
if (canonicalDataPath.exists()) {
val canonicalVersion = getCanonicalVersion(canonicalDataPath)
if (!implementedVersion.equals(canonicalVersion)) {
println(s"${f.getName}\timplemented version - $implementedVersion\tcanonical version - $canonicalVersion")
}
}
case _ =>
}
})
}
}