Skip to content

Commit

Permalink
TDEPS-238 & TDEPS-239
Browse files Browse the repository at this point in the history
* Validate deps.edn with existing spec
* Empty deps.edn is valid
* Added tests
  • Loading branch information
JarrodCTaylor committed Dec 21, 2023
1 parent f7ae85d commit 355d278
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
9 changes: 7 additions & 2 deletions src/main/clojure/clojure/tools/deps.clj
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
(:require
[clojure.java.io :as jio]
[clojure.set :as set]
[clojure.spec.alpha :as s]
[clojure.string :as str]
[clojure.tools.deps.util.concurrent :as concurrent]
[clojure.tools.deps.util.dir :as dir]
[clojure.tools.deps.util.io :as io]
[clojure.tools.deps.util.session :as session]
[clojure.tools.deps.extensions :as ext]
[clojure.tools.deps.specs :as specs]
[clojure.walk :as walk])
(:import
[clojure.lang EdnReader$ReaderException]
Expand All @@ -34,6 +36,9 @@
(let [path (.getAbsolutePath f)]
(ex-info (format fmt path) {:path path})))

(defn valid-deps? [m]
(s/valid? ::specs/deps-map m))

(defn- slurp-edn-map
"Read the file specified by the path-segments, slurp it, and read it as edn."
[^File f]
Expand All @@ -43,9 +48,9 @@
(if (str/starts-with? (.getMessage t) "EOF while reading")
(throw (io-err "Error reading edn, delimiter unmatched (%s)" f))
(throw (io-err (str "Error reading edn. " (.getMessage t) " (%s)") f)))))]
(if (map? val)
(if (valid-deps? val)
val
(throw (io-err "Expected edn map in: %s" f)))))
(throw (io-err "%s is not valid." f)))))

;; all this canonicalization is deprecated and will eventually be removed

Expand Down
6 changes: 3 additions & 3 deletions src/main/clojure/clojure/tools/deps/specs.clj
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@
(s/def ::paths :aliased/paths)
(s/def ::deps (s/map-of ::lib ::coord))
(s/def ::aliases (s/map-of ::alias any?))
(s/def ::deps-map (s/keys
:opt-un [::paths ::deps ::aliases]
:opt [:mvn/repos :mvn/local-repo :tools/usage :deps/prep-lib]))
(s/def ::deps-map (s/nilable (s/keys
:opt-un [::paths ::deps ::aliases]
:opt [:mvn/repos :mvn/local-repo :tools/usage :deps/prep-lib])))

;; lib map
;; a map of lib to resolved coordinate (a coord with a ::path) and dependent info
Expand Down
12 changes: 12 additions & 0 deletions src/test/clojure/clojure/tools/deps/test_deps.clj
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,18 @@
(let [b (deps/create-basis {:user nil :project adeps})]
(is (contains? (:classpath b) (.getAbsolutePath (jio/file bdir "src"))))))))

(deftest empty-nil-deps-is-valid
(testing "file exists but is empty (nil)"
(is (deps/valid-deps? nil))))

(deftest TDEPS-238
(testing "deps are invalid with extra nested vector in :exclusions"
(let [invalid {:deps
{'org.clojure/core.memoize
{:mvn/version "1.0.257"
:exclusions [['org.clojure/data.priority-map]]}}}]
(is (not (deps/valid-deps? invalid))))))

(comment
(test-local-root-relative-to-project-deps)
)
Expand Down

0 comments on commit 355d278

Please sign in to comment.