-
Notifications
You must be signed in to change notification settings - Fork 389
/
AllProjectHighlightingTest.scala
127 lines (104 loc) · 4.53 KB
/
AllProjectHighlightingTest.scala
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package org.jetbrains.plugins.scala.projectHighlighting
import java.util
import com.intellij.ide.highlighter.JavaFileType
import com.intellij.lang.annotation.Annotation
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.TextRange
import com.intellij.openapi.vfs.{LocalFileSystem, VirtualFile}
import com.intellij.psi.{PsiElement, PsiManager}
import com.intellij.psi.impl.PsiManagerEx
import com.intellij.psi.search.{FileTypeIndex, GlobalSearchScope}
import org.jetbrains.plugins.scala.ScalaFileType
import org.jetbrains.plugins.scala.annotator.{AnnotatorHolderMock, ScalaAnnotator}
import org.jetbrains.plugins.scala.finder.SourceFilterScope
import org.jetbrains.plugins.scala.lang.psi.ScalaPsiElement
import org.jetbrains.plugins.scala.lang.psi.api.ScalaRecursiveElementVisitor
import org.junit.Assert
/**
* @author Mikhail Mutcianko
* @since 30.08.16
*/
trait AllProjectHighlightingTest {
def getProject: Project
class DefaultReporter {
var totalErrors = 0
def reportError(file: VirtualFile, range: TextRange, message: String) = {
totalErrors += 1
println(s"Error: ${file.getName}${range.toString} - $message")
}
def updateProgress(percent: Int) = println(s"Highlighting - $percent%")
def reportResults() = {
Assert.assertTrue(s"Found $totalErrors errors while highlighting the project", totalErrors == 0)
}
}
class TCReporter extends DefaultReporter {
def escapeTC(message: String): String = {
message
.replaceAll("##", "")
.replaceAll("([\"\'\n\r\\|\\[\\]])", "\\|$1")
}
override def updateProgress(percent: Int): Unit = println(s"##teamcity[progressMessage 'Highlighting - $percent%']")
override def reportError(file: VirtualFile, range: TextRange, message: String): Unit = {
totalErrors += 1
val escaped = escapeTC(Option(message).getOrElse(""))
val testName = s"${getClass.getName}.${Option(file).map(_.getName).getOrElse("UNKNOWN")}${Option(range).map(_.toString).getOrElse("(UNKNOWN)")}"
println(s"##teamcity[testStarted name='$testName']")
println(s"##teamcity[testFailed name='$testName' message='Highlighting error' details='$escaped']")
println(s"##teamcity[testFinished name='$testName']")
}
override def reportResults(): Unit = {
if (totalErrors > 0)
println(s"##teamcity[buildProblem description='Found $totalErrors errors while highlighting the project' ]")
else
println("##teamcity[buildStatus status='SUCCESS' text='No highlighting errors found in project']")
}
}
def doAllProjectHighlightingTest(): Unit = {
import scala.collection.JavaConversions._
val reporter = if (sys.env.contains("TEAMCITY_VERSION"))
new TCReporter
else
new DefaultReporter
val searchScope =
new SourceFilterScope(GlobalSearchScope.getScopeRestrictedByFileTypes(GlobalSearchScope.projectScope(getProject),
ScalaFileType.SCALA_FILE_TYPE, JavaFileType.INSTANCE), getProject)
val files: util.Collection[VirtualFile] = FileTypeIndex.getFiles(ScalaFileType.SCALA_FILE_TYPE, searchScope)
LocalFileSystem.getInstance().refreshFiles(files)
val fileManager = PsiManager.getInstance(getProject).asInstanceOf[PsiManagerEx].getFileManager
val annotator = new ScalaAnnotator
var percent = 0
val size: Int = files.size()
for ((file, index) <- files.zipWithIndex) {
val mock = new AnnotatorHolderMock {
override def createErrorAnnotation(range: TextRange, message: String): Annotation = {
reporter.reportError(file, range, message)
super.createErrorAnnotation(range, message)
}
override def createErrorAnnotation(elt: PsiElement, message: String): Annotation = {
reporter.reportError(file, elt.getTextRange, message)
super.createErrorAnnotation(elt, message)
}
}
if ((index + 1) * 100 >= (percent + 1) * size) {
while ((index + 1) * 100 >= (percent + 1) * size) percent += 1
reporter.updateProgress(percent)
}
val psi = fileManager.findFile(file)
val visitor = new ScalaRecursiveElementVisitor {
override def visitElement(element: ScalaPsiElement) {
try {
annotator.annotate(element, mock)
} catch {
case e: Throwable =>
println(s"Exception in ${file.getName}, Stacktrace: ")
e.printStackTrace()
assert(false)
}
super.visitElement(element)
}
}
psi.accept(visitor)
}
reporter.reportResults()
}
}