From 55b1f7c5dc3979d858d5084c512fe723fcef988c Mon Sep 17 00:00:00 2001 From: Bob Date: Thu, 17 Oct 2024 09:24:02 -0400 Subject: [PATCH] Ignore missing deps file in `neil new` (#242) * - update ci task to run tests * update clojure latest version in dep test * ignore missing deps file when running new * add test that missing deps file doesn't throw * fix linting complaint * changelog * undo file checks in project ns * add deps file check in deps-new execution * remove tests that are no longer valid * undo whitespace change * add test for deps-file write from `new` - add a minimal template from a local root for use in the test - add test that checks the write to deps file, when it exists and when it doesn't --- CHANGELOG.md | 1 + bb.edn | 10 +++-- neil | 45 +++++++++++++------ src/babashka/neil/new.clj | 4 +- test-resources/new/minimal-template/deps.edn | 1 + .../resources/emptyish/emptyish/root/deps.edn | 1 + .../resources/emptyish/emptyish/template.edn | 1 + test/babashka/neil/dep_add_test.clj | 2 +- tests.clj | 25 +++++++++++ 9 files changed, 70 insertions(+), 20 deletions(-) create mode 100644 test-resources/new/minimal-template/deps.edn create mode 100644 test-resources/new/minimal-template/resources/emptyish/emptyish/root/deps.edn create mode 100644 test-resources/new/minimal-template/resources/emptyish/emptyish/template.edn diff --git a/CHANGELOG.md b/CHANGELOG.md index 5035a233..30ba9a90 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ See the [New Clojure project quickstart](https://blog.michielborkent.nl/new-cloj - [#230](https://github.com/babashka/neil/issues/230): neil dep upgrade inserts git/url into upgraded dep ([@teodorlu](https://github.com/teodorlu)) - [#237](https://github.com/babashka/neil/issues/230): more specific error reporting on invalid github token ([@teodorlu](https://github.com/teodorlu)) +- [#241](https://github.com/babashka/neil/issues/241): ignore missing deps file (instead of throwing) in `neil new` ([@bobisageek](https://github.com/bobisageek)) ## 0.3.66 diff --git a/bb.edn b/bb.edn index d971078c..cb5742c1 100644 --- a/bb.edn +++ b/bb.edn @@ -15,10 +15,12 @@ :task (do (log/info "bb run lint") (run 'lint) - (log/info "bb run tests") - (run 'tests) - (log/info "bb run tests-emacs") - (run 'tests-emacs))} + (log/info "bb run test:bb") + (run 'test:bb) + (log/info "bb run test:clj") + (run 'test:clj) + (log/info "bb run test:emacs") + (run 'test:emacs))} lint (shell "clj-kondo --lint .") gen-script {:doc "Build the neil script" diff --git a/neil b/neil index 343550de..aa8902b1 100755 --- a/neil +++ b/neil @@ -52,14 +52,17 @@ (defn url-encode [s] (URLEncoder/encode s "UTF-8")) -(def github-user (or (System/getenv "NEIL_GITHUB_USER") - (System/getenv "BABASHKA_NEIL_DEV_GITHUB_USER"))) -(def github-token (or (System/getenv "NEIL_GITHUB_TOKEN") - (System/getenv "BABASHKA_NEIL_DEV_GITHUB_TOKEN"))) +(def github-user-envvars ["NEIL_GITHUB_USER" "BABASHKA_NEIL_DEV_GITHUB_USER"]) +(def github-token-envvars ["NEIL_GITHUB_TOKEN" "BABASHKA_NEIL_DEV_GITHUB_TOKEN"]) + +(def github-user (some #(System/getenv %) github-user-envvars)) +(def github-token (some #(System/getenv %) github-token-envvars)) + +(def github-basic-auth-enabled? (and github-user github-token)) (def curl-opts (merge {:throw false} - (when (and github-user github-token) + (when github-basic-auth-enabled? {:basic-auth [github-user github-token]}))) (defn curl-get-json @@ -83,6 +86,17 @@ See neil's README for details.") nil #_(System/exit 1)) + (and (= 401 (:status response)) + (string/includes? url "api.github") + (string/includes? (:message parsed-body) "Bad credentials") + github-basic-auth-enabled?) + (binding [*out* *err*] + (println "Your neil github token is invalid or expired.") + (when-let [token-envvar (first (filter #(System/getenv %) github-token-envvars))] + (println "Please double check your " token-envvar " environment variable.")) + (println "See neil's README for more details.") + nil #_(System/exit 1)) + (contains? unexceptional-statuses (:status response)) parsed-body (= 404 (:status response)) @@ -271,6 +285,7 @@ (ns babashka.neil.new {:no-doc true} (:require + [babashka.fs :as fs] [babashka.neil.git :as git] [babashka.neil.project :as proj] [babashka.neil.utils :refer [req-resolve]] @@ -507,7 +522,8 @@ Examples: (when template-deps (deps-new-add-template-deps template-deps)) (when bb? (deps-new-set-classpath)) (deps-new-create create-opts) - (proj/assoc-project-meta! (assoc opts :dir dir :k :name :v project-name)))))))) + (when (fs/exists? (proj/resolve-deps-file dir (:deps-file opts))) + (proj/assoc-project-meta! (assoc opts :dir dir :k :name :v project-name))))))))) (ns babashka.neil.test (:require @@ -1309,12 +1325,13 @@ chmod +x bin/kaocha (r/assoc-in edn-nodes (conj path :mvn/version) version) git-sha? ;; multiple steps to force newlines - (-> edn-nodes - (r/assoc-in (conj path :git/url) git-url) - str - r/parse-string - (r/assoc-in (conj path :git/sha) version) - (r/update-in path r/dissoc :sha)) + (cond-> edn-nodes + (not (:omit-git-url opts)) (r/assoc-in (conj path :git/url) + git-url) + true str + true r/parse-string + true (r/assoc-in (conj path :git/sha) version) + true (r/update-in path r/dissoc :sha)) git-tag? ;; multiple steps to force newlines @@ -1468,7 +1485,6 @@ details on the search syntax."))) ;; => {:git/sha \"...\"} " [{:keys [lib current unstable]}] - ;; for now, just upgrade to stable versions (let [current (set/rename-keys current {:sha :git/sha :tag :git/tag})] (cond @@ -1583,7 +1599,8 @@ details on the search syntax."))) alias (assoc :alias alias) version (assoc :version version) tag (assoc :tag tag) - (and (not tag) sha) (assoc :sha sha))})))))) + (and (not tag) sha) (assoc :sha sha) + (not (:git/url current)) (assoc :omit-git-url true))})))))) (defn dep-upgrade [{:keys [opts]}] (when (or (:h opts) (:help opts)) diff --git a/src/babashka/neil/new.clj b/src/babashka/neil/new.clj index d64944eb..7d00c706 100644 --- a/src/babashka/neil/new.clj +++ b/src/babashka/neil/new.clj @@ -1,6 +1,7 @@ (ns babashka.neil.new {:no-doc true} (:require + [babashka.fs :as fs] [babashka.neil.git :as git] [babashka.neil.project :as proj] [babashka.neil.utils :refer [req-resolve]] @@ -237,4 +238,5 @@ Examples: (when template-deps (deps-new-add-template-deps template-deps)) (when bb? (deps-new-set-classpath)) (deps-new-create create-opts) - (proj/assoc-project-meta! (assoc opts :dir dir :k :name :v project-name)))))))) + (when (fs/exists? (proj/resolve-deps-file dir (:deps-file opts))) + (proj/assoc-project-meta! (assoc opts :dir dir :k :name :v project-name))))))))) diff --git a/test-resources/new/minimal-template/deps.edn b/test-resources/new/minimal-template/deps.edn new file mode 100644 index 00000000..de25d7c6 --- /dev/null +++ b/test-resources/new/minimal-template/deps.edn @@ -0,0 +1 @@ +{:paths ["resources"]} \ No newline at end of file diff --git a/test-resources/new/minimal-template/resources/emptyish/emptyish/root/deps.edn b/test-resources/new/minimal-template/resources/emptyish/emptyish/root/deps.edn new file mode 100644 index 00000000..9e26dfee --- /dev/null +++ b/test-resources/new/minimal-template/resources/emptyish/emptyish/root/deps.edn @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/test-resources/new/minimal-template/resources/emptyish/emptyish/template.edn b/test-resources/new/minimal-template/resources/emptyish/emptyish/template.edn new file mode 100644 index 00000000..9e26dfee --- /dev/null +++ b/test-resources/new/minimal-template/resources/emptyish/emptyish/template.edn @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/test/babashka/neil/dep_add_test.clj b/test/babashka/neil/dep_add_test.clj index 0b9fcf47..b4ec585b 100644 --- a/test/babashka/neil/dep_add_test.clj +++ b/test/babashka/neil/dep_add_test.clj @@ -6,4 +6,4 @@ (deftest latest-version-test (is (= "1.0.5" (neil/latest-stable-clojars-version 'hiccup/hiccup))) (is (= "2.0.0-RC3" (neil/latest-clojars-version 'hiccup/hiccup))) - (is (= "1.11.4" (neil/latest-stable-mvn-version 'org.clojure/clojure)))) + (is (= "1.12.0" (neil/latest-stable-mvn-version 'org.clojure/clojure)))) diff --git a/tests.clj b/tests.clj index ee9bb5f7..a0d4f4c6 100644 --- a/tests.clj +++ b/tests.clj @@ -204,6 +204,31 @@ (is (= (edn/read-string (slurp (fs/file "test-resources/new/my-scratch/deps.edn"))) (edn/read-string (slurp (fs/file (str target-dir "/deps.edn")))))))))) +(deftest deps-file-write-test + ; create a minimal template at a local root + (let [template-dir (fs/create-temp-dir {:prefix "neilnew"}) + _ (fs/copy-tree "test-resources/new/minimal-template/" template-dir)] + (testing "creating from a template with a deps.edn file adds a neil alias" + (let [target-dir (fs/create-temp-dir)] + (run-new-command "emptyish/emptyish" "foo/bar" (str target-dir) + "--local/root" (str template-dir) + "--overwrite" "true") + (is (str/includes? (slurp (fs/file target-dir "deps.edn")) ":neil")) + (fs/delete-tree target-dir))) + ; remove deps.edn from the template output + (fs/delete (fs/file template-dir "resources" "emptyish" "emptyish" "root" "deps.edn")) + (testing "creating from a template without deps.edn doesn't throw" + (let [target-dir (fs/create-temp-dir)] + (run-new-command "emptyish/emptyish" "foo/bar" (str target-dir) + "--local/root" (str template-dir) + "--overwrite" "true") + (is (not (fs/exists? (fs/file target-dir "deps.edn")))) + (fs/delete-tree target-dir))) + ; delete that template we created + (fs/delete-tree template-dir))) + + + (deftest clj-neil-new-test (let [{:keys [out err]} @(deps/clojure ["-M:neil" "new" "--help"] {:dir "tests-clj"