-
-
Notifications
You must be signed in to change notification settings - Fork 757
/
CliArgsSpec.kt
146 lines (122 loc) · 5.85 KB
/
CliArgsSpec.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package io.gitlab.arturbosch.detekt.cli
import com.beust.jcommander.ParameterException
import io.github.detekt.test.utils.resourceAsPath
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.assertThatCode
import org.assertj.core.api.Assertions.assertThatExceptionOfType
import org.assertj.core.api.Assertions.assertThatIllegalArgumentException
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.ValueSource
import java.nio.file.Path
import java.nio.file.Paths
internal class CliArgsSpec {
private val projectPath: Path = resourceAsPath("/").parent.parent.parent.parent.toAbsolutePath()
@Nested
inner class `Parsing the input path` {
@Test
fun `the current working directory is used if parameter is not set`() {
val cli = parseArguments(emptyArray())
assertThat(cli.inputPaths).hasSize(1)
assertThat(cli.inputPaths.first()).isEqualTo(Paths.get(System.getProperty("user.dir")))
}
@Test
fun `a single value is converted to a path`() {
val cli = parseArguments(arrayOf("--input", "$projectPath"))
assertThat(cli.inputPaths).hasSize(1)
assertThat(cli.inputPaths.first().toAbsolutePath()).isEqualTo(projectPath)
}
@Test
fun `multiple input paths can be separated by comma`() {
val mainPath = projectPath.resolve("src/main").toAbsolutePath()
val testPath = projectPath.resolve("src/test").toAbsolutePath()
val cli = parseArguments(arrayOf("--input", "$mainPath,$testPath"))
assertThat(cli.inputPaths).hasSize(2)
assertThat(cli.inputPaths.map(Path::toAbsolutePath)).containsExactlyInAnyOrder(mainPath, testPath)
}
@Test
fun `reports an error if the input path does not exist`() {
val pathToNonExistentDirectory = projectPath.resolve("nonExistent")
val params = arrayOf("--input", "$pathToNonExistentDirectory")
assertThatExceptionOfType(ParameterException::class.java)
.isThrownBy { parseArguments(params).inputPaths }
.withMessageContaining("does not exist")
}
}
@Nested
inner class `parsing config parameters` {
@Test
fun `should fail on invalid config value`() {
assertThatIllegalArgumentException()
.isThrownBy { parseArguments(arrayOf("--config", ",")).toSpec() }
assertThatExceptionOfType(ParameterException::class.java)
.isThrownBy { parseArguments(arrayOf("--config", "sfsjfsdkfsd")).toSpec() }
assertThatExceptionOfType(ParameterException::class.java)
.isThrownBy { parseArguments(arrayOf("--config", "./i.do.not.exist.yml")).toSpec() }
}
}
@Nested
inner class `Valid combination of options` {
@Nested
inner class `Baseline feature` {
@Test
fun `reports an error when using --create-baseline without a --baseline file`() {
assertThatCode { parseArguments(arrayOf("--create-baseline")) }
.isInstanceOf(HandledArgumentViolation::class.java)
.hasMessageContaining("Creating a baseline.xml requires the --baseline parameter to specify a path")
}
@Test
fun `reports an error when using --baseline file does not exist`() {
val nonExistingDirectory = projectPath.resolve("nonExistent").toString()
assertThatCode { parseArguments(arrayOf("--baseline", nonExistingDirectory)) }
.isInstanceOf(HandledArgumentViolation::class.java)
.hasMessageContaining("The file specified by --baseline should exist '$nonExistingDirectory'.")
}
@Test
fun `reports an error when using --baseline file which is not a file`() {
val directory = resourceAsPath("/cases").toString()
assertThatCode { parseArguments(arrayOf("--baseline", directory)) }
.isInstanceOf(HandledArgumentViolation::class.java)
.hasMessageContaining("The path specified by --baseline should be a file '$directory'.")
}
}
@Test
fun `throws HelpRequest on --help`() {
assertThatExceptionOfType(HelpRequest::class.java)
.isThrownBy { parseArguments(arrayOf("--help")) }
}
@Test
fun `throws HandledArgumentViolation on wrong options`() {
assertThatExceptionOfType(HandledArgumentViolation::class.java)
.isThrownBy { parseArguments(arrayOf("--unknown-to-us-all")) }
}
}
@Nested
inner class `--all-rules and --fail-fast lead to all rules being activated` {
@ParameterizedTest
@ValueSource(strings = ["--all-rules", "--fail-fast"])
fun `all rules active`(flag: String) {
val spec = parseArguments(arrayOf(flag)).toSpec()
assertThat(spec.rulesSpec.activateAllRules).isTrue()
}
}
@Nested
inner class `type resolution parameters are accepted` {
@Test
fun `--jvm-target is accepted`() {
val spec = parseArguments(arrayOf("--jvm-target", "11")).toSpec()
assertThat(spec.compilerSpec.jvmTarget).isEqualTo("11")
}
@Test
fun `--jvm-target with decimal is accepted`() {
val spec = parseArguments(arrayOf("--jvm-target", "1.8")).toSpec()
assertThat(spec.compilerSpec.jvmTarget).isEqualTo("1.8")
}
@Test
fun `--language-version is accepted`() {
val spec = parseArguments(arrayOf("--language-version", "1.6")).toSpec()
assertThat(spec.compilerSpec.languageVersion).isEqualTo("1.6")
}
}
}