Skip to content

Commit

Permalink
Add File doesNotExist assertion
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeWharton authored and evant committed Jun 6, 2024
1 parent d529997 commit c3d8358
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
8 changes: 8 additions & 0 deletions assertk/src/jvmMain/kotlin/assertk/assertions/file.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ fun Assert<File>.exists() = given { actual ->
expected("to exist")
}

/**
* Asserts the file does not exists.
*/
fun Assert<File>.doesNotExist() = given { actual ->
if (!actual.exists()) return
expected("not to exist")
}

/**
* Asserts the file is a directory.
* @see [isFile]
Expand Down
22 changes: 19 additions & 3 deletions assertk/src/jvmTest/kotlin/test/assertk/assertions/FileTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ class FileTest {

val file = File.createTempFile("exists", "txt")
val directory = Files.createTempDirectory("isDirectory").toFile()
val missing = File("/this/is/not/a/real/file/and/should/not/exist.txt").also {
assertFalse(it.exists())
}

//region exists
@Test
Expand All @@ -19,15 +22,28 @@ class FileTest {

@Test
fun exists_with_nonexistent_file_fails() {
val tempFile = File.createTempFile("exists", "txt")
tempFile.delete()
val error = assertFailsWith<AssertionError> {
assertThat(tempFile).exists()
assertThat(missing).exists()
}
assertEquals("expected to exist", error.message)
}
//endregion

//region does not exist
@Test
fun doesNotExist_with_missing_file_passes() {
assertThat(missing).doesNotExist()
}

@Test
fun doesNotExist_with_nonexistent_file_fails() {
val error = assertFailsWith<AssertionError> {
assertThat(file).doesNotExist()
}
assertEquals("expected not to exist", error.message)
}
//endregion

//region isDirectory
@Test
fun isDirectory_value_directory_passes() {
Expand Down

0 comments on commit c3d8358

Please sign in to comment.