Permalink
Browse files

Add polymorphic handling of :in and tests.

Support for sequences, strings, and files.
  • Loading branch information...
1 parent eafca30 commit ab5173c25e85207f462b25ec4aac4c75e747bb5e @Raynes committed Oct 1, 2012
Showing with 51 additions and 4 deletions.
  1. +32 −3 src/conch/sh.clj
  2. +15 −1 test/conch/sh_test.clj
  3. +4 −0 test/testfiles/inputdata
View
@@ -22,6 +22,37 @@
(doseq [buffer (get proc k)]
(f buffer proc)))))
+(defprotocol Drinkable
+ (drink [this proc]))
+
+(extend-type clojure.lang.ISeq
+ Drinkable
+ (drink [s proc]
+ (with-open [writer (java.io.PrintWriter. (:in proc))]
+ (binding [*out* writer]
+ (doseq [x s]
+ (println x))))
+ (conch/done proc)))
+
+(extend-type java.io.File
+ Drinkable
+ (drink [f proc]
+ (conch/feed-from proc (io/reader f))
+ (conch/done proc)))
+
+(extend-type java.lang.String
+ Drinkable
+ (drink [s proc]
+ (conch/feed-from-string proc s)
+ (conch/done proc)))
+
+(defn get-drunk [item proc]
+ (drink
+ (if (coll? item)
+ (seq item)
+ item)
+ proc))
+
(defn seqify? [options k]
(let [seqify (:seq options)]
(or (= seqify k)
@@ -114,9 +145,7 @@
exit-code (future (if timeout
(conch/exit-code proc timeout)
(conch/exit-code proc)))]
- (when in
- (conch/feed-from-string proc (:in options)) ;; This will become more sophisticated.
- (conch/done proc))
+ (when in (future (get-drunk in proc)))
(let [proc-out (redirect out options :out proc)
proc-err (redirect err options :err proc)]
(when (future? proc-out) @proc-out)
View
@@ -38,4 +38,18 @@
(testing "Process runs in a future"
(let [f (sh/with-programs [echo] (echo "hi" {:background true}))]
(is (future? f))
- (is (= "hi\n" @f)))))
+ (is (= "hi\n" @f)))))
+
+(deftest pipe-test
+ (sh/let-programs [errecho "test/testfiles/errecho"]
+ (with-programs [echo cat]
+ (testing "Can pipe the output of one command as the input to another."
+ (is (= "hi" (cat {:in (echo "hi" {:seq true})})))
+ (is (= "hi" (cat {:in (errecho "hi" {:seq :err})})))))))
+
+(deftest in-test
+ (with-programs [echo cat]
+ (testing "Can input from string"
+ (is (= "hi" (cat {:in "hi"}))))
+ (testing "Can input a seq"
+ (is (= "hi\nthere" (cat {:in ["hi" "there"]}))))))
View
@@ -0,0 +1,4 @@
+we
+wear
+short
+shorts

0 comments on commit ab5173c

Please sign in to comment.