aboutsummaryrefslogtreecommitdiff
path: root/compiler/test/dotty/tools/StdLibSources.scala
blob: 0c11210586a3dc3f8adbea9d104e51b8407e30a3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package dotty.tools

import java.io.File
import scala.io.Source
import org.junit.Test
import org.junit.Assert._

object StdLibSources {

  /* For debug only */
  private val useExplicitWhiteList = false

  private final val stdLibPath = "../scala-scala/src/library/"

  def blacklistFile: String = "../compiler/test/dotc/scala-collections.blacklist"
  private def whitelistFile: String = "../compiler/test/dotc/scala-collections.whitelist"

  def whitelisted: List[String] = {
    lazy val whitelistBasedOnBlacklist = all.diff(blacklisted)
    if (!useExplicitWhiteList) {
      whitelistBasedOnBlacklist
    } else if (!new File(whitelistFile).exists()) {
      genWhitelist(whitelistBasedOnBlacklist.map(_.replace(stdLibPath, "")))
      whitelistBasedOnBlacklist
    } else {
      loadList(whitelistFile)
    }
  }

  def blacklisted: List[String] = loadList(blacklistFile)

  def all: List[String] = {
    def collectAllFilesInDir(dir: File, acc: List[String]): List[String] = {
      val files = dir.listFiles()
      val acc2 = files.foldLeft(acc)((acc1, file) => if (file.isFile && file.getPath.endsWith(".scala")) file.getPath :: acc1 else acc1)
      files.foldLeft(acc2)((acc3, file) => if (file.isDirectory) collectAllFilesInDir(file, acc3) else acc3)
    }
    collectAllFilesInDir(new File(stdLibPath), Nil)
  }

  private def genWhitelist(list: List[String]): Unit = {
    println(s"Generating $whitelistFile based on $blacklistFile")
    val whitelist = new File(whitelistFile)
    val p = new java.io.PrintWriter(whitelist)
    try {
      list.foreach(p.println)
    } finally {
      p.close()
    }
  }

  private def loadList(path: String): List[String] = Source.fromFile(path, "UTF8").getLines()
    .map(_.trim) // allow identation
    .filter(!_.startsWith("#")) // allow comment lines prefixed by #
    .map(_.takeWhile(_ != '#').trim) // allow comments in the end of line
    .filter(_.nonEmpty)
    .map(stdLibPath + _)
    .toList

}

class StdLibSources {
  @Test def checkWBLists = {
    val stdlibFilesBlackListed = StdLibSources.blacklisted

    val duplicates = stdlibFilesBlackListed.groupBy(x => x).filter(_._2.size > 1).filter(_._2.size > 1)
    val msg = duplicates.map(x => s"'${x._1}' appears ${x._2.size} times").mkString(s"Duplicate entries in ${StdLibSources.blacklistFile}:\n", "\n", "\n")
    assertTrue(msg, duplicates.isEmpty)

    val filesNotInStdLib = stdlibFilesBlackListed.toSet -- StdLibSources.all
    val msg2 = filesNotInStdLib.map(x => s"'$x'").mkString(s"Entries in ${StdLibSources.blacklistFile} where not found:\n", "\n", "\n")
    assertTrue(msg2, filesNotInStdLib.isEmpty)
  }
}