Skip to content

Return last evaluated from load functions #984

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed
* Improved on the nREPL server exception messages by matching that of the REPL user friendly format (#968)
* Types created via `deftype` and `reify` may declare supertypes as abstract (taking precedence over true `abc.ABC` types) and specify their member list using `^:abstract-members` metadata (#942)
* Load functions (`load`, `load-file`, `load-reader`, etc) now return the value of the last form evaluated. (#984)

### Fixed
* Fixed inconsistent behavior with `basilisp.core/with` when the `body` contains more than one form (#981)

### Removed
* Removed `python-dateutil` and `readerwriterlock` as dependencies, switching to standard library components instead (#976)
* Removed `python-dateutil` and `readerwriterlock` as dependencies, switching to standard library components instead (#976)

### Other
* Run PyPy CI checks on Github Actions rather than CircleCI (#971)
Expand Down
17 changes: 9 additions & 8 deletions src/basilisp/core.lpy
Original file line number Diff line number Diff line change
Expand Up @@ -4516,12 +4516,13 @@
(.resolve)
(python/str))
ctx (basilisp.lang.compiler.CompilerContext. (or src "<Load Input>"))]
(doseq [form (seq (basilisp.lang.reader/read reader
*resolver*
*data-readers*))]
(basilisp.lang.compiler/compile-and-exec-form form
ctx
*ns*))))
(last
(for [form (seq (basilisp.lang.reader/read reader
*resolver*
*data-readers*))]
(basilisp.lang.compiler/compile-and-exec-form form
ctx
*ns*)))))

(defn load-file
"Read and evaluate the set of forms contained in the file located at ``path``.
Expand Down Expand Up @@ -4581,8 +4582,8 @@
:lpy:fn:`load-file` (or perhaps :lpy:fn:`load-reader` or :lpy:fn:`load-string`) to
this function."
[& paths]
(doseq [path (seq paths)]
(load-file (str (resolve-load-path path) ".lpy"))))
(last (for [path (seq paths)]
(load-file (str (resolve-load-path path) ".lpy")))))

(defn ^:inline load-string
"Read and evaluate the set of forms contained in the string ``s``\\."
Expand Down
19 changes: 16 additions & 3 deletions tests/basilisp/test_core_fns.lpy
Original file line number Diff line number Diff line change
Expand Up @@ -2302,13 +2302,15 @@

(deftest load-test

(let [load-dir-test "tests/basilisp/corpus"
load-dir-filepath (str load-dir-test "/core_load_1.lpy")]
(let [load-dir-test (str "tests/basilisp/corpus")
load-dir-filepath (str load-dir-test "/core_load_1.lpy")
load-dir-otherfile (str load-dir-test "/core_load_2.lpy")]
(try
(when (bio/exists? load-dir-test)
(shutil/rmtree load-dir-test))
(bio/make-parents load-dir-filepath)
(spit load-dir-filepath "(print :core-load-1)")
(spit load-dir-filepath "(print :core-load-1) :core-load-1")
(spit load-dir-otherfile "(print :core-load-2) :core-load-2")

(testing "relative load path"
(let [output (with-out-str (load "corpus/core-load-1"))]
Expand All @@ -2322,6 +2324,17 @@
(testing "non-existent load path"
(is (thrown? python/FileNotFoundError (load "corpus/core-load-99"))))

(testing "load multiple files"
(let [output (with-out-str (load "corpus/core-load-1"
"corpus/core-load-2"))]
(is (= ":core-load-1:core-load-2" output))))

(testing "return last form"
(let [output (load "corpus/core-load-1")]
(is (= :core-load-1 output)))
(let [output (load "corpus/core-load-1" "corpus/core-load-2")]
(is (= :core-load-2 output))))

(finally
(when (bio/exists? load-dir-test)
(shutil/rmtree load-dir-test))))))
Expand Down
Loading