[Spec tests] fix test parser exceptions handler

This commit is contained in:
anastasiia.spaseeva
2019-12-12 11:20:04 +03:00
parent fdef51e8b7
commit 8187405dbd
17 changed files with 63 additions and 51 deletions
@@ -39,7 +39,7 @@ object CommonPatterns {
"""${SINGLE_LINE_COMMENT_REGEX.format("""[\w\s]+:""")}|${MULTILINE_COMMENT_REGEX.format(""" $ASTERISK_REGEX [\w\s]+:[\s\S]*?""")}"""
val testAreaRegex = """(?<testArea>${TestArea.joinedValues})"""
val testTypeRegex = """(?<testType>${TestType.joinedValues})"""
val testInfoElementPattern: Pattern = Pattern.compile("""(?: \* )?(?<name>[A-Z ]+?)(?::\s*(?<value>.*?))?\n""")
val testInfoElementPattern: Pattern = Pattern.compile("""(?: \* )?(?<name>[A-Z ]+?)(?::[ ]?(?<value>.*?))?\n""")
val testPathBaseRegexTemplate = """^.*?$ps(?<testArea>diagnostics|psi|(?:codegen${ps}box))$ps%s"""
val testPathRegexTemplate = """$testPathBaseRegexTemplate$ps(?<testType>pos|neg)$ps%s$"""
val issuesPattern: Pattern = Pattern.compile("""(KT-[1-9]\d*)(,\s*KT-[1-9]\d*)*""")
@@ -13,6 +13,8 @@ import org.jetbrains.kotlin.spec.utils.models.SpecTestCaseInfoElementType
import org.jetbrains.kotlin.spec.utils.models.SpecTestInfoElements
import org.jetbrains.kotlin.spec.utils.parsers.TestCasePatterns.testCaseInfoPattern
import org.jetbrains.kotlin.spec.utils.parsers.CommonParser.splitByComma
import org.jetbrains.kotlin.spec.utils.validators.SpecTestValidationException
import org.jetbrains.kotlin.spec.utils.validators.SpecTestValidationFailedReason
import java.util.*
private operator fun SpecTestCase.plusAssign(addTestCase: SpecTestCase) {
@@ -30,7 +32,13 @@ private fun SpecTestCase.save(
caseInfoElements: SpecTestInfoElements<SpecTestInfoElementType>
) {
val testCaseNumbers =
caseInfoElements[SpecTestCaseInfoElementType.TESTCASE_NUMBER]!!.content.splitByComma().map { it.trim().toInt() }
caseInfoElements[SpecTestCaseInfoElementType.TESTCASE_NUMBER]!!.content.splitByComma().map {
it.trim().toIntOrNull()
?: throw SpecTestValidationException(
SpecTestValidationFailedReason.TEST_CASE_NUMBER_FORMAT,
"impossible to parse number '${it.trim()}'"
)
}
val startPosition = this.ranges[0].first
testCaseNumbers.forEach { testCaseNumber ->
@@ -31,33 +31,37 @@ data class ParsedTestFile(
)
fun parseTestInfo(testFilePath: String, testFiles: TestFiles, linkedTestType: SpecTestLinkedType): ParsedTestFile {
val patterns = linkedTestType.patterns.value
val testInfoByFilenameMatcher = patterns.testPathPattern.matcher(testFilePath)
try {
val patterns = linkedTestType.patterns.value
val testInfoByFilenameMatcher = patterns.testPathPattern.matcher(testFilePath)
if (!testInfoByFilenameMatcher.find())
throw SpecTestValidationException(SpecTestValidationFailedReason.FILENAME_NOT_VALID)
if (!testInfoByFilenameMatcher.find())
throw SpecTestValidationException(SpecTestValidationFailedReason.FILENAME_NOT_VALID)
val testInfoByContentMatcher = patterns.testInfoPattern.matcher(FileUtil.loadFile(File(testFilePath), true))
val testInfoByContentMatcher = patterns.testInfoPattern.matcher(FileUtil.loadFile(File(testFilePath), true))
if (!testInfoByContentMatcher.find())
throw SpecTestValidationException(SpecTestValidationFailedReason.TESTINFO_NOT_VALID)
if (!testInfoByContentMatcher.find())
throw SpecTestValidationException(SpecTestValidationFailedReason.TESTINFO_NOT_VALID)
val testInfoElements = CommonParser.parseTestInfoElements(
arrayOf(*CommonInfoElementType.values(), *CommonSpecTestFileInfoElementType.values(), *linkedTestType.infoElements.value),
testInfoByContentMatcher.group("infoElements")
)
val helpers = testInfoElements[CommonSpecTestFileInfoElementType.HELPERS]?.content?.splitByComma()?.toSet()
val testInfoElements = CommonParser.parseTestInfoElements(
arrayOf(*CommonInfoElementType.values(), *CommonSpecTestFileInfoElementType.values(), *linkedTestType.infoElements.value),
testInfoByContentMatcher.group("infoElements")
)
val helpers = testInfoElements[CommonSpecTestFileInfoElementType.HELPERS]?.content?.splitByComma()?.toSet()
return ParsedTestFile(
testArea = TestArea.valueOf(testInfoByContentMatcher.group("testArea").withUnderscores()),
testType = TestType.valueOf(testInfoByContentMatcher.group("testType")),
testNumber = testInfoElements[CommonSpecTestFileInfoElementType.NUMBER]!!.content.toInt(),
testDescription = testInfoElements[CommonSpecTestFileInfoElementType.DESCRIPTION]!!.content,
testInfoElements = testInfoElements,
testCasesSet = parseTestCases(testFiles),
unexpectedBehavior = testInfoElements.contains(CommonInfoElementType.UNEXPECTED_BEHAVIOUR),
issues = CommonParser.parseIssues(testInfoElements[CommonInfoElementType.ISSUES]),
helpers = helpers,
exception = testInfoElements[CommonInfoElementType.EXCEPTION]?.content?.let { TestsExceptionType.fromValue(it) }
)
return ParsedTestFile(
testArea = TestArea.valueOf(testInfoByContentMatcher.group("testArea").withUnderscores()),
testType = TestType.valueOf(testInfoByContentMatcher.group("testType")),
testNumber = testInfoElements[CommonSpecTestFileInfoElementType.NUMBER]!!.content.toInt(),
testDescription = testInfoElements[CommonSpecTestFileInfoElementType.DESCRIPTION]!!.content,
testInfoElements = testInfoElements,
testCasesSet = parseTestCases(testFiles),
unexpectedBehavior = testInfoElements.contains(CommonInfoElementType.UNEXPECTED_BEHAVIOUR),
issues = CommonParser.parseIssues(testInfoElements[CommonInfoElementType.ISSUES]),
helpers = helpers,
exception = testInfoElements[CommonInfoElementType.EXCEPTION]?.content?.let { TestsExceptionType.fromValue(it) }
)
} catch (e: Exception) {
error("Wrong format of file:\nfile://$testFilePath \n${e.message}")
}
}
@@ -23,10 +23,11 @@ enum class SpecTestValidationFailedReason(val description: String) {
UNKNOWN_FRONTEND_EXCEPTION("Unknown frontend exception. Manual analysis is required."),
UNMATCHED_FRONTEND_EXCEPTION("Unmatched frontend exception. Manual analysis is required."),
UNKNOWN("Unknown validation error."),
INCONSISTENT_REASONS("Inconsistent fail reasons: all test cases should have one fail reason within one test.")
INCONSISTENT_REASONS("Inconsistent fail reasons: all test cases should have one fail reason within one test."),
TEST_CASE_NUMBER_FORMAT("Wrong format of testcase number: only integers are allowed.")
}
class SpecTestValidationException(reason: SpecTestValidationFailedReason, details: String = "") : Exception() {
class SpecTestValidationException(reason: SpecTestValidationFailedReason, details: String = "") : Exception("${reason.description} \nDetails: $details") {
val description = "${reason.description} $details"
}