Skip to content

Commit

Permalink
Merge branch 'yogurtearl-add_path_exists_assertion'
Browse files Browse the repository at this point in the history
  • Loading branch information
evant committed May 5, 2021
2 parents 7def144 + 3d4ab76 commit 13a564b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
12 changes: 12 additions & 0 deletions assertk/src/jvmMain/kotlin/assertk/assertions/path.kt
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,15 @@ fun Assert<Path>.isSameFileAs(expected: Path) = given { actual ->
expected("${show(actual)} to be the same file as ${show(actual)} but is not")
}
}

/**
* Assert that the path exists.
*
* @param options indicating how symbolic links are handled
*/
@Suppress("SpreadOperator") // https://github.com/arturbosch/detekt/issues/391
fun Assert<Path>.exists(vararg options: LinkOption) = given { actual ->
if (!Files.exists(actual, *options)) {
expected("${show(actual)} to exist, but it does not")
}
}
22 changes: 21 additions & 1 deletion assertk/src/jvmTest/kotlin/test/assertk/assertions/PathTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import assertk.assertThat
import assertk.assertions.*
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import kotlin.test.*

private var regularFile: Path? = null
private var directory: Path? = null
private var regularFileWithText: Path? = null
private var doesNotExist: Path? = null

class PathTest {

Expand All @@ -17,6 +19,7 @@ class PathTest {
regularFile = createTempFile()
directory = createTempDir()
regularFileWithText = createTempFileWithText()
doesNotExist = Paths.get("/tmp/does_not_exist")
}

@AfterTest
Expand Down Expand Up @@ -122,6 +125,23 @@ class PathTest {
}
//endregion

//region exists
@Test fun exists_value_regularFile_passes() {
assertThat(regularFile!!).exists()
}

@Test fun exists_value_directory_passes() {
assertThat(directory!!).exists()
}

@Test fun exists_value_not_exists_fails() {
val error = assertFails {
assertThat(doesNotExist!!).exists()
}
assertEquals("expected <$doesNotExist> to exist, but it does not", error.message)
}
//endregion

//region lines
@Test fun lines_correct_string_passes() {
assertThat(regularFileWithText!!).lines().containsExactly("a", "b")
Expand All @@ -137,4 +157,4 @@ class PathTest {

private fun createTempDir() = Files.createTempDirectory("tempDir")
private fun createTempFile() = Files.createTempFile("tempFile", "").apply { toFile().writeBytes(ByteArray(10)) }
private fun createTempFileWithText() = Files.createTempFile("tempFileWithText", "").apply { toFile().writeText("a\nb", Charsets.UTF_8) }
private fun createTempFileWithText() = Files.createTempFile("tempFileWithText", "").apply { toFile().writeText("a\nb", Charsets.UTF_8) }

0 comments on commit 13a564b

Please sign in to comment.