-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FileUtils: refactoring and unit tests (#388)
- Loading branch information
Showing
12 changed files
with
256 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,4 @@ | |
*.tab | ||
*.tab.values.at | ||
misc.xml | ||
/htmlReport |
10 changes: 10 additions & 0 deletions
10
app/src/main/java/dev/arkbuilders/navigator/data/utils/DevicePathsExtractor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package dev.arkbuilders.navigator.data.utils | ||
|
||
import java.nio.file.Path | ||
|
||
enum class Sorting { | ||
DEFAULT, NAME, SIZE, LAST_MODIFIED, TYPE | ||
} | ||
interface DevicePathsExtractor { | ||
fun listDevices(): List<Path> | ||
} |
26 changes: 26 additions & 0 deletions
26
app/src/main/java/dev/arkbuilders/navigator/data/utils/DevicePathsExtractorImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package dev.arkbuilders.navigator.data.utils | ||
|
||
import dev.arkbuilders.navigator.presentation.App | ||
import java.nio.file.Path | ||
import javax.inject.Inject | ||
|
||
class DevicePathsExtractorImpl @Inject constructor( | ||
private val appInstance: App | ||
) : DevicePathsExtractor { | ||
|
||
override fun listDevices(): List<Path> = | ||
appInstance | ||
.getExternalFilesDirs(null) | ||
.toList() | ||
.filterNotNull() | ||
.filter { it.exists() } | ||
.map { | ||
it.toPath().toRealPath() | ||
.takeWhile { part -> | ||
part != ANDROID_DIRECTORY | ||
} | ||
.fold(ROOT_PATH) { parent, child -> | ||
parent.resolve(child) | ||
} | ||
} | ||
} |
81 changes: 0 additions & 81 deletions
81
app/src/main/java/dev/arkbuilders/navigator/data/utils/FileUtils.kt
This file was deleted.
Oops, something went wrong.
58 changes: 58 additions & 0 deletions
58
app/src/main/java/dev/arkbuilders/navigator/data/utils/PathExt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package dev.arkbuilders.navigator.data.utils | ||
|
||
import java.nio.file.Path | ||
import java.nio.file.Paths | ||
import kotlin.io.path.exists | ||
import kotlin.io.path.extension | ||
import kotlin.io.path.nameWithoutExtension | ||
import kotlin.io.path.notExists | ||
|
||
val ROOT_PATH: Path = Paths.get("/") | ||
|
||
val ANDROID_DIRECTORY: Path = Paths.get("Android") | ||
|
||
fun Path.findNotExistCopyName(name: Path): Path { | ||
val originalNamePath = this.resolve(name.fileName) | ||
if (originalNamePath.notExists()) | ||
return originalNamePath | ||
|
||
var filesCounter = 1 | ||
|
||
val formatNameWithCounter = | ||
"${name.nameWithoutExtension}_$filesCounter.${name.extension}" | ||
|
||
var newPath = this.resolve(formatNameWithCounter) | ||
|
||
while (newPath.exists()) { | ||
newPath = this.resolve(formatNameWithCounter) | ||
filesCounter++ | ||
} | ||
return newPath | ||
} | ||
|
||
fun findLongestCommonPrefix(paths: List<Path>): Path { | ||
if (paths.isEmpty()) { | ||
throw IllegalArgumentException( | ||
"Can't search for common prefix among empty collection" | ||
) | ||
} | ||
|
||
if (paths.size == 1) { | ||
return paths.first() | ||
} | ||
|
||
return tailrec(ROOT_PATH, paths).first | ||
} | ||
|
||
private fun tailrec(prefix: Path, paths: List<Path>): Pair<Path, List<Path>> { | ||
val grouped = paths.groupBy { it.getName(0) } | ||
if (grouped.size > 1) { | ||
return prefix to paths | ||
} | ||
|
||
val resolvedPrefix = prefix.resolve(grouped.keys.first()) | ||
val shortened = grouped.values.first() | ||
.map { resolvedPrefix.relativize(it) } | ||
|
||
return tailrec(resolvedPrefix, shortened) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
app/src/test/java/dev/arkbuilders/navigator/data/utils/DevicePathsExtractorTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package dev.arkbuilders.navigator.data.utils | ||
|
||
import dev.arkbuilders.navigator.presentation.App | ||
import io.mockk.every | ||
import io.mockk.mockk | ||
import io.mockk.verify | ||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
import org.junit.runner.RunWith | ||
import org.mockito.junit.MockitoJUnitRunner | ||
import java.io.File | ||
import java.nio.file.Path | ||
import java.nio.file.Paths | ||
import kotlin.io.path.name | ||
|
||
@RunWith(MockitoJUnitRunner::class) | ||
class DevicePathsExtractorTest { | ||
|
||
private val mockedApplication = mockk<App>() | ||
|
||
private lateinit var testee: DevicePathsExtractor | ||
|
||
@BeforeEach | ||
fun setUp() { | ||
testee = DevicePathsExtractorImpl( | ||
appInstance = mockedApplication | ||
) | ||
} | ||
|
||
@Test | ||
fun givenApplicationAvailable_whenGetExternalFileDirs_thenReturnCorrectResult() { | ||
val mockedFile = mockk<File>() | ||
val files = listOf(mockedFile) | ||
every { mockedApplication.getExternalFilesDirs(null) } returns files.toTypedArray() | ||
every { mockedFile.exists() } returns true | ||
|
||
val mockedPath = mockk<Path>() | ||
every { mockedFile.toPath() } returns mockedPath | ||
every { mockedPath.toRealPath() } returns mockedPath | ||
|
||
val directoryIterator: MutableIterator<Path> = arrayListOf( | ||
Paths.get("PATH") | ||
).iterator() | ||
every { mockedPath.iterator() } returns directoryIterator | ||
|
||
val result = testee.listDevices() | ||
|
||
verify { mockedApplication.getExternalFilesDirs(null) } | ||
assertEquals(1, result.size) | ||
assertEquals("PATH", result[0].name) | ||
} | ||
} |
Oops, something went wrong.