@@ -2,25 +2,27 @@ package com.github.ajalt.clikt.parameters.types
2
2
3
3
import com.github.ajalt.clikt.core.BadParameterValue
4
4
import com.github.ajalt.clikt.core.CliktCommand
5
+ import com.github.ajalt.clikt.core.NoRunCliktCommand
5
6
import com.github.ajalt.clikt.parameters.arguments.argument
6
7
import com.github.ajalt.clikt.parameters.arguments.multiple
7
8
import com.github.ajalt.clikt.parameters.options.option
8
9
import com.github.ajalt.clikt.parameters.options.required
9
10
import com.github.ajalt.clikt.testing.splitArgv
10
11
import com.google.common.jimfs.Configuration
11
12
import com.google.common.jimfs.Jimfs
12
- import io.kotlintest.fail
13
13
import io.kotlintest.shouldBe
14
- import java.nio.file.Files
15
- import java.nio.file.FileSystem
14
+ import io.kotlintest.shouldThrow
16
15
import org.junit.Test
16
+ import java.nio.file.FileSystem
17
+ import java.nio.file.Files
17
18
19
+ @Suppress(" unused" )
18
20
class PathTest {
19
21
val fs: FileSystem = Jimfs .newFileSystem(Configuration .unix())
20
22
21
23
@Test
22
24
fun `paths are resolved using the provided filesystem, if any` () {
23
- val command = object : CliktCommand () {
25
+ class C : CliktCommand () {
24
26
val path by option(" -p" )
25
27
.path(fileSystem = fs)
26
28
.required()
@@ -30,12 +32,12 @@ class PathTest {
30
32
}
31
33
}
32
34
33
- command .parse(splitArgv(" -p/var/log/foo" ))
35
+ C () .parse(splitArgv(" -p/var/log/foo" ))
34
36
}
35
37
36
38
@Test
37
39
fun `options can be paths` () {
38
- val command = object : CliktCommand () {
40
+ class C : CliktCommand () {
39
41
val path by option(" -p" )
40
42
.path()
41
43
.required()
@@ -45,12 +47,12 @@ class PathTest {
45
47
}
46
48
}
47
49
48
- command .parse(splitArgv(" -pfoo" ))
50
+ C () .parse(splitArgv(" -pfoo" ))
49
51
}
50
52
51
53
@Test
52
54
fun `arguments can be paths` () {
53
- val command = object : CliktCommand () {
55
+ class C : CliktCommand () {
54
56
val paths by argument()
55
57
.path()
56
58
.multiple()
@@ -60,61 +62,44 @@ class PathTest {
60
62
}
61
63
}
62
64
63
- command .parse(splitArgv(" foo bar baz" ))
65
+ C () .parse(splitArgv(" foo bar baz" ))
64
66
}
65
67
66
68
@Test
67
69
fun `fileOkay = false will reject files` () {
68
- val command = object : CliktCommand () {
70
+ class C : NoRunCliktCommand () {
69
71
val folderOnly by option(" -f" ).path(fileOkay = false , fileSystem = fs)
70
-
71
- override fun run () {
72
- }
73
72
}
74
73
75
74
Files .createDirectory(fs.getPath(" /var" ))
76
75
Files .createFile(fs.getPath(" /var/foo" ))
77
76
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. """
81
80
}
82
81
83
82
@Test
84
83
fun `folderOkay = false will reject folders` () {
85
- val command = object : CliktCommand () {
84
+ class C : NoRunCliktCommand () {
86
85
val fileOnly by option(" -f" ).path(folderOkay = false , fileSystem = fs)
87
-
88
- override fun run () {
89
- }
90
86
}
91
87
92
88
Files .createDirectories(fs.getPath(" /var/foo" ))
93
89
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. """
97
93
}
98
94
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 () {
101
98
val homeDir by option(" -h" ).path(exists = true , fileSystem = fs)
102
-
103
- override fun run () {
104
- }
105
99
}
106
100
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."""
119
104
}
120
- }
105
+ }
0 commit comments