Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Path.shouldBeAFile should fail on non-existant files #3826

Merged
merged 1 commit into from
Jan 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import io.kotest.matchers.shouldNotBe
import java.io.File
import java.nio.file.Files
import java.nio.file.Path
import kotlin.io.path.Path
import kotlin.io.path.isRegularFile
import kotlin.streams.toList

infix fun Path.shouldStartWithPath(file: File) = this should startWithPath(file)
Expand Down Expand Up @@ -66,9 +68,9 @@ fun Path.shouldBeAFile() = this should aFile()
fun Path.shouldNotBeAFile() = this shouldNot aFile()
fun aFile(): Matcher<Path> = object : Matcher<Path> {
override fun test(value: Path): MatcherResult = MatcherResult(
!Files.isDirectory(value),
{ "File $value should be a directory" },
{ "File $value should not be a directory" })
value.isRegularFile(),
{ "Path $value should be a regular file" },
{ "Path $value should not be a regular file" })
}

fun Path.shouldBeAbsolute() = this should beAbsolute()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
package com.sksamuel.kotest.matchers.paths

import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.paths.shouldBeAFile
import io.kotest.matchers.paths.shouldBeEmptyDirectory
import io.kotest.matchers.paths.shouldNotBeEmptyDirectory
import java.nio.file.Files
import kotlin.io.path.Path

@Suppress("BlockingMethodInNonBlockingContext")
class PathMatchersTest : FunSpec() {
class PathMatchersTest : FunSpec({

init {

test("directory should be empty (deprecated)") {
val path = Files.createTempDirectory("testdir")
path.shouldBeEmptyDirectory()
Files.write(path.resolve("testfile.txt"), byteArrayOf(1, 2, 3))
path.shouldNotBeEmptyDirectory()
}
context("Be a file") {
test("Should fail for files that don't exist") {
shouldThrow<AssertionError> { Path("abc").shouldBeAFile() }
}
}

test("directory should be empty") {

test("directory should be empty (deprecated)") {
val path = Files.createTempDirectory("testdir")
path.shouldBeEmptyDirectory()
Files.write(path.resolve("testfile.txt"), byteArrayOf(1, 2, 3))
path.shouldNotBeEmptyDirectory()
}

test("directory should be empty") {
val path = Files.createTempDirectory("testdir")
path.shouldBeEmptyDirectory()
Files.write(path.resolve("testfile.txt"), byteArrayOf(1, 2, 3))
path.shouldNotBeEmptyDirectory()
}
}
}
}
})