Skip to content

Commit

Permalink
Fixed PythonMethodHeuristic
Browse files Browse the repository at this point in the history
  • Loading branch information
mikkoziel committed Feb 20, 2023
1 parent a6c7530 commit cf55cb9
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ class HeuristicTest {
}
}


@Nested
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
inner class PythonMethodHeuristicTest {
Expand All @@ -201,7 +200,7 @@ class HeuristicTest {

val result = resolveUpdates(library, suggestedVersion, PythonMethodHeuristic)

result?.offset shouldBe correctPositionFor235
result?.offset shouldBe 2764
}

@Test
Expand All @@ -211,7 +210,7 @@ class HeuristicTest {

val result = resolveUpdates(library, suggestedVersion, PythonMethodHeuristic)

result?.offset shouldBe correctPositionFor235
result?.offset shouldBe 2935
}

@Test
Expand All @@ -221,7 +220,7 @@ class HeuristicTest {

val result = resolveUpdates(library, suggestedVersion, PythonMethodHeuristic)

result?.offset shouldBe correctPositionFor235
result?.offset shouldBe 3128
}

@Test
Expand All @@ -231,17 +230,17 @@ class HeuristicTest {

val result = resolveUpdates(library, suggestedVersion, PythonMethodHeuristic)

result?.offset shouldBe correctPositionFor235
result?.offset shouldBe 3058
}

@Test
fun `should return correct position offset scala dep with scala version`() {
val library = library("com.sksamuel.elastic4s", "elastic4s-client-akka_2_12", "8.5.2")
val library = library("com.sksamuel.elastic4s", "elastic4s-client-akka_2.12", "8.5.2")
val suggestedVersion = version("8.6.0")

val result = resolveUpdates(library, suggestedVersion, PythonMethodHeuristic)

result?.offset shouldBe correctPositionFor235
result?.offset shouldBe 3326
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.virtuslab.bazelsteward.core.library

abstract class LibraryId {
abstract fun associatedStrings(): List<String>
abstract fun associatedStrings(): List<List<String>>
abstract val name: String
final override fun toString(): String = name
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,65 @@ package org.virtuslab.bazelsteward.core.replacement
import org.virtuslab.bazelsteward.core.common.FileChange
import org.virtuslab.bazelsteward.core.common.TextFile
import org.virtuslab.bazelsteward.core.common.UpdateSuggestion
import java.nio.file.Path

object PythonMethodHeuristic : Heuristic {
override val name: String = "python-method"

override fun apply(files: List<TextFile>, updateSuggestion: UpdateSuggestion): LibraryUpdate? {
val currentVersion = updateSuggestion.currentLibrary.version.value
val pythonMethodRegex = Regex("\\w+.\\w+\\([a-zA-Z0-1\\n\\s\".,-=]+\\)")
val pythonMethodMatchResult = files.map { pythonMethodRegex.find(it.content)?.to(it.path) }
val pythonMethodMatchResult = getPythonMethods(files)

if (pythonMethodMatchResult.isNotEmpty()) {
val libraryMatchResult = updateSuggestion.currentLibrary.id.associatedStrings().let { libAssociatedStrings ->
(libAssociatedStrings.size - 1).let { associatedIndex ->
findRegexInListOfMatchResults(
libAssociatedStrings[associatedIndex][1],
findRegexInListOfMatchResults(libAssociatedStrings[associatedIndex][0], pythonMethodMatchResult)
)
}
}

if(pythonMethodMatchResult.isNotEmpty()) {
val regex = Regex(Regex.escape(currentVersion))
val matchResult =
pythonMethodMatchResult.map { regex.find(it!!.first.value)?.to(it.second) }
val versionGroup = matchResult.first()?.first?.groups?.get(0) ?: return null
val currentVersion = updateSuggestion.currentLibrary.version.value
val versionMatchResult = findRegexInListOfMatchResults(currentVersion, libraryMatchResult, false)
if (versionMatchResult.isEmpty()) return null

val versionOffset = libraryMatchResult.first()?.first?.range?.let {
versionMatchResult.first()?.first?.range?.first?.plus(
it.first) ?: return null
} ?: return null

return LibraryUpdate(
updateSuggestion,
listOf(
FileChange(
matchResult.first()!!.second,
versionGroup.range.first,
versionMatchResult.first()!!.second,
versionOffset,
updateSuggestion.currentLibrary.version.value.length,
updateSuggestion.suggestedVersion.value
)
)
)
}
return null;
return null
}
}

private fun getPythonMethods(files: List<TextFile>): List<Pair<MatchResult, Path>?> {
val pythonMethodRegex = Regex("\\w+.\\w+\\([a-zA-Z0-1\\n\\s\".,-=]+\\)")
return files
.map { textFile -> pythonMethodRegex.findAll(textFile.content).map { it to textFile.path }.toList() }
.flatten()
}

private fun findRegexInListOfMatchResults(
regexString: String,
listToLookFor: List<Pair<MatchResult, Path>?>,
returnOriginal: Boolean = true
) : List<Pair<MatchResult, Path>?> =
listToLookFor.mapNotNull {
it?.let {
Regex(Regex.escape(regexString)).find(it.first.value)?.let { found ->
if (returnOriginal) it else found to it.second
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ object WholeLibraryHeuristic : Heuristic {
override val name: String = "whole-library"

override fun apply(files: List<TextFile>, updateSuggestion: UpdateSuggestion): LibraryUpdate? {
val markers = updateSuggestion.currentLibrary.id.associatedStrings()
val markers = updateSuggestion.currentLibrary.id.associatedStrings().first()
val currentVersion = updateSuggestion.currentLibrary.version.value
val regex =
(markers + currentVersion).map { """(${Regex.escape(it)})""" }.reduce { acc, s -> "$acc.*$s" }.let { Regex(it) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ sealed class RuleLibraryId : LibraryId() {
override val name: String
get() = ruleName

override fun associatedStrings(): List<String> = listOf(downloadUrl, sha256)
override fun associatedStrings(): List<List<String>> = listOf(listOf(downloadUrl, sha256))

data class ReleaseArtifact(
override val sha256: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class GithubTest {
private fun simpleBranch(branch: String): GitBranch {
val split = branch.split('/', limit = 3)
val lib = object : LibraryId() {
override fun associatedStrings(): List<String> = emptyList()
override fun associatedStrings(): List<List<String>> = emptyList()
override val name = split[1]
}
val ver = SimpleVersion(split[2])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import org.virtuslab.bazelsteward.core.library.VersioningSchema
private val logger = KotlinLogging.logger {}

object BazelLibraryId : LibraryId() {
override fun associatedStrings(): List<String> = listOf("", "USE_BAZEL_VERSION")
override fun associatedStrings(): List<List<String>> = listOf(listOf("", "USE_BAZEL_VERSION"))

override val name: String
get() = "bazel"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import org.virtuslab.bazelsteward.core.library.SimpleVersion
import org.virtuslab.bazelsteward.core.library.Version

data class MavenLibraryId(val group: String, val artifact: String) : LibraryId() {
override fun associatedStrings(): List<String> = listOf(group, artifact)
override fun associatedStrings(): List<List<String>> {
return Regex("_[\\d.]+").find(artifact)?.let {
listOf(listOf(group, artifact), listOf(group, artifact.removeRange(it.range)))
} ?: listOf(listOf(group, artifact))
}

override val name: String
get() = "$group:$artifact"
Expand Down

0 comments on commit cf55cb9

Please sign in to comment.