Simplify socket REPL tests
This commit is contained in:
parent
25b8eb761b
commit
bd78a85221
1 changed files with 34 additions and 36 deletions
|
|
@ -4,19 +4,26 @@
|
||||||
[babashka.test-utils :as tu]
|
[babashka.test-utils :as tu]
|
||||||
[clojure.java.shell :refer [sh]]
|
[clojure.java.shell :refer [sh]]
|
||||||
[clojure.string :as str]
|
[clojure.string :as str]
|
||||||
[clojure.test :as t :refer [deftest is testing]]))
|
[clojure.test :as t :refer [deftest is testing]]
|
||||||
|
[clojure.java.io :as io]))
|
||||||
|
|
||||||
(def mac?
|
(set! *warn-on-reflection* true)
|
||||||
(str/includes?
|
|
||||||
(str/lower-case (System/getProperty "os.name"))
|
|
||||||
"mac"))
|
|
||||||
|
|
||||||
(defn socket-command [expr]
|
(defn socket-command [expr expected]
|
||||||
(let [expr (format "echo \"%s\n:repl/exit\" | nc 127.0.0.1 1666"
|
(with-open [socket (java.net.Socket. "127.0.0.1" 1666)
|
||||||
(pr-str expr))
|
reader (io/reader socket)
|
||||||
ret (sh "bash" "-c"
|
sw (java.io.StringWriter.)
|
||||||
expr)]
|
writer (io/writer socket)]
|
||||||
(:out ret)))
|
(binding [*out* writer]
|
||||||
|
(println (str expr))
|
||||||
|
(println ":repl/exit\n"))
|
||||||
|
(loop []
|
||||||
|
(when-let [l (.readLine ^java.io.BufferedReader reader)]
|
||||||
|
(binding [*out* sw]
|
||||||
|
(println l))
|
||||||
|
(when-not (str/includes? l expected)
|
||||||
|
(recur))))
|
||||||
|
(str sw)))
|
||||||
|
|
||||||
(deftest socket-repl-test
|
(deftest socket-repl-test
|
||||||
(try
|
(try
|
||||||
|
|
@ -35,36 +42,19 @@
|
||||||
(while (not (zero? (:exit
|
(while (not (zero? (:exit
|
||||||
(sh "bash" "-c"
|
(sh "bash" "-c"
|
||||||
"lsof -t -i:1666"))))))
|
"lsof -t -i:1666"))))))
|
||||||
(is (str/includes? (socket-command '(+ 1 2 3))
|
(is (socket-command '(+ 1 2 3) "user=> 6"))
|
||||||
"user=> 6"))
|
|
||||||
(testing "ctrl-d exits normally, doesn't print nil"
|
|
||||||
(is (str/ends-with? (:out (sh "bash" "-c"
|
|
||||||
(if mac? ;; mac doesn't support -q
|
|
||||||
"echo \"(inc 1336)\" | nc 127.0.0.1 1666"
|
|
||||||
"echo \"(inc 1336)\" | nc -q 1 127.0.0.1 1666")))
|
|
||||||
"1337\nuser=> ")))
|
|
||||||
(testing "*in*"
|
(testing "*in*"
|
||||||
(is (str/includes? (socket-command "*in*")
|
(is (socket-command "*in*" "[1 2 3]")))
|
||||||
"[1 2 3]")))
|
|
||||||
(testing "*command-line-args*"
|
(testing "*command-line-args*"
|
||||||
(is (str/includes? (socket-command '*command-line-args*)
|
(is (socket-command '*command-line-args* "\"a\" \"b\" \"c\"")))
|
||||||
"\"a\" \"b\" \"c\"")))
|
|
||||||
(testing "&env"
|
(testing "&env"
|
||||||
(socket-command '(defmacro bindings [] (mapv #(list 'quote %) (keys &env))))
|
(socket-command '(defmacro bindings [] (mapv #(list 'quote %) (keys &env))) "")
|
||||||
(socket-command '(defn bar [x y z] (bindings)))
|
(socket-command '(defn bar [x y z] (bindings)) "")
|
||||||
(is (str/includes? (socket-command '(bar 1 2 3))
|
(is (socket-command '(bar 1 2 3) "[x y z]")))
|
||||||
"[x y z]")))
|
|
||||||
(testing "reader conditionals"
|
(testing "reader conditionals"
|
||||||
(is (str/includes? (let [ret (sh "bash" "-c"
|
(is (socket-command "#?(:bb 1337 :clj 8888)" "1337")))
|
||||||
(format "echo \"%s\n:repl/exit\" | nc 127.0.0.1 1666"
|
|
||||||
"#?(:bb 1337 :clj 8888)"))]
|
|
||||||
(:out ret))
|
|
||||||
"1337")))
|
|
||||||
(testing "*1, *2, *3, *e"
|
(testing "*1, *2, *3, *e"
|
||||||
(is (= 2 (count (re-seq #"1\n" (let [ret (sh "bash" "-c"
|
(is (socket-command "1\n*1" "1")))
|
||||||
(format "echo \"%s\n*1\n:repl/exit\" | nc 127.0.0.1 1666"
|
|
||||||
"1"))]
|
|
||||||
(:out ret)))))))
|
|
||||||
(finally
|
(finally
|
||||||
(if tu/jvm?
|
(if tu/jvm?
|
||||||
(stop-repl!)
|
(stop-repl!)
|
||||||
|
|
@ -75,4 +65,12 @@
|
||||||
|
|
||||||
(comment
|
(comment
|
||||||
(socket-repl-test)
|
(socket-repl-test)
|
||||||
|
(stop-repl!)
|
||||||
|
(start-repl! "0.0.0.0:1666" {:bindings {(with-meta '*in*
|
||||||
|
{:sci/deref! true})
|
||||||
|
(delay [1 2 3])
|
||||||
|
'*command-line-args*
|
||||||
|
["a" "b" "c"]}
|
||||||
|
:env (atom {})})
|
||||||
|
(socket-command "(+ 1 2 3)" "6")
|
||||||
)
|
)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue