Skip to content

Commit 0434cb5

Browse files
committed
Fix extractjar task ci (#33272)
Remove tasks to check license and notice and add build integration test instead. Closes #33201
1 parent 30ce264 commit 0434cb5

File tree

17 files changed

+202
-121
lines changed

17 files changed

+202
-121
lines changed

build.gradle

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,12 @@
2020
import com.github.jengelman.gradle.plugins.shadow.ShadowPlugin
2121
import org.apache.tools.ant.taskdefs.condition.Os
2222
import org.elasticsearch.gradle.BuildPlugin
23-
import org.elasticsearch.gradle.LoggedExec
2423
import org.elasticsearch.gradle.Version
2524
import org.elasticsearch.gradle.VersionCollection
2625
import org.elasticsearch.gradle.VersionProperties
2726
import org.elasticsearch.gradle.plugin.PluginBuildPlugin
2827
import org.gradle.plugins.ide.eclipse.model.SourceFolder
2928

30-
import java.nio.file.Files
31-
import java.nio.file.Path
32-
3329
plugins {
3430
id 'com.gradle.build-scan' version '1.13.2'
3531
}
@@ -580,62 +576,6 @@ wrapper {
580576
}
581577
}
582578

583-
static void assertLinesInFile(final Path path, final List<String> expectedLines) {
584-
final List<String> actualLines = Files.readAllLines(path)
585-
int line = 0
586-
for (final String expectedLine : expectedLines) {
587-
final String actualLine = actualLines.get(line)
588-
if (expectedLine != actualLine) {
589-
throw new GradleException("expected line [${line + 1}] in [${path}] to be [${expectedLine}] but was [${actualLine}]")
590-
}
591-
line++
592-
}
593-
}
594-
595-
/*
596-
* Check that all generated JARs have our NOTICE.txt and an appropriate
597-
* LICENSE.txt in them. We configurate this in gradle but we'd like to
598-
* be extra paranoid.
599-
*/
600-
subprojects { project ->
601-
project.tasks.withType(Jar).whenTaskAdded { jarTask ->
602-
final Task extract = project.task("extract${jarTask.name.capitalize()}", type: LoggedExec) {
603-
dependsOn jarTask
604-
ext.destination = project.buildDir.toPath().resolve("jar-extracted/${jarTask.name}")
605-
commandLine "${->new File(rootProject.compilerJavaHome, 'bin/jar')}",
606-
'xf', "${-> jarTask.outputs.files.singleFile}", 'META-INF/LICENSE.txt', 'META-INF/NOTICE.txt'
607-
workingDir destination
608-
onlyIf {jarTask.enabled}
609-
doFirst {
610-
project.delete(destination)
611-
Files.createDirectories(destination)
612-
}
613-
}
614-
615-
final Task checkNotice = project.task("verify${jarTask.name.capitalize()}Notice") {
616-
dependsOn extract
617-
onlyIf {jarTask.enabled}
618-
doLast {
619-
final List<String> noticeLines = Files.readAllLines(project.noticeFile.toPath())
620-
final Path noticePath = extract.destination.resolve('META-INF/NOTICE.txt')
621-
assertLinesInFile(noticePath, noticeLines)
622-
}
623-
}
624-
project.check.dependsOn checkNotice
625-
626-
final Task checkLicense = project.task("verify${jarTask.name.capitalize()}License") {
627-
dependsOn extract
628-
onlyIf {jarTask.enabled}
629-
doLast {
630-
final List<String> licenseLines = Files.readAllLines(project.licenseFile.toPath())
631-
final Path licensePath = extract.destination.resolve('META-INF/LICENSE.txt')
632-
assertLinesInFile(licensePath, licenseLines)
633-
}
634-
}
635-
project.check.dependsOn checkLicense
636-
}
637-
}
638-
639579
/* Remove assemble/dependenciesInfo on all qa projects because we don't need to publish
640580
* artifacts for them. */
641581
gradle.projectsEvaluated {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ import org.gradle.util.GradleVersion
5656
import java.nio.charset.StandardCharsets
5757
import java.time.ZoneOffset
5858
import java.time.ZonedDateTime
59+
5960
/**
6061
* Encapsulates build configuration for elasticsearch projects.
6162
*/
@@ -739,6 +740,7 @@ class BuildPlugin implements Plugin<Project> {
739740
}
740741
from(project.noticeFile.parent) {
741742
include project.noticeFile.name
743+
rename { 'NOTICE.txt' }
742744
}
743745
}
744746
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.elasticsearch.gradle;
20+
21+
import org.apache.commons.io.IOUtils;
22+
import org.elasticsearch.gradle.test.GradleIntegrationTestCase;
23+
import org.gradle.testkit.runner.BuildResult;
24+
25+
import java.io.File;
26+
import java.io.IOException;
27+
import java.io.InputStream;
28+
import java.nio.charset.StandardCharsets;
29+
import java.util.zip.ZipEntry;
30+
import java.util.zip.ZipFile;
31+
32+
public class BuildPluginIT extends GradleIntegrationTestCase {
33+
34+
public void testPluginCanBeApplied() {
35+
BuildResult result = getGradleRunner("elasticsearch.build")
36+
.withArguments("hello", "-s")
37+
.build();
38+
assertTaskSuccessful(result, ":hello");
39+
assertOutputContains("build plugin can be applied");
40+
}
41+
42+
public void testCheckTask() {
43+
BuildResult result = getGradleRunner("elasticsearch.build")
44+
.withArguments("check", "assemble", "-s", "-Dlocal.repo.path=" + getLocalTestRepoPath())
45+
.build();
46+
assertTaskSuccessful(result, ":check");
47+
}
48+
49+
public void testLicenseAndNotice() throws IOException {
50+
BuildResult result = getGradleRunner("elasticsearch.build")
51+
.withArguments("clean", "assemble", "-s", "-Dlocal.repo.path=" + getLocalTestRepoPath())
52+
.build();
53+
54+
assertTaskSuccessful(result, ":assemble");
55+
56+
assertBuildFileExists(result, "elasticsearch.build", "distributions/elasticsearch.build.jar");
57+
58+
try (ZipFile zipFile = new ZipFile(new File(
59+
getBuildDir("elasticsearch.build"), "distributions/elasticsearch.build.jar"
60+
))) {
61+
ZipEntry licenseEntry = zipFile.getEntry("META-INF/LICENSE.txt");
62+
ZipEntry noticeEntry = zipFile.getEntry("META-INF/NOTICE.txt");
63+
assertNotNull("Jar does not have META-INF/LICENSE.txt", licenseEntry);
64+
assertNotNull("Jar does not have META-INF/NOTICE.txt", noticeEntry);
65+
try (
66+
InputStream license = zipFile.getInputStream(licenseEntry);
67+
InputStream notice = zipFile.getInputStream(noticeEntry)
68+
) {
69+
assertEquals("this is a test license file", IOUtils.toString(license, StandardCharsets.UTF_8.name()));
70+
assertEquals("this is a test notice file", IOUtils.toString(notice, StandardCharsets.UTF_8.name()));
71+
}
72+
}
73+
}
74+
75+
76+
}

buildSrc/src/test/java/org/elasticsearch/gradle/precommit/NamingConventionsTaskIT.java

Lines changed: 36 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,74 +2,57 @@
22

33
import org.elasticsearch.gradle.test.GradleIntegrationTestCase;
44
import org.gradle.testkit.runner.BuildResult;
5-
import org.gradle.testkit.runner.GradleRunner;
6-
import org.gradle.testkit.runner.TaskOutcome;
75

86
import java.util.Arrays;
7+
import java.util.HashSet;
98

109
public class NamingConventionsTaskIT extends GradleIntegrationTestCase {
1110

12-
public void testPluginCanBeApplied() {
13-
BuildResult result = GradleRunner.create()
14-
.withProjectDir(getProjectDir("namingConventionsSelfTest"))
15-
.withArguments("hello", "-s", "-PcheckForTestsInMain=false")
16-
.withPluginClasspath()
17-
.build();
18-
19-
assertEquals(TaskOutcome.SUCCESS, result.task(":hello").getOutcome());
20-
String output = result.getOutput();
21-
assertTrue(output, output.contains("build plugin can be applied"));
22-
}
23-
2411
public void testNameCheckFailsAsItShould() {
25-
BuildResult result = GradleRunner.create()
26-
.withProjectDir(getProjectDir("namingConventionsSelfTest"))
12+
BuildResult result = getGradleRunner("namingConventionsSelfTest")
2713
.withArguments("namingConventions", "-s", "-PcheckForTestsInMain=false")
28-
.withPluginClasspath()
2914
.buildAndFail();
3015

31-
assertNotNull("task did not run", result.task(":namingConventions"));
32-
assertEquals(TaskOutcome.FAILED, result.task(":namingConventions").getOutcome());
33-
String output = result.getOutput();
34-
for (String line : Arrays.asList(
35-
"Found inner classes that are tests, which are excluded from the test runner:",
36-
"* org.elasticsearch.test.NamingConventionsCheckInMainIT$InternalInvalidTests",
37-
"Classes ending with [Tests] must subclass [UnitTestCase]:",
38-
"* org.elasticsearch.test.NamingConventionsCheckInMainTests",
39-
"* org.elasticsearch.test.NamingConventionsCheckInMainIT",
40-
"Not all subclasses of UnitTestCase match the naming convention. Concrete classes must end with [Tests]:",
41-
"* org.elasticsearch.test.WrongName")) {
42-
assertTrue(
43-
"expected: '" + line + "' but it was not found in the output:\n" + output,
44-
output.contains(line)
45-
);
46-
}
16+
assertTaskFailed(result, ":namingConventions");
17+
assertOutputContains(
18+
result.getOutput(),
19+
// TODO: java9 Set.of
20+
new HashSet<>(
21+
Arrays.asList(
22+
"Not all subclasses of UnitTestCase match the naming convention. Concrete classes must end with [Tests]:",
23+
"* org.elasticsearch.test.WrongName",
24+
"Found inner classes that are tests, which are excluded from the test runner:",
25+
"* org.elasticsearch.test.NamingConventionsCheckInMainIT$InternalInvalidTests",
26+
"Classes ending with [Tests] must subclass [UnitTestCase]:",
27+
"* org.elasticsearch.test.NamingConventionsCheckInMainTests",
28+
"* org.elasticsearch.test.NamingConventionsCheckInMainIT"
29+
)
30+
)
31+
);
4732
}
4833

4934
public void testNameCheckFailsAsItShouldWithMain() {
50-
BuildResult result = GradleRunner.create()
51-
.withProjectDir(getProjectDir("namingConventionsSelfTest"))
35+
BuildResult result = getGradleRunner("namingConventionsSelfTest")
5236
.withArguments("namingConventions", "-s", "-PcheckForTestsInMain=true")
53-
.withPluginClasspath()
5437
.buildAndFail();
5538

56-
assertNotNull("task did not run", result.task(":namingConventions"));
57-
assertEquals(TaskOutcome.FAILED, result.task(":namingConventions").getOutcome());
58-
59-
String output = result.getOutput();
60-
for (String line : Arrays.asList(
61-
"Classes ending with [Tests] or [IT] or extending [UnitTestCase] must be in src/test/java:",
62-
"* org.elasticsearch.test.NamingConventionsCheckBadClasses$DummyInterfaceTests",
63-
"* org.elasticsearch.test.NamingConventionsCheckBadClasses$DummyAbstractTests",
64-
"* org.elasticsearch.test.NamingConventionsCheckBadClasses$InnerTests",
65-
"* org.elasticsearch.test.NamingConventionsCheckBadClasses$NotImplementingTests",
66-
"* org.elasticsearch.test.NamingConventionsCheckBadClasses$WrongNameTheSecond",
67-
"* org.elasticsearch.test.NamingConventionsCheckBadClasses$WrongName")) {
68-
assertTrue(
69-
"expected: '" + line + "' but it was not found in the output:\n"+output,
70-
output.contains(line)
71-
);
72-
}
39+
assertTaskFailed(result, ":namingConventions");
40+
assertOutputContains(
41+
result.getOutput(),
42+
// TODO: java9 Set.of
43+
new HashSet<>(
44+
Arrays.asList(
45+
"Classes ending with [Tests] or [IT] or extending [UnitTestCase] must be in src/test/java:",
46+
"* org.elasticsearch.test.NamingConventionsCheckBadClasses$DummyInterfaceTests",
47+
"* org.elasticsearch.test.NamingConventionsCheckBadClasses$DummyInterfaceTests",
48+
"* org.elasticsearch.test.NamingConventionsCheckBadClasses$DummyAbstractTests",
49+
"* org.elasticsearch.test.NamingConventionsCheckBadClasses$InnerTests",
50+
"* org.elasticsearch.test.NamingConventionsCheckBadClasses$NotImplementingTests",
51+
"* org.elasticsearch.test.NamingConventionsCheckBadClasses$WrongNameTheSecond",
52+
"* org.elasticsearch.test.NamingConventionsCheckBadClasses$WrongName"
53+
)
54+
)
55+
);
7356
}
7457

7558
}

buildSrc/src/test/java/org/elasticsearch/gradle/test/GradleIntegrationTestCase.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.nio.file.Path;
1111
import java.util.List;
1212
import java.util.Objects;
13+
import java.util.Set;
1314
import java.util.stream.Collectors;
1415
import java.util.stream.Stream;
1516

@@ -47,6 +48,12 @@ protected void assertOutputContains(String output, String... lines) {
4748
}
4849
}
4950

51+
protected void assertOutputContains(String output, Set<String> lines) {
52+
for (String line : lines) {
53+
assertOutputContains(output, line);
54+
}
55+
}
56+
5057
protected void assertOutputContains(String output, String line) {
5158
assertTrue(
5259
"Expected the following line in output:\n\n" + line + "\n\nOutput is:\n" + output,
@@ -82,7 +89,7 @@ private void assertTaskOutcome(BuildResult result, String taskName, TaskOutcome
8289
"\n\nOutput is:\n" + result.getOutput());
8390
}
8491
assertEquals(
85-
"Expected task to be successful but it was: " + task.getOutcome() +
92+
"Expected task `" + taskName +"` to be successful but it was: " + task.getOutcome() +
8693
taskOutcome + "\n\nOutput is:\n" + result.getOutput() ,
8794
taskOutcome,
8895
task.getOutcome()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
this is a test license file
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
this is a test notice file
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
plugins {
2+
id 'java'
3+
id 'elasticsearch.build'
4+
}
5+
6+
ext.licenseFile = file("LICENSE")
7+
ext.noticeFile = file("NOTICE")
8+
9+
dependencies {
10+
compile "junit:junit:${versions.junit}"
11+
// missing classes in thirdparty audit
12+
compile 'org.hamcrest:hamcrest-core:1.3'
13+
}
14+
15+
repositories {
16+
mavenCentral()
17+
repositories {
18+
maven {
19+
url System.getProperty("local.repo.path")
20+
}
21+
}
22+
}
23+
24+
// todo remove offending rules
25+
forbiddenApisMain.enabled = false
26+
forbiddenApisTest.enabled = false
27+
// requires dependency on testing fw
28+
jarHell.enabled = false
29+
// we don't have tests for now
30+
test.enabled = false
31+
32+
task hello {
33+
doFirst {
34+
println "build plugin can be applied"
35+
}
36+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
42a25dc3219429f0e5d060061f71acb49bf010a0

buildSrc/src/testKit/elasticsearch.build/licenses/hamcrest-core-LICENSE.txt

Whitespace-only changes.

buildSrc/src/testKit/elasticsearch.build/licenses/hamcrest-core-NOTICE.txt

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2973d150c0dc1fefe998f834810d68f278ea58ec

buildSrc/src/testKit/elasticsearch.build/licenses/junit-LICENSE.txt

Whitespace-only changes.

buildSrc/src/testKit/elasticsearch.build/licenses/junit-NOTICE.txt

Whitespace-only changes.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.elasticsearch;
20+
21+
/**
22+
* This is just a test class
23+
*/
24+
public class SampleClass {
25+
26+
}

0 commit comments

Comments
 (0)