-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathlive_reload_test.clj
More file actions
117 lines (109 loc) · 4.06 KB
/
live_reload_test.clj
File metadata and controls
117 lines (109 loc) · 4.06 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
(ns scicloj.clay.v2.live-reload-test
(:require [scicloj.clay.v2.live-reload :as lr]
[babashka.fs :as fs]
[clojure.test :refer [deftest is]]))
(defn path-str [& args]
(str (apply (comp fs/canonicalize fs/path) args)))
(deftest subdir-paths-test
(fs/with-temp-dir [test-dir]
(is (= #{(path-str test-dir "foo" "a")
(path-str test-dir "foo" "a" "b")
(path-str test-dir "bar" "a")}
(lr/subdirs [(path-str test-dir "foo" "a")
(path-str test-dir "foo" "a" "b")
(path-str test-dir "bar" "a")
(path-str test-dir "baz" "a")]
[(path-str test-dir "foo")
(path-str test-dir "bar")])))))
(deftest exclude-subdir-paths-test
(fs/with-temp-dir [test-dir]
(is (= #{(path-str test-dir "foo")
(path-str test-dir "bar")}
(lr/roots #{(path-str test-dir "foo")
(path-str test-dir "bar")})))
(is (= #{(path-str test-dir "foo")
(path-str test-dir "bar")}
(lr/roots #{(path-str test-dir "foo")
(path-str test-dir "foo" "a")
(path-str test-dir "bar")
(path-str test-dir "bar" "b")})))))
(deftest dirs-to-watch-test
(fs/with-temp-dir [test-dir]
(is (= #{(path-str test-dir "bar")}
(lr/dirs-to-watch #{(path-str test-dir "foo")}
#{}
#{(path-str test-dir "foo" "a.clj")
(path-str test-dir "foo" "b" "a.clj")
(path-str test-dir "bar" "a.clj")
(path-str test-dir "bar" "b" "a.clj")})))))
(defn mock-make-fn [spec]
(println "mock-make-fn: " spec))
(defn create-dirs [dirs]
(doseq [dir dirs]
(fs/create-dirs dir)))
(deftest start-stop-watching-dirs!-test
(fs/with-temp-dir [test-dir]
(let [dirs [(path-str test-dir "foo")
(path-str test-dir "foo")
(path-str test-dir "bar")]]
(create-dirs dirs)
(lr/watch-dirs! dirs mock-make-fn {})
(is (= #{(path-str test-dir "foo")
(path-str test-dir "bar")}
(lr/watched-dirs))
"watch a dir exactly once")
(lr/stop-watching-dirs! dirs)
(is (= #{}
(lr/watched-dirs))
"all being stopped"))))
(deftest start!-stop!-test
(fs/with-temp-dir [test-dir]
(let [file1 (path-str test-dir "foo" "bar" "a.clj")]
(fs/create-dirs (fs/parent file1))
(lr/start! mock-make-fn
{:live-reload true
:source-path file1
:watch-dirs []}
[file1])
(is (= #{file1}
(lr/watched-files))
"watch first file")
(is (= #{(str (fs/parent file1))}
(lr/watched-dirs))
"watch first dir")
(lr/stop!)
(is (= #{}
(lr/watched-files)))
(is (= #{}
(lr/watched-dirs)))
;; do it again
(lr/start! mock-make-fn
{:live-reload true
:source-path file1
:watch-dirs []}
[file1])
(is (= #{(str (fs/parent file1))}
(lr/watched-dirs))
"watch first dir")
;; make other files
(let [files [(path-str test-dir "foo" "a.clj")
(path-str test-dir "foo" "baz" "a.clj")
(path-str test-dir "bar" "a.clj")]]
(create-dirs (map fs/parent files))
(lr/start! mock-make-fn
{:live-reload true
:source-path files
:watch-dirs []}
files)
(is (= (set (apply vector file1 files))
(lr/watched-files))
"watch files twice")
(is (= #{(path-str test-dir "foo")
(path-str test-dir "bar")}
(lr/watched-dirs))
"watch dirs twice")
(lr/stop!)
(is (= #{}
(lr/watched-files)))
(is (= #{}
(lr/watched-dirs)))))))