diff --git a/src/babashka/main.clj b/src/babashka/main.clj index f5307886..f34237f4 100644 --- a/src/babashka/main.clj +++ b/src/babashka/main.clj @@ -479,7 +479,10 @@ If neither -e, -f, or --socket-repl are specified, then the first argument that (let [res (cp/source-for-namespace loader namespace nil)] (when uberscript (swap! uberscript-sources conj (:source res))) res))) - _ (when file (vars/bindRoot sci/file (.getCanonicalPath (io/file file)))) + _ (when file + (let [canonical-path (.getCanonicalPath (io/file file))] + (vars/bindRoot sci/file canonical-path) + (System/setProperty "babashka.file" canonical-path))) ;; TODO: pull more of these values to compile time opts {:aliases aliases :namespaces (-> namespaces diff --git a/test-resources/babashka/file_property1.clj b/test-resources/babashka/file_property1.clj new file mode 100644 index 00000000..5c93a114 --- /dev/null +++ b/test-resources/babashka/file_property1.clj @@ -0,0 +1,6 @@ +(ns babashka.file-property1 + (:require [clojure.java.io :as io])) + +(prn (= *file* (System/getProperty "babashka.file"))) + +(load-file (.getPath (io/file "test-resources" "babashka" "file_property2.clj"))) diff --git a/test-resources/babashka/file_property2.clj b/test-resources/babashka/file_property2.clj new file mode 100644 index 00000000..39e6b0f2 --- /dev/null +++ b/test-resources/babashka/file_property2.clj @@ -0,0 +1,3 @@ +(ns babashka.file-property2) + +(prn (= *file* (System/getProperty "babashka.file"))) diff --git a/test/babashka/main_test.clj b/test/babashka/main_test.clj index f708e38a..2f516f72 100644 --- a/test/babashka/main_test.clj +++ b/test/babashka/main_test.clj @@ -486,11 +486,10 @@ (deftest data-diff-test (is (= [[nil 1] [nil 2] [1 nil 2]] (bb nil "(require '[clojure.data :as d]) (d/diff [1 1 2] [1 2 2])")))) -(deftest version-test +(deftest version-property-test (is (= "true\ntrue\nfalse\n" (test-utils/bb nil (.getPath (io/file "test-resources" "babashka" "version.clj")))))) - (defmethod clojure.test/assert-expr 'working? [msg form] (let [body (next form)] `(do ~@body @@ -503,6 +502,12 @@ (testing "bb executes the empty expression and doesn't start a REPL" (is (working? (test-utils/bb nil "-e" ""))))) +(deftest file-property-test + (is (= "true\nfalse\n" + (test-utils/bb nil (.getPath (io/file "test-resources" "babashka" "file_property1.clj"))))) + (is (= "true\n" + (test-utils/bb nil (.getPath (io/file "test-resources" "babashka" "file_property2.clj")))))) + ;;;; Scratch (comment