diff --git a/.github/workflows/build-and-deploy.yml b/.github/workflows/build-and-deploy.yml index b4f730a30c9..aeee05e6b06 100644 --- a/.github/workflows/build-and-deploy.yml +++ b/.github/workflows/build-and-deploy.yml @@ -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 diff --git a/start-site/src/test/java/io/spring/start/site/ProjectGenerationIntegrationTests.java b/start-site/src/test/java/io/spring/start/site/ProjectGenerationIntegrationTests.java index 3d85246c9a7..e4c3d219f1c 100644 --- a/start-site/src/test/java/io/spring/start/site/ProjectGenerationIntegrationTests.java +++ b/start-site/src/test/java/io/spring/start/site/ProjectGenerationIntegrationTests.java @@ -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; @@ -127,8 +127,8 @@ Stream 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()); @@ -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))) @@ -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); diff --git a/start-site/src/test/java/io/spring/start/site/test/TemporaryFiles.java b/start-site/src/test/java/io/spring/start/site/test/TemporaryFiles.java new file mode 100644 index 00000000000..70151d6053c --- /dev/null +++ b/start-site/src/test/java/io/spring/start/site/test/TemporaryFiles.java @@ -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 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"); + } + +} diff --git a/start-site/src/test/java/io/spring/start/site/test/TestMavenVersionResolver.java b/start-site/src/test/java/io/spring/start/site/test/TestMavenVersionResolver.java index bf27e56c7ab..3c6fedb07fd 100644 --- a/start-site/src/test/java/io/spring/start/site/test/TestMavenVersionResolver.java +++ b/start-site/src/test/java/io/spring/start/site/test/TestMavenVersionResolver.java @@ -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; @@ -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 resolveDependencies(String groupId, String artifactId, String version) { diff --git a/start-site/src/test/resources/application-test.yml b/start-site/src/test/resources/application-test.yml index 53df269549b..604ee888f61 100644 --- a/start-site/src/test/resources/application-test.yml +++ b/start-site/src/test/resources/application-test.yml @@ -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"