Simplify socket REPL tests

This commit is contained in:
Michiel Borkent 2019-12-07 21:30:40 +01:00 committed by GitHub
parent 25b8eb761b
commit bd78a85221
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -4,19 +4,26 @@
[babashka.test-utils :as tu]
[clojure.java.shell :refer [sh]]
[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?
(str/includes?
(str/lower-case (System/getProperty "os.name"))
"mac"))
(set! *warn-on-reflection* true)
(defn socket-command [expr]
(let [expr (format "echo \"%s\n:repl/exit\" | nc 127.0.0.1 1666"
(pr-str expr))
ret (sh "bash" "-c"
expr)]
(:out ret)))
(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))
(when-not (str/includes? l expected)
(recur))))
(str sw)))
(deftest socket-repl-test
(try
@ -35,36 +42,19 @@
(while (not (zero? (:exit
(sh "bash" "-c"
"lsof -t -i:1666"))))))
(is (str/includes? (socket-command '(+ 1 2 3))
"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=> ")))
(is (socket-command '(+ 1 2 3) "user=> 6"))
(testing "*in*"
(is (str/includes? (socket-command "*in*")
"[1 2 3]")))
(is (socket-command "*in*" "[1 2 3]")))
(testing "*command-line-args*"
(is (str/includes? (socket-command '*command-line-args*)
"\"a\" \"b\" \"c\"")))
(is (socket-command '*command-line-args* "\"a\" \"b\" \"c\"")))
(testing "&env"
(socket-command '(defmacro bindings [] (mapv #(list 'quote %) (keys &env))))
(socket-command '(defn bar [x y z] (bindings)))
(is (str/includes? (socket-command '(bar 1 2 3))
"[x y z]")))
(socket-command '(defmacro bindings [] (mapv #(list 'quote %) (keys &env))) "")
(socket-command '(defn bar [x y z] (bindings)) "")
(is (socket-command '(bar 1 2 3) "[x y z]")))
(testing "reader conditionals"
(is (str/includes? (let [ret (sh "bash" "-c"
(format "echo \"%s\n:repl/exit\" | nc 127.0.0.1 1666"
"#?(:bb 1337 :clj 8888)"))]
(:out ret))
"1337")))
(is (socket-command "#?(:bb 1337 :clj 8888)" "1337")))
(testing "*1, *2, *3, *e"
(is (= 2 (count (re-seq #"1\n" (let [ret (sh "bash" "-c"
(format "echo \"%s\n*1\n:repl/exit\" | nc 127.0.0.1 1666"
"1"))]
(:out ret)))))))
(is (socket-command "1\n*1" "1")))
(finally
(if tu/jvm?
(stop-repl!)
@ -75,4 +65,12 @@
(comment
(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")
)