From bd78a8522128877a4c712d862942d2ee064016c1 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Sat, 7 Dec 2019 21:30:40 +0100 Subject: [PATCH] Simplify socket REPL tests --- test/babashka/impl/socket_repl_test.clj | 70 ++++++++++++------------- 1 file changed, 34 insertions(+), 36 deletions(-) diff --git a/test/babashka/impl/socket_repl_test.clj b/test/babashka/impl/socket_repl_test.clj index 7b7f41ac..2edae210 100644 --- a/test/babashka/impl/socket_repl_test.clj +++ b/test/babashka/impl/socket_repl_test.clj @@ -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") )