-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Closed
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 78c6e49
Add actual test files.
eamonnmcmanus 2f02b7a
Add a missing pom.xml change.
eamonnmcmanus 19ba036
Use Gradle installation in /usr/share/gradle if present and >= 7.0.
eamonnmcmanus cfdd8a5
Exclude GradleTest from the Java 7 inputs to CompileWithEclipseTest.
eamonnmcmanus e9f2278
Add some diagnostic prints.
eamonnmcmanus f5b2789
More print statements.
eamonnmcmanus 5829fad
Where are thou, Gradle?
eamonnmcmanus cd72025
Find Gradle by resolving the symlink /usr/bin/gradle.
eamonnmcmanus fca0d4c
Maybe we can just use GRADLE_HOME.
eamonnmcmanus 5409d37
Looks like GRADLE_HOME is incorrectly set in the GitHub Actions envir…
eamonnmcmanus 3d1c93c
Remove print statements.
eamonnmcmanus 23d2819
Incorporate suggestions from @tbroyer.
eamonnmcmanus e08d638
Address review comments from @cgdecker.
eamonnmcmanus 11a9dc9
Add temporary debug prints to see why we're not finding the Gradle in…
eamonnmcmanus 46d7cf8
Fix compilation error.
eamonnmcmanus 1922e33
Fix .endsWith mistake.
eamonnmcmanus 73dec7c
More print statements.
eamonnmcmanus 7ddc4de
Got those old resolve calls backwards.
eamonnmcmanus 32c46d6
Remove print statements. Phew.
eamonnmcmanus acac5cd
Remove an unnecessary clause.
eamonnmcmanus 3993c36
Move a declaration inside a block to join its initialization.
eamonnmcmanus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
177 changes: 177 additions & 0 deletions
177
value/src/it/functional/src/test/java/com/google/auto/value/GradleTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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!