Skip to content

Commit 4f2a5ff

Browse files
committed
semanticdb tests use new utils class functions
1 parent 5617e3e commit 4f2a5ff

File tree

3 files changed

+31
-124
lines changed

3 files changed

+31
-124
lines changed

semanticdb/src/dotty/semanticdb/Main.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ object Main {
8888
println("Compile error:")
8989
println(reporter)
9090
} else {
91-
val classNames = Utils.getClassNames(cliArgs("classpath"), cliArgs("input"))
9291
val scalaFile = Paths.get(cliArgs("input")).toAbsolutePath
92+
val classNames = Utils.getClassNames(Paths.get(cliArgs("classpath")), scalaFile)
9393
val sdbconsumer = new SemanticdbConsumer(scalaFile)
9494
val _ = ConsumeTasty(cliArgs("classpath"), classNames, sdbconsumer)
9595
val textDocument = sdbconsumer.toSemanticdb()

semanticdb/src/dotty/semanticdb/Utils.scala

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,48 +30,48 @@ object Utils {
3030
}
3131

3232
/** List all tasty files occuring in the folder f or one of its subfolders */
33-
def recursiveListFiles(f: File): Array[File] = {
34-
val pattern = ".*\\.tasty".r
33+
def recursiveListFiles(f: File, prefix : String = ""): Array[File] = {
34+
val pattern = (prefix + ".*\\.tasty").r
3535
val files = f.listFiles
3636
val folders = files.filter(_.isDirectory)
3737
val tastyfiles = files.filter(_.toString match {
3838
case pattern(x: _*) => true
3939
case _ => false
4040
})
41-
tastyfiles ++ folders.flatMap(recursiveListFiles)
41+
tastyfiles ++ folders.flatMap(recursiveListFiles(_, prefix))
4242
}
4343

4444
/** Returns a mapping from *.scala file to a list of tasty files. */
45-
def getTastyFiles(classPath: Path): HashMap[String, List[Path]] = {
46-
val source_to_tasty: HashMap[String, List[Path]] = HashMap()
47-
val tastyfiles = recursiveListFiles(classPath.toFile())
48-
recursiveListFiles(classPath.toFile()).map(tasty_path => {
49-
val (classpath, classname) = getClasspathClassname(tasty_path.toPath())
45+
def getTastyFiles(classPath: Path, prefix : String = ""): HashMap[String, List[Path]] = {
46+
val sourceToTasty: HashMap[String, List[Path]] = HashMap()
47+
val tastyfiles = recursiveListFiles(classPath.toFile(), prefix)
48+
recursiveListFiles(classPath.toFile()).map(tastyPath => {
49+
val (classpath, classname) = getClasspathClassname(tastyPath.toPath())
5050
// We add an exception here to avoid crashing if we encountered
5151
// a bad tasty file
5252
try {
5353
val inspecter = new TastyScalaFileInferrer
5454
ConsumeTasty(classpath, classname :: Nil, inspecter)
5555
inspecter.sourcePath.foreach(
5656
source =>
57-
source_to_tasty +=
58-
(source -> (tasty_path
59-
.toPath() :: source_to_tasty.getOrElse(source, Nil))))
57+
sourceToTasty +=
58+
(source -> (tastyPath
59+
.toPath().toAbsolutePath :: sourceToTasty.getOrElse(source, Nil))))
6060
} catch {
61-
case _: InvocationTargetException => println(tasty_path)
61+
case _: InvocationTargetException => println(tastyPath)
6262
}
6363
})
64-
source_to_tasty
64+
sourceToTasty
6565
}
6666

6767
/*
6868
Returns the list of names of class defined inside the scala file [scalaFile]
6969
extracted from the compilation artifacts found in [classPath].
7070
*/
71-
def getClassNames(classPath: String, scalaFile: String): List[String] = {
71+
def getClassNames(classPath: Path, scalaFile: Path, prefix : String = ""): List[String] = {
7272
val tastyFiles =
73-
getTastyFiles(Paths.get(classPath).toAbsolutePath)
74-
.getOrElse(Paths.get(scalaFile).toAbsolutePath.toString, Nil)
73+
getTastyFiles(classPath.toAbsolutePath, prefix)
74+
.getOrElse(scalaFile.toString, Nil)
7575

7676
val tastyClasses = tastyFiles.map(getClasspathClassname)
7777
val (_, classnames) = tastyClasses.unzip

semanticdb/test/dotty/semanticdb/Tests.scala

Lines changed: 14 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -14,133 +14,40 @@ import scala.tasty.Reflection
1414
import scala.tasty.file.TastyConsumer
1515
import java.lang.reflect.InvocationTargetException
1616

17-
class TastyInspecter extends TastyConsumer {
18-
var source_path: Option[String] = None
19-
final def apply(reflect: Reflection)(root: reflect.Tree): Unit = {
20-
import reflect._
21-
object ChildTraverser extends TreeTraverser {
22-
override def traverseTree(tree: Tree)(implicit ctx: Context): Unit =
23-
tree match {
24-
case IsClassDef(cdef) => {
25-
cdef.symbol.annots.foreach { annot =>
26-
annot match {
27-
case Term.Apply(Term.Select(Term.New(t), _),
28-
List(Term.Literal(Constant.String(path))))
29-
if t.symbol.name == "SourceFile" =>
30-
// we found the path to a file. In this case, we do not need to
31-
// continue traversing the tree
32-
source_path = Some(path)
33-
case x => super.traverseTree(tree)
34-
}
35-
true
36-
}
37-
}
38-
case _ => {
39-
if (source_path == None)
40-
super.traverseTree(tree)
41-
else
42-
()
43-
}
44-
}
45-
}
46-
ChildTraverser.traverseTree(root)(reflect.rootContext)
47-
}
48-
}
49-
5017
class Tests {
5118

5219
// TODO: update scala-0.13 on version change (or resolve automatically)
5320
final def tastyClassDirectory =
54-
"out/bootstrap/dotty-semanticdb/scala-0.12/test-classes"
55-
val sourceroot = Paths.get("semanticdb", "input").toAbsolutePath
56-
val sourceDirectory = sourceroot.resolve("src/main/scala")
21+
Paths.get("out/bootstrap/dotty-semanticdb/scala-0.12/test-classes/")
5722

23+
val sourceroot = Paths.get("semanticdb/input").toAbsolutePath
24+
val sourceDirectory = sourceroot.resolve("src/main/scala")
5825
val semanticdbClassDirectory = sourceroot.resolve("target/scala-2.12/classes")
5926
val semanticdbLoader =
6027
new Semanticdbs.Loader(sourceroot, List(semanticdbClassDirectory))
6128

62-
val source_to_tasty =
63-
getTastyFiles(
64-
Paths.get("out", "bootstrap", "dotty-semanticdb/").toAbsolutePath)
65-
6629
/** Returns the SemanticDB for this Scala source file. */
6730
def getScalacSemanticdb(scalaFile: Path): s.TextDocument = {
6831
semanticdbLoader.resolve(scalaFile).get
6932
}
7033

71-
/** List all tasty files occuring in the folder f or one of its subfolders */
72-
def recursiveListFiles(f: File): Array[File] = {
73-
val pattern = ".*test-classes/example.*\\.tasty".r
74-
val files = f.listFiles
75-
val folders = files.filter(_.isDirectory)
76-
val tastyfiles = files.filter(_.toString match {
77-
case pattern(x: _*) => true
78-
case _ => false
79-
})
80-
tastyfiles ++ folders.flatMap(recursiveListFiles)
81-
}
82-
83-
/** Returns a mapping from *.scala file to a list of tasty files. */
84-
def getTastyFiles(artifactsPath: Path): HashMap[String, List[Path]] = {
85-
val source_to_tasty: HashMap[String, List[Path]] = HashMap()
86-
val tastyfiles = recursiveListFiles(artifactsPath.toFile())
87-
recursiveListFiles(artifactsPath.toFile()).map(tasty_path => {
88-
val (classpath, classname) = getClasspathClassname(tasty_path.toPath())
89-
// We add an exception here to avoid crashing if we encountered
90-
// a bad tasty file
91-
try {
92-
val inspecter = new TastyInspecter
93-
ConsumeTasty(classpath, classname :: Nil, inspecter)
94-
inspecter.source_path.foreach(
95-
source =>
96-
source_to_tasty +=
97-
(source -> (tasty_path
98-
.toPath() :: source_to_tasty.getOrElse(source, Nil))))
99-
} catch {
100-
case _: InvocationTargetException => println(tasty_path)
101-
}
102-
})
103-
source_to_tasty
104-
}
105-
106-
/** Infers a tuple (class path, class name) from a given path */
107-
def getClasspathClassname(file: Path): (String, String) = {
108-
val pat = """(.*)\..*""".r
109-
val classpath = file.getParent().getParent().toString()
110-
val modulename = file.getParent().getFileName().toString()
111-
val sourcename =
112-
file.toFile().getName().toString() match {
113-
case pat(name) => name
114-
case _ => ""
115-
}
116-
return (classpath, modulename + "." + sourcename)
117-
}
118-
119-
def getTastySemanticdb(scalaFile: Path): s.TextDocument = {
120-
val scalac = getScalacSemanticdb(scalaFile)
121-
122-
val tasty_files = source_to_tasty.getOrElse(
123-
sourceDirectory.resolve(scalaFile).toString,
124-
Nil)
125-
126-
val tasty_classes = tasty_files.map(getClasspathClassname)
127-
// If we have more than one classpath then something went wrong
128-
if (tasty_classes.groupBy((a, _) => a).size != 1) {
129-
scalac
130-
} else {
131-
val (classpaths, classnames) = tasty_classes.unzip
132-
val sdbconsumer = new SemanticdbConsumer(scalaFile)
133-
134-
val _ = ConsumeTasty(classpaths.head, classnames, sdbconsumer)
135-
sdbconsumer.toSemanticdb()
136-
}
34+
/** Returns the SemanticDB for this Scala source file. */
35+
def getTastySemanticdb(classPath: Path, scalaFile: Path) : s.TextDocument = {
36+
val classNames = Utils.getClassNames(classPath, scalaFile, "example/")
37+
println(classPath)
38+
println(classNames)
39+
println(scalaFile)
40+
val sdbconsumer = new SemanticdbConsumer(scalaFile)
41+
42+
val _ = ConsumeTasty(classPath.toString, classNames, sdbconsumer)
43+
sdbconsumer.toSemanticdb()
13744
}
13845

13946
/** Fails the test if the s.TextDocument from tasty and semanticdb-scalac are not the same. */
14047
def checkFile(filename: String): Unit = {
14148
val path = sourceDirectory.resolve(filename)
14249
val scalac = getScalacSemanticdb(path)
143-
val tasty = getTastySemanticdb(path)
50+
val tasty = getTastySemanticdb(tastyClassDirectory, path)
14451
println(tasty)
14552
val obtained = Semanticdbs.printTextDocument(tasty)
14653
val expected = Semanticdbs.printTextDocument(scalac)

0 commit comments

Comments
 (0)