From 355d278e40c8b1527eb14d3bf63087d17ec25fd2 Mon Sep 17 00:00:00 2001 From: JarrodCTaylor Date: Thu, 21 Dec 2023 10:28:07 -0600 Subject: [PATCH] TDEPS-238 & TDEPS-239 * Validate deps.edn with existing spec * Empty deps.edn is valid * Added tests --- src/main/clojure/clojure/tools/deps.clj | 9 +++++++-- src/main/clojure/clojure/tools/deps/specs.clj | 6 +++--- src/test/clojure/clojure/tools/deps/test_deps.clj | 12 ++++++++++++ 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/main/clojure/clojure/tools/deps.clj b/src/main/clojure/clojure/tools/deps.clj index 9dbdfaf4..705216ed 100644 --- a/src/main/clojure/clojure/tools/deps.clj +++ b/src/main/clojure/clojure/tools/deps.clj @@ -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] @@ -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] @@ -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 diff --git a/src/main/clojure/clojure/tools/deps/specs.clj b/src/main/clojure/clojure/tools/deps/specs.clj index 2d4a2310..fb805a20 100644 --- a/src/main/clojure/clojure/tools/deps/specs.clj +++ b/src/main/clojure/clojure/tools/deps/specs.clj @@ -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 diff --git a/src/test/clojure/clojure/tools/deps/test_deps.clj b/src/test/clojure/clojure/tools/deps/test_deps.clj index c9a7a9b2..622cbb0f 100644 --- a/src/test/clojure/clojure/tools/deps/test_deps.clj +++ b/src/test/clojure/clojure/tools/deps/test_deps.clj @@ -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) )