fix: Actual functioning version checker

Also testing for potential edge-cases
This commit is contained in:
Ahmad Ansori Palembani 2024-06-15 09:31:25 +07:00
parent ec37c0f87d
commit c17be3831c
Signed by: null2264
GPG key ID: BA64F8B60AF3EFB6
2 changed files with 22 additions and 10 deletions

View file

@ -18,16 +18,19 @@ data class Version(
throw IllegalArgumentException("Can't compare two different version type")
}
return when {
major > other.major ||
minor > other.minor ||
patch > other.patch ||
hotfix > other.hotfix ||
build > other.build ||
stage.weight > other.stage.weight
-> 1
else -> 0
}
// On nightly we only care about build number
if (type == Type.NIGHTLY) return build.compareTo(other.build)
var rt = (major.compareTo(other.major) +
minor.compareTo(other.minor) +
patch.compareTo(other.patch) +
hotfix.compareTo(other.hotfix)).compareTo(0)
// if semver is equals, check version stage (release (3) > beta (2) > alpha (1))
if (rt == 0) rt = stage.weight.compareTo(other.stage.weight)
// if stage is also equals, we compare build number
if (rt == 0) rt = build.compareTo(other.build)
return rt
}
override fun toString(): String {

View file

@ -44,6 +44,7 @@ class AppUpdateCheckerTest {
@Test
fun `Check new beta version`() {
assertFalse(isNewVersion("1.2.3-b1", "1.2.3-b2"))
assertFalse(isNewVersion("1.2.2-b3", "1.2.3-b2"))
assertTrue(isNewVersion("1.2.3-b3", "1.2.3-b2"))
assertTrue(isNewVersion("1.2.4-b1", "1.2.3-b1"))
}
@ -52,12 +53,19 @@ class AppUpdateCheckerTest {
fun `Beta should get Prod build`() {
assertTrue(isNewVersion("1.2.4", "1.2.3-b2"))
assertTrue(isNewVersion("1.2.3", "1.2.3-b2"))
assertFalse(isNewVersion("1.2.2", "1.2.3-b2"))
}
@Test
fun `Prod should get latest Prod build`() {
assertFalse(isNewVersion("1.2.1", "1.2.2"))
assertFalse(isNewVersion("1.2.2.1", "1.2.3"))
assertFalse(isNewVersion("1.2.3", "1.2.3.1"))
assertFalse(isNewVersion("1.2.3.1", "1.2.3.2"))
assertTrue(isNewVersion("1.2.4", "1.2.3"))
assertTrue(isNewVersion("1.2.4.1", "1.2.4"))
assertTrue(isNewVersion("1.2.4.2", "1.2.4.1"))
}
@Test
@ -73,6 +81,7 @@ class AppUpdateCheckerTest {
@Test
fun `Latest version check`() {
assertFalse(isNewVersion("1.2.3", "1.2.3"))
assertFalse(isNewVersion("1.2.3.1", "1.2.3.1"))
assertFalse(isNewVersion("1.2.3-r1", "1.2.3-r1"))
assertFalse(isNewVersion("1.2.3-r1", "1.2.3-r1"))