/
CompilationOutputsFixture.groovy
164 lines (139 loc) · 5.78 KB
/
CompilationOutputsFixture.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
/*
* Copyright 2013 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.integtests.fixtures
import groovy.io.FileType
import org.codehaus.groovy.control.CompilerConfiguration
import org.gradle.internal.FileUtils
import org.gradle.util.internal.TextUtil
import java.util.function.Predicate
import static org.apache.commons.io.FilenameUtils.removeExtension
import static org.spockframework.util.CollectionUtil.asSet
class CompilationOutputsFixture {
private final File targetDir
private final List<String> includeExtensions
private final String sourceSet
private final String lang
CompilationOutputsFixture(File targetDir) {
this(targetDir, [])
}
/**
* Tracks outputs in given target dir considering only the files by the given extensions (ignoring case)
*/
CompilationOutputsFixture(File targetDir, List<String> includeExtensions, String sourceSet = "main", String lang = "java") {
assert targetDir != null
this.targetDir = targetDir
this.sourceSet = sourceSet
this.lang = lang
this.includeExtensions = includeExtensions
}
private List<File> snapshot = []
// Executes optional operation and makes a snapshot of outputs (sets the last modified timestamp to zero for all files)
public <T> T snapshot(Closure<T> operation = null) {
T result = operation?.call()
snapshot.clear()
targetDir.eachFileRecurse(FileType.FILES) {
if (isIncluded(it)) {
it.lastModified = 0
snapshot << it
}
}
result
}
private boolean isIncluded(File file) {
includeExtensions.empty || includeExtensions.any {
FileUtils.hasExtensionIgnoresCase(file.name, it)
}
}
//asserts none of the files changed/added since last snapshot
void noneRecompiled() {
recompiledFiles([])
}
//asserts file changed/added since last snapshot
void recompiledFile(File file) {
recompiledFiles([file])
}
//asserts files changed/added since last snapshot
void recompiledFiles(File... files) {
recompiledFiles(files as List)
}
//asserts files changed/added since last snapshot
void recompiledFiles(Collection<File> files) {
def expectedNames = files.collect({ removeExtension(it.name) }) as Set
assert changedFileNames == expectedNames
}
//asserts has the exact set of output files
void hasFiles(File... files) {
def expectedNames = files.collect({ removeExtension(it.name) }) as Set
assert getFiles { true } == expectedNames
}
//asserts file changed/added since last snapshot
void recompiledFile(String fileName) {
recompiledFiles(fileName)
}
//asserts files changed/added since last snapshot
void recompiledFiles(String... fileNames) {
def expectedNames = fileNames.collect({ removeExtension(it) }) as Set
assert changedFileNames == expectedNames
}
//asserts classes changed/added since last snapshot. Class means file name without extension.
void recompiledClasses(String... classNames) {
assert changedFileNames == asSet(classNames)
}
void recompiledFqn(String... classNames) {
assert getChangedFileNames(true) == asSet(classNames)
}
//asserts files deleted since last snapshot.
void deletedFiles(String... fileNames) {
def expectedNames = fileNames.collect({ removeExtension(it) }) as Set
def deleted = snapshot.findAll { !it.exists() }.collect { removeExtension(it.name) } as Set
assert deleted == expectedNames
}
//asserts classes deleted since last snapshot. Class means file name without extension.
void deletedClasses(String... classNames) {
def deleted = snapshot.findAll { !it.exists() }.collect { removeExtension(it.name) } as Set
assert deleted == asSet(classNames)
}
private Set<String> getChangedFileNames(boolean qualified = false) {
// Get all of the files that do not have a zero last modified timestamp
return getFiles(qualified) { it.lastModified() > 0 }
}
private Set<String> getFiles(boolean qualified = false, Predicate<File> criteria) {
// Get all of the files that do not have a zero last modified timestamp
def changed = new HashSet()
def dir = qualified ? new File(new File(targetDir, lang), sourceSet) : targetDir
dir.eachFileRecurse(FileType.FILES) {
if (isIncluded(it)) {
if (criteria.test(it)) {
if (qualified) {
String relative = dir.toPath().relativize(it.toPath()).toString()
changed << TextUtil.normaliseFileSeparators(removeExtension(relative)).replace('/', '.')
} else {
changed << removeExtension(it.name)
}
}
}
}
changed
}
Object evaluate(@GroovyBuildScriptLanguage String script) {
CompilerConfiguration config = new CompilerConfiguration()
config.classpath.add("${targetDir}/$lang/$sourceSet".toString())
new GroovyShell(config).evaluate(script)
}
void execute(@GroovyBuildScriptLanguage String script) {
evaluate(script)
}
}