Skip to content

Commit e7e22ad

Browse files
committed
Clean up test
1 parent 2366cac commit e7e22ad

File tree

1 file changed

+26
-41
lines changed
  • clikt/src/test/kotlin/com/github/ajalt/clikt/parameters/types

1 file changed

+26
-41
lines changed

clikt/src/test/kotlin/com/github/ajalt/clikt/parameters/types/PathTest.kt

Lines changed: 26 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,27 @@ package com.github.ajalt.clikt.parameters.types
22

33
import com.github.ajalt.clikt.core.BadParameterValue
44
import com.github.ajalt.clikt.core.CliktCommand
5+
import com.github.ajalt.clikt.core.NoRunCliktCommand
56
import com.github.ajalt.clikt.parameters.arguments.argument
67
import com.github.ajalt.clikt.parameters.arguments.multiple
78
import com.github.ajalt.clikt.parameters.options.option
89
import com.github.ajalt.clikt.parameters.options.required
910
import com.github.ajalt.clikt.testing.splitArgv
1011
import com.google.common.jimfs.Configuration
1112
import com.google.common.jimfs.Jimfs
12-
import io.kotlintest.fail
1313
import io.kotlintest.shouldBe
14-
import java.nio.file.Files
15-
import java.nio.file.FileSystem
14+
import io.kotlintest.shouldThrow
1615
import org.junit.Test
16+
import java.nio.file.FileSystem
17+
import java.nio.file.Files
1718

19+
@Suppress("unused")
1820
class PathTest {
1921
val fs: FileSystem = Jimfs.newFileSystem(Configuration.unix())
2022

2123
@Test
2224
fun `paths are resolved using the provided filesystem, if any`() {
23-
val command = object : CliktCommand() {
25+
class C : CliktCommand() {
2426
val path by option("-p")
2527
.path(fileSystem = fs)
2628
.required()
@@ -30,12 +32,12 @@ class PathTest {
3032
}
3133
}
3234

33-
command.parse(splitArgv("-p/var/log/foo"))
35+
C().parse(splitArgv("-p/var/log/foo"))
3436
}
3537

3638
@Test
3739
fun `options can be paths`() {
38-
val command = object : CliktCommand() {
40+
class C : CliktCommand() {
3941
val path by option("-p")
4042
.path()
4143
.required()
@@ -45,12 +47,12 @@ class PathTest {
4547
}
4648
}
4749

48-
command.parse(splitArgv("-pfoo"))
50+
C().parse(splitArgv("-pfoo"))
4951
}
5052

5153
@Test
5254
fun `arguments can be paths`() {
53-
val command = object : CliktCommand() {
55+
class C : CliktCommand() {
5456
val paths by argument()
5557
.path()
5658
.multiple()
@@ -60,61 +62,44 @@ class PathTest {
6062
}
6163
}
6264

63-
command.parse(splitArgv("foo bar baz"))
65+
C().parse(splitArgv("foo bar baz"))
6466
}
6567

6668
@Test
6769
fun `fileOkay = false will reject files`() {
68-
val command = object : CliktCommand() {
70+
class C : NoRunCliktCommand() {
6971
val folderOnly by option("-f").path(fileOkay = false, fileSystem = fs)
70-
71-
override fun run() {
72-
}
7372
}
7473

7574
Files.createDirectory(fs.getPath("/var"))
7675
Files.createFile(fs.getPath("/var/foo"))
7776

78-
expectBadParameter("Invalid value for \"-f\": Directory \"/var/foo\" is a file.") {
79-
command.parse(splitArgv("-f/var/foo"))
80-
}
77+
shouldThrow<BadParameterValue> {
78+
C().parse(splitArgv("-f/var/foo"))
79+
}.message shouldBe """Invalid value for "-f": Directory "/var/foo" is a file."""
8180
}
8281

8382
@Test
8483
fun `folderOkay = false will reject folders`() {
85-
val command = object : CliktCommand() {
84+
class C : NoRunCliktCommand() {
8685
val fileOnly by option("-f").path(folderOkay = false, fileSystem = fs)
87-
88-
override fun run() {
89-
}
9086
}
9187

9288
Files.createDirectories(fs.getPath("/var/foo"))
9389

94-
expectBadParameter("Invalid value for \"-f\": File \"/var/foo\" is a directory.") {
95-
command.parse(splitArgv("-f/var/foo"))
96-
}
90+
shouldThrow<BadParameterValue> {
91+
C().parse(splitArgv("-f/var/foo"))
92+
}.message shouldBe """Invalid value for "-f": File "/var/foo" is a directory."""
9793
}
9894

99-
@Test fun `exists = true will reject paths that don't exist` () {
100-
val command = object : CliktCommand() {
95+
@Test
96+
fun `exists = true will reject paths that don't exist`() {
97+
class C : NoRunCliktCommand() {
10198
val homeDir by option("-h").path(exists = true, fileSystem = fs)
102-
103-
override fun run() {
104-
}
10599
}
106100

107-
expectBadParameter("Invalid value for \"-h\": Path \"/home/cli\" does not exist.") {
108-
command.parse(splitArgv("-h /home/cli"))
109-
}
110-
}
111-
112-
private inline fun expectBadParameter(message: String, fn: () -> Unit) {
113-
try {
114-
fn()
115-
fail("parse should have failed with a BadParameterValue exception")
116-
} catch (e: BadParameterValue) {
117-
e.message shouldBe message
118-
}
101+
shouldThrow<BadParameterValue> {
102+
C().parse(splitArgv("-h /home/cli"))
103+
}.message shouldBe """Invalid value for "-h": Path "/home/cli" does not exist."""
119104
}
120-
}
105+
}

0 commit comments

Comments
 (0)