Fix patterns
This commit is contained in:
committed by
Pavel Punegov
parent
fed1a5d63f
commit
fd45aea4da
@@ -564,10 +564,13 @@ class RunExternalTestGroup extends RunStandaloneKonanTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
List<String> createTestFiles() {
|
List<String> createTestFiles() {
|
||||||
def packagePattern = ~/(?m)^\s*package\s+([a-zA-z-][a-zA-Z0-9._-]*)/
|
def identifier = /[a-zA-Z_][a-zA-Z0-9_]/
|
||||||
|
def fullQualified = /[a-zA-Z_][a-zA-Z0-9_.]/
|
||||||
|
def importRegex = /(?m)^\s*import\s+/
|
||||||
|
|
||||||
|
def packagePattern = ~/(?m)^\s*package\s+(${fullQualified}*)/
|
||||||
def boxPattern = ~/(?m)fun\s+box\s*\(\s*\)/
|
def boxPattern = ~/(?m)fun\s+box\s*\(\s*\)/
|
||||||
def importPattern = ~/(?m)^\s*import\s+([a-zA-z-][a-zA-Z0-9._*-]*)/
|
def classPattern = ~/.*(class|object|enum)\s+(${identifier}*).*/
|
||||||
def classPattern = ~/.*(class|object|enum)\s+([a-zA-z-][a-zA-Z0-9._-]*).*/
|
|
||||||
|
|
||||||
def sourceName = "_" + normalize(project.file(source).name)
|
def sourceName = "_" + normalize(project.file(source).name)
|
||||||
def packages = new LinkedHashSet()
|
def packages = new LinkedHashSet()
|
||||||
@@ -583,7 +586,7 @@ class RunExternalTestGroup extends RunStandaloneKonanTest {
|
|||||||
pkg = "$sourceName.$pkg"
|
pkg = "$sourceName.$pkg"
|
||||||
text = text.replaceFirst(packagePattern, "package $pkg")
|
text = text.replaceFirst(packagePattern, "package $pkg")
|
||||||
} else {
|
} else {
|
||||||
pkg = sourceName + (pkg ? ".$pkg" : '')
|
pkg = sourceName
|
||||||
text = "package $pkg\n" + text
|
text = "package $pkg\n" + text
|
||||||
}
|
}
|
||||||
if (text =~ boxPattern) {
|
if (text =~ boxPattern) {
|
||||||
@@ -594,18 +597,19 @@ class RunExternalTestGroup extends RunStandaloneKonanTest {
|
|||||||
// TODO: optimize files writes
|
// TODO: optimize files writes
|
||||||
for (String filePath : result) {
|
for (String filePath : result) {
|
||||||
def text = project.file(filePath).text
|
def text = project.file(filePath).text
|
||||||
def m = (text =~ importPattern)
|
// Find if there are any imports in the file
|
||||||
if (m) {
|
def matcher = (text =~ ~/${importRegex}(${fullQualified}*)/)
|
||||||
|
if (matcher) {
|
||||||
// Prepend package name to found imports
|
// Prepend package name to found imports
|
||||||
for (int i = 0; i < m.count; i++) {
|
for (int i = 0; i < matcher.count; i++) {
|
||||||
String importStatement = m[i][1]
|
String importStatement = matcher[i][1]
|
||||||
def subImport = importStatement.with {
|
def subImport = importStatement.with {
|
||||||
int dotIdx = indexOf('.')
|
int dotIdx = indexOf('.')
|
||||||
dotIdx > 0 ? substring(0, dotIdx) : it
|
dotIdx > 0 ? substring(0, dotIdx) : it
|
||||||
}
|
}
|
||||||
if (packages.contains(subImport)) {
|
if (packages.contains(subImport)) {
|
||||||
// add only to those who import packages from the test files
|
// add only to those who import packages from the test files
|
||||||
text = text.replaceFirst(~/(?m)^\s*import\s+${Pattern.quote(importStatement)}/,
|
text = text.replaceFirst(~/${importRegex}${Pattern.quote(importStatement)}/,
|
||||||
"import $sourceName.$importStatement")
|
"import $sourceName.$importStatement")
|
||||||
} else if (text =~ classPattern) {
|
} else if (text =~ classPattern) {
|
||||||
// special case for import from the local class
|
// special case for import from the local class
|
||||||
@@ -613,7 +617,7 @@ class RunExternalTestGroup extends RunStandaloneKonanTest {
|
|||||||
for (int j = 0; j < clsMatcher.count; j++) {
|
for (int j = 0; j < clsMatcher.count; j++) {
|
||||||
def cl = (text =~ classPattern)[j][2]
|
def cl = (text =~ classPattern)[j][2]
|
||||||
if (subImport == cl) {
|
if (subImport == cl) {
|
||||||
text = text.replaceFirst(~/(?m)^\s*import\s+${Pattern.quote(importStatement)}/,
|
text = text.replaceFirst(~/${importRegex}${Pattern.quote(importStatement)}/,
|
||||||
"import $sourceName.$importStatement")
|
"import $sourceName.$importStatement")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -633,9 +637,7 @@ class RunExternalTestGroup extends RunStandaloneKonanTest {
|
|||||||
text.eachLine {
|
text.eachLine {
|
||||||
def line = it
|
def line = it
|
||||||
packages.each { String pkg ->
|
packages.each { String pkg ->
|
||||||
if (line.contains("$pkg.") &&
|
if (line.contains("$pkg.") && ! (line =~ packagePattern || line =~ importRegex)) {
|
||||||
! (line =~ packagePattern || line =~ importPattern)) {
|
|
||||||
|
|
||||||
def idx = line.indexOf("$pkg")
|
def idx = line.indexOf("$pkg")
|
||||||
if (! (idx > 0 && Character.isJavaIdentifierPart(line.charAt(idx - 1))) ) {
|
if (! (idx > 0 && Character.isJavaIdentifierPart(line.charAt(idx - 1))) ) {
|
||||||
line = line.substring(0, idx) + "$sourceName.$pkg" + line.substring(idx + pkg.length())
|
line = line.substring(0, idx) + "$sourceName.$pkg" + line.substring(idx + pkg.length())
|
||||||
|
|||||||
Reference in New Issue
Block a user