diff options
author | Paul Phillips <paulp@improving.org> | 2010-03-02 22:28:45 +0000 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2010-03-02 22:28:45 +0000 |
commit | 649c18aeae2136b416683d8437012cd40dac1b59 (patch) | |
tree | 9699ff34bd694f51ba223b704315730a3ba04d73 /src | |
parent | 1b5c4b93ecf8f63e81c23c38b277b5aad41d5551 (diff) | |
download | scala-649c18aeae2136b416683d8437012cd40dac1b59.tar.gz scala-649c18aeae2136b416683d8437012cd40dac1b59.tar.bz2 scala-649c18aeae2136b416683d8437012cd40dac1b59.zip |
Added --grep command line option to partest.
test with the string "Manifest" in the source file, you may now do:
./partest --grep Manifest
No review.
Diffstat (limited to 'src')
5 files changed, 66 insertions, 34 deletions
diff --git a/src/compiler/scala/tools/nsc/io/Process.scala b/src/compiler/scala/tools/nsc/io/Process.scala index 8297ab8d4d..306c88a854 100644 --- a/src/compiler/scala/tools/nsc/io/Process.scala +++ b/src/compiler/scala/tools/nsc/io/Process.scala @@ -86,7 +86,7 @@ object Process this } - def withCwd(cwd: File): this.type = { + def withCwd(cwd: Path): this.type = { if (cwd != null) pb directory cwd.jfile @@ -113,7 +113,7 @@ object Process def apply( command: String, env: Map[String, String] = null, - cwd: File = null, + cwd: Path = null, redirect: Boolean = false ): Process = exec(shell(command), env, cwd) @@ -126,7 +126,7 @@ object Process def exec( command: Seq[String], env: Map[String, String] = null, - cwd: File = null, + cwd: Path = null, redirect: Boolean = false ): Process = new ProcessBuilder(command: _*) withEnv env withCwd cwd start diff --git a/src/compiler/scala/tools/nsc/util/CommandLineParser.scala b/src/compiler/scala/tools/nsc/util/CommandLineParser.scala index d2d46a6c8e..c3c99b1e1f 100644 --- a/src/compiler/scala/tools/nsc/util/CommandLineParser.scala +++ b/src/compiler/scala/tools/nsc/util/CommandLineParser.scala @@ -23,14 +23,24 @@ trait ParserUtil extends Parsers { protected implicit def parser2parserPlus[T](p: Parser[T]): ParserPlus[T] = new ParserPlus(p) } -class CommandLine(val args: List[String], val unaryArguments: List[String]) { - def this(args: List[String]) = this(args, Nil) - def this(args: Array[String]) = this(args.toList) - def this(args: Array[String], unaryArguments: List[String]) = this(args.toList, unaryArguments) +case class CommandLine( + args: List[String], + unaryArguments: List[String], + binaryArguments: List[String] +) { + def this(args: List[String]) = this(args, Nil, Nil) + def this(args: Array[String]) = this(args.toList, Nil, Nil) + def this(args: Array[String], unaryArguments: List[String]) = this(args.toList, unaryArguments, Nil) def this(line: String) = this(CommandLineParser tokenize line) - def withUnaryArguments(xs: List[String]) = new CommandLine(args, xs) + def withUnaryArgs(xs: List[String]) = copy(unaryArguments = xs) + def withBinaryArgs(xs: List[String]) = copy(binaryArguments = xs) + def enforceArity = true + def assumeBinary = true + + val Terminator = "--" + def errorFn(msg: String) = println(msg) /** argMap is option -> argument (or "" if it is a unary argument) @@ -40,18 +50,18 @@ class CommandLine(val args: List[String], val unaryArguments: List[String]) { val residual = new ListBuffer[String] def isOption(s: String) = s startsWith "-" def isUnary(s: String) = isOption(s) && (unaryArguments contains s) - def isBinary(s: String) = isOption(s) && !(unaryArguments contains s) + def isBinary(s: String) = isOption(s) && !isUnary(s) && (assumeBinary || (binaryArguments contains s)) def missingArg(opt: String, what: String) = errorFn("Option '%s' requires argument, found %s instead.".format(opt, what)) def loop(args: List[String]): Map[String, String] = args match { - case Nil => Map() - case x :: xs if !isOption(x) => residual += x ; loop(xs) - case x :: xs if isUnary(x) => Map(x -> "") ++ loop(xs) - case x :: Nil => if (enforceArity) missingArg(x, "EOF") ; Map(x -> "") - case "--" :: xs => residual ++= xs ; Map() - case x :: "--" :: xs => residual ++= xs ; Map(x -> "") - case x1 :: x2 :: xs => + case Nil => Map() + case x :: xs if !isOption(x) => residual += x ; loop(xs) + case x :: xs if isUnary(x) => Map(x -> "") ++ loop(xs) + case x :: Nil => if (enforceArity) missingArg(x, "EOF") ; Map(x -> "") + case Terminator :: xs => residual ++= xs ; Map() + case x :: Terminator :: xs => residual ++= xs ; Map(x -> "") + case x1 :: x2 :: xs => if (enforceArity && isOption(x2)) missingArg(x1, x2) diff --git a/src/partest/scala/tools/partest/nest/ConsoleRunner.scala b/src/partest/scala/tools/partest/nest/ConsoleRunner.scala index 3701985e1a..f7eca614ac 100644 --- a/src/partest/scala/tools/partest/nest/ConsoleRunner.scala +++ b/src/partest/scala/tools/partest/nest/ConsoleRunner.scala @@ -16,10 +16,10 @@ import scala.tools.nsc.Properties.{ versionMsg, setProp } import scala.tools.nsc.util.CommandLineParser import scala.tools.nsc.io import scala.tools.nsc.interpreter.returning -import io.{ Path } +import io.{ Path, Process } class ConsoleRunner extends DirectRunner { - import PathSettings.srcDir + import PathSettings.{ srcDir, testRoot } case class TestSet(kind: String, filter: Path => Boolean, msg: String) @@ -60,8 +60,12 @@ class ConsoleRunner extends DirectRunner { "--failed", "--version", "--ansi", "--debug" ) ::: testSetArgs + private val binaryArgs = List( + "--grep", "--srcpath", "--buildpath", "--classpath" + ) + def main(argstr: String) { - val parsed = CommandLineParser(argstr) withUnaryArguments unaryArgs + val parsed = CommandLineParser(argstr) withUnaryArgs unaryArgs withBinaryArgs binaryArgs val args = parsed.residualArgs /** Early return on no args, version, or invalid args */ @@ -82,12 +86,6 @@ class ConsoleRunner extends DirectRunner { else new ConsoleFileManager // auto detection, see ConsoleFileManager.findLatest def argNarrowsTests(x: String) = denotesTestSet(x) || denotesTestFile(x) || denotesTestDir(x) - val enabledTestSets: List[TestSet] = { - val enabledArgs = testSetArgs filter parsed.isSet - - if (args.isEmpty && (enabledArgs.isEmpty || (parsed isSet "--all"))) testSets - else enabledArgs map testSetArgMap - } NestUI._verbose = parsed isSet "--verbose" fileManager.showDiff = parsed isSet "--show-diff" @@ -98,14 +96,33 @@ class ConsoleRunner extends DirectRunner { if (parsed isSet "--timeout") fileManager.timeout = parsed("--timeout") if (parsed isSet "--debug") setProp("partest.debug", "true") - testFiles :::= args map { arg => - val file = new File(arg) - if (!file.exists) { - NestUI.failure("File \"%s\" not found\n" format arg) - System.exit(1) + def addTestFile(file: File) = { + if (!file.exists) + NestUI.failure("Test file '%s' not found, skipping.\n" format file) + else { + NestUI.verbose("adding test file " + file) + testFiles +:= file } + } + + // If --grep is given we suck in every file it matches. + parsed get "--grep" foreach { expr => + val allFiles = srcDir.deepList() filter (_ hasExtension "scala") map (_.toFile) toList + val files = allFiles filter (_.slurp() contains expr) - returning[File](file)(x => NestUI.verbose("adding test file " + x)) + if (files.isEmpty) NestUI.failure("--grep string '%s' matched no files." format expr) + else NestUI.verbose("--grep string '%s' matched %d file(s)".format(expr, files.size)) + + files foreach (x => addTestFile(x.jfile)) + } + args foreach (x => addTestFile(new File(x))) + + // If no file arguments were given, we assume --all + val enabledTestSets: List[TestSet] = { + val enabledArgs = testSetArgs filter parsed.isSet + + if (args.isEmpty && !(parsed isSet "--grep") && (enabledArgs.isEmpty || (parsed isSet "--all"))) testSets + else enabledArgs map testSetArgMap } val dir = diff --git a/src/partest/scala/tools/partest/nest/NestUI.scala b/src/partest/scala/tools/partest/nest/NestUI.scala index c05730c4ff..efff4e8375 100644 --- a/src/partest/scala/tools/partest/nest/NestUI.scala +++ b/src/partest/scala/tools/partest/nest/NestUI.scala @@ -80,6 +80,7 @@ object NestUI { println(" --scalacheck run ScalaCheck tests") println(" --script run script runner tests") println(" --shootout run shootout tests") + println(" --grep <expr> run all tests whose source file contains <expr>") println println(" Other options:") println(" --pack pick compiler/library in build/pack, and run all tests") diff --git a/src/partest/scala/tools/partest/nest/PathSettings.scala b/src/partest/scala/tools/partest/nest/PathSettings.scala index 4ecee721bc..41bba5782e 100644 --- a/src/partest/scala/tools/partest/nest/PathSettings.scala +++ b/src/partest/scala/tools/partest/nest/PathSettings.scala @@ -18,17 +18,21 @@ object PathSettings { private def cwd = Directory.Current getOrElse error("user.dir property not set") private def isPartestDir(d: Directory) = (d.name == "test") && (d / srcDirName isDirectory) + // Directory <root>/test lazy val testRoot: Directory = testRootDir getOrElse { val candidates: List[Directory] = (cwd :: cwd.parents) flatMap (d => List(d, Directory(d / "test"))) candidates find isPartestDir getOrElse error("Directory 'test' not found.") } + // Directory <root>/test/files lazy val srcDir = Directory(testRoot / srcDirName normalize) - lazy val scalaCheck = { - val libdir = Directory(testRoot / "files" / "lib") - libdir.files find (_.name startsWith "scalacheck") getOrElse error("No scalacheck jar found in '%s'" format libdir) + // Directory <root>/test/files/lib + lazy val srcLibDir = Directory(srcDir / "lib") + + lazy val scalaCheck = srcLibDir.files find (_.name startsWith "scalacheck") getOrElse { + error("No scalacheck jar found in '%s'" format srcLibDir) } } |