@Nullable private static String getResourceRoot(@NotNull Class context, @NotNull String path) { URL url = context.getResource(path); if (url == null) { url = ClassLoader.getSystemResource(path.substring(1)); } return url != null ? extractRoot(url, path) : null; }
public static void setLocalPtyLib() { if ("false".equals(System.getProperty(PtyUtil.PREFERRED_NATIVE_FOLDER_KEY))) { System.clearProperty(PtyUtil.PREFERRED_NATIVE_FOLDER_KEY); } else { System.setProperty(PtyUtil.PREFERRED_NATIVE_FOLDER_KEY, getBuiltNativeFolder().toString()); } } }
@NotNull public static String[] getJavaCommand(@NotNull Class<?> aClass, String... args) { List<String> result = new ArrayList<>(); result.add(getJavaExecutablePath()); result.add("-cp"); result.add(getJarPathForClass(aClass)); result.add(aClass.getName()); result.addAll(Arrays.asList(args)); return result.toArray(new String[0]); }
final CountDownLatch latch = new CountDownLatch(1); String[] cmd = TestUtil.getJavaCommand(RepeatTextWithTimeout.class, "2", "1000", "Hello, World");
@Override public void setUp() throws Exception { super.setUp(); TestUtil.setLocalPtyLib(); }
/** * Copied from com.intellij.openapi.application.PathManager#getJarPathForClass. */ @NotNull private static String getJarPathForClass(@NotNull Class aClass) { String resourceRoot = getResourceRoot(aClass, "/" + aClass.getName().replace('.', '/') + ".class"); return new File(Objects.requireNonNull(resourceRoot)).getAbsolutePath(); }
@NotNull private static String extractRoot(@NotNull URL resourceURL, @NotNull String resourcePath) { if (!resourcePath.startsWith("/") && !resourcePath.startsWith("\\")) { throw new IllegalStateException("precondition failed: " + resourcePath); } String resultPath = null; String protocol = resourceURL.getProtocol(); if ("file".equals(protocol)) { String path = urlToFile(resourceURL).getPath(); String testPath = path.replace('\\', '/'); String testResourcePath = resourcePath.replace('\\', '/'); if (testPath.endsWith(testResourcePath)) { resultPath = path.substring(0, path.length() - resourcePath.length()); } } if (resultPath == null) { throw new IllegalStateException("Cannot extract '" + resourcePath + "' from '" + resourceURL + "', " + protocol); } return resultPath; }
public void testConsoleMode() throws Exception { String[] command; if (Platform.isWindows()) { File file = new File(TestUtil.getTestDataPath(), "console-mode-test1.bat"); assumeTrue(file.exists()); command = new String[] { "cmd.exe", "/c", file.getAbsolutePath() }; } else { File file = new File(TestUtil.getTestDataPath(), "console-mode-test1.sh"); assumeTrue(file.exists()); command = new String[] { "/bin/sh", file.getAbsolutePath() }; } PtyProcess pty = new PtyProcessBuilder(command).setConsole(true).start(); final CountDownLatch latch = new CountDownLatch(2); Gobbler stdout = startReader(pty.getInputStream(), latch); Gobbler stderr = startReader(pty.getErrorStream(), latch); assertTrue(latch.await(4, TimeUnit.SECONDS)); stdout.awaitFinish(); stderr.awaitFinish(); pty.destroy(); assertEquals("abcdefz\r\n", stdout.getOutput()); assertEquals("ABCDEFZ\r\n", stderr.getOutput()); }
final CountDownLatch latch = new CountDownLatch(1); String[] cmd = TestUtil.getJavaCommand(RepeatTextWithTimeout.class, "15", "1000", "Hello, World");
@Before public void setUp() { TestUtil.setLocalPtyLib(); Assume.assumeTrue(Platform.isWindows()); }
final CountDownLatch latch = new CountDownLatch(1); String[] cmd = TestUtil.getJavaCommand(RepeatTextWithTimeout.class, "2", "1000", "Hello, World");
@Test public void locationsListedCorrectly() throws IOException { Path nativeFolder = TestUtil.getBuiltNativeFolder(); List<String> fsNativePaths = new ArrayList<>(); Files.walkFileTree(nativeFolder, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { fsNativePaths.add(nativeFolder.relativize(file).toString().replace("\\", "/")); return FileVisitResult.CONTINUE; } }); Collections.sort(fsNativePaths); List<String> expectedNativePaths = Arrays.asList(ExtractedNative.LOCATIONS); Collections.sort(expectedNativePaths); Assert.assertEquals(expectedNativePaths, fsNativePaths); }
@Test public void testGettingWorkingDirectory() throws Exception { String[] cmd = TestUtil.getJavaCommand(RepeatTextWithTimeout.class, "1", "1000", "Hello, World\n"); String workingDir = new File("test\\testData").getAbsolutePath(); PtyProcessBuilder builder = new PtyProcessBuilder(cmd) .setDirectory(workingDir) .setConsole(false) .setCygwin(false); WinPtyProcess process = (WinPtyProcess) builder.start(); assertEquals(workingDir, trimTrailingSlash(process.getWorkingDirectory())); process.waitFor(); }
@Before public void setUp() throws Exception { myPrevClassLoader = null; if (shouldAddNativeDirToClassPath()) { myPrevClassLoader = Thread.currentThread().getContextClassLoader(); myResourceNamePrefix = ""; URL url = TestUtil.getBuiltNativeFolder().toUri().toURL(); LOG.info("Adding " + url + " to current thread classpath"); ClassLoader urlCl = URLClassLoader.newInstance(new URL[]{url}, myPrevClassLoader); Thread.currentThread().setContextClassLoader(urlCl); } }
/** * Tests that getting and setting the window size for a file descriptor works. */ public void testGetAndSetWinSize() throws Exception { String[] cmd = TestUtil.getJavaCommand(RepeatTextWithTimeout.class, "2", "1000", "Hello, World"); PtyProcess pty = PtyProcess.exec(cmd); WinSize ws = new WinSize(); ws.ws_col = 120; ws.ws_row = 30; pty.setWinSize(ws); WinSize ws1 = pty.getWinSize(); assertNotNull(ws1); assertEquals(120, ws1.ws_col); assertEquals(30, ws1.ws_row); pty.waitFor(); }