Implement a walk extension function for java.nio.file.Path #KT-52909
This commit is contained in:
committed by
Space
parent
7e5abdb1c0
commit
e7b37b3497
@@ -4,8 +4,12 @@
|
||||
*/
|
||||
|
||||
package kotlin.jdk7.test
|
||||
import java.nio.file.Path
|
||||
|
||||
import java.io.IOException
|
||||
import java.nio.file.*
|
||||
import java.nio.file.attribute.BasicFileAttributes
|
||||
import kotlin.io.path.deleteIfExists
|
||||
import kotlin.io.path.exists
|
||||
import kotlin.test.*
|
||||
|
||||
abstract class AbstractPathTest {
|
||||
@@ -17,10 +21,24 @@ abstract class AbstractPathTest {
|
||||
}
|
||||
|
||||
fun Path.cleanupRecursively(): Path {
|
||||
cleanUpActions.add(this to { it.toFile().deleteRecursively() })
|
||||
cleanUpActions.add(this to {
|
||||
if (it.exists(LinkOption.NOFOLLOW_LINKS)) Files.walkFileTree(it, cleanupVisitor)
|
||||
})
|
||||
return this
|
||||
}
|
||||
|
||||
private val cleanupVisitor = object : SimpleFileVisitor<Path>() {
|
||||
override fun visitFile(file: Path, attrs: BasicFileAttributes): FileVisitResult {
|
||||
file.deleteIfExists()
|
||||
return super.visitFile(file, attrs)
|
||||
}
|
||||
|
||||
override fun postVisitDirectory(dir: Path, exc: IOException?): FileVisitResult {
|
||||
dir.deleteIfExists()
|
||||
return super.postVisitDirectory(dir, exc)
|
||||
}
|
||||
}
|
||||
|
||||
@AfterTest
|
||||
fun cleanUp() {
|
||||
for ((path, action) in cleanUpActions) {
|
||||
@@ -31,4 +49,16 @@ abstract class AbstractPathTest {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun withRestrictedRead(vararg paths: Path, block: () -> Unit) {
|
||||
try {
|
||||
if (paths.all { it.toFile().setReadable(false) }) {
|
||||
block()
|
||||
} else {
|
||||
System.err.println("Couldn't restrict read access")
|
||||
}
|
||||
} finally {
|
||||
paths.forEach { it.toFile().setReadable(true) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user