Skip to content

Commit

Permalink
chore: Rewrite using dsl two tests related to permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
guicamest committed Jan 4, 2024
1 parent bb2addd commit 44aadeb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,9 @@ import java.util.stream.Stream
import kotlin.io.path.absolute
import kotlin.io.path.createDirectory
import kotlin.io.path.createSymbolicLinkPointingTo
import kotlin.io.path.createTempDirectory
import kotlin.io.path.createTempFile
import kotlin.io.path.div
import kotlin.io.path.fileSize
import kotlin.io.path.isReadable
import kotlin.io.path.setPosixFilePermissions
import kotlin.io.path.writeText
import kotlin.test.Test

Expand Down Expand Up @@ -124,15 +121,15 @@ class DuplicateFinderTest {
}
}

private val noPermissions = PosixFilePermissions.fromString("-".repeat(9))

@Test
fun `directory exists and is not readable`() {
inLinux { fs ->
val notReadableDirectory = createTempDirectory(fs.rootDirectories.first())
repeat(2) {
createTempFile(directory = notReadableDirectory).writeText("hi")
}
val notReadablePermissions = PosixFilePermissions.fromString("-".repeat(9))
notReadableDirectory.setPosixFilePermissions(notReadablePermissions)
val notReadableDirectory =
directory(fs = fs, posixPermissions = noPermissions) {
repeat(2) { file(content = "hi") }
}.path
assertThat(notReadableDirectory).isNot(
Condition({ file -> file.isReadable() }, "not readable"),
)
Expand All @@ -147,14 +144,10 @@ class DuplicateFinderTest {
@Test
fun `files are not readable`() {
inLinux { fs ->
val notReadablePermissions = PosixFilePermissions.fromString("-".repeat(9))
val directory = createTempDirectory(fs.rootDirectories.first())
repeat(2) {
createTempFile(directory = directory).apply {
writeText("hi")
setPosixFilePermissions(notReadablePermissions)
}
}
val directory =
directory(fs = fs) {
repeat(2) { file(content = "hi", posixPermissions = noPermissions) }
}.path
val execution = findDuplicates(duplicateFinder, directory = directory)
runTest {
assertThat(execution.duplicateEntries()).isEmpty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ package com.sleepcamel.gduplicatefinder.core
import java.nio.file.FileSystem
import java.nio.file.FileSystems
import java.nio.file.Path
import java.nio.file.attribute.PosixFilePermission
import kotlin.io.path.createDirectories
import kotlin.io.path.createSymbolicLinkPointingTo
import kotlin.io.path.createTempDirectory
import kotlin.io.path.createTempFile
import kotlin.io.path.deleteExisting
import kotlin.io.path.deleteIfExists
import kotlin.io.path.div
import kotlin.io.path.setPosixFilePermissions
import kotlin.io.path.writeText

fun directory(
Expand All @@ -36,8 +38,13 @@ fun directory(

fun directory(
fs: FileSystem = FileSystems.getDefault(),
posixPermissions: Set<PosixFilePermission>? = null,
configure: TestDirectory.() -> Unit,
): TestDirectory = TestDirectory(fs.tempDirectory, null, false).apply(configure)
): TestDirectory =
TestDirectory(fs.tempDirectory, null, false).apply {
configure(this)
if (posixPermissions != null) path.setPosixFilePermissions(posixPermissions)
}

fun directory(
path: Path,
Expand Down Expand Up @@ -79,6 +86,7 @@ class TestDirectory(
fun file(
name: String? = null,
content: String,
posixPermissions: Set<PosixFilePermission>? = null,
): Path =
if (name == null) {
createTempFile(directory = path)
Expand All @@ -87,6 +95,7 @@ class TestDirectory(
}.apply {
files.add(this)
writeText(content)
if (posixPermissions != null) setPosixFilePermissions(posixPermissions)
}

fun symlink(
Expand Down

0 comments on commit 44aadeb

Please sign in to comment.