Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replacement Heuristic #132

Merged
merged 8 commits into from
Feb 11, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ class BazelVersionFileSearch(config: Config) {
private val fileNames = setOf(".bazelversion", ".bazeliskrc")

val bazelVersionFiles: List<BazelFileSearch.BazelFile> by lazy {
config.path.listDirectoryEntries().filter { fileNames.contains(it.name) }.map { BazelFileSearch.BazelFile(it) }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, let's cleanup the naming.
Let BazelFileSearch expose:

  • BazelFile as interface (just like you did)
  • factory method for creating the lazy version, i.e. def createBazelFile(path: Path): BazelFile
    So make BazelFileLazy private, just return it from the factory method.
    Also rename BazelFileLazy to LazyBazelFile.

It is still not a perfect design, all of this needs to be eventually renamed to something more generic than BazelFile, but let's at least keep what we have now in some order.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

config.path.listDirectoryEntries().filter { fileNames.contains(it.name) }.map { BazelFileSearch.BazelFileLazy(it) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,18 @@ import java.nio.file.Path
import kotlin.io.path.readText

class BazelFileSearch(config: Config) {
data class BazelFile(val path: Path) {
interface BazelFile {
val path: Path
val content: String
}

data class BazelFileLazy(override val path: Path) : BazelFile {
override val content: String
get() = path.readText()
}

data class BazelFileTest(override val path: Path, override val content: String) : BazelFile
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put this class in tests rather than in sources and use it from tests. Also I'd name it differently. Minimum would be to have TestBazelFile. It would also avoid the problem of being detected as a test class by junit :p

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed


private val fileNames = setOf("""BUILD.bazel""", "BUILD", "WORKSPACE")
private val fileSuffix = ".bzl"

Expand All @@ -22,5 +29,5 @@ class BazelFileSearch(config: Config) {
.toList()
}

val buildDefinitions: List<BazelFile> by lazy { buildPaths.map { BazelFile(it) } }
val buildDefinitions: List<BazelFile> by lazy { buildPaths.map { BazelFileLazy(it) } }
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package org.virtuslab.bazelsteward.core.common
import org.virtuslab.bazelsteward.core.library.Library
import org.virtuslab.bazelsteward.core.library.LibraryId
import org.virtuslab.bazelsteward.core.library.Version
import org.virtuslab.bazelsteward.core.replacement.VersionHeuristic
import org.virtuslab.bazelsteward.core.replacement.WholeVersionHeuristic
import java.nio.file.Path

class FileUpdateSearch {
Expand All @@ -23,15 +25,14 @@ class FileUpdateSearch {
files: List<BazelFileSearch.BazelFile>,
updateSuggestion: UpdateLogic.UpdateSuggestion<Lib>
): FileChangeSuggestion? {
val markers = updateSuggestion.currentLibrary.id.associatedStrings()
val currentVersion = updateSuggestion.currentLibrary.version.value
val regex =
(markers + currentVersion).map { """(${Regex.escape(it)})""" }.reduce { acc, s -> "$acc.*$s" }.let { Regex(it) }
val matchResult = files.firstNotNullOfOrNull { regex.find(it.content)?.to(it.path) } ?: return null
val versionGroup = matchResult.first.groups[3] ?: return null
return FileChangeSuggestion(
updateSuggestion.currentLibrary, updateSuggestion.suggestedVersion, matchResult.second, versionGroup.range.first
val allHeuristics = listOf(
WholeVersionHeuristic(),
VersionHeuristic(),
)
for (heuristic in allHeuristics) {
heuristic.apply(files, updateSuggestion)?.let { return it }
}
return null
}

fun <Lib : LibraryId> searchBazelVersionFiles(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.virtuslab.bazelsteward.core.replacement

import org.virtuslab.bazelsteward.core.common.BazelFileSearch
import org.virtuslab.bazelsteward.core.common.FileUpdateSearch
import org.virtuslab.bazelsteward.core.common.UpdateLogic
import org.virtuslab.bazelsteward.core.library.LibraryId

interface Heuristic {
val name: String
fun <Lib : LibraryId> apply(
files: List<BazelFileSearch.BazelFile>,
updateSuggestion: UpdateLogic.UpdateSuggestion<Lib>
): FileUpdateSearch.FileChangeSuggestion?
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.virtuslab.bazelsteward.core.replacement

import org.virtuslab.bazelsteward.core.common.BazelFileSearch
import org.virtuslab.bazelsteward.core.common.FileUpdateSearch
import org.virtuslab.bazelsteward.core.common.UpdateLogic
import org.virtuslab.bazelsteward.core.library.LibraryId

class VersionHeuristic : Heuristic {
override val name: String
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doesn't kotlin allow for override val name: String = "version"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

get() = "version"

override fun <Lib : LibraryId> apply(
files: List<BazelFileSearch.BazelFile>,
updateSuggestion: UpdateLogic.UpdateSuggestion<Lib>
): FileUpdateSearch.FileChangeSuggestion? {
val currentVersion = updateSuggestion.currentLibrary.version.value
val regex =
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wait what? If current version is just one string, why are you mapping over it? Doesn't it map over all chars?
In case of version 1.0.0, you are looking for \Q1\E.*\Q.\E.*\Q0\E.*\Q.\E.*\Q0\E.* instead of \Q1.0.0\E

So you could find versions like 1.4.0.0 for example

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

currentVersion.map { """(${Regex.escape(it.toString())})""" }.reduce { acc, s -> "$acc.*$s" }.let { Regex(it) }
val matchResult = files.firstNotNullOfOrNull { regex.find(it.content)?.to(it.path) } ?: return null
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see a logic where it fails in case the version is found more than once (in one or more files).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

val versionGroup = matchResult.first.groups[0] ?: return null
return FileUpdateSearch.FileChangeSuggestion(
updateSuggestion.currentLibrary, updateSuggestion.suggestedVersion, matchResult.second, versionGroup.range.first
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.virtuslab.bazelsteward.core.replacement

import org.virtuslab.bazelsteward.core.common.BazelFileSearch
import org.virtuslab.bazelsteward.core.common.FileUpdateSearch
import org.virtuslab.bazelsteward.core.common.UpdateLogic
import org.virtuslab.bazelsteward.core.library.LibraryId

class WholeVersionHeuristic : Heuristic {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see an issue with naming here.
In our domain we have Library that has LibraryId (group and artifact in case of maven) and version.

The basic heuristic looks for version only. But it looks for the whole version.

This heuristic however looks for the whole library, i.e. library id and version.

Thus version and whole-version doesn't reflect well what happens.

Could call it version-only and whole-library

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

override val name: String
get() = "whole-version"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above, could getter be avoided?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed


override fun <Lib : LibraryId> apply(
files: List<BazelFileSearch.BazelFile>,
updateSuggestion: UpdateLogic.UpdateSuggestion<Lib>
): FileUpdateSearch.FileChangeSuggestion? {
val markers = updateSuggestion.currentLibrary.id.associatedStrings()
val currentVersion = updateSuggestion.currentLibrary.version.value
val regex =
(markers + currentVersion).map { """(${Regex.escape(it)})""" }.reduce { acc, s -> "$acc.*$s" }.let { Regex(it) }
val matchResult = files.firstNotNullOfOrNull { regex.find(it.content)?.to(it.path) } ?: return null
val versionGroup = matchResult.first.groups[3] ?: return null
return FileUpdateSearch.FileChangeSuggestion(
updateSuggestion.currentLibrary, updateSuggestion.suggestedVersion, matchResult.second, versionGroup.range.first
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,251 @@
package org.virtuslab.bazelsteward.core.replacement

import io.kotest.matchers.shouldBe
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import org.virtuslab.bazelsteward.core.common.BazelFileSearch
import org.virtuslab.bazelsteward.core.common.FileUpdateSearch
import org.virtuslab.bazelsteward.core.common.UpdateLogic
import org.virtuslab.bazelsteward.core.config.BumpingStrategy
import org.virtuslab.bazelsteward.core.library.SemanticVersion
import org.virtuslab.bazelsteward.core.library.VersioningSchema
import org.virtuslab.bazelsteward.maven.MavenCoordinates
import org.virtuslab.bazelsteward.maven.MavenLibraryId
import kotlin.io.path.Path

class HeuristicTest {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no case for version being located in a variable and string interpolation is used to include it.
Also there is no test for when there are 2 libraries with the same version - version heuristic should fail.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed


private fun getBazelFile(fileName: String): BazelFileSearch.BazelFile {
val url = this::class.java.classLoader.getResource(fileName)!!
return BazelFileSearch.BazelFileTest(Path(url.path), url.readText())
}

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

@Test
fun `should return right position offset`() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right -> correct, as it might be confusing otherwise.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

val fileUpdateSearch = FileUpdateSearch()
val lib = MavenCoordinates(
MavenLibraryId("com.7theta", "utilis"),
SemanticVersion(2, 3, 5, "", ""),
VersioningSchema.SemVer,
BumpingStrategy.Default
)
val suggestedVersion = SemanticVersion(2, 4, 0, "", "")
val updateSuggestion = UpdateLogic.UpdateSuggestion(lib, suggestedVersion)

val files = listOf(getBazelFile("WORKSPACE.bzlignore"))

val result: List<FileUpdateSearch.FileChangeSuggestion> =
fileUpdateSearch.searchBuildFiles(files, listOf(updateSuggestion))
result[0].position shouldBe 2377
}

@Test
fun `should return right position offset with wrong artifact`() {
val fileUpdateSearch = FileUpdateSearch()
val lib = MavenCoordinates(
MavenLibraryId("com.10theta", "utilis"),
SemanticVersion(2, 3, 5, "", ""),
VersioningSchema.SemVer,
BumpingStrategy.Default
)
val suggestedVersion = SemanticVersion(2, 4, 0, "", "")
val updateSuggestion = UpdateLogic.UpdateSuggestion(lib, suggestedVersion)

val files = listOf(getBazelFile("WORKSPACE.bzlignore"))

val result: List<FileUpdateSearch.FileChangeSuggestion> =
fileUpdateSearch.searchBuildFiles(files, listOf(updateSuggestion))
result[0].position shouldBe 2377
}

@Test
fun `should return right position offset without artifact`() {
val fileUpdateSearch = FileUpdateSearch()
val lib = MavenCoordinates(
MavenLibraryId("", ""),
SemanticVersion(2, 3, 5, "", ""),
VersioningSchema.SemVer,
BumpingStrategy.Default
)
val suggestedVersion = SemanticVersion(2, 4, 0, "", "")
val updateSuggestion = UpdateLogic.UpdateSuggestion(lib, suggestedVersion)

val files = listOf(getBazelFile("WORKSPACE.bzlignore"))

val result: List<FileUpdateSearch.FileChangeSuggestion> =
fileUpdateSearch.searchBuildFiles(files, listOf(updateSuggestion))
result[0].position shouldBe 2377
}
}

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

@Test
fun `should return right position WholeVersionHeuristic`() {
val wholeVersionHeuristic = WholeVersionHeuristic()
val lib = MavenCoordinates(
MavenLibraryId("com.7theta", "utilis"),
SemanticVersion(2, 3, 5, "", ""),
VersioningSchema.SemVer,
BumpingStrategy.Default
)
val suggestedVersion = SemanticVersion(2, 4, 0, "", "")
val updateSuggestion = UpdateLogic.UpdateSuggestion(lib, suggestedVersion)

val files = listOf(getBazelFile("WORKSPACE.bzlignore"))

val result = wholeVersionHeuristic.apply(files, updateSuggestion)!!
result.position shouldBe 2377
}

@Test
fun `should return right position WholeVersionHeuristic with wrong artifact`() {
val wholeVersionHeuristic = WholeVersionHeuristic()
val lib = MavenCoordinates(
MavenLibraryId("com.10theta", "utilis"),
SemanticVersion(2, 3, 5, "", ""),
VersioningSchema.SemVer,
BumpingStrategy.Default
)
val suggestedVersion = SemanticVersion(2, 4, 0, "", "")
val updateSuggestion = UpdateLogic.UpdateSuggestion(lib, suggestedVersion)

val files = listOf(getBazelFile("WORKSPACE.bzlignore"))

val result = wholeVersionHeuristic.apply(files, updateSuggestion)
result shouldBe null
}

@Test
fun `should return right position WholeVersionHeuristic without artifact`() {
val wholeVersionHeuristic = WholeVersionHeuristic()
val lib = MavenCoordinates(
MavenLibraryId("", ""),
SemanticVersion(2, 3, 5, "", ""),
VersioningSchema.SemVer,
BumpingStrategy.Default
)
val suggestedVersion = SemanticVersion(2, 4, 0, "", "")
val updateSuggestion = UpdateLogic.UpdateSuggestion(lib, suggestedVersion)

val files = listOf(getBazelFile("WORKSPACE.bzlignore"))

val result = wholeVersionHeuristic.apply(files, updateSuggestion)!!
result.position shouldBe 2377
}
}

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

@Test
fun `should return right position offset VersionHeuristic`() {
val versionHeuristic = VersionHeuristic()
val lib = MavenCoordinates(
MavenLibraryId("com.7theta", "utilis"),
SemanticVersion(2, 3, 5, "", ""),
VersioningSchema.SemVer,
BumpingStrategy.Default
)
val suggestedVersion = SemanticVersion(2, 4, 0, "", "")
val updateSuggestion = UpdateLogic.UpdateSuggestion(lib, suggestedVersion)

val files = listOf(getBazelFile("WORKSPACE.bzlignore"))

val result = versionHeuristic.apply(files, updateSuggestion)!!
result.position shouldBe 2377
}

@Test
fun `should return right position offset VersionHeuristic with wrong artifact`() {
val versionHeuristic = VersionHeuristic()
val lib = MavenCoordinates(
MavenLibraryId("com.10theta", "utilis"),
SemanticVersion(2, 3, 5, "", ""),
VersioningSchema.SemVer,
BumpingStrategy.Default
)
val suggestedVersion = SemanticVersion(2, 4, 0, "", "")
val updateSuggestion = UpdateLogic.UpdateSuggestion(lib, suggestedVersion)

val files = listOf(getBazelFile("WORKSPACE.bzlignore"))

val result = versionHeuristic.apply(files, updateSuggestion)!!
result.position shouldBe 2377
}

@Test
fun `should return right position offset VersionHeuristic without artifact`() {
val versionHeuristic = VersionHeuristic()
val lib = MavenCoordinates(
MavenLibraryId("", ""),
SemanticVersion(2, 3, 5, "", ""),
VersioningSchema.SemVer,
BumpingStrategy.Default
)
val suggestedVersion = SemanticVersion(2, 4, 0, "", "")
val updateSuggestion = UpdateLogic.UpdateSuggestion(lib, suggestedVersion)

val files = listOf(getBazelFile("WORKSPACE.bzlignore"))

val result = versionHeuristic.apply(files, updateSuggestion)!!
result.position shouldBe 2377
}
}

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

@Test
fun `should return same position offset for WholeVersionHeuristic and VersionHeuristic`() {
val lib = MavenCoordinates(
MavenLibraryId("com.7theta", "utilis"),
SemanticVersion(2, 3, 5, "", ""),
VersioningSchema.SemVer,
BumpingStrategy.Default
)
val suggestedVersion = SemanticVersion(2, 4, 0, "", "")
val updateSuggestion = UpdateLogic.UpdateSuggestion(lib, suggestedVersion)

val files = listOf(getBazelFile("WORKSPACE.bzlignore"))

val wholeVersionHeuristic = WholeVersionHeuristic()
val result1 = wholeVersionHeuristic.apply(files, updateSuggestion)!!
val versionHeuristic = VersionHeuristic()
val result2 = versionHeuristic.apply(files, updateSuggestion)!!

result1.position shouldBe result2.position
}

@Test
fun `should return different position offset for WholeVersionHeuristic and VersionHeuristic`() {
val lib = MavenCoordinates(
MavenLibraryId("com.10theta", "utilis"),
SemanticVersion(2, 3, 5, "", ""),
VersioningSchema.SemVer,
BumpingStrategy.Default
)
val suggestedVersion = SemanticVersion(2, 4, 0, "", "")
val updateSuggestion = UpdateLogic.UpdateSuggestion(lib, suggestedVersion)

val files = listOf(getBazelFile("WORKSPACE.bzlignore"))

val wholeVersionHeuristic = WholeVersionHeuristic()
val result1 = wholeVersionHeuristic.apply(files, updateSuggestion)
val versionHeuristic = VersionHeuristic()
val result2 = versionHeuristic.apply(files, updateSuggestion)!!

result1 shouldBe null
result2.position shouldBe 2377
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
filegroup(
name = "resources",
srcs = glob([".*.yaml"]),
srcs = glob([
".*.yaml",
"*.bzlignore",
]),
visibility = ["//visibility:public"],
)
Loading