Skip to content

Commit

Permalink
Remove remnants of 1.2 support.
Browse files Browse the repository at this point in the history
  • Loading branch information
marick committed May 8, 2013
1 parent e1cc4e4 commit 6b180ab
Show file tree
Hide file tree
Showing 8 changed files with 226 additions and 271 deletions.
10 changes: 5 additions & 5 deletions src/midje/checking/checkers/simple.clj
Expand Up @@ -7,7 +7,7 @@
[midje.checking.checkers.defining :only [as-checker checker defchecker]]
[midje.checking.checkers.util :only [named-as-call]]
[midje.util.exceptions :only [captured-throwable?]]
[midje.util.ecosystem :only [clojure-1-3? +M -M *M]]
[midje.util.ecosystem :only [clojure-1-3?]]
[clojure.algo.monads :only [domonad set-m]])
(:import [midje.util.exceptions ICapturedThrowable]))

Expand Down Expand Up @@ -39,7 +39,7 @@
(letfn [(abs [n]
(if (pos? n)
n
(-M n)))] ;; -M not strictly necessary, but...
(-' n)))] ;; -' not strictly necessary, but...

(defchecker roughly
"With two arguments, accepts a value within delta of the
Expand All @@ -48,10 +48,10 @@
([expected delta]
(checker [actual]
(and (number? actual)
(>= expected (-M actual delta))
(<= expected (+M actual delta)))))
(>= expected (-' actual delta))
(<= expected (+' actual delta)))))
([expected]
(roughly expected (abs (*M 0.001 expected))))))
(roughly expected (abs (*' 0.001 expected))))))


;; Concerning Throwables
Expand Down
205 changes: 101 additions & 104 deletions src/midje/data/project_state.clj
Expand Up @@ -8,112 +8,109 @@
[midje.emission.colorize :as color]
[midje.config :as config]))

(ecosystem/when-1-3+

(require '[clojure.tools.namespace.repl :as nsrepl]
'[clojure.tools.namespace.dir :as nsdir]
'[clojure.tools.namespace.track :as nstrack]
'[clojure.tools.namespace.reload :as nsreload])
(require '[clojure.tools.namespace.repl :as nsrepl]
'[clojure.tools.namespace.dir :as nsdir]
'[clojure.tools.namespace.track :as nstrack]
'[clojure.tools.namespace.reload :as nsreload])


;;; Querying the project tree

(defn namespaces []
(mapcat namespaces-in-dir (ecosystem/leiningen-paths)))

;; For some purposes, it matters that the :test-paths files come
;; before the :source-paths files. That happens to always be true,
;; but the name below emphasizes it.
(def namespaces-test-first namespaces)

(defn unglob-partial-namespaces [namespaces]
(mapcat #(if (= \* (last %))
(namespaces-on-classpath :prefix (apply str (butlast %)))
[(symbol %)])
(map str namespaces)))


;;; Responding to changed files

;; tools.ns keys are annoyingly long. Shorthand.
(def unload-key :clojure.tools.namespace.track/unload)
(def load-key :clojure.tools.namespace.track/load)
(def filemap-key :clojure.tools.namespace.file/filemap)
(def deps-key :clojure.tools.namespace.track/deps)
(def time-key :clojure.tools.namespace.dir/time)

;; Global state.

(defonce state-tracker (atom (nstrack/tracker)))

(defn file-modification-time [file]
(.lastModified file))

(defn latest-modification-time [state-tracker]
(let [ns-to-file (invert (filemap-key state-tracker))
relevant-files (map ns-to-file (load-key state-tracker))]
(apply max (time-key state-tracker)
(map file-modification-time relevant-files))))


(defn require-namespaces! [namespaces on-require-failure clean-dependents]
(letfn [(broken-source-file? [the-ns]
(try
(require the-ns :reload)
false
(catch Throwable t
(on-require-failure the-ns t)
true)))

(shorten-ns-list-by-trying-first [[the-ns & remainder]]
(if (broken-source-file? the-ns)
(clean-dependents the-ns remainder)
remainder))]

(loop [namespaces namespaces]
(when (not (empty? namespaces))
(recur (shorten-ns-list-by-trying-first namespaces))))))

;; TODO: clojure.tools.namespace also finds a transitive closure when it finds
;; the namespaces to reload, but I don't see quite how to hook into that mechanism,
;; so I roll my own.
(defn mkfn:clean-dependents [state-tracker]
(fn [failing-namespace other-namespaces]
(loop [[root-to-handle & roots-to-handle-later] [failing-namespace]
surviving-namespaces other-namespaces]
(if (nil? root-to-handle)
surviving-namespaces
(let [actual-dependent-set (set (get-in state-tracker [deps-key :dependents root-to-handle]))
[new-roots unkilled-descendents] (separate actual-dependent-set surviving-namespaces)]
(recur (concat roots-to-handle-later new-roots)
unkilled-descendents))))))
(defn namespaces []
(mapcat namespaces-in-dir (ecosystem/leiningen-paths)))

;; For some purposes, it matters that the :test-paths files come
;; before the :source-paths files. That happens to always be true,
;; but the name below emphasizes it.
(def namespaces-test-first namespaces)

(defn unglob-partial-namespaces [namespaces]
(mapcat #(if (= \* (last %))
(namespaces-on-classpath :prefix (apply str (butlast %)))
[(symbol %)])
(map str namespaces)))


;;; Responding to changed files

;; tools.ns keys are annoyingly long. Shorthand.
(def unload-key :clojure.tools.namespace.track/unload)
(def load-key :clojure.tools.namespace.track/load)
(def filemap-key :clojure.tools.namespace.file/filemap)
(def deps-key :clojure.tools.namespace.track/deps)
(def time-key :clojure.tools.namespace.dir/time)

;; Global state.

(defonce state-tracker (atom (nstrack/tracker)))

(defn file-modification-time [file]
(.lastModified file))

(defn latest-modification-time [state-tracker]
(let [ns-to-file (invert (filemap-key state-tracker))
relevant-files (map ns-to-file (load-key state-tracker))]
(apply max (time-key state-tracker)
(map file-modification-time relevant-files))))


(defn require-namespaces! [namespaces on-require-failure clean-dependents]
(letfn [(broken-source-file? [the-ns]
(try
(require the-ns :reload)
false
(catch Throwable t
(on-require-failure the-ns t)
true)))

(shorten-ns-list-by-trying-first [[the-ns & remainder]]
(if (broken-source-file? the-ns)
(clean-dependents the-ns remainder)
remainder))]

(loop [namespaces namespaces]
(when (not (empty? namespaces))
(recur (shorten-ns-list-by-trying-first namespaces))))))

;; TODO: clojure.tools.namespace also finds a transitive closure when it finds
;; the namespaces to reload, but I don't see quite how to hook into that mechanism,
;; so I roll my own.
(defn mkfn:clean-dependents [state-tracker]
(fn [failing-namespace other-namespaces]
(loop [[root-to-handle & roots-to-handle-later] [failing-namespace]
surviving-namespaces other-namespaces]
(if (nil? root-to-handle)
surviving-namespaces
(let [actual-dependent-set (set (get-in state-tracker [deps-key :dependents root-to-handle]))
[new-roots unkilled-descendents] (separate actual-dependent-set surviving-namespaces)]
(recur (concat roots-to-handle-later new-roots)
unkilled-descendents))))))

(defn react-to-tracker! [state-tracker options]
(let [namespaces (load-key state-tracker)]
(when (not (empty? namespaces))
( (:namespace-stream-checker options)
namespaces
#(require-namespaces! namespaces
(:on-require-failure options)
(mkfn:clean-dependents state-tracker))))))

(defn prepare-for-next-scan [state-tracker]
(assoc state-tracker time-key (latest-modification-time state-tracker)
unload-key []
load-key []))

(defn mkfn:scan-and-react [options scanner]
(fn []
(swap! state-tracker
#(let [new-tracker (apply scanner % (:files options))]
(react-to-tracker! new-tracker options)
(prepare-for-next-scan new-tracker)))))


(defn mkfn:react-to-changes [options]
(mkfn:scan-and-react options nsdir/scan))

(defn load-everything [options]
((mkfn:scan-and-react options nsdir/scan-all)))

)
(defn react-to-tracker! [state-tracker options]
(let [namespaces (load-key state-tracker)]
(when (not (empty? namespaces))
( (:namespace-stream-checker options)
namespaces
#(require-namespaces! namespaces
(:on-require-failure options)
(mkfn:clean-dependents state-tracker))))))

(defn prepare-for-next-scan [state-tracker]
(assoc state-tracker
time-key (latest-modification-time state-tracker)
unload-key []
load-key []))

(defn mkfn:scan-and-react [options scanner]
(fn []
(swap! state-tracker
#(let [new-tracker (apply scanner % (:files options))]
(react-to-tracker! new-tracker options)
(prepare-for-next-scan new-tracker)))))


(defn mkfn:react-to-changes [options]
(mkfn:scan-and-react options nsdir/scan))

(defn load-everything [options]
((mkfn:scan-and-react options nsdir/scan-all)))
2 changes: 1 addition & 1 deletion src/midje/emission/state.clj
Expand Up @@ -39,7 +39,7 @@
`(do
(def ~atom-name (atom :undefined))
(defn ~name [] (deref ~atom-name))
(ecosystem/when-1-3+ (.setDynamic (var ~atom-name)))
(.setDynamic (var ~atom-name))
(def ~fresh-name ~(zipmap keys (repeat 0)))
(defn ~set-name [newval#] (swap! ~atom-name (constantly newval#)))
(defn ~reset-name [] (reset! ~atom-name ~fresh-name))
Expand Down
81 changes: 24 additions & 57 deletions src/midje/util/ecosystem.clj
Expand Up @@ -19,39 +19,10 @@
(and (= 1 (:major *clojure-version*))
(= 3 (:minor *clojure-version*))))

(defn clojure-1-2-X? []
(and (= 1 (:major *clojure-version*))
(= 2 (:minor *clojure-version*))))

(defn clojure-1-2-0? []
(and (= 1 (:major *clojure-version*))
(= 2 (:minor *clojure-version*))
(= 0 (:incremental *clojure-version*))))

(defmacro when-1-3+ [& body]
(when-not (= 2 (:minor *clojure-version*))
`(do ~@body)))

(defmacro when-1-3- [& body]
(when (< (:minor *clojure-version*) 4)
`(do ~@body)))

(defmacro when-1-4+ [& body]
(when (>= (:minor *clojure-version*) 4)
`(do ~@body)))

(defmacro unless-1-2-0
"Skip body completely - including 'Unable to resolve classname' errors."
[& body]
(when-not (clojure-1-2-0?)
`(do ~@body)))

;; The following works because in 1.2 it's parsed as [+ '1].

(def +M (first [+' 1]))
(def -M (first [-' 1]))
(def *M (first [*' 1]))

;;

(defn getenv [var]
Expand Down Expand Up @@ -89,32 +60,28 @@
;; get that from `lein repl`, so the default value from `project.clj`
;; is returned.

(when-1-3+

(def leiningen-paths-var nil)

(defmacro around-initial-paths [& body]
`(let [original# leiningen-paths-var]
(try
(alter-var-root #'leiningen-paths-var (constantly nil))
~@body
(finally (alter-var-root #'leiningen-paths-var (constantly original#))))))

(defn set-leiningen-paths! [project]
;; Note that the order is guaranteed: test paths come before project paths.
(alter-var-root #'leiningen-paths-var
(constantly (concat (:test-paths project) (:source-paths project)))))

(defn- project-with-paths []
(try
(project/read)
(catch java.io.FileNotFoundException e
{:test-paths ["test"]})))
(def leiningen-paths-var nil)

(defmacro around-initial-paths [& body]
`(let [original# leiningen-paths-var]
(try
(alter-var-root #'leiningen-paths-var (constantly nil))
~@body
(finally (alter-var-root #'leiningen-paths-var (constantly original#))))))

(defn set-leiningen-paths! [project]
;; Note that the order is guaranteed: test paths come before project paths.
(alter-var-root #'leiningen-paths-var
(constantly (concat (:test-paths project) (:source-paths project)))))

(defn- project-with-paths []
(try
(project/read)
(catch java.io.FileNotFoundException e
{:test-paths ["test"]})))

(defn leiningen-paths []
(or leiningen-paths-var
(do
(set-leiningen-paths! (project-with-paths))
leiningen-paths-var)))

)
(defn leiningen-paths []
(or leiningen-paths-var
(do
(set-leiningen-paths! (project-with-paths))
leiningen-paths-var)))

0 comments on commit 6b180ab

Please sign in to comment.