This repository has been archived by the owner on Feb 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
DetektIntegrationTest.groovy
166 lines (135 loc) · 5.74 KB
/
DetektIntegrationTest.groovy
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package com.novoda.staticanalysis.internal.detekt
import com.novoda.test.Fixtures
import com.novoda.test.TestProject
import com.novoda.test.TestProjectRule
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.Parameterized
import static com.novoda.test.LogsSubject.assertThat
@RunWith(Parameterized.class)
class DetektIntegrationTest {
private static final String DETEKT_NOT_APPLIED = 'The Detekt plugin is configured but not applied. Please apply the plugin in your build script.'
private static final String OUTPUT_NOT_DEFINED = 'Output not defined! To analyze the results, `output` needs to be defined in Detekt profile.'
@Parameterized.Parameters(name = "{0}")
static Iterable<TestProjectRule> rules() {
return [TestProjectRule.forKotlinProject(), TestProjectRule.forAndroidKotlinProject()]
}
@Rule
public final TestProjectRule projectRule
DetektIntegrationTest(TestProjectRule projectRule) {
this.projectRule = projectRule
}
@Test
void shouldFailBuildOnConfigurationWhenNoOutputNotDefined() {
def emptyConfiguration = detektWith("")
def result = createProjectWithZeroThreshold(Fixtures.Detekt.SOURCES_WITH_WARNINGS)
.withToolsConfig(emptyConfiguration)
.buildAndFail('check')
assertThat(result.logs).contains(OUTPUT_NOT_DEFINED)
}
@Test
void shouldFailBuildOnConfigurationWhenDetektConfiguredButNotApplied() {
def result = projectRule.newProject()
.withToolsConfig(detektConfiguration(Fixtures.Detekt.SOURCES_WITH_ERRORS))
.buildAndFail('check')
assertThat(result.logs).contains(DETEKT_NOT_APPLIED)
}
@Test
void shouldFailBuildWhenDetektWarningsOverTheThreshold() {
def result = createProjectWithZeroThreshold(Fixtures.Detekt.SOURCES_WITH_WARNINGS)
.buildAndFail('check')
assertThat(result.logs).containsLimitExceeded(0, 1)
assertThat(result.logs).containsDetektViolations(0, 1,
result.buildFileUrl('reports/detekt-report.html'))
}
@Test
void shouldFailBuildWhenDetektErrorsOverTheThreshold() {
def result = createProjectWithZeroThreshold(Fixtures.Detekt.SOURCES_WITH_ERRORS)
.buildAndFail('check')
assertThat(result.logs).containsLimitExceeded(1, 0)
assertThat(result.logs).containsDetektViolations(1, 0,
result.buildFileUrl('reports/detekt-report.html'))
}
@Test
void shouldNotFailWhenDetektIsNotConfigured() throws Exception {
def result = createProjectWithoutDetekt()
.build('check')
assertThat(result.logs).doesNotContainDetektViolations()
}
@Test
void shouldNotFailWhenWarningsAreWithinThreshold() throws Exception {
def result = createProjectWith(Fixtures.Detekt.SOURCES_WITH_WARNINGS, 1, 0)
.build('check')
assertThat(result.logs).containsDetektViolations(0, 1,
result.buildFileUrl('reports/detekt-report.html'))
}
@Test
void shouldNotFailWhenErrorsAreWithinThreshold() throws Exception {
def result = createProjectWith(Fixtures.Detekt.SOURCES_WITH_ERRORS, 0, 1)
.build('check')
assertThat(result.logs).containsDetektViolations(1, 0,
result.buildFileUrl('reports/detekt-report.html'))
}
@Test
void shouldNotFailBuildWhenNoDetektWarningsOrErrorsEncounteredAndNoThresholdTrespassed() {
def testProject = projectRule.newProject()
.withPlugin("io.gitlab.arturbosch.detekt", "1.0.0.RC6-2")
.withPenalty('''{
maxWarnings = 0
maxErrors = 0
}''')
.withToolsConfig(detektConfigurationWithoutInput())
TestProject.Result result = testProject
.build('check')
assertThat(result.logs).doesNotContainLimitExceeded()
assertThat(result.logs).doesNotContainDetektViolations()
}
private TestProject createProjectWithZeroThreshold(File sources) {
createProjectWith(sources)
}
private TestProject createProjectWith(File sources, int maxWarnings = 0, int maxErrors = 0) {
projectRule.newProject()
.withPlugin("io.gitlab.arturbosch.detekt", "1.0.0.RC6-2")
.withSourceSet('main', sources)
.withPenalty("""{
maxWarnings = ${maxWarnings}
maxErrors = ${maxErrors}
}""")
.withToolsConfig(detektConfiguration(sources))
}
private TestProject createProjectWithoutDetekt() {
projectRule.newProject()
.withPlugin("io.gitlab.arturbosch.detekt", "1.0.0.RC6-2")
.withSourceSet('main', Fixtures.Detekt.SOURCES_WITH_WARNINGS)
.withPenalty('''{
maxWarnings = 0
maxErrors = 0
}''')
}
private static String detektConfiguration(File input) {
detektWith """
config = '${Fixtures.Detekt.RULES}'
output = "\$buildDir/reports"
// The input just needs to be configured for the tests.
// Probably detekt doesn't pick up the changed source sets.
// In a example project it was not needed.
input = "${input}"
"""
}
private static String detektConfigurationWithoutInput() {
detektWith """
config = '${Fixtures.Detekt.RULES}'
output = "\$buildDir/reports"
"""
}
private static String detektWith(String mainProfile) {
"""
detekt {
profile('main') {
${mainProfile.stripIndent()}
}
}
"""
}
}