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
/
TestProject.groovy
172 lines (139 loc) · 4.7 KB
/
TestProject.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
167
168
169
170
171
172
package com.novoda.test
import org.gradle.testkit.runner.BuildResult
import org.gradle.testkit.runner.GradleRunner
import org.gradle.testkit.runner.TaskOutcome
import javax.annotation.Nullable
abstract class TestProject<T extends TestProject> {
private static final Closure<String> EXTENSION_TEMPLATE = { TestProject project ->
"""
staticAnalysis {
${(project.penalty ?: '').replace(' ', '')}
${(project.toolsConfig ?: '').replace(' ', ' ')}
}
${project.additionalConfiguration}
"""
}
private final File projectDir
private final GradleRunner gradleRunner
private final Closure<String> template
String additionalConfiguration = ''
Map<String, List<File>> sourceSets = [main: []]
List<String> plugins = []
String penalty
String toolsConfig = ''
TestProject(Closure<String> template) {
this.template = template
this.projectDir = createProjectDir("${System.currentTimeMillis()}")
this.gradleRunner = GradleRunner.create()
.withProjectDir(projectDir)
.withPluginClasspath()
.forwardStdOutput(new OutputStreamWriter(System.out))
.forwardStdError(new OutputStreamWriter(System.out))
}
private static File createProjectDir(String path) {
File dir = new File(Fixtures.BUILD_DIR, "test-projects/$path")
dir.deleteDir()
dir.mkdirs()
return dir
}
List<String> defaultArguments() {
Collections.emptyList()
}
public T withFile(File source, String path) {
write(source.text, path)
return this
}
public T withFile(String text, String path) {
write(text, path)
return this
}
private void write(String text, String path) {
File file = new File(gradleRunner.projectDir, path)
file.parentFile.mkdirs()
file.text = text
}
public T copyIntoSourceSet(String sourceSet, File srcDir) {
srcDir.listFiles().each {
withFile(it, "src/${sourceSet}/java/${it.name}")
}
return this
}
public T withSourceSet(String sourceSet, File... srcDirs) {
sourceSets[sourceSet] = srcDirs
return this
}
public T withPenalty(String penalty) {
this.penalty = "penalty $penalty"
return this
}
public T withToolsConfig(String toolsConfig) {
this.toolsConfig = toolsConfig
return this
}
public T withAdditionalConfiguration(String additionalConfiguration) {
this.additionalConfiguration = additionalConfiguration
return this
}
public T withPlugin(String plugin, String version = null) {
this.plugins.add("id '$plugin' ${version ? "version '$version'" : ""}")
return this
}
public Result build(String... arguments) {
BuildResult buildResult = newRunner(arguments).build()
createResult(buildResult)
}
private GradleRunner newRunner(String... arguments) {
new File(projectDir, 'build.gradle').text = template.call(this)
List<String> defaultArgs = defaultArguments()
List<String> args = new ArrayList<>(arguments.size() + defaultArgs.size())
args.addAll(defaultArgs)
args.addAll(arguments)
gradleRunner.withArguments(args)
}
private createResult(BuildResult buildResult) {
new Result(buildResult, new File(projectDir, 'build'))
}
public Result buildAndFail(String... arguments) {
BuildResult buildResult = newRunner(arguments).buildAndFail()
createResult(buildResult)
}
public void deleteDir() {
projectDir.deleteDir()
}
String projectDir() {
return projectDir
}
protected static String formatExtension(TestProject project) {
EXTENSION_TEMPLATE.call(project)
}
protected static String formatPlugins(TestProject project) {
project.plugins.join('\n')
}
public static class Result {
private final BuildResult buildResult
private final File buildDir
Result(BuildResult buildResult, File buildDir) {
this.buildResult = buildResult
this.buildDir = buildDir
}
Logs getLogs() {
new Logs(buildResult.output)
}
String buildFileUrl(String path) {
new File(buildDir, path).path
}
List<String> getTasksPaths() {
buildResult.tasks.collect { it.path }
}
@Nullable
TaskOutcome outcome(String taskPath) {
buildResult.task(taskPath).outcome
}
public static class Logs {
final String output
Logs(String output) {
this.output = output
}
}
}
}