@@ -5,7 +5,7 @@ import dotty.tools.dotc.{Main, Bench, Driver}
55import dotty .tools .dotc .reporting .Reporter
66import scala .collection .mutable .ListBuffer
77import scala .reflect .io .{ Path , Directory , File => SFile }
8- import scala .tools .partest .nest .FileManager
8+ import scala .tools .partest .nest .{ FileManager , NestUI }
99import java .io .{ RandomAccessFile , File => JFile }
1010
1111import org .junit .Test
@@ -19,15 +19,19 @@ import org.junit.Test
1919 * generated sources.
2020 *
2121 * Through overriding the partestableXX methods, tests can always be run as
22- * JUnit compiler tests.
22+ * JUnit compiler tests. Run tests cannot be run by JUnit, only by partest.
2323 *
24- * A test can either be a file or a directory. The test is in a parent
25- * directory that determines the kind of test:
24+ * A test can either be a file or a directory. Partest will generate a
25+ * <test>-<kind>.log file with output of failed tests. Partest reads compiler
26+ * flags and the number of errors expected from a neg test from <test>.flags
27+ * and <test>.nerr files (also generated). The test is in a parent directory
28+ * that determines the kind of test:
2629 * - pos: checks that compilation succeeds
2730 * - neg: checks that compilation fails with the given number of errors
28- * (- run: compilation succeeds and running generates the given output.)
29- * For partest, compiler flags and the number of errors expected from a neg
30- * test are read from test.flags and test.nerr files (also generated).
31+ * - run: compilation succeeds, partest: test run generates the output in
32+ * <test>.check. Run tests always need to be:
33+ * object Test { def main(args: Array[String]): Unit = ... }
34+ *
3135 */
3236abstract class CompilerTest extends DottyTest {
3337
@@ -60,8 +64,11 @@ abstract class CompilerTest extends DottyTest {
6064 CompilerTest .init
6165
6266 /** Always run with JUnit. */
63- def compileLine (cmdLine : String , xerrors : Int = 0 )(implicit defaultOptions : List [String ]): Unit =
67+ def compileLine (cmdLine : String , xerrors : Int = 0 )(implicit defaultOptions : List [String ]): Unit = {
68+ if (generatePartestFiles)
69+ NestUI .echoWarning(" WARNING: compileLine will always run with JUnit, no partest files generated." )
6470 compileArgs(cmdLine.split(" \n " ), xerrors)
71+ }
6572
6673 /** Compiles the given code file.
6774 *
@@ -73,12 +80,15 @@ abstract class CompilerTest extends DottyTest {
7380 * @param extension the file extension, .scala by default
7481 * @param defaultOptions more arguments to the compiler
7582 */
76- def compileFile (prefix : String , fileName : String , args : List [String ] = Nil , xerrors : Int = 0 , extension : String = " .scala" )
83+ def compileFile (prefix : String , fileName : String , args : List [String ] = Nil , xerrors : Int = 0 ,
84+ extension : String = " .scala" , runTest : Boolean = false )
7785 (implicit defaultOptions : List [String ]): Unit = {
7886 if (! generatePartestFiles || ! partestableFile(prefix, fileName, extension, args ++ defaultOptions, xerrors)) {
87+ if (runTest)
88+ NestUI .echoWarning(s " WARNING: run tests can only be run by partest, JUnit just verifies compilation: $prefix$fileName$extension" )
7989 compileArgs((s " $prefix$fileName$extension" :: args).toArray, xerrors)
8090 } else {
81- val kind = testKind(xerrors)
91+ val kind = testKind(prefix, xerrors, runTest )
8292 println(s " generating partest files for test file: $prefix$fileName$extension of kind $kind" )
8393
8494 val sourceFile = new JFile (prefix + fileName + extension)
@@ -90,11 +100,17 @@ abstract class CompilerTest extends DottyTest {
90100 }
91101 }
92102 }
103+ def runFile (prefix : String , fileName : String , args : List [String ] = Nil , xerrors : Int = 0 ,
104+ extension : String = " .scala" )(implicit defaultOptions : List [String ]): Unit =
105+ compileFile(prefix, fileName, args, xerrors, extension, true )
93106
94107 /** Compiles the code files in the given directory together. If args starts
95108 * with "-deep", all files in subdirectories (and so on) are included. */
96- def compileDir (prefix : String , dirName : String , args : List [String ] = Nil , xerrors : Int = 0 )(implicit defaultOptions : List [String ]): Unit = {
109+ def compileDir (prefix : String , dirName : String , args : List [String ] = Nil , xerrors : Int = 0 , runTest : Boolean = false )
110+ (implicit defaultOptions : List [String ]): Unit = {
97111 if (! generatePartestFiles || ! partestableDir(prefix, dirName, args ++ defaultOptions, xerrors)) {
112+ if (runTest)
113+ NestUI .echoWarning(s " WARNING: run tests can only be run by partest, JUnit just verifies compilation: $prefix$dirName" )
98114 val dir = Directory (prefix + dirName)
99115 val (files, normArgs) = args match {
100116 case " -deep" :: args1 => (dir.deepFiles, args1)
@@ -107,18 +123,24 @@ abstract class CompilerTest extends DottyTest {
107123 case " -deep" :: args1 => (flattenDir(prefix, dirName), args1 ++ defaultOptions, " deep" )
108124 case _ => (new JFile (prefix + dirName), args ++ defaultOptions, " shallow" )
109125 }
110- val kind = testKind(xerrors)
126+ val kind = testKind(prefix, xerrors, runTest )
111127 println(s " generating partest files for test directory ( $deep): $prefix$dirName of kind $kind" )
112128
113129 if (sourceDir.exists) {
114130 val firstDest = Directory (DPConfig .testRoot + JFile .separator + kind + JFile .separator + dirName)
115- computeDestAndCopyFiles(sourceDir, firstDest, kind, args ++ defaultOptions, xerrors.toString)
116- if (deep == " deep" ) sourceDir.delete
131+ computeDestAndCopyFiles(sourceDir, firstDest, kind, flags, xerrors.toString)
132+ if (deep == " deep" ) {
133+ sourceDir.listFiles.foreach(_.delete)
134+ sourceDir.delete
135+ }
117136 } else {
118137 throw new java.io.FileNotFoundException (s " Unable to locate test dir $prefix$dirName" )
119138 }
120139 }
121140 }
141+ def runDir (prefix : String , dirName : String , args : List [String ] = Nil , xerrors : Int = 0 )
142+ (implicit defaultOptions : List [String ]): Unit =
143+ compileDir(prefix, dirName, args, xerrors, true )
122144
123145 /** Compiles each source in the directory path separately by calling
124146 * compileFile resp. compileDir. */
@@ -161,10 +183,18 @@ abstract class CompilerTest extends DottyTest {
161183 }
162184
163185 // In particular, don't copy flags from scalac tests
164- private val extensionsToCopy = scala.collection.immutable.HashSet (" scala" , " java" )
186+ private val extensionsToCopy = scala.collection.immutable.HashSet (" scala" , " java" , " check " )
165187
166188 /** Determines what kind of test to run. */
167- private def testKind (xerrors : Int ) = if (xerrors > 0 ) " neg" else " pos"
189+ private def testKind (prefixDir : String , xerrors : Int , runTest : Boolean ) = {
190+ if (runTest) " run"
191+ else if (xerrors > 0 ) " neg"
192+ else if (prefixDir.endsWith(" run" + JFile .separator)) {
193+ NestUI .echoWarning(" WARNING: test is being run as pos test despite being in a run directory. " +
194+ " Use runFile/runDir instead of compileFile/compileDir to do a run test" )
195+ " pos"
196+ } else " pos"
197+ }
168198
169199 /** The three possibilities: no generated sources exist yet, the same sources
170200 * exist already, different sources exist. */
@@ -184,7 +214,7 @@ abstract class CompilerTest extends DottyTest {
184214 val flags = oldFlags.map(f => if (f == oldOutput) partestOutput else f)
185215
186216 getExisting(dest).isDifferent(source, flags, nerr) match {
187- case NotExists => copyFiles(source, dest, partestOutput, flags, nerr)
217+ case NotExists => copyFiles(source, dest, partestOutput, flags, nerr, kind )
188218 case ExistsSame => // nothing else to do
189219 case ExistsDifferent =>
190220 val nextDest = dest.parent / (dest match {
@@ -195,8 +225,8 @@ abstract class CompilerTest extends DottyTest {
195225 }
196226 }
197227
198- /** Copies the test sources and creates flags, nerr and output files. */
199- private def copyFiles (sourceFile : Path , dest : Path , partestOutput : String , flags : List [String ], nerr : String ) = {
228+ /** Copies the test sources. Creates flags, nerr, check and output files. */
229+ private def copyFiles (sourceFile : Path , dest : Path , partestOutput : String , flags : List [String ], nerr : String , kind : String ) = {
200230 recCopyFiles(sourceFile, dest)
201231
202232 new JFile (partestOutput).mkdirs
@@ -205,17 +235,24 @@ abstract class CompilerTest extends DottyTest {
205235 dest.changeExtension(" flags" ).createFile(true ).writeAll(flags.mkString(" " ))
206236 if (nerr != " 0" )
207237 dest.changeExtension(" nerr" ).createFile(true ).writeAll(nerr)
238+ sourceFile.changeExtension(" check" ).ifFile({ check =>
239+ if (kind == " run" )
240+ FileManager .copyFile(check.jfile, dest.changeExtension(" check" ).jfile)
241+ else
242+ NestUI .echoWarning(s " WARNING: ignoring $check for test kind $kind" )
243+ })
244+
208245 }
209246
210- /** Recursively copy over files and directories, excluding extensions that
211- * aren't in extensionsToCopy. */
247+ /** Recursively copy over source files and directories, excluding extensions
248+ * that aren't in extensionsToCopy. */
212249 private def recCopyFiles (sourceFile : Path , dest : Path ): Unit = {
213250 processFileDir(sourceFile, { sf =>
214251 if (extensionsToCopy.contains(sf.extension)) {
215252 dest.parent.jfile.mkdirs
216253 FileManager .copyFile(sourceFile.jfile, dest.jfile)
217254 } else {
218- println (s " warning : ignoring $sf" )
255+ NestUI .echoWarning (s " WARNING : ignoring $sf" )
219256 }
220257 }, { sdir =>
221258 dest.jfile.mkdirs
0 commit comments