Copilot commented on code in PR #3656: URL: https://github.com/apache/maven-surefire/pull/3656#discussion_r4729362871
########## surefire-its/src/test/resources/fork-temp-files-cleaned/src/test/java/forkfiles/ForkFiles.java: ########## @@ -0,0 +1,84 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 forkfiles; + +import java.io.File; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.fail; + +/** + * Looks at the directory holding the fork's temporary files: the booter properties ({@code surefire-*tmp}), + * the system properties ({@code surefire_*tmp}) and the booter jar ({@code surefirebooter*.jar}). + * Only the files of the running fork should be there, not the ones of earlier forks. + */ +final class ForkFiles { + private ForkFiles() {} + + static void assertOnlyThisForkFilesAreLeft() { + File tempDir = forkTempDir(); + String[] names = tempDir.list(); + List<String> listing = new ArrayList<>(); + if (names != null) { + Collections.addAll(listing, names); + } + Collections.sort(listing); + System.out.println("Fork temp dir " + tempDir + " holds " + listing); + + if (!Boolean.parseBoolean(System.getProperty("checkForkFiles", "true"))) { + return; + } + + int booterJars = 0; + int booterProperties = 0; + int systemProperties = 0; + for (String name : listing) { + if (name.startsWith("surefirebooter") && name.endsWith(".jar")) { + booterJars++; + } else if (name.startsWith("surefire-") && name.endsWith("tmp")) { + booterProperties++; + } else if (name.startsWith("surefire_") && name.endsWith("tmp")) { + systemProperties++; + } + } + if (booterJars > 1 || booterProperties > 1 || systemProperties > 1) { + fail("Files of earlier forks are still in " + tempDir + ": " + booterJars + " booter jars, " + + booterProperties + " booter properties files, " + systemProperties + + " system properties files. Listing: " + listing); + } + } + + // the fork runs from the manifest-only booter jar, which sits in the fork temp dir + private static File forkTempDir() { + String classPath = System.getProperty("java.class.path"); Review Comment: The manifest-only launch replaces `java.class.path` with the test classpath before these tests run, so this lookup misses the booter JAR. On Windows, the fork directory is under the system temp directory rather than `target/surefire`, causing the cleanup tests to fail even when cleanup works. Read `surefire.real.class.path`, which preserves the original classpath, falling back to `java.class.path`. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
