public static PtyProcess exec(String[] command, Map<String, String> environment, String workingDirectory, boolean console, boolean cygwin, File logFile) throws IOException { PtyProcessBuilder builder = new PtyProcessBuilder(command) .setEnvironment(environment) .setDirectory(workingDirectory) .setConsole(console) .setCygwin(cygwin) .setLogFile(logFile); return builder.start(); } }
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()); }
public void testExecCat() throws Exception { if (Platform.isWindows()) { return; } PtyProcess pty = new PtyProcessBuilder(new String[]{"cat"}).start(); Gobbler stdout = startReader(pty.getInputStream(), null); assertTrue("Process terminated unexpectedly", pty.isRunning()); pty.getOutputStream().write("Hello".getBytes(StandardCharsets.UTF_8)); pty.getOutputStream().flush(); Thread.sleep(1000); assertEquals("Hello", stdout.getOutput()); PtyHelpers.getInstance().kill(pty.getPid(), PtyHelpers.SIGPIPE); Thread.sleep(1000); assertTrue("Process is alive unexpectedly", !pty.isRunning()); assertEquals(PtyHelpers.SIGPIPE, pty.exitValue()); }
@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(); }
@Test public void testChangingWorkingDirectory() throws IOException, InterruptedException { File testNodeScript = new File("test\\testData\\change-working-dir.bat"); Assert.assertTrue(testNodeScript.isFile()); String workingDir1 = testNodeScript.getAbsoluteFile().getParent(); String workingDir2 = testNodeScript.getAbsoluteFile().getParentFile().getParentFile().getParent(); String[] cmd = {testNodeScript.getAbsolutePath(), "1", workingDir2, "2"}; PtyProcessBuilder builder = new PtyProcessBuilder(cmd) .setDirectory(workingDir1) .setConsole(false) .setEnvironment(System.getenv()) .setCygwin(false); WinPtyProcess process = (WinPtyProcess) builder.start(); printProcessOutput(process); assertEquals(workingDir1, trimTrailingSlash(process.getWorkingDirectory())); if (process.waitFor(2, TimeUnit.SECONDS)) { Assert.fail("Process exited unexpectedly"); } assertEquals(workingDir2, trimTrailingSlash(process.getWorkingDirectory())); }