[#532] Prefer .getAbsoluteFile over .getCanonicalFile for preserving

This commit is contained in:
Michiel Borkent 2020-08-14 11:39:48 +02:00 committed by GitHub
parent 6c5f7ef964
commit 9b68099543
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 25 additions and 12 deletions

View file

@ -568,7 +568,7 @@ Babashka sets the following system properties:
- `babashka.version`: the version string, e.g. `"1.2.0"`
- `babashka.main`: the `--main` argument
- `babashka.file`: the `--file` argument (normalized using `.getCanonicalPath`)
- `babashka.file`: the `--file` argument (normalized using `.getAbsolutePath`)
## `__name__ == "__main__"` pattern

2
sci

@ -1 +1 @@
Subproject commit 12ab6c489e0a2a6eba56c108e63b86c53287ab7e
Subproject commit 2e4d6e027688a9518b9cb25b9f26e274321ac8ab

View file

@ -19,8 +19,8 @@
(when (.exists f)
(if url?
;; manual conversion, faster than going through .toURI
(java.net.URL. "file" nil (.getCanonicalPath f))
{:file (.getCanonicalPath f)
(java.net.URL. "file" nil (.getAbsolutePath f))
{:file (.getAbsolutePath f)
:source (slurp f)}))))
resource-paths)))
@ -32,7 +32,7 @@
(if url?
;; manual conversion, faster than going through .toURI
(java.net.URL. "jar" nil
(str "file:" (.getCanonicalPath jar-file) "!/" path))
(str "file:" (.getAbsolutePath jar-file) "!/" path))
{:file path
:source (slurp (.getInputStream jar entry))})))
resource-paths)))

View file

@ -296,7 +296,7 @@ If neither -e, -f, or --socket-repl are specified, then the first argument that
(let [f (io/file f)
s (slurp f)]
(sci/with-bindings {sci/ns @sci/ns
sci/file (.getCanonicalPath f)}
sci/file (.getAbsolutePath f)}
(sci/eval-string* sci-ctx s))))
{:sci.impl/op :needs-ctx}))
@ -484,9 +484,9 @@ If neither -e, -f, or --socket-repl are specified, then the first argument that
(when uberscript (swap! uberscript-sources conj (:source res)))
res)))
_ (when file
(let [canonical-path (.getCanonicalPath (io/file file))]
(vars/bindRoot sci/file canonical-path)
(System/setProperty "babashka.file" canonical-path)))
(let [abs-path (.getAbsolutePath (io/file file))]
(vars/bindRoot sci/file abs-path)
(System/setProperty "babashka.file" abs-path)))
;; TODO: pull more of these values to compile time
opts {:aliases aliases
:namespaces (-> namespaces

View file

@ -1,8 +1,9 @@
(ns babashka.file-var-test
(:require
[babashka.test-utils :as tu]
[clojure.java.io :as io]
[clojure.string :as str]
[clojure.test :as t :refer [deftest is]]))
[clojure.test :as t :refer [deftest is testing]]))
(defn bb [input & args]
(apply tu/bb (when (some? input) (str input)) (map str args)))
@ -15,4 +16,9 @@
(is (str/ends-with? f1 "file_var_classpath.bb"))
(is (str/ends-with? f2 "loaded_by_file_var.bb"))
(is (str/ends-with? f3 "file_var.bb"))
(is (str/ends-with? f4 "file_var.bb"))))
(is (str/ends-with? f4 "file_var.bb")))
(testing "file var uses absolute path"
(is (str/includes?
(bb nil (io/file "test" ".." "test"
"babashka" "scripts" "simple_file_var.bb"))
".."))))

View file

@ -519,7 +519,13 @@
(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"))))))
(test-utils/bb nil (.getPath (io/file "test-resources" "babashka" "file_property2.clj")))))
(is (apply =
(bb nil (.getPath (io/file "test" "babashka" "scripts" "simple_file_var.bb")))))
(let [res (bb nil (.getPath (io/file "test" ".." "test" "babashka"
"scripts" "simple_file_var.bb")))]
(is (apply = res))
(is (str/includes? (first res) ".."))))
;;;; Scratch

View file

@ -0,0 +1 @@
[*file* (System/getProperty "babashka.file")]