Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a test for Gradle incremental compilation with AutoValue. #1076

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
4fe72cc
Add a test for Gradle incremental compilation with AutoValue.
eamonnmcmanus Apr 18, 2021
78c6e49
Add actual test files.
eamonnmcmanus Apr 18, 2021
2f02b7a
Add a missing pom.xml change.
eamonnmcmanus Apr 18, 2021
19ba036
Use Gradle installation in /usr/share/gradle if present and >= 7.0.
eamonnmcmanus Apr 18, 2021
cfdd8a5
Exclude GradleTest from the Java 7 inputs to CompileWithEclipseTest.
eamonnmcmanus Apr 18, 2021
e9f2278
Add some diagnostic prints.
eamonnmcmanus Apr 18, 2021
f5b2789
More print statements.
eamonnmcmanus Apr 18, 2021
5829fad
Where are thou, Gradle?
eamonnmcmanus Apr 18, 2021
cd72025
Find Gradle by resolving the symlink /usr/bin/gradle.
eamonnmcmanus Apr 18, 2021
fca0d4c
Maybe we can just use GRADLE_HOME.
eamonnmcmanus Apr 18, 2021
5409d37
Looks like GRADLE_HOME is incorrectly set in the GitHub Actions envir…
eamonnmcmanus Apr 18, 2021
3d1c93c
Remove print statements.
eamonnmcmanus Apr 19, 2021
23d2819
Incorporate suggestions from @tbroyer.
eamonnmcmanus Apr 20, 2021
e08d638
Address review comments from @cgdecker.
eamonnmcmanus Apr 21, 2021
11a9dc9
Add temporary debug prints to see why we're not finding the Gradle in…
eamonnmcmanus Apr 21, 2021
46d7cf8
Fix compilation error.
eamonnmcmanus Apr 21, 2021
1922e33
Fix .endsWith mistake.
eamonnmcmanus Apr 21, 2021
73dec7c
More print statements.
eamonnmcmanus Apr 21, 2021
7ddc4de
Got those old resolve calls backwards.
eamonnmcmanus Apr 21, 2021
32c46d6
Remove print statements. Phew.
eamonnmcmanus Apr 21, 2021
acac5cd
Remove an unnecessary clause.
eamonnmcmanus Apr 21, 2021
3993c36
Move a declaration inside a block to join its initialization.
eamonnmcmanus Apr 21, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions value/src/it/functional/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@
<artifactId>compile-testing</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>dev.gradleplugins</groupId>
<artifactId>gradle-test-kit</artifactId>
<version>6.8.3</version>
<scope>test</scope>
</dependency>
Comment on lines +93 to +98
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, didn't know about these redistributions (by former Gradle, Inc. employees), this is great!

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
Expand Down Expand Up @@ -194,6 +200,9 @@
<version>2.22.2</version>
<configuration>
<argLine>${test.jvm.flags}</argLine>
<systemPropertyVariables>
<autoValueVersion>${project.version}</autoValueVersion>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ public static void setSourceRoot() {
f ->
f.getName().equals("AutoValueJava8Test.java")
|| f.getName().equals("AutoOneOfJava8Test.java")
|| f.getName().equals("EmptyExtension.java");
|| f.getName().equals("EmptyExtension.java")
|| f.getName().equals("GradleTest.java");

@Test
public void compileWithEclipseJava7() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
/*
* Copyright 2021 Google LLC
*
* 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 com.google.auto.value;
cgdecker marked this conversation as resolved.
Show resolved Hide resolved

import static com.google.common.truth.Truth.assertThat;
import static java.nio.charset.StandardCharsets.UTF_8;

import com.google.common.collect.ImmutableList;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;
import org.gradle.testkit.runner.BuildResult;
import org.gradle.testkit.runner.GradleRunner;
import org.gradle.util.GradleVersion;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class GradleTest {
@Rule public TemporaryFolder fakeProject = new TemporaryFolder();

private static final String BUILD_GRADLE_TEXT =
String.join("\n",
"plugins {",
" id 'java-library'",
"}",
"repositories {",
" maven { url = uri('${localRepository}') }",
"}",
"dependencies {",
" compileOnlyApi 'com.google.auto.value:auto-value-annotations:${autoValueVersion}'",
" annotationProcessor 'com.google.auto.value:auto-value:${autoValueVersion}'",
"}");

private static final String FOO_TEXT =
String.join("\n",
"package com.example;",
"",
"import com.google.auto.value.AutoValue;",
"",
"@AutoValue",
"abstract class Foo {",
" abstract String bar();",
"",
" static Foo of(String bar) {",
" return new AutoValue_Foo(bar);",
" }",
"}");

private static final Optional<File> GRADLE_INSTALLATION = getGradleInstallation();

@Test
public void basic() throws IOException {
String autoValueVersion = System.getProperty("autoValueVersion");
assertThat(autoValueVersion).isNotNull();
eamonnmcmanus marked this conversation as resolved.
Show resolved Hide resolved
String localRepository = System.getProperty("localRepository");
assertThat(localRepository).isNotNull();

// Set up the fake Gradle project.
String buildGradleText = expandSystemProperties(BUILD_GRADLE_TEXT);
writeFile(fakeProject.newFile("build.gradle").toPath(), buildGradleText);
Path srcDir = fakeProject.newFolder("src", "main", "java", "com", "example").toPath();
writeFile(srcDir.resolve("Foo.java"), FOO_TEXT);

// Build it the first time.
BuildResult result1 = buildFakeProject();
assertThat(result1.getOutput()).contains(
"Full recompilation is required because no incremental change information is available");
Path output = fakeProject.getRoot().toPath()
.resolve("build/classes/java/main/com/example/AutoValue_Foo.class");
assertThat(Files.exists(output)).isTrue();

// Add a source file to the project.
String barText = FOO_TEXT.replace("Foo", "Bar");
Path barFile = srcDir.resolve("Bar.java");
writeFile(barFile, barText);

// Build it a second time.
BuildResult result2 = buildFakeProject();
assertThat(result2.getOutput()).doesNotContain("Full recompilation is required");

// Remove the second source file and build a third time. If incremental annotation processing
// is not working, this will produce a message like this:
// Full recompilation is required because com.google.auto.value.processor.AutoValueProcessor
// is not incremental
Files.delete(barFile);
BuildResult result3 = buildFakeProject();
assertThat(result3.getOutput()).doesNotContain("Full recompilation is required");
}

private BuildResult buildFakeProject() throws IOException {
GradleRunner runner = GradleRunner.create()
.withProjectDir(fakeProject.getRoot())
.withArguments("--info", "compileJava");
if (GRADLE_INSTALLATION.isPresent()) {
runner.withGradleInstallation(GRADLE_INSTALLATION.get());
} else {
runner.withGradleVersion(GradleVersion.current().getVersion());
}
return runner.build();
}

private static Optional<File> getGradleInstallation() {
String gradleHome = System.getenv("GRADLE_HOME");
if (gradleHome != null) {
File gradleHomeFile = new File(gradleHome);
if (gradleHomeFile.isDirectory()) {
return Optional.of(new File(gradleHome));
}
}
eamonnmcmanus marked this conversation as resolved.
Show resolved Hide resolved
try {
Path gradleExecutable = Paths.get("/usr/bin/gradle");
Path gradleLink =
gradleExecutable.resolveSibling(Files.readSymbolicLink(gradleExecutable));
if (!gradleLink.endsWith("bin/gradle")) {
return Optional.empty();
}
Path installationPath = gradleLink.getParent().getParent();
if (!Files.isDirectory(installationPath)) {
return Optional.empty();
}
Optional<Path> coreJar;
Pattern corePattern = Pattern.compile("gradle-core-([0-9]+)\\..*\\.jar");
try (Stream<Path> files = Files.walk(installationPath.resolve("lib"))) {
coreJar =
files.filter(p -> {
Matcher matcher = corePattern.matcher(p.getFileName().toString());
if (matcher.matches()) {
int version = Integer.parseInt(matcher.group(1));
if (version >= 5) {
return true;
}
}
return false;
}).findFirst();
}
return coreJar.map(unused -> installationPath.toFile());
} catch (IOException e) {
return Optional.empty();
}
}

private static String expandSystemProperties(String s) {
for (String name : System.getProperties().stringPropertyNames()) {
String value = System.getProperty(name);
s = s.replace("${" + name + "}", value);
}
return s;
}

private static void writeFile(Path file, String text) throws IOException {
Files.write(file, ImmutableList.of(text), UTF_8);
}
}