-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
script.clj
executable file
·94 lines (76 loc) · 2.59 KB
/
script.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env bb
(ns script
(:require [babashka.pods :as pods]
[clojure.java.shell :refer [sh]]
[clojure.test :as t :refer [deftest is testing]]
[clojure.java.io :as io]))
(prn (pods/load-pod "./pod-babashka-fswatcher"))
(require '[pod.babashka.fswatcher :as fw])
(def events (atom []))
(def callback
(fn [event]
;; (prn :event event)
(swap! events conj event)))
(def watcher (fw/watch "test" callback {:delay-ms 250 :recursive true}))
(Thread/sleep 200)
(sh "touch" *file*)
(Thread/sleep 1000)
(prn :events @events)
(fw/unwatch watcher)
(fw/unwatch watcher) ;; idempotency
(def ev1 @events)
(sh "touch" *file*)
(Thread/sleep 1000)
(def ev2 @events)
(deftest events-test
(is (= 1 (count ev1)))
(is (= (:path (first ev1)) "test/script.clj"))
(testing "No new events after unwatch"
(is (= (count ev1) (count ev2)))))
(deftest dedup-test
(reset! events [])
(let [watcher (fw/watch "test" #(swap! events conj %) {:delay-ms 50 :recursive true})]
(sh "touch" *file*)
(Thread/sleep 5)
(sh "touch" *file*)
(Thread/sleep 5)
(sh "touch" *file*)
;;wait for timer to end
(Thread/sleep 60)
(prn :events-dedup @events)
(testing "tests that the events that happened inside the interval were deduped."
(is (= 1 (count @events))))
(fw/unwatch watcher)))
(deftest dedup-outside-interval-test
(reset! events [])
(let [watcher (fw/watch "test" #(swap! events conj %) {:delay-ms 50 :recursive true})]
(sh "touch" *file*)
(Thread/sleep 51)
(sh "touch" *file*)
(Thread/sleep 60)
(prn :events-dedup-outside-interval @events)
(testing "events outside of dedup interval come through."
(is (= 2 (count @events))))
(fw/unwatch watcher)))
(deftest no-dedup-test
(reset! events [])
(let [watcher (fw/watch "test" #(swap! events conj %) {:delay-ms 50 :recursive true :dedup false})]
(sh "touch" *file*)
(Thread/sleep 60)
(prn :events-no-dedup @events)
(testing "`dedup :false` won't dedup"
(is (not (= 1 (count @events)))))
(fw/unwatch watcher)))
(deftest recursive-dedup-test
(let [ev (atom [])
file-name "test/dir/anotherdir/bla.txt"
_ (clojure.java.io/make-parents file-name)
watcher (fw/watch "test" #(swap! ev conj %) {:delay-ms 50 :recursive true})]
(spit file-name "whatever")
(Thread/sleep 60)
(prn :events-recursive-dedup @ev)
(testing "dedup recursive works"
(is (= @ev [{:type :write, :path "test/dir/anotherdir/bla.txt"}]))
(fw/unwatch watcher))))
(let [{:keys [:fail :error]} (t/run-tests)]
(System/exit (+ fail error)))