mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-21 05:46:03 +00:00
feat: local builds reuse the add-on packs a CI build installed (#1265)
This commit is contained in:
1 parent
c2c80c3540
commit
4992cf76dd
2 files changed
+74
-4
No files matched your search
@@ -19,7 +19,10 @@ import java.io.FileInputStream
|
||||
* yet to its pr-N release on DroidDeck-CI. The APK pins the pack's SHA-256, so only the bytes this build
|
||||
* was made with install, whichever host serves them.
|
||||
*/
|
||||
internal class AddonPack(val name: String, private val sha256: String, val size: Long, private val required: List<String>) {
|
||||
internal class AddonPack(
|
||||
val name: String, private val sha256: String, val size: Long, private val required: List<String>,
|
||||
private val reuseDeviceCopy: Boolean = sha256.isEmpty() && !BuildConfig.CI_BUILD,
|
||||
) {
|
||||
sealed interface State {
|
||||
data object Missing : State
|
||||
data class Downloading(val progress: Float) : State
|
||||
@@ -40,10 +43,20 @@ internal class AddonPack(val name: String, private val sha256: String, val size:
|
||||
|
||||
private fun root(context: Context) = File(context.filesDir, "addons/$name")
|
||||
/** App-private and written only after the archive matched the pinned digest. */
|
||||
fun dir(context: Context) = File(root(context), sha256)
|
||||
fun dir(context: Context) = File(root(context), version(context))
|
||||
fun file(context: Context, relative: String) = File(dir(context), relative)
|
||||
|
||||
fun installed(context: Context): Boolean = available && File(dir(context), COMPLETE).isFile
|
||||
fun installed(context: Context): Boolean = version(context).isNotEmpty() && File(dir(context), COMPLETE).isFile
|
||||
|
||||
// A build made outside CI pins no packs, so it runs the newest one a CI build already verified and installed.
|
||||
private fun version(context: Context): String = if (!reuseDeviceCopy) sha256 else deviceCopy(context).orEmpty()
|
||||
|
||||
private fun deviceCopy(context: Context): String? = root(context).listFiles().orEmpty()
|
||||
.filter { dir ->
|
||||
dir.isDirectory && runCatching { File(dir, COMPLETE).readText().trim() }.getOrNull() == dir.name &&
|
||||
required.all { File(dir, it).isFile }
|
||||
}
|
||||
.maxByOrNull { it.lastModified() }?.name
|
||||
|
||||
fun refresh(context: Context): State {
|
||||
val now = mutableState.value
|
||||
@@ -86,7 +99,7 @@ internal class AddonPack(val name: String, private val sha256: String, val size:
|
||||
|
||||
private fun install(context: Context): Boolean {
|
||||
if (!available) {
|
||||
Log.w(TAG, "$name: this build has no pack")
|
||||
Log.w(TAG, if (reuseDeviceCopy) "$name: this build has no pack and none is installed" else "$name: this build has no pack")
|
||||
return false
|
||||
}
|
||||
val root = root(context).apply { mkdirs() }
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
package com.droiddeck.launcher.core
|
||||
|
||||
import android.content.Context
|
||||
import androidx.test.core.app.ApplicationProvider
|
||||
import java.io.File
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
class AddonPackTest {
|
||||
private val context: Context = ApplicationProvider.getApplicationContext()
|
||||
private val root get() = File(context.filesDir, "addons/sample")
|
||||
|
||||
@Before fun clean() {
|
||||
root.deleteRecursively()
|
||||
}
|
||||
|
||||
private fun installed(version: String, complete: Boolean = true, modified: Long = 1_000L): File =
|
||||
File(root, version).apply {
|
||||
mkdirs()
|
||||
File(this, "tool").writeText("x")
|
||||
if (complete) File(this, AddonPack.COMPLETE).writeText(version)
|
||||
setLastModified(modified)
|
||||
}
|
||||
|
||||
@Test fun unpinnedLocalBuildReusesTheNewestCompletePack() {
|
||||
installed("a".repeat(64), modified = 1_000L)
|
||||
installed("b".repeat(64), modified = 2_000L)
|
||||
installed("c".repeat(64), complete = false, modified = 3_000L)
|
||||
val pack = AddonPack("sample", "", 0L, listOf("tool"), reuseDeviceCopy = true)
|
||||
|
||||
assertTrue(pack.installed(context))
|
||||
assertEquals("b".repeat(64), pack.dir(context).name)
|
||||
}
|
||||
|
||||
@Test fun unpinnedLocalBuildWithNothingInstalledHasNoPack() {
|
||||
assertFalse(AddonPack("sample", "", 0L, listOf("tool"), reuseDeviceCopy = true).installed(context))
|
||||
}
|
||||
|
||||
@Test fun pinnedBuildIgnoresOtherInstalledVersions() {
|
||||
installed("b".repeat(64))
|
||||
val pack = AddonPack("sample", "a".repeat(64), 1L, listOf("tool"))
|
||||
|
||||
assertFalse(pack.installed(context))
|
||||
assertEquals("a".repeat(64), pack.dir(context).name)
|
||||
}
|
||||
|
||||
@Test fun ciBuildWithoutAPinNeverReuses() {
|
||||
installed("b".repeat(64))
|
||||
assertFalse(AddonPack("sample", "", 0L, listOf("tool"), reuseDeviceCopy = false).installed(context))
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user