Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
namenu committed Oct 2, 2023
1 parent 9306434 commit dd00761
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 20 deletions.
32 changes: 18 additions & 14 deletions src/namenu/deps_diff.clj
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,20 @@
[data _]
(output/cli data))

(defn diff*
[{:keys [base target aliases]}]
(let [deps-from (resolve-deps (read-edn base) aliases)
deps-to (resolve-deps (read-edn target) aliases)

key-set (comp set keys)

[removed-deps added-deps common-deps] (data/diff (key-set deps-from) (key-set deps-to))]
;; don't need to sort here
{:removed (into (sorted-map) (select-keys deps-from removed-deps))
:added (into (sorted-map) (select-keys deps-to added-deps))
:modified (into (sorted-map) (set/difference (set (select-keys deps-to common-deps))
(select-keys deps-from common-deps)))}))

(defn diff
"
opts
Expand All @@ -72,31 +86,21 @@
(assert (s/valid? ::spec/aliases aliases))
(assert (s/valid? ::spec/format format))

(let [deps-from (resolve-deps (read-edn base) aliases)
deps-to (resolve-deps (read-edn target) aliases)

key-set (comp set keys)

[removed-deps added-deps common-deps] (data/diff (key-set deps-from) (key-set deps-to))
modified-deps (set/difference (set (select-keys deps-to common-deps))
(select-keys deps-from common-deps))]
(make-output
{:removed (into (sorted-map) (select-keys deps-from removed-deps))
:added (into (sorted-map) (select-keys deps-to added-deps))
:modified (into (sorted-map) modified-deps)}
opts)))
(make-output (diff* opts) opts))

(comment
;; git show e0f4689c07bc652492bf03eba7edac20ab2bee0f:test/resources/base.edn > base.edn
;; clojure -X namenu.deps-diff/diff base.edn deps.edn

(diff {:base "HEAD" :target "deps.edn" :format :cli})
(diff {:base "test-resources/base/deps.edn"
:target "test-resources/target/deps.edn"
:aliases [:poly]})

(diff {:base "b2a1ca302959b720e703618a912a4b140389ee55" :target "deps.edn"
:format :cli})

(read-edn "HEAD:deps.edn")
(read-edn "HEAD:test/resources/base.edn")

(resolve-deps (read-edn "HEAD:deps.edn") [])
(resolve-deps {:deps {'green-labs/gosura {:git/url "https://github.com/green-labs/gosura"
Expand Down
1 change: 1 addition & 0 deletions test-resources/base/brick/deps.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{:deps {com.github.seancorfield/next.jdbc {:mvn/version "1.2.796"}}}
6 changes: 4 additions & 2 deletions test-resources/base/deps.edn
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
{:deps {hato/hato {:mvn/version "0.9.0"}
metosin/reitit {:mvn/version "0.4.0"}}} Ï
{:deps {org.clojure/clojure {:mvn/version "1.10.3"}}
:aliases {:dev {:extra-deps {hato/hato {:mvn/version "0.9.0"}
metosin/reitit {:mvn/version "0.4.0"}}}
:poly {:extra-deps {poly/core-api {:local/root "test-resources/base/brick"}}}}}
1 change: 1 addition & 0 deletions test-resources/target/brick/deps.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{:deps {com.github.seancorfield/next.jdbc {:mvn/version "1.3.847"}}}
8 changes: 5 additions & 3 deletions test-resources/target/deps.edn
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{:deps {metosin/reitit {:mvn/version "0.5.1"}
org.clojure/clojure {:mvn/version "1.11.1"}}
:aliases {:test {:extra-deps {prismatic/schema {:mvn/version "1.2.0"}}}}}
{:deps {org.clojure/clojure {:mvn/version "1.11.1"}}
:aliases {:dev {:extra-deps {metosin/reitit {:mvn/version "0.5.1"}
org.clojure/clojure {:mvn/version "1.11.1"}}}
:poly {:extra-deps {poly/core-api {:local/root "test-resources/target/brick"}}}
:test {:extra-deps {prismatic/schema {:mvn/version "1.2.0"}}}}}
28 changes: 27 additions & 1 deletion test/namenu/deps_diff/test.clj
Original file line number Diff line number Diff line change
@@ -1 +1,27 @@
(ns namenu.deps-diff.test)
(ns namenu.deps-diff.test
(:require [clojure.test :refer [deftest testing is]]
[namenu.deps-diff :refer [diff*]]))

(deftest diff-test
(testing "without aliases"
(let [d (diff* {:base "test-resources/base/deps.edn"
:target "test-resources/target/deps.edn"})]
(is (empty? (:removed d)))
(is (empty? (:added d)))
(is (not-empty (:modified d)))))

(testing "aliases"
(let [d (diff* {:base "test-resources/base/deps.edn"
:target "test-resources/target/deps.edn"
:aliases [:dev]})]
(is (= (update-vals d count)
{:removed 1, :added 11, :modified 31}))))

(testing "local/root test"
(let [d (diff* {:base "test-resources/base/deps.edn"
:target "test-resources/target/deps.edn"
:aliases [:poly]})]
(is (contains? (:modified d)
'com.github.seancorfield/next.jdbc)))))

(clojure.test/run-tests)

0 comments on commit dd00761

Please sign in to comment.