restructure and test version checker
This commit is contained in:
parent
469ea2d1f4
commit
04618156cc
2 changed files with 108 additions and 16 deletions
|
@ -23,7 +23,7 @@ fun isConnected(): Boolean {
|
||||||
|
|
||||||
// convert inputstream to String
|
// convert inputstream to String
|
||||||
@Throws(IOException::class)
|
@Throws(IOException::class)
|
||||||
fun InputStream.findVersion(): String? {
|
inline fun InputStream.findVersion(): String? {
|
||||||
val regex = "(.*)version(.*)\"(((\\d+)\\.)+(\\d+))\"(.*)".toRegex()
|
val regex = "(.*)version(.*)\"(((\\d+)\\.)+(\\d+))\"(.*)".toRegex()
|
||||||
return bufferedReader()
|
return bufferedReader()
|
||||||
.readLines()
|
.readLines()
|
||||||
|
@ -41,19 +41,31 @@ fun checkVersion() = if (isConnected()) {
|
||||||
val request = HttpGet("https://raw.githubusercontent.com/MilosKozak/AndroidAPS/master/app/build.gradle")
|
val request = HttpGet("https://raw.githubusercontent.com/MilosKozak/AndroidAPS/master/app/build.gradle")
|
||||||
val response: HttpResponse = DefaultHttpClient().execute(request)
|
val response: HttpResponse = DefaultHttpClient().execute(request)
|
||||||
val version: String? = response.entity.content?.findVersion()
|
val version: String? = response.entity.content?.findVersion()
|
||||||
val comparison = version?.compareTo(BuildConfig.VERSION_NAME.replace("\"", "")) ?: 0
|
compareWithCurrentVersion(version, BuildConfig.VERSION_NAME)
|
||||||
if (comparison == 0) {
|
|
||||||
log.debug("Version equal to master of fetch failed")
|
|
||||||
} else if (comparison > 0) {
|
|
||||||
log.debug("Version outdated. Found $version")
|
|
||||||
val notification = Notification(Notification.NEWVERSIONDETECTED, String.format(MainApp.gs(R.string.versionavailable), version.toString()), Notification.LOW)
|
|
||||||
MainApp.bus().post(EventNewNotification(notification))
|
|
||||||
} else {
|
|
||||||
log.debug("Version newer than master. Are you developer?")
|
|
||||||
}
|
|
||||||
} catch (e: IOException) {
|
} catch (e: IOException) {
|
||||||
log.debug("Github master version check error: $e")
|
log.debug("Github master version check error: $e")
|
||||||
}
|
}
|
||||||
}.start()
|
}.start()
|
||||||
} else
|
} else
|
||||||
log.debug("Github master version no checked. No connectivity")
|
log.debug("Github master version no checked. No connectivity")
|
||||||
|
|
||||||
|
fun compareWithCurrentVersion(newVersion: String?, currentVersion: String) {
|
||||||
|
val comparison = newVersion?.versionStrip()?.compareTo(currentVersion.versionStrip()) ?: 0
|
||||||
|
when {
|
||||||
|
comparison == 0 -> log.debug("Version equal to master of fetch failed")
|
||||||
|
comparison > 0 -> {
|
||||||
|
log.debug("Version ${currentVersion} outdated. Found $newVersion")
|
||||||
|
val notification = Notification(Notification.NEWVERSIONDETECTED, String.format(MainApp.gs(R.string.versionavailable), newVersion.toString()), Notification.LOW)
|
||||||
|
MainApp.bus().post(EventNewNotification(notification))
|
||||||
|
}
|
||||||
|
else -> log.debug("Version newer than master. Are you developer?")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun String.versionStrip() = this.mapNotNull {
|
||||||
|
when (it) {
|
||||||
|
in '0'..'9' -> it
|
||||||
|
'.' -> it
|
||||||
|
else -> null
|
||||||
|
}
|
||||||
|
}.joinToString (separator = "")
|
|
@ -1,8 +1,18 @@
|
||||||
package info.nightscout.androidaps.plugins.general.versionChecker
|
package info.nightscout.androidaps.plugins.general.versionChecker
|
||||||
|
|
||||||
|
import com.squareup.otto.Bus
|
||||||
|
import info.nightscout.androidaps.MainApp
|
||||||
|
import info.nightscout.androidaps.logging.L
|
||||||
import org.junit.Assert.assertEquals
|
import org.junit.Assert.assertEquals
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
|
import org.junit.runner.RunWith
|
||||||
|
import org.mockito.ArgumentMatchers
|
||||||
|
import org.mockito.Mockito.*
|
||||||
|
import org.powermock.api.mockito.PowerMockito
|
||||||
|
import org.powermock.core.classloader.annotations.PrepareForTest
|
||||||
|
import org.powermock.modules.junit4.PowerMockRunner
|
||||||
|
|
||||||
|
@RunWith(PowerMockRunner::class)
|
||||||
class VersionCheckerUtilsKtTest {
|
class VersionCheckerUtilsKtTest {
|
||||||
@Test
|
@Test
|
||||||
fun findVersionMatchesRegularVersion() {
|
fun findVersionMatchesRegularVersion() {
|
||||||
|
@ -17,9 +27,10 @@ class VersionCheckerUtilsKtTest {
|
||||||
assertEquals("2.2.2", detectedVersion)
|
assertEquals("2.2.2", detectedVersion)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 04. Break stuff.mp3 like it's 1999. Again. Pizza delivery! For i c wiener ...
|
|
||||||
//@Test
|
// In case we merge a "x.x.x-dev" into master, don't see it as update.
|
||||||
fun findVersionMatchesCustomVersion() {
|
@Test
|
||||||
|
fun `should return null on non-digit versions on master`() {
|
||||||
val buildGradle = """blabla
|
val buildGradle = """blabla
|
||||||
| android {
|
| android {
|
||||||
| aosenuthoae
|
| aosenuthoae
|
||||||
|
@ -28,7 +39,7 @@ class VersionCheckerUtilsKtTest {
|
||||||
| appName = "Aaoeu"
|
| appName = "Aaoeu"
|
||||||
""".trimMargin()
|
""".trimMargin()
|
||||||
val detectedVersion: String? = buildGradle.byteInputStream().findVersion()
|
val detectedVersion: String? = buildGradle.byteInputStream().findVersion()
|
||||||
assertEquals("2.2.2", detectedVersion)
|
assertEquals(null, detectedVersion)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -37,4 +48,73 @@ class VersionCheckerUtilsKtTest {
|
||||||
val detectedVersion: String? = buildGradle.byteInputStream().findVersion()
|
val detectedVersion: String? = buildGradle.byteInputStream().findVersion()
|
||||||
assertEquals(null, detectedVersion)
|
assertEquals(null, detectedVersion)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testVersionStrip() {
|
||||||
|
assertEquals("2.2.2", "2.2.2".versionStrip())
|
||||||
|
assertEquals("2.2.2", "2.2.2-dev".versionStrip())
|
||||||
|
assertEquals("2.2.2", "2.2.2dev".versionStrip())
|
||||||
|
assertEquals("2.2.2", """"2.2.2"""".versionStrip())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@PrepareForTest(MainApp::class, L::class)
|
||||||
|
fun `should find update1`() {
|
||||||
|
val bus = prepareCompareTests()
|
||||||
|
compareWithCurrentVersion(newVersion = "2.2.3", currentVersion = "2.2.1")
|
||||||
|
verify(bus, times(1)).post(any())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@PrepareForTest(MainApp::class, L::class)
|
||||||
|
fun `should find update2`() {
|
||||||
|
val bus = prepareCompareTests()
|
||||||
|
compareWithCurrentVersion(newVersion = "2.2.3", currentVersion = "2.2.1-dev")
|
||||||
|
verify(bus, times(1)).post(any())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@PrepareForTest(MainApp::class, L::class)
|
||||||
|
fun `should find update3`() {
|
||||||
|
val bus = prepareCompareTests()
|
||||||
|
compareWithCurrentVersion(newVersion = "2.2.3", currentVersion = "2.1")
|
||||||
|
verify(bus, times(1)).post(any())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@PrepareForTest(MainApp::class, L::class)
|
||||||
|
fun `should find update4`() {
|
||||||
|
val bus = prepareCompareTests()
|
||||||
|
|
||||||
|
compareWithCurrentVersion(newVersion = "2.2", currentVersion = "2.1.1")
|
||||||
|
|
||||||
|
verify(bus, times(1)).post(any())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@PrepareForTest(MainApp::class, L::class)
|
||||||
|
fun `should find update5`() {
|
||||||
|
val bus = prepareCompareTests()
|
||||||
|
compareWithCurrentVersion(newVersion = "2.2.1", currentVersion = "2.2-dev")
|
||||||
|
verify(bus, times(1)).post(any())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@PrepareForTest(MainApp::class, L::class)
|
||||||
|
fun `should find update6`() {
|
||||||
|
val bus = prepareCompareTests()
|
||||||
|
compareWithCurrentVersion(newVersion = "2.2.1", currentVersion = "2.2dev")
|
||||||
|
verify(bus, times(1)).post(any())
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private fun prepareCompareTests(): Bus {
|
||||||
|
PowerMockito.mockStatic(MainApp::class.java)
|
||||||
|
val mainApp = mock<MainApp>(MainApp::class.java)
|
||||||
|
`when`(MainApp.instance()).thenReturn(mainApp)
|
||||||
|
val bus = mock(Bus::class.java)
|
||||||
|
`when`(MainApp.bus()).thenReturn(bus)
|
||||||
|
`when`(MainApp.gs(ArgumentMatchers.anyInt())).thenReturn("some dummy string")
|
||||||
|
return bus
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue