1. Miki Tebeka
  2. popen


Miki Tebeka  committed 2e53269

wait->join, use -> and optional arguments

  • Participants
  • Parent commits bcd7a03
  • Branches default

Comments (0)

Files changed (2)

File src/popen.clj

View file
 (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)))

File test/popen_test.clj

View file
   (: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)))))