2019-08-31 18:17:36 +00:00
|
|
|
(ns babashka.impl.socket-repl-test
|
|
|
|
|
(:require
|
|
|
|
|
[babashka.impl.socket-repl :refer [start-repl! stop-repl!]]
|
|
|
|
|
[babashka.test-utils :as tu]
|
2020-04-03 21:51:54 +00:00
|
|
|
[clojure.java.io :as io]
|
2019-08-31 18:17:36 +00:00
|
|
|
[clojure.java.shell :refer [sh]]
|
|
|
|
|
[clojure.string :as str]
|
2019-12-07 20:30:40 +00:00
|
|
|
[clojure.test :as t :refer [deftest is testing]]
|
2019-12-24 09:01:32 +00:00
|
|
|
[sci.impl.opts :refer [init]]))
|
2019-09-01 19:13:37 +00:00
|
|
|
|
2019-12-07 20:30:40 +00:00
|
|
|
(set! *warn-on-reflection* true)
|
2019-08-31 18:17:36 +00:00
|
|
|
|
2019-12-07 20:30:40 +00:00
|
|
|
(defn socket-command [expr expected]
|
|
|
|
|
(with-open [socket (java.net.Socket. "127.0.0.1" 1666)
|
|
|
|
|
reader (io/reader socket)
|
|
|
|
|
sw (java.io.StringWriter.)
|
|
|
|
|
writer (io/writer socket)]
|
|
|
|
|
(binding [*out* writer]
|
|
|
|
|
(println (str expr))
|
|
|
|
|
(println ":repl/exit\n"))
|
|
|
|
|
(loop []
|
|
|
|
|
(when-let [l (.readLine ^java.io.BufferedReader reader)]
|
|
|
|
|
(binding [*out* sw]
|
|
|
|
|
(println l))
|
2019-12-07 22:10:34 +00:00
|
|
|
(recur)))
|
2019-12-07 21:17:26 +00:00
|
|
|
(let [s (str sw)]
|
|
|
|
|
(is (str/includes? s expected)
|
|
|
|
|
(format "\"%s\" does not contain \"%s\""
|
|
|
|
|
s expected))
|
|
|
|
|
s)))
|
2019-09-02 10:50:37 +00:00
|
|
|
|
2019-08-31 18:17:36 +00:00
|
|
|
(deftest socket-repl-test
|
2019-09-02 10:50:37 +00:00
|
|
|
(try
|
|
|
|
|
(if tu/jvm?
|
2019-12-24 09:01:32 +00:00
|
|
|
(start-repl! "0.0.0.0:1666" (init {:bindings {'*command-line-args*
|
|
|
|
|
["a" "b" "c"]}
|
|
|
|
|
:env (atom {})
|
|
|
|
|
:features #{:bb}}))
|
2019-09-02 10:50:37 +00:00
|
|
|
(future
|
|
|
|
|
(sh "bash" "-c"
|
|
|
|
|
"echo '[1 2 3]' | ./bb --socket-repl 0.0.0.0:1666 a b c")))
|
|
|
|
|
;; wait for server to be available
|
|
|
|
|
(when tu/native?
|
|
|
|
|
(while (not (zero? (:exit
|
|
|
|
|
(sh "bash" "-c"
|
|
|
|
|
"lsof -t -i:1666"))))))
|
2019-12-07 22:10:34 +00:00
|
|
|
(is (socket-command "(+ 1 2 3)" "user=> 6"))
|
2019-09-02 10:50:37 +00:00
|
|
|
(testing "*command-line-args*"
|
2019-12-07 20:30:40 +00:00
|
|
|
(is (socket-command '*command-line-args* "\"a\" \"b\" \"c\"")))
|
2019-10-20 16:21:57 +00:00
|
|
|
(testing "&env"
|
2019-12-07 22:10:34 +00:00
|
|
|
(socket-command "(defmacro bindings [] (mapv #(list 'quote %) (keys &env)))" "bindings")
|
|
|
|
|
(socket-command "(defn bar [x y z] (bindings))" "bar")
|
|
|
|
|
(is (socket-command "(bar 1 2 3)" "[x y z]")))
|
2019-10-26 21:53:10 +00:00
|
|
|
(testing "reader conditionals"
|
2019-12-07 20:30:40 +00:00
|
|
|
(is (socket-command "#?(:bb 1337 :clj 8888)" "1337")))
|
2019-10-28 10:22:12 +00:00
|
|
|
(testing "*1, *2, *3, *e"
|
2019-12-07 20:30:40 +00:00
|
|
|
(is (socket-command "1\n*1" "1")))
|
2020-01-19 19:01:49 +00:00
|
|
|
(testing "*ns*"
|
|
|
|
|
(is (socket-command "(ns foo.bar) (ns-name *ns*)" "foo.bar")))
|
2019-09-02 10:50:37 +00:00
|
|
|
(finally
|
|
|
|
|
(if tu/jvm?
|
|
|
|
|
(stop-repl!)
|
|
|
|
|
(sh "bash" "-c"
|
|
|
|
|
"kill -9 $(lsof -t -i:1666)")))))
|
2019-08-31 18:17:36 +00:00
|
|
|
|
|
|
|
|
;;;; Scratch
|
|
|
|
|
|
|
|
|
|
(comment
|
2019-09-01 19:13:37 +00:00
|
|
|
(socket-repl-test)
|
2019-12-07 22:10:34 +00:00
|
|
|
(dotimes [_ 1000]
|
|
|
|
|
(t/run-tests))
|
2019-12-07 20:30:40 +00:00
|
|
|
(stop-repl!)
|
2019-12-20 22:51:24 +00:00
|
|
|
(start-repl! "0.0.0.0:1666" {:bindings {(with-meta '*input*
|
2019-12-07 20:30:40 +00:00
|
|
|
{:sci/deref! true})
|
|
|
|
|
(delay [1 2 3])
|
|
|
|
|
'*command-line-args*
|
|
|
|
|
["a" "b" "c"]}
|
|
|
|
|
:env (atom {})})
|
|
|
|
|
(socket-command "(+ 1 2 3)" "6")
|
2019-08-31 18:17:36 +00:00
|
|
|
)
|