|
| 1 | +import java.io.File |
| 2 | + |
| 3 | +import testgen.{CanonicalDataParser, Exercise} |
| 4 | + |
| 5 | +import scala.io.Source |
| 6 | + |
| 7 | +/** |
| 8 | + * Tool to verify implemented version of exercises vs the canonical version. |
| 9 | + * The scala project and the problem-specifications project should be |
| 10 | + * pulled before running this tool. |
| 11 | + */ |
| 12 | +object VersionCheck { |
| 13 | + private def getImplementedVersion(file: File): String = { |
| 14 | + val prefix = "/** @version " |
| 15 | + val suffix1 = " */" |
| 16 | + val suffix2 = " **/" |
| 17 | + val versionLine = Source.fromFile(file).getLines |
| 18 | + .find(l => l.startsWith(prefix)) |
| 19 | + versionLine match { |
| 20 | + case Some(l) => |
| 21 | + l.stripPrefix(prefix) |
| 22 | + .stripSuffix(suffix1) |
| 23 | + .stripSuffix(suffix2) |
| 24 | + case None => "No version in file" |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + private def getCanonicalVersion(file: File): String = { |
| 29 | + val exercise @ Exercise(name, version, cases, comments) = |
| 30 | + CanonicalDataParser.parse(file) |
| 31 | + version |
| 32 | + } |
| 33 | + |
| 34 | + private def getCanonicaDataPath(exerciseName: String, |
| 35 | + problemSpecDir: String): File = |
| 36 | + new File(new File(problemSpecDir, exerciseName), "canonical-data.json") |
| 37 | + |
| 38 | + private val ignoreExercises: Set[String] = |
| 39 | + Source.fromFile(new File("./src/main/resources/version-check-ignore.txt")).getLines.toSet |
| 40 | + |
| 41 | + def main(args: Array[String]): Unit = { |
| 42 | + if (args.length != 1) { |
| 43 | + println("Usage VersionCheck /path/ where path is the filesystem path to the problem-specifications exercises directory.") |
| 44 | + System.exit(-1) |
| 45 | + } |
| 46 | + |
| 47 | + val problemSpecDir = args.head |
| 48 | + |
| 49 | + val exerciseDirs = new File("../exercises").listFiles() |
| 50 | + .toList |
| 51 | + .filter(f => f.isDirectory) |
| 52 | + .filter(f => !ignoreExercises.contains(f.getName)) |
| 53 | + .sorted |
| 54 | + |
| 55 | + exerciseDirs.foreach(f => { |
| 56 | + new File(f, "src/test/scala").listFiles() |
| 57 | + .toList |
| 58 | + .filter(testFile => testFile.getName.endsWith("Test.scala")) match { |
| 59 | + case x::_ => |
| 60 | + val implementedVersion = getImplementedVersion(x) |
| 61 | + val canonicalDataPath = getCanonicaDataPath(f.getName, problemSpecDir) |
| 62 | + if (canonicalDataPath.exists()) { |
| 63 | + val canonicalVersion = getCanonicalVersion(canonicalDataPath) |
| 64 | + if (!implementedVersion.equals(canonicalVersion)) { |
| 65 | + println(s"${f.getName}\timplemented version - $implementedVersion\tcanonical version - $canonicalVersion") |
| 66 | + } |
| 67 | + } |
| 68 | + case _ => |
| 69 | + } |
| 70 | + }) |
| 71 | + } |
| 72 | +} |
0 commit comments