Permalink
Browse files

Unit tests for watching a directory

  • Loading branch information...
1 parent b60e845 commit 89857d881932b7161801922b39f712ed680b9e70 @bivory committed Mar 9, 2013
Showing with 57 additions and 6 deletions.
  1. +3 −1 project.clj
  2. +1 −1 src/watch_do/watcher.clj
  3. +3 −4 test/watch_do/core_test.clj
  4. +50 −0 test/watch_do/watcher_test.clj
View
@@ -4,5 +4,7 @@
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.4.0"]
- [org.clojure/tools.cli "0.2.2"]]
+ [org.clojure/tools.cli "0.2.2"]
+ [me.raynes/fs "1.4.0"]]
+ :profiles {:dev {:dependencies [[midje "1.5.0"]]}}
:main watch-do.core)
View
@@ -110,7 +110,7 @@
(throw (IllegalArgumentException. (str "Bad path: " pathname)))
parent))
path)
- k (println "watching" path watch-dir)
+ ;;k (println "watching" path watch-dir)
watch-key (.register watch-dir watcher watch-events)]
(swap! watch-stats
assoc-in
@@ -1,7 +1,6 @@
(ns watch-do.core-test
- (:use clojure.test
+ (:use midje.sweet
watch-do.core))
-(deftest a-test
- (testing "FIXME, I fail."
- (is (= 0 1))))
+(fact "A simple test is true"
+ true => true)
@@ -0,0 +1,50 @@
+(ns watch-do.watcher-test
+ (:use midje.sweet
+ watch-do.watcher)
+ (:require [me.raynes.fs :as fs]))
+
+(def path "test/tmp/")
+;; Create the testing directory if it doesn't exist.
+(when (not (fs/exists? path)) (fs/mkdir path))
+
+(defn cb
+ "Test helper that handles the callback when a watched path changes."
+ [check ev ctx]
+ ;;(println ev ctx)
+ (swap! check (constantly {:ev ev :path (.toString ctx)})))
+
+(defn watching-for
+ "Test helper that verifies that the change event is the expended event."
+ [check parent-path ev path]
+ ;;(println "watching for" ev path)
+ (Thread/sleep 15000)
+ ;;(println "saw" (:ev @check) (str parent-path (:path @check)))
+ (and (= (:ev @check) ev)
+ (= (str parent-path (:path @check)) path)))
+
+(fact "A user can watch a directory for changes"
+ (let [check (atom {})
+ dir (str path (fs/temp-name "tmp-dir"))
+ file (str path (fs/temp-name "tmp"))]
+ (watch-path path
+ :create (partial cb check)
+ :modify (partial cb check)
+ :delete (partial cb check))
+
+ ;; Create
+ (fs/mkdir dir)
+ (watching-for check path :create dir) => true
+
+ ;; Delete
+ (fs/delete-dir dir)
+ (watching-for check path :delete dir) => true
+
+ ;; Create File
+ (fs/touch file)
+ (watching-for check path :create file) => true
+
+ ;; Delete File
+ (fs/delete file)
+ (watching-for check path :delete file) => true
+
+ (unwatch-path path)))

0 comments on commit 89857d8

Please sign in to comment.