UNPKG

5.93 kBtext/coffeescriptView Raw
1# ...
2# requirements
3fs = require 'fs'
4path = require 'path'
5{exec} = require 'child_process'
6fsu = require '../lib/fs-util'
7
8# ...
9# defining global watcher var
10watcher = null
11base_path = path.join __dirname, 'tmp-watcher'
12fs.mkdirSync base_path, '0755'
13
14# ...
15# helper methods for tests #7 and #8
16build_paths = ->
17 ls = {}
18 ls[location] = location for location in locations = [
19 (path.join base_path, 'a/b/c/d/e/cinco.coffee'),
20 (path.join base_path, 'a/b/c/d/e'),
21 (path.join base_path, 'a/b/c/d/quatro.coffee'),
22 (path.join base_path, 'a/b/c/d'),
23 (path.join base_path, 'a/b/c/tres.coffee'),
24 (path.join base_path, 'a/b/c'),
25 (path.join base_path, 'a/b/dois.coffee'),
26 (path.join base_path, 'a/b'),
27 (path.join base_path, 'a/um.coffee'),
28 (path.join base_path, 'a')
29 ]
30 ls.length = locations.length
31 ls
32
33create_structure = ()->
34 # build dirs
35 fs.mkdirSync dir for dir in [
36 a = (path.join __dirname, 'a'),
37 b = (path.join __dirname, 'a/b'),
38 c = (path.join __dirname, 'a/b/c'),
39 d = (path.join __dirname, 'a/b/c/d'),
40 e = (path.join __dirname, 'a/b/c/d/e')
41 ]
42
43 # build files
44 fs.writeFileSync file, '' for file in [
45 (path.join a, 'um.coffee'),
46 (path.join b, 'dois.coffee'),
47 (path.join c, 'tres.coffee'),
48 (path.join d, 'quatro.coffee'),
49 (path.join e, 'cinco.coffee')
50 ]
51
52 # move structure into tmp dir
53 exec "cd #{__dirname} && mv a #{base_path}"
54
55delete_structure = ->
56 exec "cd #{base_path} && rm -rf a"
57
58describe '• FS Watcher', ->
59 # ...
60 # 1) watching dir
61 describe 'When watching a directory tree', ->
62 it 'the `watch` event should be emitted properly', (done)->
63
64 watcher = fsu.watch base_path, /.coffee$/m, true
65 watcher.once 'watch', (f)->
66 f.location.should.equal base_path
67 done()
68
69 # ...
70 # 2) creating dir
71 describe 'When creating a dir inside that tree', ->
72 it 'the `watch` and `create` event should be emitted properly', (done)->
73
74 dirpath = path.resolve "#{base_path}/app"
75 created = false
76
77 # on dir creation, `create` event should come before the `watch` event
78 watcher.once 'create', (f)->
79 created = true
80 f.location.should.equal dirpath
81
82 watcher.once 'watch', (f)->
83 created.should.equal true
84 f.location.should.equal dirpath
85 done()
86
87 fs.mkdirSync dirpath
88
89 # ...
90 # 3) deleting dir
91 describe 'When deleting this dir', ->
92
93 it 'the `unwatch` and `delete` events should be emitted properly', (done)->
94
95 dirpath = path.resolve "#{base_path}/app"
96 unwatched = false
97
98 # on dir deletion, `unwatch` event should come before the `delete` event
99 watcher.once 'unwatch', (f)->
100 unwatched = true
101 f.location.should.equal dirpath
102
103 watcher.once 'delete', (f)->
104 unwatched.should.equal true
105 f.location.should.equal dirpath
106 done()
107
108 fs.rmdirSync dirpath
109
110 # ...
111 # 4) creating file
112 describe 'When creating a file inside the watched dir', ->
113
114 it 'the `created` and `watch` events should be emitted properly', (done)->
115
116 filepath = path.resolve "#{base_path}/file.coffee"
117 created = false
118
119 # on file creation, `create` event should come before the `watch` event
120 watcher.once 'create', (f)->
121 created = true
122 f.location.should.equal filepath
123
124 watcher.once 'watch', (f)->
125 created.should.equal true
126 f.location.should.equal filepath
127 done()
128
129 fs.writeFileSync filepath, 'first line\n', 'utf-8'
130
131 # ...
132 # 5) updating file
133 describe 'When updating this file (a little delay needed here, pay no mind)', ->
134
135 it 'the `change` event should be emitted properly', (done)->
136
137 filepath = path.resolve "#{base_path}/file.coffee"
138 watcher.once 'change', (f)->
139 f.location.should.equal filepath
140 done()
141
142 # gives time to fs process the watching action before modifying the file
143 # in order to provoke the 'change' event`
144 setTimeout ->
145 fs.appendFileSync filepath, 'second line\n', 'utf-8'
146 , 1000
147
148 # ...
149 # 6) deleting file
150 describe 'When deleting this file', ->
151
152 it 'the `unwatch` and `delete` events should be emitted properly', (done)->
153
154 filepath = path.resolve "#{base_path}/file.coffee"
155 unwatched = false
156
157 # on dir deletion, `unwatch` event should come before the `delete` event
158 watcher.once 'unwatch', (f)->
159 unwatched = true
160 f.location.should.equal filepath
161
162 watcher.once 'delete', (f)->
163 unwatched.should.equal true
164 f.location.should.equal filepath
165 done()
166
167 fs.unlinkSync filepath
168
169 # ...
170 # 7) Creating a dir with many sub fs (dirs and files)
171 describe 'When moving an existent structure inside the watched tree', ->
172
173 it 'the `create` and `watch` events should be emitted properly for all files and dirs', (done)->
174
175 # paths for comparison
176 ls = build_paths()
177 created = 0
178 watched = 0
179
180 # setting up listeners
181 watcher.on 'create', (f)->
182 created++ if (f.location.should.equal ls[f.location])
183
184 watcher.on 'watch', (f)->
185 watched++ if (f.location.should.equal ls[f.location])
186 if watched is ls.length and created is ls.length
187 done()
188
189 create_structure()
190
191 # ...
192 # 8) Deleting a dir with many sub fs (dirs and files)
193 describe 'When deleting this structure', ->
194
195 it 'the `delete` and `unwatch` events should be emitted properly for all files and dirs', (done)->
196
197 ls = build_paths()
198 unwatched = 0
199 deleted = 0
200
201 # setting up listeners
202 watcher.on 'unwatch', (f)->
203 unwatched++ if (f.location.should.equal ls[f.location])
204
205 watcher.on 'delete', (f)->
206 deleted++ if (f.location.should.equal ls[f.location])
207 if unwatched is ls.length and deleted is ls.length
208 done()
209
210 delete_structure()
\No newline at end of file