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

Add File doesNotExist assertion #540

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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