Skip to content

Commit

Permalink
Update fileTree test util to only use NIO Path (#3892)
Browse files Browse the repository at this point in the history
`fileTree.kt` contains a utility for logging pretty file trees, which is useful in providing human-readable context for test failures.

This PR updates the utility to only use NIO Path, rather than the older `java.io.File`.

This change was split off from #3814 to make the PR smaller
  • Loading branch information
adam-enko authored Oct 31, 2024
1 parent 2ed83e5 commit 9696cc2
Showing 1 changed file with 10 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,17 @@
*/
package org.jetbrains.dokka.gradle.utils

import java.io.File
import java.nio.file.Path
import kotlin.io.path.*

// based on https://gist.github.com/mfwgenerics/d1ec89eb80c95da9d542a03b49b5e15b
// context: https://kotlinlang.slack.com/archives/C0B8MA7FA/p1676106647658099


fun Path.toTreeString(
fileFilter: FileFilter = FileFilter { true },
): String =
toFile().toTreeString(fileFilter = fileFilter)


fun File.toTreeString(
fileFilter: FileFilter = FileFilter { true },
): String = when {
isDirectory -> name + "/\n" + buildTreeString(dir = this, fileFilter = fileFilter)
isDirectory() -> name + "/\n" + buildTreeString(dir = this, fileFilter = fileFilter)
else -> name
}

Expand All @@ -28,21 +22,21 @@ fun File.toTreeString(
* Optionally include/exclude files. Directories will always be included.
*/
fun interface FileFilter {
operator fun invoke(file: File): Boolean
operator fun invoke(file: Path): Boolean
}


private fun FileFilter.matches(file: File): Boolean =
if (file.isDirectory) {
private fun FileFilter.matches(file: Path): Boolean =
if (file.isDirectory()) {
// don't include directories that have no matches
file.walk().any { it.isFile && invoke(it) }
file.walk().any { it.isRegularFile() && invoke(it) }
} else {
invoke(file)
}


private fun buildTreeString(
dir: File,
dir: Path,
fileFilter: FileFilter = FileFilter { true },
margin: String = "",
): String {
Expand All @@ -58,7 +52,7 @@ private fun buildTreeString(
buildString {
append("$margin${currentPrefix}${entry.name}")

if (entry.isDirectory) {
if (entry.isDirectory()) {
append("/")
if (entry.countDirectoryEntries(fileFilter) > 0) {
append("\n")
Expand All @@ -69,19 +63,11 @@ private fun buildTreeString(
}
}

private fun File.listDirectoryEntries(): Sequence<File> =
walkTopDown()
.maxDepth(1)
.filter { it != this@listDirectoryEntries }
.sortedWith(FileSorter)


private fun File.countDirectoryEntries(
private fun Path.countDirectoryEntries(
fileFilter: FileFilter,
): Int =
listDirectoryEntries()
.filter { file -> fileFilter.matches(file) }
.count()
listDirectoryEntries().count { file -> fileFilter.matches(file) }


private data class PrefixPair(
Expand All @@ -98,17 +84,3 @@ private data class PrefixPair(
val LAST_ENTRY = PrefixPair("└── ", " ")
}
}


/**
* Directories before files, otherwise sort by filename.
*/
private object FileSorter : Comparator<File> {
override fun compare(o1: File, o2: File): Int {
return when {
o1.isDirectory && o2.isFile -> -1 // directories before files
o1.isFile && o2.isDirectory -> +1 // files after directories
else -> o1.name.compareTo(o2.name)
}
}
}

0 comments on commit 9696cc2

Please sign in to comment.