Permalink
Browse files

Add more jar tests (Issue #70).

  • Loading branch information...
1 parent 5816773 commit 0889a70701246c664eda462797771c69fd6badb3 @emezeske committed Apr 10, 2012
@@ -32,7 +32,7 @@
(defn- parse-notify-command [build]
(assoc build :parsed-notify-command
- (config/parse-shell-command (:notify-command build))))
+ (config/parse-shell-command (:notify-command build))))
(defn- run-compiler [project {:keys [crossover-path crossovers builds]} build-ids watch?]
(doseq [build-id build-ids]
@@ -3,15 +3,17 @@
(:require
[clojure.java.io :as io]
[clojure.string :as s]
+ [fs.core :as fs]
[leiningen.cljsbuild.config :as config]))
-(defn- file-bytes
+(defn file-bytes
"Reads a file into a byte array"
- [file]
- (with-open [fis (java.io.FileInputStream. file)]
- (let [ba (byte-array (.length file))]
- (.read fis ba)
- ba)))
+ [filename]
+ (let [file (io/file filename)]
+ (with-open [input (java.io.FileInputStream. file)]
+ (let [data (byte-array (.length file))]
+ (.read input data)
+ data))))
(defn relative-path
"Given two normalized path strings, returns a path string of the second relative to the first."
@@ -22,19 +24,27 @@
(str child " is not a child of " parent))))
(s/replace relative #"^[\\/]" "")))
+(defn join-paths [& paths]
+ (apply str (interpose "/" paths)))
+
+(defn canonical-path [path]
+ (.getCanonicalPath (io/file path)))
+
;; The reason we return a :bytes filespec is that it's the only way of
;; specifying a file's destination path inside the jar and is contents
;; independently. Obviously this presents issues if there are any very
;; large files - this should be fixable in leiningen 2.0.
-(defn- path-filespecs
+(defn path-filespecs
"Given a path, returns a seq of filespecs representing files on the path."
[path]
- (let [dir (io/file path)
- files (file-seq dir)]
- (for [file (filter #(not (.isDirectory %)) files)]
+ (let [filenames (apply concat
+ (for [[root _ filenames] (fs/iterate-dir path)]
+ (for [file filenames]
+ (join-paths root file))))]
+ (for [filename filenames]
{:type :bytes
- :path (relative-path (.getCanonicalPath dir) (.getCanonicalPath file))
- :bytes (file-bytes file)})))
+ :path (relative-path (canonical-path path) (canonical-path filename))
+ :bytes (file-bytes filename)})))
(defn get-filespecs
"Returns a seq of filespecs for cljs dirs (as passed to leiningen.jar/write-jar)"
@@ -1,12 +1,75 @@
(ns leiningen.test.cljsbuild.jar
(:use
leiningen.cljsbuild.jar
- midje.sweet))
+ midje.sweet)
+ (:require
+ [fs.core :as fs]))
(fact
(relative-path "/" "/a") => "a"
(relative-path "/a/b/c" "/a/b/c/d/e") => "d/e"
(relative-path "" "a") => (throws Exception)
(relative-path "/a/b/c" "/a/b") => (throws Exception))
-; TODO: More tests!
+(defn- make-bytes [s]
+ byte-array (map (comp byte int) s))
+
+(fact
+ (let [root "/a/b"
+ basename-1 "file1"
+ basename-2 "file2"
+ path-1 (join-paths root basename-1)
+ path-2 (join-paths root basename-2)
+ bytes-1 (make-bytes "some-bytes")
+ bytes-2 (make-bytes "other-bytes")]
+ (path-filespecs root) => [{:type :bytes
+ :path basename-1
+ :bytes bytes-1}
+ {:type :bytes
+ :path basename-2
+ :bytes bytes-2}]
+ (provided
+ (fs/iterate-dir root) => [[root [] [basename-1 basename-2]]] :times 1
+ (file-bytes path-1) => bytes-1 :times 1
+ (file-bytes path-2) => bytes-2 :times 1)))
+
+(defn make-jar-directory [root basename contents]
+ {:root (join-paths root basename)
+ :filespec
+ {:type :bytes
+ :path basename
+ :bytes (make-bytes contents)}})
+
+(defn make-jar-directories [names]
+ (let [directories (for [n (map name names)]
+ (make-jar-directory
+ (join-paths "/root" n)
+ (str "file-" n)
+ (str "contents-" n)))]
+ (into {} (map vector names directories))))
+
+(fact
+ (let [entries (make-jar-directories [:a :b :c])
+ project {:cljsbuild
+ {:crossover-path (get-in entries [:a :root])
+ :crossover-jar true
+ :builds
+ [{:source-path (get-in entries [:b :root])
+ :jar true}
+ {:source-path (get-in entries [:c :root])
+ :jar true}
+ {:source-path "not-in-jar"}]}}
+ root-a (get-in entries [:a :root])
+ root-b (get-in entries [:b :root])
+ root-c (get-in entries [:c :root])]
+ (get-filespecs project) => (just (set (map :filespec (vals entries))))
+ (provided
+ (path-filespecs root-a) => [(get-in entries [:a :filespec])] :times 1
+ (path-filespecs root-b) => [(get-in entries [:b :filespec])] :times 1
+ (path-filespecs root-c) => [(get-in entries [:c :filespec])] :times 1)
+ (let [entries (dissoc entries :a)
+ project (assoc project :crossover-jar false)]
+ (get-filespecs project) => (just (set (map :filespec (vals entries))))
+ (provided
+ (path-filespecs root-b) => [(get-in entries [:b :filespec])] :times 1
+ (path-filespecs root-c) => [(get-in entries [:c :filespec])] :times 1))))
@@ -14,8 +14,8 @@
(filter #(types (ext %)) files)))
(defn find-dir-files [root files types]
- (for [files (filter-by-ext files types)]
- (join-paths root files)))
+ (for [file (filter-by-ext files types)]
+ (join-paths root file)))
(defn find-files [dir types]
(let [iter (fs/iterate-dir dir)]
@@ -26,6 +26,8 @@
["a/b" "" ["m.z" "n.z" "o.b"]]]
:times 1))
+; FIXME: There appears to be a race condition in this test (or perhaps the
+; code under test). The test fails periodically!
(unfinished call-in-threads)
(fact

0 comments on commit 0889a70

Please sign in to comment.