8265078: jpackage tests on Windows leave large temp files

Reviewed-by: asemenyuk, kcr
This commit is contained in:
Andy Herrick 2021-04-14 19:56:24 +00:00
parent 05f851e45d
commit e167577888
3 changed files with 30 additions and 2 deletions

View File

@ -44,6 +44,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
@ -56,6 +57,8 @@ import javax.xml.stream.XMLStreamWriter;
public class IOUtils {
public static void deleteRecursive(Path directory) throws IOException {
final AtomicReference<IOException> exception = new AtomicReference<>();
if (!Files.exists(directory)) {
return;
}
@ -67,7 +70,11 @@ public class IOUtils {
if (Platform.getPlatform() == Platform.WINDOWS) {
Files.setAttribute(file, "dos:readonly", false);
}
Files.delete(file);
try {
Files.delete(file);
} catch (IOException ex) {
exception.compareAndSet(null, ex);
}
return FileVisitResult.CONTINUE;
}
@ -83,10 +90,17 @@ public class IOUtils {
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException e)
throws IOException {
Files.delete(dir);
try {
Files.delete(dir);
} catch (IOException ex) {
exception.compareAndSet(null, ex);
}
return FileVisitResult.CONTINUE;
}
});
if (exception.get() != null) {
throw exception.get();
}
}
public static void copyRecursive(Path src, Path dest) throws IOException {

View File

@ -89,6 +89,13 @@ public final class Executor extends CommandArguments<Executor> {
return this;
}
public Executor setWindowsTmpDir(String tmp) {
TKit.assertTrue(TKit.isWindows(),
"setWindowsTmpDir is only valid on Windows platform");
winTmpDir = tmp;
return this;
}
/**
* Configures this instance to save full output that command will produce.
* This function is mutual exclusive with
@ -279,6 +286,9 @@ public final class Executor extends CommandArguments<Executor> {
command.add(executablePath().toString());
command.addAll(args);
ProcessBuilder builder = new ProcessBuilder(command);
if (winTmpDir != null) {
builder.environment().put("TMP", winTmpDir);
}
StringBuilder sb = new StringBuilder(getPrintableCommandLine());
if (withSavedOutput()) {
builder.redirectErrorStream(true);
@ -426,6 +436,7 @@ public final class Executor extends CommandArguments<Executor> {
private Set<SaveOutputType> saveOutputType;
private Path directory;
private boolean removePath;
private String winTmpDir = null;
private static enum SaveOutputType {
NONE, FULL, FIRST_LINE, DUMP

View File

@ -643,6 +643,9 @@ public final class JPackageCommand extends CommandArguments<JPackageCommand> {
exec.setToolProvider(JavaTool.JPACKAGE);
} else {
exec.setExecutable(JavaTool.JPACKAGE);
if (TKit.isWindows()) {
exec.setWindowsTmpDir(System.getProperty("java.io.tmpdir"));
}
}
return exec;