From a44f07a665edabbc5ce8e5e3e1f31ba37c6e28a1 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Sat, 20 Mar 2021 13:13:28 +0100 Subject: [PATCH] test --- src/babashka/main.clj | 33 ++++++++++++++++++++++++--------- test/babashka/bb_edn_test.clj | 24 ++++++++++++++---------- test/babashka/test_utils.clj | 15 ++++++++++++--- 3 files changed, 50 insertions(+), 22 deletions(-) diff --git a/src/babashka/main.clj b/src/babashka/main.clj index 59d50a57..2e1278ee 100644 --- a/src/babashka/main.clj +++ b/src/babashka/main.clj @@ -362,18 +362,14 @@ Use -- to separate script command line args from bb command line args. (println msg) {:exec (fn [] [nil exit])})) -(def ^:dynamic *bb-edn* - (delay - (let [bb-edn-file (or (System/getenv "BABASHKA_EDN") - "bb.edn")] - (when (fs/exists? bb-edn-file) - (edn/read-string (slurp bb-edn-file)))))) +(def bb-edn + (atom nil)) (defn parse-opts [options] (let [fst (when options (first options)) key? (when fst (str/starts-with? fst ":")) k (when key? (keyword (subs fst 1))) - bb-edn (when k @*bb-edn*) + bb-edn (when k @bb-edn) tasks (when (and k bb-edn) (:tasks bb-edn)) user-task (when tasks (get tasks k))] @@ -516,6 +512,21 @@ Use -- to separate script command line args from bb command line args. opts-map))] opts)))) +(defn resolve-task [task {:keys [:command-line-args]}] + (case (:task/type task) + :babashka + (let [cmd-line-args (get task :args)] + (parse-opts (seq (map str (concat cmd-line-args command-line-args))))) + :shell + (let [args (get task :args) + args (into (vec args) command-line-args)] + {:exec (fn [] + [nil + (-> (p/process args {:inherit true}) + p/check + :exit)])}) + (error (str "No such task: " (:task/type task)) 1))) + (def should-load-inits? "if true, then we should still load preloads and user.clj" (volatile! true)) @@ -739,8 +750,12 @@ Use -- to separate script command line args from bb command line args. [& args] (handle-pipe!) (handle-sigint!) - (when-let [bb-edn @*bb-edn*] - (deps/add-deps bb-edn)) + (let [bb-edn-file (or (System/getenv "BABASHKA_EDN") + "bb.edn")] + (when (fs/exists? bb-edn-file) + (let [edn (edn/read-string (slurp bb-edn-file))] + (reset! bb-edn edn) + (deps/add-deps edn)))) (if-let [dev-opts (System/getenv "BABASHKA_DEV")] (let [{:keys [:n]} (if (= "true" dev-opts) {:n 1} (edn/read-string dev-opts)) diff --git a/test/babashka/bb_edn_test.clj b/test/babashka/bb_edn_test.clj index f81bae88..aa193920 100644 --- a/test/babashka/bb_edn_test.clj +++ b/test/babashka/bb_edn_test.clj @@ -1,22 +1,26 @@ (ns babashka.bb-edn-test {:clj-kondo/config '{:linters {:unresolved-symbol {:exclude [working?]}}}} (:require + [babashka.fs :as fs] [babashka.test-utils :as test-utils] [clojure.edn :as edn] - [clojure.java.io :as io] - [clojure.java.shell :refer [sh]] - [clojure.string :as str] - [clojure.test :as test :refer [deftest is testing *report-counters*]] - [flatland.ordered.map :refer [ordered-map]] - [sci.core :as sci]) - ) + [clojure.test :as test :refer [deftest is]])) -(defn bb [input & args] +(defn bb [& args] (edn/read-string {:readers *data-readers* :eof nil} - (apply test-utils/bb (when (some? input) (str input)) (map str args)))) + (apply test-utils/bb nil (map str args)))) (deftest foobar-test - (prn :foobar)) + (let [temp-dir (fs/create-temp-dir) + temp-file (fs/create-file (fs/path temp-dir "temp-file.txt")) + bb-edn-file (fs/file temp-dir "bb.edn") + bb-edn `{:tasks {:clean {:task/type :shell + :args ["rm" ~(str temp-file)]}}}] + (spit bb-edn-file bb-edn) + (is (fs/exists? temp-file)) + (binding [test-utils/*bb-edn-path* (str bb-edn-file)] + (bb :clean)) + (is (not (fs/exists? temp-file))))) diff --git a/test/babashka/test_utils.clj b/test/babashka/test_utils.clj index 9c64e9ae..5c6f40cf 100644 --- a/test/babashka/test_utils.clj +++ b/test/babashka/test_utils.clj @@ -3,14 +3,19 @@ [babashka.impl.classpath :as cp] [babashka.main :as main] [babashka.process :as p] + [clojure.edn :as edn] [sci.core :as sci] [sci.impl.vars :as vars])) (set! *warn-on-reflection* true) +(def ^:dynamic *bb-edn-path* nil) + (defn bb-jvm [input-or-opts & args] (reset! cp/cp-state nil) (reset! main/env {}) + (when-let [path *bb-edn-path*] + (reset! main/bb-edn (edn/read-string (slurp path)))) (let [os (java.io.StringWriter.) es (if-let [err (:err input-or-opts)] err (java.io.StringWriter.)) @@ -44,9 +49,13 @@ (defn bb-native [input & args] (let [res (p/process (into ["./bb"] args) - {:in input - :out :string - :err :string}) + (cond-> {:in input + :out :string + :err :string} + *bb-edn-path* + (assoc + :env (assoc (into {} (System/getenv)) + "BABASHKA_EDN" *bb-edn-path*)))) res (deref res) exit (:exit res) error? (pos? exit)]