Check that exactly one (existing) source file is found per namespace when compiling

This commit is contained in:
Thomas Hancock 2014-06-19 18:55:56 +01:00
parent 4aefda6d47
commit c2cc916dc6
2 changed files with 20 additions and 11 deletions

View file

@ -5,7 +5,7 @@
[bultitude.core :as b] [bultitude.core :as b]
[clojure.java.io :as io]) [clojure.java.io :as io])
(:refer-clojure :exclude [compile]) (:refer-clojure :exclude [compile])
(:import (java.io PushbackReader))) (:import (java.io PushbackReader FileNotFoundException)))
(defn- regex? [str-or-re] (defn- regex? [str-or-re]
(instance? java.util.regex.Pattern str-or-re)) (instance? java.util.regex.Pattern str-or-re))
@ -29,15 +29,27 @@
(b/namespaces-on-classpath :classpath (map io/file source-paths)) (b/namespaces-on-classpath :classpath (map io/file source-paths))
(find-namespaces-by-regex project aot))) (find-namespaces-by-regex project aot)))
(defn solitary-file-for-namespace
[namespace files]
(cond
(= 1 (count files)) (first files)
(= 0 (count files)) (throw (FileNotFoundException.
(str "Source file cannot be found for namespace \"" namespace "\"")))
:else (throw (IllegalStateException.
(str "Multiple source files found for namespace \"" namespace "\"")))))
(defn stale-namespaces (defn stale-namespaces
"Return a seq of namespaces that are both compilable and that have missing or "Return a seq of namespaces that are both compilable and that have missing or
out-of-date class files." out-of-date class files."
[project] [project]
(for [namespace (compilable-namespaces project) (for [namespace (compilable-namespaces project)
:let [rel-source (b/path-for namespace) :let [rel-source (b/path-for namespace)
source (first (for [source-path (:source-paths project) source (solitary-file-for-namespace
:let [file (io/file source-path rel-source)]] namespace
file))] (for [source-path (:source-paths project)
:let [file (io/file source-path rel-source)]
:when (.exists file)]
file))]
:when source :when source
:let [rel-compiled (.replaceFirst rel-source "\\.clj$" "__init.class") :let [rel-compiled (.replaceFirst rel-source "\\.clj$" "__init.class")
compiled (io/file (:compile-path project) rel-compiled)] compiled (io/file (:compile-path project) rel-compiled)]

View file

@ -86,13 +86,10 @@
(deftest bad-aot-test (deftest bad-aot-test
(is (re-find #"does\.not\.exist|does\/not\/exist" (is (thrown? java.io.FileNotFoundException (compile (assoc sample-project :aot '[does.not.exist])))))
(with-out-str
(binding [*err* *out*] (deftest multiple-namespace-files
(try (is (thrown? IllegalStateException (compile (assoc sample-project :source-paths ["src/" "src/"] :aot :all)))))
(compile (assoc sample-project
:aot '[does.not.exist]))
(catch clojure.lang.ExceptionInfo _)))))))
(deftest compilation-specs-tests (deftest compilation-specs-tests
(is (= '[foo bar] (compilation-specs ["foo" "bar"]))) (is (= '[foo bar] (compilation-specs ["foo" "bar"])))