Skip to content

Commit 98406ae

Browse files
committed
Fix reflective method lookup to work for both scalac & dotty
1 parent d78523d commit 98406ae

File tree

1 file changed

+28
-21
lines changed

1 file changed

+28
-21
lines changed

compiler/test/dotty/tools/dotc/ParallelTesting.scala

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import java.nio.file.{ Files, Path, Paths, NoSuchFileException }
1515
import java.util.concurrent.{ Executors => JExecutors, TimeUnit }
1616
import scala.util.control.NonFatal
1717
import scala.util.Try
18+
import scala.collection.mutable
1819
import java.util.HashMap
1920

2021
trait ParallelTesting {
@@ -539,13 +540,31 @@ trait ParallelTesting {
539540
else (dirs, f :: files)
540541
}
541542

543+
private def getCallingMethod(): String = {
544+
val seen = mutable.Set.empty[String]
545+
Thread.currentThread.getStackTrace
546+
.filter { elem =>
547+
if (seen.contains(elem.getMethodName)) false
548+
else { seen += elem.getMethodName; true }
549+
}
550+
.take(6).find { elem =>
551+
val callingClass = Class.forName(elem.getClassName)
552+
classOf[ParallelTesting].isAssignableFrom(callingClass) &&
553+
elem.getFileName != "ParallelTesting.scala"
554+
}
555+
.map(_.getMethodName)
556+
.getOrElse {
557+
throw new IllegalStateException("Unable to reflectively find calling method")
558+
}
559+
}
560+
542561
def compileFile(f: String, flags: Array[String])(implicit outDirectory: String): CompilationTest = {
543-
// each calling method gets its own unique output directory, in which we
544-
// place the dir being compiled:
545-
val callingMethod = Thread.currentThread.getStackTrace.apply(3).getMethodName
546-
val outDir = outDirectory + callingMethod + "/"
547562
val sourceFile = new JFile(f)
548563
val parent = sourceFile.getParentFile
564+
val outDir =
565+
outDirectory + getCallingMethod + "/" +
566+
sourceFile.getName.substring(0, sourceFile.getName.lastIndexOf('.')) + "/"
567+
549568
require(
550569
sourceFile.exists && !sourceFile.isDirectory &&
551570
(parent ne null) && parent.exists && parent.isDirectory,
@@ -561,10 +580,7 @@ trait ParallelTesting {
561580
}
562581

563582
def compileDir(f: String, flags: Array[String])(implicit outDirectory: String): CompilationTest = {
564-
// each calling method gets its own unique output directory, in which we
565-
// place the dir being compiled:
566-
val callingMethod = Thread.currentThread.getStackTrace.apply(3).getMethodName
567-
val outDir = outDirectory + callingMethod + "/"
583+
val outDir = outDirectory + getCallingMethod + "/"
568584
val sourceDir = new JFile(f)
569585
requirements(f, sourceDir, outDir)
570586

@@ -573,18 +589,15 @@ trait ParallelTesting {
573589
else Array(f)
574590

575591
// Directories in which to compile all containing files with `flags`:
576-
val targetDir = new JFile(outDir)
592+
val targetDir = new JFile(outDir + "/" + sourceDir.getName + "/")
577593
targetDir.mkdirs()
578594

579595
val target = ConcurrentCompilationTarget(flatten(sourceDir), flags, targetDir)
580596
new CompilationTest(target)
581597
}
582598

583599
def compileList(files: List[String], flags: Array[String])(implicit outDirectory: String): CompilationTest = {
584-
// each calling method gets its own unique output directory, in which we
585-
// place the dir being compiled:
586-
val callingMethod = Thread.currentThread.getStackTrace.apply(3).getMethodName
587-
val outDir = outDirectory + callingMethod + "/"
600+
val outDir = outDirectory + getCallingMethod + "/" + testName + "/"
588601

589602
// Directories in which to compile all containing files with `flags`:
590603
val targetDir = new JFile(outDir)
@@ -598,10 +611,7 @@ trait ParallelTesting {
598611
}
599612

600613
def compileFilesInDir(f: String, flags: Array[String])(implicit outDirectory: String): CompilationTest = {
601-
// each calling method gets its own unique output directory, in which we
602-
// place the dir being compiled:
603-
val callingMethod = Thread.currentThread.getStackTrace.apply(3).getMethodName
604-
val outDir = outDirectory + callingMethod + "/"
614+
val outDir = outDirectory + getCallingMethod + "/"
605615
val sourceDir = new JFile(f)
606616
requirements(f, sourceDir, outDir)
607617

@@ -616,10 +626,7 @@ trait ParallelTesting {
616626
}
617627

618628
def compileShallowFilesInDir(f: String, flags: Array[String])(implicit outDirectory: String): CompilationTest = {
619-
// each calling method gets its own unique output directory, in which we
620-
// place the dir being compiled:
621-
val callingMethod = Thread.currentThread.getStackTrace.apply(3).getMethodName
622-
val outDir = outDirectory + callingMethod + "/"
629+
val outDir = outDirectory + getCallingMethod + "/"
623630
val sourceDir = new JFile(f)
624631
requirements(f, sourceDir, outDir)
625632

0 commit comments

Comments
 (0)