Skip to content

Commit

Permalink
Add test for build-cache
Browse files Browse the repository at this point in the history
  • Loading branch information
marcospereira committed Nov 3, 2023
1 parent cb34cf5 commit 968c4b4
Showing 1 changed file with 39 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,50 @@ public static Stream<Arguments> runGradleBuild() throws IOException {
@ParameterizedTest
@MethodSource
public void runGradleBuild(Path projectDir, String gradleVersion) {
BuildResult result = runner(projectDir, gradleVersion)
.withArguments("--configuration-cache", TASK_NAME)
.build();

Assertions.assertNotEquals(TaskOutcome.FAILED, result.task(TASK_NAME).getOutcome(), String.format("Build failed in %s with Gradle Version %s", projectDir, gradleVersion));
}

public static Stream<Arguments> checkBuildCache() throws IOException {
return runGradleBuild();
}

@ParameterizedTest
@MethodSource
public void checkBuildCache(Path projectDir, String gradleVersion) {
// Populates the cache
runner(projectDir, gradleVersion)
.withArguments("--build-cache", TASK_NAME)
.build();

// Should result in build-cache usage even with a `clean`
BuildResult result = runner(projectDir, gradleVersion)
.withArguments("--build-cache", "--console=plain", "clean", TASK_NAME)
.build();

Assertions.assertNotEquals(TaskOutcome.FAILED, result.task(TASK_NAME).getOutcome(), String.format("Build failed in %s with Gradle Version %s", projectDir, gradleVersion));

String output = result.getOutput();
String expectGenerateFromCache = "Task :generateJte FROM-CACHE";
String expectPrecompileFromCache = "Task :precompileJte FROM-CACHE";

// Some test projects use precompile and some use generate
boolean jteFromCache = output.contains(expectGenerateFromCache) || output.contains(expectPrecompileFromCache);

Assertions.assertTrue(jteFromCache, () -> String.format("Expect %s or %s to be included in the following output: \n%s", expectGenerateFromCache, expectPrecompileFromCache, output));
}

private GradleRunner runner(Path projectDir, String gradleVersion) {
GradleRunner runner = GradleRunner.create()
.withProjectDir(projectDir.toFile())
.withTestKitDir(Paths.get("build").resolve(projectDir.getFileName()).toAbsolutePath().toFile())
.withArguments("--configuration-cache", TASK_NAME);
.withTestKitDir(Paths.get("build").resolve(projectDir.getFileName()).toAbsolutePath().toFile());

if (!DEFAULT.equals(gradleVersion)) {
runner = runner.withGradleVersion(gradleVersion);
}

BuildResult result = runner.build();

Assertions.assertNotEquals(TaskOutcome.FAILED, result.task(TASK_NAME).getOutcome(), String.format("Build failed in %s with Gradle Version %s", projectDir, gradleVersion));
return runner;
}
}

0 comments on commit 968c4b4

Please sign in to comment.