Skip to content

Commit

Permalink
Use regex to clean up semantic version string
Browse files Browse the repository at this point in the history
  • Loading branch information
Antonis Lilis committed Nov 25, 2022
1 parent 398ff12 commit 02fec9c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ class WordPressPublicData @Inject constructor(private val packageManagerWrapper:
fun currentPackageVersion(): String? = packageManagerWrapper.getPackageInfo(currentPackageId())?.versionName

fun nonSemanticPackageVersion(): String? {
val rawVersion = currentPackageVersion()
val rawVersion = currentPackageVersion() ?: return null

// Clean app semantic versioning info. E.g 21.2-rc-3 turns to 21.2
val wordPressVersion = rawVersion?.split("-")?.getOrNull(0) ?: rawVersion ?: ""
// Clean app semantic versioning and keep ony major-minor version info. E.g 21.2-rc-3 turns to 21.2
val majorMinorRegex = "^(\\d*)(\\.(\\d*))".toRegex()
val wordPressVersion = majorMinorRegex.find(rawVersion)?.value ?: return null

// Version is supported by org.wordpress.android.util.helpers.Version.Version
// Verify that the resulting version is supported by org.wordpress.android.util.helpers.Version.Version
val versionIsSupportedForComparison = Regex("[0-9]+(\\.[0-9]+)*").matchEntire(wordPressVersion) != null

return if(versionIsSupportedForComparison) wordPressVersion else null
return if (versionIsSupportedForComparison) wordPressVersion else null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,30 @@ class WordPressPublicDataTest {
Assertions.assertThat(actual).isEqualTo(expected)
}

@Test
fun `Empty versions should return a null non semantic version`() {
mockVersion("")
val actual = classToTest.nonSemanticPackageVersion()
val expected = null
Assertions.assertThat(actual).isEqualTo(expected)
}

@Test
fun `Only the major-minor version information is returned`() {
mockVersion("21.3.1")
val actual = classToTest.nonSemanticPackageVersion()
val expected = "21.3"
Assertions.assertThat(actual).isEqualTo(expected)
}

@Test
fun `When only the major is provided a null non semantic version is returned`() {
mockVersion("21")
val actual = classToTest.nonSemanticPackageVersion()
val expected = null
Assertions.assertThat(actual).isEqualTo(expected)
}

private fun mockVersion(version: String) {
val packageInfo = PackageInfo().apply { versionName = version }
whenever(packageManagerWrapper.getPackageInfo(any(), any())).thenReturn(packageInfo)
Expand Down

0 comments on commit 02fec9c

Please sign in to comment.