Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion test/langtools/tools/javac/SystemFilesClosed.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,22 @@
*/

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.SimpleJavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;
Expand All @@ -53,6 +57,11 @@ public class SystemFilesClosed {

@Test
void testSystemFilesClosed() throws Exception {
// Probe lsof availability before doing the jlink/compile work
if (!lsofCommand().isPresent()) {
Assumptions.abort("lsof command is not available on this system");
}

String targetSystem = base.toString();
int ret = java.util.spi.ToolProvider.findFirst("jlink")
.orElseThrow()
Expand Down Expand Up @@ -83,7 +92,8 @@ public static void main(String[] args) {
}

Process process = new ProcessBuilder()
.command("lsof", "-p", String.valueOf(ProcessHandle.current().pid()))
.command(lsofCommand().orElseThrow(() -> new RuntimeException("lsof command is not available on this system")),
"-p", String.valueOf(ProcessHandle.current().pid()))
.redirectOutput(ProcessBuilder.Redirect.PIPE)
.redirectError(ProcessBuilder.Redirect.INHERIT)
.start();
Expand All @@ -103,4 +113,17 @@ public void setUp(TestInfo info) {
.orElseThrow()
.getName());
}

static Optional<String> lsofCommandCache = Arrays.stream(new String[] {
"/usr/bin/lsof",
"/usr/sbin/lsof",
"/bin/lsof",
"/sbin/lsof",
"/usr/local/bin/lsof"})
.filter(args -> new File(args).exists())
.findFirst();

static Optional<String> lsofCommand() {
return lsofCommandCache;
}
}