Miki Tebeka avatar Miki Tebeka committed 2e53269

wait->join, use -> and optional arguments

Comments (0)

Files changed (2)

 (ns popen
   (require [clojure.java.io :as io]))
 
-(defn popen [& args]
-  (let [builder (ProcessBuilder. args)]
-    (.start builder)))
+(defn popen [args &{:keys [redirect dir]}]
+  (-> (ProcessBuilder. args)
+    (.directory (if (nil? dir) nil (io/file dir)))
+    (.redirectErrorStream (boolean redirect))
+    (.start)))
 
 (defprotocol Popen
   (stdout [this] "Process standard output (read from)")
   (stdin [this] "Process standard input (write to)")
-  (stderr [this] "Process standard error (write to)")
-  (wait [this] "Wait for process to terminate, return exit code")
+  (stderr [this] "Process standard error (read from)")
+  (join [this] "Wait for process to terminate, return exit code")
   (exit-code [this] "Process exit code (will wait for termination)")
   (running? [this] "Return true if process still running")
   (kill [this] "Kill process"))
   (stdout [this] (io/reader (.getInputStream this)))
   (stdin [this] (io/writer (.getOutputStream this)))
   (stderr [this] (io/reader (.getErrorStream this)))
-  (wait [this] (.waitFor this))
+  (join [this] (.waitFor this))
   (exit-code [this] (exit-code- this))
   (running? [this] (nil? (exit-code- this)))
   (kill [this] (.destroy this)))

test/popen_test.clj

   (:use [clojure.test]))
 
 (deftest popen-test
-  (is 1 1))
+  (let [p (popen/popen ["ls" "/tmp"])]
+    (is (instance? Process p))
+    (is (not (nil? (popen/stdout p))))
+    (is (not (nil? (popen/stderr p))))
+    (is (zero? (popen/join p)))
+    (is (zero? (popen/exit-code p)))
+    (is (nil? (popen/kill p)))))
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.