From a0d73074db846b0837708994bd6a74499575e8f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Kozie=C5=82?= Date: Tue, 21 Feb 2023 17:48:14 +0100 Subject: [PATCH] Fixes after review --- .../app/VersionReplacementHeuristicTest.kt | 10 +++++ .../PythonFunctionCallHeuristic.kt | 40 ++++++------------- .../core/replacement/VersionOnlyHeuristic.kt | 15 ++++--- .../VersionReplacementHeuristic.kt | 17 +++++--- .../core/replacement/WholeLibraryHeuristic.kt | 13 +++--- .../bazel/version/BazelUpdater.kt | 2 +- .../bazelsteward/maven/MavenCoordinates.kt | 2 +- 7 files changed, 52 insertions(+), 47 deletions(-) diff --git a/app/src/test/kotlin/org/virtuslab/bazelsteward/app/VersionReplacementHeuristicTest.kt b/app/src/test/kotlin/org/virtuslab/bazelsteward/app/VersionReplacementHeuristicTest.kt index 3424ac53..c621af78 100644 --- a/app/src/test/kotlin/org/virtuslab/bazelsteward/app/VersionReplacementHeuristicTest.kt +++ b/app/src/test/kotlin/org/virtuslab/bazelsteward/app/VersionReplacementHeuristicTest.kt @@ -228,6 +228,16 @@ class VersionReplacementHeuristicTest { result?.offset shouldBe correctPositionFor3212 } + @Test + fun `should return null for wrong scala dep`() { + val library = library("org.scalactic", "scalactic", "3.2.90") + val suggestedVersion = version("4.0.0") + + val result = resolveUpdates(library, suggestedVersion, PythonFunctionCallHeuristic) + + result shouldBe null + } + @Test fun `should return correct position offset scala dep named parameters`() { val library = library("junit", "junit", "4.13.2") diff --git a/core/src/main/kotlin/org/virtuslab/bazelsteward/core/replacement/PythonFunctionCallHeuristic.kt b/core/src/main/kotlin/org/virtuslab/bazelsteward/core/replacement/PythonFunctionCallHeuristic.kt index ea3fd061..f3d5b53b 100644 --- a/core/src/main/kotlin/org/virtuslab/bazelsteward/core/replacement/PythonFunctionCallHeuristic.kt +++ b/core/src/main/kotlin/org/virtuslab/bazelsteward/core/replacement/PythonFunctionCallHeuristic.kt @@ -8,30 +8,27 @@ object PythonFunctionCallHeuristic : VersionReplacementHeuristic { override val name: String = "python-function-call" override fun apply(files: List, updateSuggestion: UpdateSuggestion): LibraryUpdate? { - val pythonFunctionMatchResultList = getPythonFunctionCalls(files) + val functionCalls = getFunctionCalls(files) - if (pythonFunctionMatchResultList.isNotEmpty()) { - - val libAssociatedStrings = updateSuggestion.currentLibrary.id.associatedStrings() - val libraryMatchResultList = libAssociatedStrings.flatMap { - var listToLookFor = pythonFunctionMatchResultList - it.flatMap { stringForRegex -> - filterRegexInListOfMatchResults(stringForRegex, listToLookFor).also { res -> listToLookFor = res } + if (functionCalls.isNotEmpty()) { + val associatedStringVariants = updateSuggestion.currentLibrary.id.associatedStrings() + val functionCallsWithAssociatedStrings = functionCalls.filter { call -> + associatedStringVariants.any { strings -> + strings.all { call.matchedText.contains(it) } } } val currentVersion = updateSuggestion.currentLibrary.version.value - val versionMatchResultList = filterRegexInListOfMatchResults(currentVersion, libraryMatchResultList, false) - if (versionMatchResultList.isEmpty()) return null - - val versionOffset = libraryMatchResultList.first().getRangeStart() + versionMatchResultList.first().getRangeStart() + val (parentMatch, matchedVersion) = functionCallsWithAssociatedStrings.firstNotNullOfOrNull { call -> + Regex.fromLiteral(currentVersion).find(call.matchedText)?.let { call.subMatch(it) } + } ?: return null return LibraryUpdate( updateSuggestion, listOf( FileChange( - versionMatchResultList.first().path, - versionOffset, + matchedVersion.origin, + parentMatch.offset + matchedVersion.offset, updateSuggestion.currentLibrary.version.value.length, updateSuggestion.suggestedVersion.value ) @@ -41,21 +38,10 @@ object PythonFunctionCallHeuristic : VersionReplacementHeuristic { return null } - private fun getPythonFunctionCalls(files: List): List { + private fun getFunctionCalls(files: List): List { val pythonMethodRegex = Regex("""\w+\([\w\n\s".\-,=]+\)""") return files.flatMap { textFile -> - pythonMethodRegex.findAll(textFile.content).map { MatchResultPath(it, textFile.path) } + pythonMethodRegex.findAll(textFile.content).map { MatchedText(it, textFile.path) } } } - - private fun filterRegexInListOfMatchResults( - stringForRegex: String, - listToLookFor: List, - returnOriginal: Boolean = true - ): List = - listToLookFor.mapNotNull { - Regex(Regex.escape(stringForRegex)).find(it.getValue())?.let { found -> - if (returnOriginal) it else MatchResultPath(found, it.path) - } - } } diff --git a/core/src/main/kotlin/org/virtuslab/bazelsteward/core/replacement/VersionOnlyHeuristic.kt b/core/src/main/kotlin/org/virtuslab/bazelsteward/core/replacement/VersionOnlyHeuristic.kt index 8dd1e9cc..2cb3aea5 100644 --- a/core/src/main/kotlin/org/virtuslab/bazelsteward/core/replacement/VersionOnlyHeuristic.kt +++ b/core/src/main/kotlin/org/virtuslab/bazelsteward/core/replacement/VersionOnlyHeuristic.kt @@ -10,15 +10,20 @@ object VersionOnlyHeuristic : VersionReplacementHeuristic { override fun apply(files: List, updateSuggestion: UpdateSuggestion): LibraryUpdate? { val currentVersion = updateSuggestion.currentLibrary.version.value val regex = Regex(Regex.escape(currentVersion)) - val matchResult = files.firstNotNullOfOrNull { regex.find(it.content)?.to(it.path) } ?: return null - matchResult.first.next()?.let { return null } - val versionGroup = matchResult.first.groups[0] ?: return null + val matchResult = files.firstNotNullOfOrNull { f -> + regex.find(f.content)?.let { + MatchedText(it, f.path) + } + } ?: return null + matchResult.match.next()?.let { return null } + val versionOffset = matchResult.offsetLastMatchGroup ?: return null + return LibraryUpdate( updateSuggestion, listOf( FileChange( - matchResult.second, - versionGroup.range.first, + matchResult.origin, + versionOffset, updateSuggestion.currentLibrary.version.value.length, updateSuggestion.suggestedVersion.value ) diff --git a/core/src/main/kotlin/org/virtuslab/bazelsteward/core/replacement/VersionReplacementHeuristic.kt b/core/src/main/kotlin/org/virtuslab/bazelsteward/core/replacement/VersionReplacementHeuristic.kt index 0a397015..5955b2b9 100644 --- a/core/src/main/kotlin/org/virtuslab/bazelsteward/core/replacement/VersionReplacementHeuristic.kt +++ b/core/src/main/kotlin/org/virtuslab/bazelsteward/core/replacement/VersionReplacementHeuristic.kt @@ -10,14 +10,19 @@ data class LibraryUpdate( val fileChanges: List ) -data class MatchResultPath( - val matchResult: MatchResult, - val path: Path +data class MatchedText( + val match: MatchResult, + val origin: Path ) { - fun getRangeStart(): Int = matchResult.range.start - fun getGroup(index: Int): MatchGroup? = matchResult.groups[index] + val offset: Int + get() = match.range.start - fun getValue(): String = matchResult.value + val offsetLastMatchGroup: Int? + get() = match.groups.last()?.range?.start + val matchedText: String + get() = match.value + + fun subMatch(other: MatchResult): Pair = this to MatchedText(other, origin) } interface VersionReplacementHeuristic { diff --git a/core/src/main/kotlin/org/virtuslab/bazelsteward/core/replacement/WholeLibraryHeuristic.kt b/core/src/main/kotlin/org/virtuslab/bazelsteward/core/replacement/WholeLibraryHeuristic.kt index 86504a23..925b13d0 100644 --- a/core/src/main/kotlin/org/virtuslab/bazelsteward/core/replacement/WholeLibraryHeuristic.kt +++ b/core/src/main/kotlin/org/virtuslab/bazelsteward/core/replacement/WholeLibraryHeuristic.kt @@ -13,22 +13,21 @@ object WholeLibraryHeuristic : VersionReplacementHeuristic { val regexes = markers.map { marker -> (marker + currentVersion).map { """(${Regex.escape(it)})""" }.reduce { acc, s -> "$acc.*$s" }.toRegex() } - val matchResultsList = regexes.mapNotNull { regex -> + val matchResult = regexes.firstNotNullOfOrNull { regex -> files.firstNotNullOfOrNull { textFile -> regex.find(textFile.content)?.let { - MatchResultPath(it, textFile.path) + MatchedText(it, textFile.path) } } - } - if (matchResultsList.isEmpty()) return null - val versionGroup = matchResultsList.first().getGroup(3) ?: return null + } ?: return null + val versionOffset = matchResult.offsetLastMatchGroup ?: return null return LibraryUpdate( updateSuggestion, listOf( FileChange( - matchResultsList.first().path, - versionGroup.range.start, + matchResult.origin, + versionOffset, updateSuggestion.currentLibrary.version.value.length, updateSuggestion.suggestedVersion.value ) diff --git a/kinds/bazel/version/src/main/kotlin/org/virtuslab/bazelsteward/bazel/version/BazelUpdater.kt b/kinds/bazel/version/src/main/kotlin/org/virtuslab/bazelsteward/bazel/version/BazelUpdater.kt index 4ce63fbc..1db0647c 100644 --- a/kinds/bazel/version/src/main/kotlin/org/virtuslab/bazelsteward/bazel/version/BazelUpdater.kt +++ b/kinds/bazel/version/src/main/kotlin/org/virtuslab/bazelsteward/bazel/version/BazelUpdater.kt @@ -9,7 +9,7 @@ import org.virtuslab.bazelsteward.core.library.VersioningSchema private val logger = KotlinLogging.logger {} object BazelLibraryId : LibraryId() { - override fun associatedStrings(): List> = listOf(listOf("", "USE_BAZEL_VERSION")) + override fun associatedStrings(): List> = listOf(listOf("USE_BAZEL_VERSION")) override val name: String get() = "bazel" diff --git a/kinds/maven/src/main/kotlin/org/virtuslab/bazelsteward/maven/MavenCoordinates.kt b/kinds/maven/src/main/kotlin/org/virtuslab/bazelsteward/maven/MavenCoordinates.kt index e3be1e3b..612322b7 100644 --- a/kinds/maven/src/main/kotlin/org/virtuslab/bazelsteward/maven/MavenCoordinates.kt +++ b/kinds/maven/src/main/kotlin/org/virtuslab/bazelsteward/maven/MavenCoordinates.kt @@ -7,7 +7,7 @@ import org.virtuslab.bazelsteward.core.library.Version data class MavenLibraryId(val group: String, val artifact: String) : LibraryId() { override fun associatedStrings(): List> { - return Regex("""_[\d.]+${'$'}""").find(artifact)?.let { + return Regex("""_[\d.]+$""").find(artifact)?.let { listOf(listOf(group, artifact), listOf(group, artifact.removeRange(it.range))) } ?: listOf(listOf(group, artifact)) }