Skip to content

Commit

Permalink
Added PythonMethodHeuristic
Browse files Browse the repository at this point in the history
Try in PythonMethodHeuristic Logic

Fixed PythonMethodHeuristic
  • Loading branch information
mikkoziel committed Feb 20, 2023
1 parent ba6ca01 commit e1c530d
Show file tree
Hide file tree
Showing 10 changed files with 157 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import org.virtuslab.bazelsteward.core.common.TextFile
import org.virtuslab.bazelsteward.core.common.UpdateSuggestion
import org.virtuslab.bazelsteward.core.library.SemanticVersion
import org.virtuslab.bazelsteward.core.replacement.LibraryUpdateResolver
import org.virtuslab.bazelsteward.core.replacement.PythonMethodHeuristic
import org.virtuslab.bazelsteward.core.replacement.VersionOnlyHeuristic
import org.virtuslab.bazelsteward.core.replacement.VersionReplacementHeuristic
import org.virtuslab.bazelsteward.core.replacement.WholeLibraryHeuristic
Expand Down Expand Up @@ -188,6 +189,61 @@ class VersionReplacementHeuristicTest {
}
}

@Nested
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
inner class PythonMethodHeuristicTest {

@Test
fun `should return correct position offset maven artifact`() {
val library = library("com.google.guava", "guava-testlib", "31.1.0-jre")
val suggestedVersion = version("32.0.0-jre")

val result = resolveUpdates(library, suggestedVersion, PythonMethodHeuristic)

result?.offset shouldBe 2764
}

@Test
fun `should return correct position offset maven artifact named parameters`() {
val library = library("com.google.truth", "truth", "1.1.3")
val suggestedVersion = version("1.2.0")

val result = resolveUpdates(library, suggestedVersion, PythonMethodHeuristic)

result?.offset shouldBe 2935
}

@Test
fun `should return correct position offset scala dep`() {
val library = library("org.scalactic", "scalactic", "3.2.12")
val suggestedVersion = version("4.0.0")

val result = resolveUpdates(library, suggestedVersion, PythonMethodHeuristic)

result?.offset shouldBe 3128
}

@Test
fun `should return correct position offset scala dep named parameters`() {
val library = library("junit", "junit", "4.13.2")
val suggestedVersion = version("4.14.0")

val result = resolveUpdates(library, suggestedVersion, PythonMethodHeuristic)

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 suggestedVersion = version("8.6.0")

val result = resolveUpdates(library, suggestedVersion, PythonMethodHeuristic)

result?.offset shouldBe 3326
}
}

@Nested
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
inner class CompareHeuristicTest {
Expand Down Expand Up @@ -235,7 +291,7 @@ class VersionReplacementHeuristicTest {

private val resolver = LibraryUpdateResolver()

private val allHeuristics = listOf(WholeLibraryHeuristic, VersionOnlyHeuristic).toTypedArray()
private val allHeuristics = listOf(WholeLibraryHeuristic, VersionOnlyHeuristic, PythonMethodHeuristic).toTypedArray()

private fun resolveUpdates(
library: MavenCoordinates,
Expand Down
20 changes: 20 additions & 0 deletions app/src/test/resources/WORKSPACE.bzlignore
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,26 @@ maven_install(
"io.get-coursier:interface:1.0.11",
"commons-io:commons-io:2.11.0",
"io.grpc:grpc-core:%s" % GRPC_VERSION,
maven.artifact(
"com.google.guava",
"guava-testlib",
"31.1.0-jre",
testonly = True
),
maven.artifact(
group = "com.google.truth",
artifact = "truth",
version = "1.1.3",
),
_scala_dep(
group = "junit",
artifact = "junit",
version = "4.13.2",
),
_scala_dep("org.scalactic", "scalactic", "3.2.12"),
_java_dep("org.slf4j", "slf4j-api", "1.7.36"),
_java_dep("com.github.luben", "zstd-jni", "1.5.2-5"),
_scala_dep("com.sksamuel.elastic4s", "elastic4s-client-akka", "8.5.2"),
],
fetch_sources = False,
repositories = [
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
@@ -0,0 +1,67 @@
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 : VersionReplacementHeuristic {
override val name: String = "python-method"

override fun apply(files: List<TextFile>, updateSuggestion: UpdateSuggestion): LibraryUpdate? {
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)
)
}
}

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(
versionMatchResult.first()!!.second,
versionOffset,
updateSuggestion.currentLibrary.version.value.length,
updateSuggestion.suggestedVersion.value
)
)
)
}
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 : VersionReplacementHeuristic {
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 @@ -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 @@ -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 @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.virtuslab.bazelsteward.core.DependencyKind
import org.virtuslab.bazelsteward.core.PathPattern
import org.virtuslab.bazelsteward.core.library.Library
import org.virtuslab.bazelsteward.core.library.Version
import org.virtuslab.bazelsteward.core.replacement.PythonMethodHeuristic
import org.virtuslab.bazelsteward.core.replacement.VersionOnlyHeuristic
import org.virtuslab.bazelsteward.core.replacement.VersionReplacementHeuristic
import org.virtuslab.bazelsteward.core.replacement.WholeLibraryHeuristic
Expand Down Expand Up @@ -37,6 +38,7 @@ class MavenDependencyKind(

override val defaultVersionReplacementHeuristics: List<VersionReplacementHeuristic> = listOf(
WholeLibraryHeuristic,
VersionOnlyHeuristic
VersionOnlyHeuristic,
PythonMethodHeuristic
)
}

0 comments on commit e1c530d

Please sign in to comment.