Skip to content

Commit 13a564b

Browse files
committed
Merge branch 'yogurtearl-add_path_exists_assertion'
2 parents 7def144 + 3d4ab76 commit 13a564b

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

  • assertk/src

assertk/src/jvmMain/kotlin/assertk/assertions/path.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,15 @@ fun Assert<Path>.isSameFileAs(expected: Path) = given { actual ->
8787
expected("${show(actual)} to be the same file as ${show(actual)} but is not")
8888
}
8989
}
90+
91+
/**
92+
* Assert that the path exists.
93+
*
94+
* @param options indicating how symbolic links are handled
95+
*/
96+
@Suppress("SpreadOperator") // https://github.com/arturbosch/detekt/issues/391
97+
fun Assert<Path>.exists(vararg options: LinkOption) = given { actual ->
98+
if (!Files.exists(actual, *options)) {
99+
expected("${show(actual)} to exist, but it does not")
100+
}
101+
}

assertk/src/jvmTest/kotlin/test/assertk/assertions/PathTest.kt

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ import assertk.assertThat
44
import assertk.assertions.*
55
import java.nio.file.Files
66
import java.nio.file.Path
7+
import java.nio.file.Paths
78
import kotlin.test.*
89

910
private var regularFile: Path? = null
1011
private var directory: Path? = null
1112
private var regularFileWithText: Path? = null
13+
private var doesNotExist: Path? = null
1214

1315
class PathTest {
1416

@@ -17,6 +19,7 @@ class PathTest {
1719
regularFile = createTempFile()
1820
directory = createTempDir()
1921
regularFileWithText = createTempFileWithText()
22+
doesNotExist = Paths.get("/tmp/does_not_exist")
2023
}
2124

2225
@AfterTest
@@ -122,6 +125,23 @@ class PathTest {
122125
}
123126
//endregion
124127

128+
//region exists
129+
@Test fun exists_value_regularFile_passes() {
130+
assertThat(regularFile!!).exists()
131+
}
132+
133+
@Test fun exists_value_directory_passes() {
134+
assertThat(directory!!).exists()
135+
}
136+
137+
@Test fun exists_value_not_exists_fails() {
138+
val error = assertFails {
139+
assertThat(doesNotExist!!).exists()
140+
}
141+
assertEquals("expected <$doesNotExist> to exist, but it does not", error.message)
142+
}
143+
//endregion
144+
125145
//region lines
126146
@Test fun lines_correct_string_passes() {
127147
assertThat(regularFileWithText!!).lines().containsExactly("a", "b")
@@ -137,4 +157,4 @@ class PathTest {
137157

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

0 commit comments

Comments
 (0)