Skip to content

Commit

Permalink
Ignore missing deps file in neil new (#242)
Browse files Browse the repository at this point in the history
* - 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
  • Loading branch information
bobisageek authored Oct 17, 2024
1 parent 6728367 commit 55b1f7c
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 6 additions & 4 deletions bb.edn
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
45 changes: 31 additions & 14 deletions neil
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
Expand Down Expand Up @@ -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]]
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down
4 changes: 3 additions & 1 deletion src/babashka/neil/new.clj
Original file line number Diff line number Diff line change
@@ -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]]
Expand Down Expand Up @@ -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)))))))))
1 change: 1 addition & 0 deletions test-resources/new/minimal-template/deps.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{:paths ["resources"]}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
2 changes: 1 addition & 1 deletion test/babashka/neil/dep_add_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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))))
25 changes: 25 additions & 0 deletions tests.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 55b1f7c

Please sign in to comment.