Skip to content

Commit

Permalink
move valid-deps? to specs ns and add explain-deps
Browse files Browse the repository at this point in the history
  • Loading branch information
puredanger committed Dec 27, 2023
1 parent c29a44c commit bb591e9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
8 changes: 2 additions & 6 deletions src/main/clojure/clojure/tools/deps.clj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
(: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]
Expand All @@ -36,9 +35,6 @@
(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 @@ -48,9 +44,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 (valid-deps? val)
(if (specs/valid-deps? val)
val
(throw (io-err "%s is not valid." f)))))
(throw (io-err (str "Error reading deps %s. " (specs/explain-deps val)) f)))))

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

Expand Down
29 changes: 29 additions & 0 deletions src/main/clojure/clojure/tools/deps/specs.clj
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,34 @@
(s/def :prep/ensure ::path)
(s/def :prep/fn symbol?)

(defn valid-deps?
"Determine whether the deps map is valid according to the specs"
[deps-map]
(s/valid? ::deps-map deps-map))

(defn explain-deps
"If a spec is invalid, return a message explaining why, suitable
for an error message"
[deps-map]
(let [err-data (s/explain-data ::deps-map deps-map)]
(if (nil? err-data)
"Failed spec, reason unknown"
(str "Failed spec, most likely cause:" (System/lineSeparator)
(first (clojure.string/split-lines (s/explain-str ::deps-map deps-map)))))))

(defn explain-deps
"If a spec is invalid, return a message explaining why, suitable
for an error message"
[deps-map]
(let [err-data (s/explain-data ::deps-map deps-map)]
(if (nil? err-data)
"Failed spec, reason unknown"
(let [problems (->> (::s/problems err-data)
(sort-by #(- (count (:in %))))
(sort-by #(- (count (:path %)))))
{:keys [path pred val reason via in]} (first problems)]
(str "Found: " (pr-str val) ", expected: " (if reason reason (s/abbrev pred)))))))

;; API

(s/fdef clojure.tools.deps/resolve-deps
Expand All @@ -137,6 +165,7 @@
:args (s/cat :libs ::lib-map, :paths ::paths, :classpath-args ::classpath-args)
:ret string?)


(comment
;; some scratch code to recursively check every deps.edn under
;; a root directory whether it's valid against the specs
Expand Down
5 changes: 3 additions & 2 deletions src/test/clojure/clojure/tools/deps/test_deps.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
[clojure.tools.deps.util :as util]
[clojure.tools.deps.extensions :as ext]
[clojure.tools.deps.extensions.faken :as fkn]
[clojure.tools.deps.specs :as specs]
[clojure.tools.deps.util.dir :as dir]
[clojure.tools.deps.util.maven :as mvn])
(:import
Expand Down Expand Up @@ -297,15 +298,15 @@

(deftest empty-nil-deps-is-valid
(testing "file exists but is empty (nil)"
(is (deps/valid-deps? nil))))
(is (specs/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))))))
(is (not (specs/valid-deps? invalid))))))

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

0 comments on commit bb591e9

Please sign in to comment.