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

Replacement Heuristic #132

merged 8 commits into from
Feb 11, 2023

Conversation

mikkoziel
Copy link
Contributor

Fixes #95

@@ -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

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

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

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


class WholeVersionHeuristic : Heuristic {
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

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

val currentVersion = updateSuggestion.currentLibrary.version.value
val regex =
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

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

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

fun `should return null when two libraries have same version`() {
val versionOnlyHeuristic = VersionOnlyHeuristic()
val lib = MavenCoordinates(
MavenLibraryId("", ""),
Copy link
Collaborator

Choose a reason for hiding this comment

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

To have a real use case, use actual library names here. It would also good to add test for updating these 2 libraries with standard heuristic (or at least to the general updater tests) to show that these 2 libraries can be updated.

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

@lukaszwawrzyk lukaszwawrzyk added this pull request to the merge queue Feb 11, 2023
@lukaszwawrzyk lukaszwawrzyk removed this pull request from the merge queue due to the queue being cleared Feb 11, 2023
@lukaszwawrzyk lukaszwawrzyk merged commit 6c6b063 into main Feb 11, 2023
MarconZet pushed a commit that referenced this pull request Feb 23, 2023
* Change replacement heuristic

* Expanded heuristic testes

* Fixes after lint

* Fixes after buildifier github action failed

* Fixes after buildifier github action failed

* Fixes after review

* Changes after lint

* Added test cases for same version of two libraries
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Extend heuristics for version replacement
2 participants