Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e4b9bbd

Browse files
eamonnmcmanusGoogle Java Core Libraries
authored and
Google Java Core Libraries
committedApr 21, 2021
Add a test for Gradle incremental compilation with AutoValue.
Partial fix for #1075. Still need a test for AutoFactory. Many thanks to @tbroyer for help developing this. Closes #1076. RELNOTES=n/a PiperOrigin-RevId: 369716434
1 parent 20ab154 commit e4b9bbd

File tree

3 files changed

+187
-1
lines changed

3 files changed

+187
-1
lines changed
 

‎value/src/it/functional/pom.xml

+9
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@
9090
<artifactId>compile-testing</artifactId>
9191
<scope>test</scope>
9292
</dependency>
93+
<dependency>
94+
<groupId>dev.gradleplugins</groupId>
95+
<artifactId>gradle-test-kit</artifactId>
96+
<version>6.8.3</version>
97+
<scope>test</scope>
98+
</dependency>
9399
<dependency>
94100
<groupId>org.mockito</groupId>
95101
<artifactId>mockito-core</artifactId>
@@ -194,6 +200,9 @@
194200
<version>2.22.2</version>
195201
<configuration>
196202
<argLine>${test.jvm.flags}</argLine>
203+
<systemPropertyVariables>
204+
<autoValueVersion>${project.version}</autoValueVersion>
205+
</systemPropertyVariables>
197206
</configuration>
198207
</plugin>
199208
</plugins>

‎value/src/it/functional/src/test/java/com/google/auto/value/CompileWithEclipseTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ public static void setSourceRoot() {
6666
public @Rule TemporaryFolder tmp = new TemporaryFolder();
6767

6868
private static final ImmutableSet<String> IGNORED_TEST_FILES =
69-
ImmutableSet.of("AutoValueNotEclipseTest.java", "CompileWithEclipseTest.java");
69+
ImmutableSet.of(
70+
"AutoValueNotEclipseTest.java", "CompileWithEclipseTest.java", "GradleTest.java");
7071

7172
private static final Predicate<File> JAVA_FILE =
7273
f -> f.getName().endsWith(".java") && !IGNORED_TEST_FILES.contains(f.getName());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
/*
2+
* Copyright 2021 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.google.auto.value;
17+
18+
import static com.google.common.truth.Truth.assertThat;
19+
import static java.nio.charset.StandardCharsets.UTF_8;
20+
21+
import com.google.common.collect.ImmutableList;
22+
import java.io.File;
23+
import java.io.IOException;
24+
import java.nio.file.Files;
25+
import java.nio.file.Path;
26+
import java.nio.file.Paths;
27+
import java.util.Optional;
28+
import java.util.regex.Matcher;
29+
import java.util.regex.Pattern;
30+
import java.util.stream.Stream;
31+
import org.gradle.testkit.runner.BuildResult;
32+
import org.gradle.testkit.runner.GradleRunner;
33+
import org.gradle.util.GradleVersion;
34+
import org.junit.Rule;
35+
import org.junit.Test;
36+
import org.junit.rules.TemporaryFolder;
37+
import org.junit.runner.RunWith;
38+
import org.junit.runners.JUnit4;
39+
40+
@RunWith(JUnit4.class)
41+
public class GradleTest {
42+
@Rule public TemporaryFolder fakeProject = new TemporaryFolder();
43+
44+
private static final String BUILD_GRADLE_TEXT =
45+
String.join("\n",
46+
"plugins {",
47+
" id 'java-library'",
48+
"}",
49+
"repositories {",
50+
" maven { url = uri('${localRepository}') }",
51+
"}",
52+
"dependencies {",
53+
" compileOnlyApi 'com.google.auto.value:auto-value-annotations:${autoValueVersion}'",
54+
" annotationProcessor 'com.google.auto.value:auto-value:${autoValueVersion}'",
55+
"}");
56+
57+
private static final String FOO_TEXT =
58+
String.join("\n",
59+
"package com.example;",
60+
"",
61+
"import com.google.auto.value.AutoValue;",
62+
"",
63+
"@AutoValue",
64+
"abstract class Foo {",
65+
" abstract String bar();",
66+
"",
67+
" static Foo of(String bar) {",
68+
" return new AutoValue_Foo(bar);",
69+
" }",
70+
"}");
71+
72+
private static final Optional<File> GRADLE_INSTALLATION = getGradleInstallation();
73+
74+
@Test
75+
public void basic() throws IOException {
76+
String autoValueVersion = System.getProperty("autoValueVersion");
77+
assertThat(autoValueVersion).isNotNull();
78+
String localRepository = System.getProperty("localRepository");
79+
assertThat(localRepository).isNotNull();
80+
81+
// Set up the fake Gradle project.
82+
String buildGradleText = expandSystemProperties(BUILD_GRADLE_TEXT);
83+
writeFile(fakeProject.newFile("build.gradle").toPath(), buildGradleText);
84+
Path srcDir = fakeProject.newFolder("src", "main", "java", "com", "example").toPath();
85+
writeFile(srcDir.resolve("Foo.java"), FOO_TEXT);
86+
87+
// Build it the first time.
88+
BuildResult result1 = buildFakeProject();
89+
assertThat(result1.getOutput()).contains(
90+
"Full recompilation is required because no incremental change information is available");
91+
Path output = fakeProject.getRoot().toPath()
92+
.resolve("build/classes/java/main/com/example/AutoValue_Foo.class");
93+
assertThat(Files.exists(output)).isTrue();
94+
95+
// Add a source file to the project.
96+
String barText = FOO_TEXT.replace("Foo", "Bar");
97+
Path barFile = srcDir.resolve("Bar.java");
98+
writeFile(barFile, barText);
99+
100+
// Build it a second time.
101+
BuildResult result2 = buildFakeProject();
102+
assertThat(result2.getOutput()).doesNotContain("Full recompilation is required");
103+
104+
// Remove the second source file and build a third time. If incremental annotation processing
105+
// is not working, this will produce a message like this:
106+
// Full recompilation is required because com.google.auto.value.processor.AutoValueProcessor
107+
// is not incremental
108+
Files.delete(barFile);
109+
BuildResult result3 = buildFakeProject();
110+
assertThat(result3.getOutput()).doesNotContain("Full recompilation is required");
111+
}
112+
113+
private BuildResult buildFakeProject() throws IOException {
114+
GradleRunner runner = GradleRunner.create()
115+
.withProjectDir(fakeProject.getRoot())
116+
.withArguments("--info", "compileJava");
117+
if (GRADLE_INSTALLATION.isPresent()) {
118+
runner.withGradleInstallation(GRADLE_INSTALLATION.get());
119+
} else {
120+
runner.withGradleVersion(GradleVersion.current().getVersion());
121+
}
122+
return runner.build();
123+
}
124+
125+
private static Optional<File> getGradleInstallation() {
126+
String gradleHome = System.getenv("GRADLE_HOME");
127+
if (gradleHome != null) {
128+
File gradleHomeFile = new File(gradleHome);
129+
if (gradleHomeFile.isDirectory()) {
130+
return Optional.of(new File(gradleHome));
131+
}
132+
}
133+
try {
134+
Path gradleExecutable = Paths.get("/usr/bin/gradle");
135+
Path gradleLink =
136+
gradleExecutable.resolveSibling(Files.readSymbolicLink(gradleExecutable));
137+
if (!gradleLink.endsWith("bin/gradle")) {
138+
return Optional.empty();
139+
}
140+
Path installationPath = gradleLink.getParent().getParent();
141+
if (!Files.isDirectory(installationPath)) {
142+
return Optional.empty();
143+
}
144+
Optional<Path> coreJar;
145+
Pattern corePattern = Pattern.compile("gradle-core-([0-9]+)\\..*\\.jar");
146+
try (Stream<Path> files = Files.walk(installationPath.resolve("lib"))) {
147+
coreJar =
148+
files.filter(p -> {
149+
Matcher matcher = corePattern.matcher(p.getFileName().toString());
150+
if (matcher.matches()) {
151+
int version = Integer.parseInt(matcher.group(1));
152+
if (version >= 5) {
153+
return true;
154+
}
155+
}
156+
return false;
157+
}).findFirst();
158+
}
159+
return coreJar.map(unused -> installationPath.toFile());
160+
} catch (IOException e) {
161+
return Optional.empty();
162+
}
163+
}
164+
165+
private static String expandSystemProperties(String s) {
166+
for (String name : System.getProperties().stringPropertyNames()) {
167+
String value = System.getProperty(name);
168+
s = s.replace("${" + name + "}", value);
169+
}
170+
return s;
171+
}
172+
173+
private static void writeFile(Path file, String text) throws IOException {
174+
Files.write(file, ImmutableList.of(text), UTF_8);
175+
}
176+
}

0 commit comments

Comments
 (0)
Please sign in to comment.