|
| 1 | +import sbt._ |
| 2 | +import sbt.complete._, Parser._, Parsers._ |
| 3 | + |
| 4 | +object PartestUtil { |
| 5 | + private case class TestFiles(srcPath: String, globalBase: File, testBase: File) { |
| 6 | + private val testCaseDir = new SimpleFileFilter(f => f.isDirectory && f.listFiles.nonEmpty && !(f.getParentFile / (f.name + ".res")).exists) |
| 7 | + private val testCaseFilter = GlobFilter("*.scala") | GlobFilter("*.java") | GlobFilter("*.res") || testCaseDir |
| 8 | + private def testCaseFinder = (testBase / srcPath).*(AllPassFilter).*(testCaseFilter) |
| 9 | + private val basePaths = allTestCases.map(_._2.split('/').take(3).mkString("/") + "/").distinct |
| 10 | + |
| 11 | + def allTestCases = testCaseFinder.pair(relativeTo(globalBase)) |
| 12 | + def basePathExamples = new FixedSetExamples(basePaths) |
| 13 | + private def equiv(f1: File, f2: File) = f1.getCanonicalFile == f2.getCanonicalFile |
| 14 | + def parentChain(f: File): Iterator[File] = |
| 15 | + if (f == null || !f.exists) Iterator() |
| 16 | + else Iterator(f) ++ (if (f.getParentFile == null) Nil else parentChain(f.getParentFile)) |
| 17 | + def isParentOf(parent: File, f2: File, maxDepth: Int) = |
| 18 | + parentChain(f2).take(maxDepth).exists(p1 => equiv(p1, parent)) |
| 19 | + def isTestCase(f: File) = { |
| 20 | + val grandParent = if (f != null && f.getParentFile != null) f.getParentFile.getParentFile else null |
| 21 | + grandParent != null && equiv(grandParent, testBase / srcPath) && testCaseFilter.accept(f) |
| 22 | + } |
| 23 | + def mayContainTestCase(f: File) = { |
| 24 | + isParentOf(testBase / srcPath, f, 2) || isParentOf(f, testBase / srcPath, Int.MaxValue) |
| 25 | + } |
| 26 | + } |
| 27 | + /** A parser for the custom `partest` command */ |
| 28 | + def partestParser(globalBase: File, testBase: File): Parser[String] = { |
| 29 | + val knownUnaryOptions = List( |
| 30 | + "--pos", "--neg", "--run", "--jvm", "--res", "--ant", "--scalap", "--specialized", |
| 31 | + "--scalacheck", "--instrumented", "--presentation", "--failed", "--update-check", |
| 32 | + "--show-diff", "--verbose", "--terse", "--debug", "--version", "--self-test", "--help") |
| 33 | + val srcPathOption = "--srcpath" |
| 34 | + val grepOption = "--grep" |
| 35 | + |
| 36 | + // HACK: if we parse `--srpath scaladoc`, we overwrite this var. The parser for test file paths |
| 37 | + // then lazily creates the examples based on the current value. |
| 38 | + // TODO is there a cleaner way to do this with SBT's parser infrastructure? |
| 39 | + var srcPath = "files" |
| 40 | + var _testFiles: TestFiles = null |
| 41 | + def testFiles = { |
| 42 | + if (_testFiles == null || _testFiles.srcPath != srcPath) _testFiles = new TestFiles(srcPath, globalBase, testBase) |
| 43 | + _testFiles |
| 44 | + } |
| 45 | + val TestPathParser = ParserUtil.FileParser( |
| 46 | + new SimpleFileFilter(f => testFiles.isTestCase(f)), |
| 47 | + new SimpleFileFilter(f => testFiles.mayContainTestCase(f)), globalBase) |
| 48 | + |
| 49 | + // allow `--grep "is unchecked" | --grep *t123*, in the spirit of ./bin/partest-ack |
| 50 | + // superset of the --grep built into partest itself. |
| 51 | + val Grep = { |
| 52 | + def expandGrep(x: String): Seq[String] = { |
| 53 | + val matchingFileContent = try { |
| 54 | + val Pattern = ("(?i)" + x).r |
| 55 | + testFiles.allTestCases.filter { |
| 56 | + case (testFile, testPath) => |
| 57 | + val assocFiles = List(".check", ".flags").map(testFile.getParentFile / _) |
| 58 | + val sourceFiles = if (testFile.isFile) List(testFile) else testFile.**(AllPassFilter).get.toList |
| 59 | + val allFiles = testFile :: assocFiles ::: sourceFiles |
| 60 | + allFiles.exists { f => f.exists && f.isFile && Pattern.findFirstIn(IO.read(f)).isDefined } |
| 61 | + } |
| 62 | + } catch { |
| 63 | + case _: Throwable => Nil |
| 64 | + } |
| 65 | + val matchingFileName = try { |
| 66 | + val filter = GlobFilter("*" + x + "*") |
| 67 | + testFiles.allTestCases.filter(x => filter.accept(x._1.name)) |
| 68 | + } catch { |
| 69 | + case t: Throwable => Nil |
| 70 | + } |
| 71 | + (matchingFileContent ++ matchingFileName).map(_._2).distinct.sorted |
| 72 | + } |
| 73 | + |
| 74 | + val completion = Completions.strict(Set("<filename glob>", "<regex> (for source, flags or checkfile contents)").map(s => Completion.displayOnly(s))) |
| 75 | + val tokenCompletion = TokenCompletions.fixed((seen, level) => completion) |
| 76 | + |
| 77 | + val globOrPattern = StringBasic.map(expandGrep).flatMap { |
| 78 | + case Seq() => failure("no tests match pattern / glob") |
| 79 | + case x => success(x.mkString(" ")) |
| 80 | + } |
| 81 | + token(grepOption <~ Space) ~> token(globOrPattern, tokenCompletion) |
| 82 | + } |
| 83 | + |
| 84 | + val SrcPath = ((token(srcPathOption) <~ Space) ~ token(StringBasic.examples(Set("files", "pending", "scaladoc")))) map { |
| 85 | + case opt ~ path => |
| 86 | + srcPath = path |
| 87 | + opt + " " + path |
| 88 | + } |
| 89 | + val P = oneOf(knownUnaryOptions.map(x => token(x))) | SrcPath | TestPathParser | Grep |
| 90 | + (Space ~> repsep(P, oneOrMore(Space))).map(_.mkString(" ")).?.map(_.getOrElse("")) <~ OptSpace |
| 91 | + } |
| 92 | +} |
0 commit comments