From 4db28b45e809bd40e68835ff2564bd326831e0cb Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Fri, 20 Mar 2020 15:12:15 +0100 Subject: [PATCH] [#293] reset ns after load-file --- src/babashka/main.clj | 10 +++++++--- test/babashka/main_test.clj | 10 +++++++--- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/babashka/main.clj b/src/babashka/main.clj index 3199d587..f7f5371e 100644 --- a/src/babashka/main.clj +++ b/src/babashka/main.clj @@ -29,7 +29,8 @@ [sci.impl.interpreter :refer [eval-string* eval-form]] [sci.impl.opts :as sci-opts] [sci.impl.unrestrict :refer [*unrestricted*]] - [sci.impl.vars :as vars]) + [sci.impl.vars :as vars] + [sci.impl.types :as sci-types]) (:gen-class)) (binding [*unrestricted* true] @@ -207,9 +208,12 @@ Everything after that is bound to *command-line-args*.")) (defn load-file* [sci-ctx f] (let [f (io/file f) - s (slurp f)] + s (slurp f) + prev-ns @vars/current-ns] (sci/with-bindings {vars/current-file (.getCanonicalPath f)} - (eval-string* sci-ctx s)))) + (try + (eval-string* sci-ctx s) + (finally (sci-types/setVal vars/current-ns prev-ns)))))) (defn start-socket-repl! [address ctx] (socket-repl/start-repl! address ctx) diff --git a/test/babashka/main_test.clj b/test/babashka/main_test.clj index 503db371..c4a90a47 100644 --- a/test/babashka/main_test.clj +++ b/test/babashka/main_test.clj @@ -118,9 +118,13 @@ (deftest load-file-test (let [tmp (java.io.File/createTempFile "script" ".clj")] - (spit tmp "(defn foo [x y] (+ x y)) (defn bar [x y] (* x y))") - (is (= "120\n" (test-utils/bb nil (format "(load-file \"%s\") (bar (foo 10 30) 3)" - (.getPath tmp))))))) + (spit tmp "(ns foo) (defn foo [x y] (+ x y)) (defn bar [x y] (* x y))") + (is (= "120\n" (test-utils/bb nil (format "(load-file \"%s\") (foo/bar (foo/foo 10 30) 3)" + (.getPath tmp))))) + (testing "namespace is restored after load file" + (is (= 'start-ns + (bb nil (format "(ns start-ns) (load-file \"%s\") (ns-name *ns*)" + (.getPath tmp)))))))) (deftest eval-test (is (= "120\n" (test-utils/bb nil "(eval '(do (defn foo [x y] (+ x y))