Fix ensureDbDirectoryExists

This commit is contained in:
XiangRongLin 2020-12-19 16:53:11 +01:00
parent af119db1d7
commit fcfdcd1025
2 changed files with 21 additions and 2 deletions

View File

@ -45,10 +45,9 @@ class ContentSettingsManager(private val fileLocator: NewPipeFileLocator) {
* @return Whether the directory exists afterwards. * @return Whether the directory exists afterwards.
*/ */
fun ensureDbDirectoryExists(): Boolean { fun ensureDbDirectoryExists(): Boolean {
return !fileLocator.dbDir.exists() && !fileLocator.dbDir.mkdir() return fileLocator.dbDir.exists() || fileLocator.dbDir.mkdir()
} }
fun extractDb(filePath: String): Boolean { fun extractDb(filePath: String): Boolean {
val success = ZipHelper.extractFileFromZip(filePath, fileLocator.db.path, "newpipe.db") val success = ZipHelper.extractFileFromZip(filePath, fileLocator.db.path, "newpipe.db")
if (success) { if (success) {

View File

@ -4,6 +4,7 @@ import android.content.SharedPreferences
import org.junit.Assert.assertEquals import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue import org.junit.Assert.assertTrue
import org.junit.Assume
import org.junit.Before import org.junit.Before
import org.junit.Test import org.junit.Test
import org.junit.runner.RunWith import org.junit.runner.RunWith
@ -85,6 +86,25 @@ class ContentSettingsManagerTest {
assertFalse(settings.exists()) assertFalse(settings.exists())
} }
@Test
fun `Ensuring db directory existence must work`() {
val dir = Files.createTempDirectory("newpipe_").toFile()
Assume.assumeTrue(dir.delete())
`when`(fileLocator.dbDir).thenReturn(dir)
ContentSettingsManager(fileLocator).ensureDbDirectoryExists()
assertTrue(dir.exists())
}
@Test
fun `Ensuring db directory existence must work when the directory already exists`() {
val dir = Files.createTempDirectory("newpipe_").toFile()
`when`(fileLocator.dbDir).thenReturn(dir)
ContentSettingsManager(fileLocator).ensureDbDirectoryExists()
assertTrue(dir.exists())
}
@Test @Test
fun `The database must be extracted from the zip file`() { fun `The database must be extracted from the zip file`() {
val db = File.createTempFile("newpipe_", "") val db = File.createTempFile("newpipe_", "")