-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
CheckstylePluginMultiProjectTest.groovy
150 lines (126 loc) · 5.47 KB
/
CheckstylePluginMultiProjectTest.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
/*
* Copyright 2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.gradle.api.plugins.quality.checkstyle
import org.gradle.integtests.fixtures.AbstractIntegrationSpec
import org.gradle.test.fixtures.file.TestFile
import org.hamcrest.Matcher
import static org.gradle.util.Matchers.containsLine
import static org.gradle.util.internal.TextUtil.getPlatformLineSeparator
import static org.hamcrest.CoreMatchers.containsString
class CheckstylePluginMultiProjectTest extends AbstractIntegrationSpec {
def "configures checkstyle extension to read config from root project in a single project build"() {
given:
buildFile << javaProjectUsingCheckstyle()
file('src/main/java/Dummy.java') << javaClassWithNewLineAtEnd()
file('config/checkstyle/checkstyle.xml') << simpleCheckStyleConfig()
expect:
succeeds('checkstyleMain')
checkStyleReportFile(testDirectory).text.contains('Dummy.java')
}
def "fails when root project does contain config in default location"() {
given:
settingsFile << "include 'child'"
file('child/build.gradle') << javaProjectUsingCheckstyle()
file('child/src/main/java/Dummy.java') << javaClassWithNewLineAtEnd()
file('child/config/checkstyle/checkstyle.xml') << simpleCheckStyleConfig()
expect:
fails(':child:checkstyleMain')
checkStyleReportFile(file('child')).assertDoesNotExist()
}
def "configures checkstyle extension to read config from root project in a flat multi-project build"() {
given:
settingsFile << "include 'child:grand'"
file('child/grand/build.gradle') << javaProjectUsingCheckstyle()
file('child/grand/src/main/java/Dummy.java') << javaClassWithNewLineAtEnd()
file('config/checkstyle/checkstyle.xml') << simpleCheckStyleConfig()
expect:
succeeds(':child:grand:checkstyleMain')
checkStyleReportFile(file('child/grand')).text.contains('Dummy.java')
}
def "configures checkstyle extension to read config from root project in a deeply nested multi-project build"() {
given:
settingsFile << "include 'a:b:c'"
file('a/b/c/build.gradle') << javaProjectUsingCheckstyle()
file('a/b/c/src/main/java/Dummy.java') << javaClassWithNewLineAtEnd()
file('config/checkstyle/checkstyle.xml') << simpleCheckStyleConfig()
expect:
succeeds(':a:b:c:checkstyleMain')
checkStyleReportFile(file('a/b/c')).text.contains('Dummy.java')
}
def "configures checkstyle extension to read config from root project in a multi-project build even if sub project config is available"() {
given:
settingsFile << "include 'child:grand'"
file('child/grand/build.gradle') << javaProjectUsingCheckstyle()
file('child/grand/src/main/java/Dummy.java') << javaClassWithNewLineAtEnd()
file('child/grand/config/checkstyle/checkstyle.xml') << invalidCheckStyleConfig()
file('config/checkstyle/checkstyle.xml') << simpleCheckStyleConfig()
expect:
succeeds(':child:grand:checkstyleMain')
checkStyleReportFile(file('child/grand')).text.contains('Dummy.java')
}
def "explicitly configures checkstyle extension to point to config directory"() {
given:
settingsFile << "include 'child'"
file('child/build.gradle') << javaProjectUsingCheckstyle()
file('child/build.gradle') << """
checkstyle {
configDirectory = file('config/checkstyle')
}
"""
file('child/src/main/java/Dummy.java') << javaClassWithNewLineAtEnd()
file('child/config/checkstyle/checkstyle.xml') << simpleCheckStyleConfig()
expect:
succeeds(':child:checkstyleMain')
checkStyleReportFile(file('child')).text.contains('Dummy.java')
}
static String simpleCheckStyleConfig() {
"""
<!DOCTYPE module PUBLIC
"-//Puppy Crawl//DTD Check Configuration 1.2//EN"
"http://www.puppycrawl.com/dtds/configuration_1_2.dtd">
<module name="Checker">
<module name="NewlineAtEndOfFile"/>
</module>
"""
}
static String invalidCheckStyleConfig() {
'INVALID AND SHOULD NEVER BE READ'
}
static TestFile checkStyleReportFile(File projectDir) {
new TestFile(projectDir, 'build/reports/checkstyle/main.html')
}
static String javaProjectUsingCheckstyle() {
"""
apply plugin: 'java'
apply plugin: 'checkstyle'
${mavenCentralRepository()}
"""
}
static String javaClassWithNewLineAtEnd() {
"public class Dummy {}${getPlatformLineSeparator()}"
}
static String multilineJavaClass() {
return """
package org.sample;
class MyClass {
int i = 0;
}
"""
}
static Matcher<String> containsClass(String className) {
containsLine(containsString(className.replace(".", File.separator)))
}
}