Skip to content

Commit 809e934

Browse files
committed
Remove deprecation warnings to prepare for Gradle 5 (sourceSets.main.output.classesDirs) (#30389)
* Remove deprecation warnings to prepare for Gradle 5 Gradle replaced `project.sourceSets.main.output.classesDir` of type `File` with `project.sourceSets.main.output.classesDirs` of type `FileCollection` (see [SourceSetOutput](https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/java/org/gradle/api/tasks/SourceSetOutput.java)) Build output is now stored on a per language folder. There are a few places where we use that, here's these and how it's fixed: - Randomized Test execution - look in all test folders ( pass the multi dir configuration to the ant runner ) - DRY the task configuration by introducing `basedOn` for `RandomizedTestingTask` DSL - Extend the naming convention test to support passing in multiple directories - Fix the standalon test plugin, the dires were not passed trough, checked with a debuger and the statement had no affect due to a missing `=`. Closes #30354 * Only check Java tests, PR feedback - Name checker was ran for Groovy tests that don't adhere to the same convections causing the check to fail - implement PR feedback * Replace `add` with `addAll` This worked because the list is passed to `project.files` that does the right thing. * Revert "Only check Java tests, PR feedback" This reverts commit 9bd9389. * Remove `basedOn` helper * Bring some changes back Previus revert accidentally reverted too much * Fix negation * add back public * revert name check changes * Revert "revert name check changes" This reverts commit a2800c0. * Pass all dirs to name check Only run on Java for build-tools, this is safe because it's a self test. It needs more work before we could pass in the Groovy classes as well as these inherit from `GroovyTestCase` * remove self tests from name check The self complicates the task setup and disable real checks on build-tools. With this change there are no more self tests, and the build-tools tests adhere to the conventions. The self test will be replaced by gradle test kit, thus the addition of the Gradle plugin builder plugin. * First test to run a Gradle build * Add tests that replace the name check self test * Clean up integ test base class * Always run tests * Align with test naming conventions * Make integ. test case inherit from unit test case The check requires this * Remove `import static org.junit.Assert.*`
1 parent 4020e4e commit 809e934

32 files changed

+636
-314
lines changed

buildSrc/build.gradle

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@
1717
* under the License.
1818
*/
1919

20-
import java.nio.file.Files
2120

22-
import org.gradle.util.GradleVersion
21+
import java.nio.file.Files
2322

24-
apply plugin: 'groovy'
23+
plugins {
24+
id 'java-gradle-plugin'
25+
id 'groovy'
26+
}
2527

2628
group = 'org.elasticsearch.gradle'
2729

@@ -83,9 +85,10 @@ repositories {
8385
}
8486

8587
dependencies {
86-
compile gradleApi()
8788
compile localGroovy()
8889
compile "com.carrotsearch.randomizedtesting:junit4-ant:${props.getProperty('randomizedrunner')}"
90+
compile "com.carrotsearch.randomizedtesting:randomizedtesting-runner:${props.getProperty('randomizedrunner')}"
91+
8992
compile("junit:junit:${props.getProperty('junit')}") {
9093
transitive = false
9194
}
@@ -97,8 +100,10 @@ dependencies {
97100
compile 'de.thetaphi:forbiddenapis:2.5'
98101
compile 'org.apache.rat:apache-rat:0.11'
99102
compile "org.elasticsearch:jna:4.5.1"
103+
testCompile "junit:junit:${props.getProperty('junit')}"
100104
}
101105

106+
102107
// Gradle 2.14+ removed ProgressLogger(-Factory) classes from the public APIs
103108
// Use logging dependency instead
104109
// Gradle 4.3.1 stopped releasing the logging jars to jcenter, just use the last available one
@@ -113,14 +118,12 @@ dependencies {
113118
*****************************************************************************/
114119
// this will only happen when buildSrc is built on its own during build init
115120
if (project == rootProject) {
116-
117121
repositories {
118122
if (System.getProperty("repos.mavenLocal") != null) {
119123
mavenLocal()
120124
}
121125
mavenCentral()
122126
}
123-
test.exclude 'org/elasticsearch/test/NamingConventionsCheckBadClasses*'
124127
}
125128

126129
/*****************************************************************************
@@ -145,9 +148,6 @@ if (project != rootProject) {
145148
jarHell.enabled = false
146149
thirdPartyAudit.enabled = false
147150

148-
// test for elasticsearch.build tries to run with ES...
149-
test.enabled = false
150-
151151
// TODO: re-enable once randomizedtesting gradle code is published and removed from here
152152
licenseHeaders.enabled = false
153153

@@ -158,14 +158,7 @@ if (project != rootProject) {
158158
}
159159

160160
namingConventions {
161-
testClass = 'org.elasticsearch.test.NamingConventionsCheckBadClasses$UnitTestCase'
162-
integTestClass = 'org.elasticsearch.test.NamingConventionsCheckBadClasses$IntegTestCase'
163-
}
164-
165-
task namingConventionsMain(type: org.elasticsearch.gradle.precommit.NamingConventionsTask) {
166-
checkForTestsInMain = true
167-
testClass = namingConventions.testClass
168-
integTestClass = namingConventions.integTestClass
161+
testClass = 'org.elasticsearch.gradle.test.GradleUnitTestCase'
162+
integTestClass = 'org.elasticsearch.gradle.test.GradleIntegrationTestCase'
169163
}
170-
precommit.dependsOn namingConventionsMain
171164
}

buildSrc/src/main/groovy/com/carrotsearch/gradle/junit4/RandomizedTestingPlugin.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class RandomizedTestingPlugin implements Plugin<Project> {
8989
description = 'Runs unit tests with the randomized testing framework'
9090
dependsOn oldTestTask.dependsOn, 'testClasses'
9191
classpath = oldTestTask.classpath
92-
testClassesDir = oldTestTask.project.sourceSets.test.output.classesDir
92+
testClassesDirs = oldTestTask.project.sourceSets.test.output.classesDirs
9393
}
9494

9595
// hack so check task depends on custom test

buildSrc/src/main/groovy/com/carrotsearch/gradle/junit4/RandomizedTestingTask.groovy

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,20 @@ import groovy.xml.NamespaceBuilder
66
import groovy.xml.NamespaceBuilderSupport
77
import org.apache.tools.ant.BuildException
88
import org.apache.tools.ant.DefaultLogger
9+
import org.apache.tools.ant.Project
910
import org.apache.tools.ant.RuntimeConfigurable
1011
import org.apache.tools.ant.UnknownElement
12+
import org.elasticsearch.gradle.BuildPlugin
1113
import org.gradle.api.DefaultTask
1214
import org.gradle.api.InvalidUserDataException
1315
import org.gradle.api.file.FileCollection
1416
import org.gradle.api.file.FileTreeElement
15-
import org.gradle.api.internal.tasks.options.Option
1617
import org.gradle.api.specs.Spec
1718
import org.gradle.api.tasks.Input
1819
import org.gradle.api.tasks.InputDirectory
1920
import org.gradle.api.tasks.Optional
2021
import org.gradle.api.tasks.TaskAction
22+
import org.gradle.api.tasks.options.Option
2123
import org.gradle.api.tasks.util.PatternFilterable
2224
import org.gradle.api.tasks.util.PatternSet
2325
import org.gradle.internal.logging.progress.ProgressLoggerFactory
@@ -43,8 +45,8 @@ class RandomizedTestingTask extends DefaultTask {
4345
@Input
4446
String parallelism = '1'
4547

46-
@InputDirectory
47-
File testClassesDir
48+
@Input
49+
FileCollection testClassesDirs
4850

4951
@Optional
5052
@Input
@@ -220,15 +222,15 @@ class RandomizedTestingTask extends DefaultTask {
220222
listener = new DefaultLogger(
221223
errorPrintStream: System.err,
222224
outputPrintStream: System.out,
223-
messageOutputLevel: org.apache.tools.ant.Project.MSG_INFO)
225+
messageOutputLevel: Project.MSG_INFO)
224226
} else {
225227
// we want to buffer the info, and emit it if the test fails
226228
antLoggingBuffer = new ByteArrayOutputStream()
227229
PrintStream stream = new PrintStream(antLoggingBuffer, true, "UTF-8")
228230
listener = new DefaultLogger(
229231
errorPrintStream: stream,
230232
outputPrintStream: stream,
231-
messageOutputLevel: org.apache.tools.ant.Project.MSG_INFO)
233+
messageOutputLevel: Project.MSG_INFO)
232234
}
233235
project.ant.project.addBuildListener(listener)
234236
}
@@ -251,12 +253,10 @@ class RandomizedTestingTask extends DefaultTask {
251253
if (argLine != null) {
252254
jvmarg(line: argLine)
253255
}
254-
fileset(dir: testClassesDir) {
255-
for (String includePattern : patternSet.getIncludes()) {
256-
include(name: includePattern)
257-
}
258-
for (String excludePattern : patternSet.getExcludes()) {
259-
exclude(name: excludePattern)
256+
testClassesDirs.each { testClassDir ->
257+
fileset(dir: testClassDir) {
258+
patternSet.getIncludes().each { include(name: it) }
259+
patternSet.getExcludes().each { exclude(name: it) }
260260
}
261261
}
262262
for (Map.Entry<String, Object> prop : systemProperties) {

buildSrc/src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,7 @@ class BuildPlugin implements Plugin<Project> {
751751
project.extensions.add('additionalTest', { String name, Closure config ->
752752
RandomizedTestingTask additionalTest = project.tasks.create(name, RandomizedTestingTask.class)
753753
additionalTest.classpath = test.classpath
754-
additionalTest.testClassesDir = test.testClassesDir
754+
additionalTest.testClassesDirs = test.testClassesDirs
755755
additionalTest.configure(commonTestConfig(project))
756756
additionalTest.configure(config)
757757
test.dependsOn(additionalTest)

buildSrc/src/main/groovy/org/elasticsearch/gradle/LoggedExec.groovy

Lines changed: 0 additions & 45 deletions
This file was deleted.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.elasticsearch.gradle;
2+
3+
import groovy.lang.Closure;
4+
import org.gradle.api.GradleException;
5+
import org.gradle.api.Task;
6+
import org.gradle.api.tasks.Exec;
7+
8+
import java.io.ByteArrayOutputStream;
9+
import java.io.IOException;
10+
import java.util.stream.Collectors;
11+
12+
/**
13+
* A wrapper around gradle's Exec task to capture output and log on error.
14+
*/
15+
public class LoggedExec extends Exec {
16+
17+
protected ByteArrayOutputStream output = new ByteArrayOutputStream();
18+
19+
public LoggedExec() {
20+
if (getLogger().isInfoEnabled() == false) {
21+
setStandardOutput(output);
22+
setErrorOutput(output);
23+
setIgnoreExitValue(true);
24+
doLast(new Closure<Void>(this, this) {
25+
public void doCall(Task it) throws IOException {
26+
if (getExecResult().getExitValue() != 0) {
27+
for (String line : output.toString("UTF-8").split("\\R")) {
28+
getLogger().error(line);
29+
}
30+
throw new GradleException(
31+
"Process \'" + getExecutable() + " " +
32+
getArgs().stream().collect(Collectors.joining(" "))+
33+
"\' finished with non-zero exit value " +
34+
String.valueOf(getExecResult().getExitValue())
35+
);
36+
}
37+
}
38+
});
39+
}
40+
}
41+
}

buildSrc/src/main/groovy/org/elasticsearch/gradle/VersionProperties.groovy

Lines changed: 0 additions & 41 deletions
This file was deleted.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package org.elasticsearch.gradle;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
import java.util.Properties;
8+
9+
/**
10+
* Accessor for shared dependency versions used by elasticsearch, namely the elasticsearch and lucene versions.
11+
*/
12+
public class VersionProperties {
13+
public static Version getElasticsearch() {
14+
return elasticsearch;
15+
}
16+
17+
public static String getLucene() {
18+
return lucene;
19+
}
20+
21+
public static Map<String, String> getVersions() {
22+
return versions;
23+
}
24+
25+
private static final Version elasticsearch;
26+
private static final String lucene;
27+
private static final Map<String, String> versions = new HashMap<String, String>();
28+
static {
29+
Properties props = getVersionProperties();
30+
elasticsearch = Version.fromString(props.getProperty("elasticsearch"));
31+
lucene = props.getProperty("lucene");
32+
for (String property : props.stringPropertyNames()) {
33+
versions.put(property, props.getProperty(property));
34+
}
35+
}
36+
37+
private static Properties getVersionProperties() {
38+
Properties props = new Properties();
39+
InputStream propsStream = VersionProperties.class.getResourceAsStream("/version.properties");
40+
if (propsStream == null) {
41+
throw new RuntimeException("/version.properties resource missing");
42+
}
43+
try {
44+
props.load(propsStream);
45+
} catch (IOException e) {
46+
throw new RuntimeException(e);
47+
}
48+
return props;
49+
}
50+
}

buildSrc/src/main/groovy/org/elasticsearch/gradle/precommit/LoggerUsageTask.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ public class LoggerUsageTask extends LoggedExec {
5050
List files = []
5151
// But only if the source sets that will make them exist
5252
if (project.sourceSets.findByName("main")) {
53-
files.add(project.sourceSets.main.output.classesDir)
53+
files.addAll(project.sourceSets.main.output.classesDirs.getFiles())
5454
dependsOn project.tasks.classes
5555
}
5656
if (project.sourceSets.findByName("test")) {
57-
files.add(project.sourceSets.test.output.classesDir)
57+
files.addAll(project.sourceSets.test.output.classesDirs.getFiles())
5858
dependsOn project.tasks.testClasses
5959
}
6060
/* In an extra twist, it isn't good enough that the source set

0 commit comments

Comments
 (0)