Skip to content

Commit

Permalink
Improve caching of repositories in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mhalbritter committed Feb 19, 2024
1 parent d086832 commit aa976be
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 13 deletions.
9 changes: 4 additions & 5 deletions .github/workflows/build-and-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,14 @@ jobs:
- name: Cache Maven/Gradle repositories for tests
uses: actions/cache@v4
with:
path: |
/tmp/maven-version-resolver-cache
/tmp/homes
path: ~/start-spring-io-cache
# See https://github.com/actions/cache/blob/main/tips-and-workarounds.md#update-a-cache
key: test-repositories-${{ runner.os }}-${{ github.run_id }}
restore-keys: |
test-repositories-${{ runner.os }}
restore-keys: test-repositories-${{ runner.os }}-

- name: Build with Maven
env:
START_SPRING_IO_TMPDIR: ~/start-spring-io-cache
run: ./mvnw --batch-mode --update-snapshots verify

- name: Set up Azure
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@
import io.spring.initializr.web.project.ProjectGenerationInvoker;
import io.spring.initializr.web.project.ProjectRequest;
import io.spring.initializr.web.project.WebProjectRequest;
import io.spring.start.site.test.TemporaryFiles;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;
import org.junit.jupiter.params.ParameterizedTest;
Expand Down Expand Up @@ -127,8 +127,8 @@ Stream<Arguments> parameters() {

@ParameterizedTest(name = "{0} - {1} - {2} - {3}")
@MethodSource("parameters")
void projectBuilds(Version bootVersion, Packaging packaging, Language language, BuildSystem buildSystem,
@TempDir Path directory) throws IOException, InterruptedException {
void projectBuilds(Version bootVersion, Packaging packaging, Language language, BuildSystem buildSystem)
throws IOException, InterruptedException {
WebProjectRequest request = new WebProjectRequest();
request.setBootVersion(bootVersion.toString());
request.setLanguage(language.id());
Expand All @@ -143,7 +143,8 @@ void projectBuilds(Version bootVersion, Packaging packaging, Language language,
try {
ProcessBuilder processBuilder = createProcessBuilder(buildSystem, home);
processBuilder.directory(project.toFile());
Path output = Files.createTempFile(directory, "output-", ".log");
Path output = TemporaryFiles.newTemporaryDirectory("ProjectGenerationIntegrationTests-projectBuilds")
.resolve("output.log");
processBuilder.redirectError(output.toFile());
processBuilder.redirectOutput(output.toFile());
assertThat(processBuilder.start().waitFor()).describedAs(String.join("\n", Files.readAllLines(output)))
Expand Down Expand Up @@ -215,7 +216,7 @@ void release(Path home) {

private Path createTempDirectory() {
try {
Path path = Path.of(System.getProperty("java.io.tmpdir"))
Path path = TemporaryFiles.getTempDir()
.resolve("homes")
.resolve(this.prefix + "-" + this.counter.getAndIncrement());
Files.createDirectories(path);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright 2012-2024 the original author or authors.
*
* 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
*
* https://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 io.spring.start.site.test;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;

import org.springframework.util.FileSystemUtils;

/**
* Utility to work with temporary files.
*
* @author Moritz Halbritter
*/
public final class TemporaryFiles {

private static final AtomicBoolean CLEANUP_REGISTERED = new AtomicBoolean();

private static final Set<Path> CLEANUP_PATHS = ConcurrentHashMap.newKeySet();

private TemporaryFiles() {
}

public static Path newTemporaryDirectory(String prefix) throws IOException {
String name = prefix + System.nanoTime();
Path path = getTempDir().resolve(name);
Files.createDirectories(path);
registerCleanup();
CLEANUP_PATHS.add(path);
return path;
}

private static void registerCleanup() {
if (CLEANUP_REGISTERED.compareAndSet(false, true)) {
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
for (Path cleanupPath : CLEANUP_PATHS) {
try {
FileSystemUtils.deleteRecursively(cleanupPath);
}
catch (IOException ex) {
// Ignore
}
}
}));
}
}

public static Path getTempDir() {
String property = System.getenv("START_SPRING_IO_TMPDIR");
if (property != null) {
return Path.of(property);
}
property = System.getProperty("java.io.tmpdir");
if (property != null) {
return Path.of(property);
}
throw new IllegalStateException(
"Unable to find temporary directory. Please set either the env variable START_SPRING_IO_TMPDIR, or the system property java.io.tmpdir");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package io.spring.start.site.test;

import java.nio.file.Path;
import java.util.Map;

import io.spring.initializr.versionresolver.MavenVersionResolver;
Expand All @@ -31,7 +30,7 @@ public class TestMavenVersionResolver implements MavenVersionResolver {
private static final TestMavenVersionResolver INSTANCE = new TestMavenVersionResolver();

private final MavenVersionResolver delegate = MavenVersionResolver
.withCacheLocation(Path.of(System.getProperty("java.io.tmpdir")).resolve("maven-version-resolver-cache"));
.withCacheLocation(TemporaryFiles.getTempDir().resolve("maven-version-resolver-cache"));

@Override
public Map<String, String> resolveDependencies(String groupId, String artifactId, String version) {
Expand Down
2 changes: 1 addition & 1 deletion start-site/src/test/resources/application-test.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
application:
maven-version-resolver:
cache-directory: "${java.io.tmpdir}/maven-version-resolver-cache"
cache-directory: "${START_SPRING_IO_TMPDIR:${java.io.tmpdir}}/maven-version-resolver-cache"

0 comments on commit aa976be

Please sign in to comment.