-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_makestatic.py
479 lines (383 loc) · 18 KB
/
test_makestatic.py
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
# coding: utf-8
import os
import sys
import time
import atexit
import shutil
import tempfile
import unittest
import threading
from warnings import catch_warnings
from contextlib import closing, contextmanager
from flask import Flask
from werkzeug.exceptions import NotFound
from flask.ext.makestatic import MakeStatic, RuleMissing
from flask.ext.makestatic._compat import StringIO
from flask.ext.makestatic.watcher import ThreadedWatcher
TEST_APPS = os.path.join(os.path.dirname(__file__), 'test_apps')
sys.path.insert(0, TEST_APPS)
def get_temporary_filename(directory=None):
filename = tempfile.mkstemp(dir=directory)[1]
@atexit.register
def remove_file():
try:
os.remove(filename)
except EnvironmentError:
pass
return filename
def get_temporary_directory():
directory = tempfile.mkdtemp()
@atexit.register
def remove_directory():
try:
shutil.rmtree(directory)
except EnvironmentError:
pass
return directory
def bump_modification_time(path):
stat = os.stat(path)
os.utime(path, (stat.st_atime, stat.st_mtime + 1))
@contextmanager
def catch_stdout():
old_stdout = sys.stdout
sys.stdout = rv = StringIO()
try:
yield rv
finally:
sys.stdout = old_stdout
class MakeStaticTestCase(unittest.TestCase):
def tearDown(self):
for test_app_dir in os.listdir(TEST_APPS):
static_dir = os.path.join(TEST_APPS, test_app_dir, 'static')
if os.path.isdir(static_dir):
for root, dirs, files in os.walk(static_dir, topdown=False):
for file in files:
if file == '.gitignore':
continue
os.remove(os.path.join(root, file))
for dir in dirs:
os.rmdir(os.path.join(root, dir))
@contextmanager
def make_static(self, import_name, config=None):
app = Flask(import_name)
if config is not None:
app.config.update(config)
make_static = MakeStatic(app)
watcher = make_static.watch(sleep=0.01)
try:
yield app, make_static
finally:
watcher.stop()
@contextmanager
def make_static_in_app_context(self, import_name, config=None):
app = Flask(import_name)
if config is not None:
app.config.update(config)
make_static = MakeStatic()
make_static.init_app(app)
with app.app_context():
watcher = make_static.watch(sleep=0.01)
try:
yield app, make_static
finally:
watcher.stop()
def get_watcher_contexts(self, import_name, config=None):
yield self.make_static(import_name, config)
yield self.make_static_in_app_context(import_name, config)
def test_send_static_file(self):
for context in self.get_watcher_contexts('working'):
with context as (app, make_static):
with app.test_request_context():
with closing(app.send_static_file('foo')) as response:
self.assertEqual(response.status_code, 200)
with closing(app.send_static_file('spam')) as response:
self.assertEqual(response.status_code, 200)
with closing(app.send_static_file('bar')) as response:
self.assertEqual(response.status_code, 200)
response.direct_passthrough = False
self.assertEqual(response.data, b'abc\ndef\n')
self.assertRaises(NotFound, app.send_static_file, 'baz')
with closing(app.send_static_file('eggs.css')) as response:
self.assertEqual(response.status_code, 200)
with catch_warnings(record=True): # silence RuleMissing warning
with catch_stdout() as stdout:
filename = os.path.basename(get_temporary_filename(
directory=make_static.assets_folder
))
time.sleep(0.05)
self.assertEqual(
stdout.getvalue(),
'Flask-MakeStatic: detected new asset %s, compiling\n' %
filename
)
self.assertRaises(NotFound, app.send_static_file, filename)
with catch_stdout() as stdout:
bump_modification_time(
os.path.join(make_static.assets_folder, 'foo')
)
time.sleep(0.05)
self.assertEqual(
stdout.getvalue(),
'Flask-MakeStatic: detected change in foo, compiling\n'
)
with closing(app.send_static_file('foo')) as response:
self.assertEqual(response.status_code, 200)
def test_send_static_file_globbing(self):
config = {'MAKESTATIC_FILEPATTERN_FORMAT': 'globbing'}
for context in self.get_watcher_contexts('working_globbing', config):
with context as (app, make_static):
with app.test_request_context():
with closing(app.send_static_file('foo')) as response:
self.assertEqual(response.status_code, 200)
with closing(app.send_static_file('spam')) as response:
self.assertEqual(response.status_code, 200)
with closing(app.send_static_file('bar')) as response:
self.assertEqual(response.status_code, 200)
response.direct_passthrough = False
self.assertEqual(response.data, b'abc\ndef\n')
self.assertRaises(NotFound, app.send_static_file, 'baz')
with closing(app.send_static_file('eggs.css')) as response:
self.assertEqual(response.status_code, 200)
with catch_warnings(record=True): # silence RuleMissing warning
with catch_stdout() as stdout:
filename = os.path.basename(get_temporary_filename(
directory=make_static.assets_folder
))
time.sleep(0.05)
self.assertEqual(
stdout.getvalue(),
'Flask-MakeStatic: detected new asset %s, compiling\n' %
filename
)
self.assertRaises(NotFound, app.send_static_file, filename)
with catch_stdout() as stdout:
bump_modification_time(
os.path.join(make_static.assets_folder, 'foo')
)
time.sleep(0.05)
self.assertEqual(
stdout.getvalue(),
'Flask-MakeStatic: detected change in foo, compiling\n'
)
with closing(app.send_static_file('foo')) as response:
self.assertEqual(response.status_code, 200)
def test_static_view(self):
for context in self.get_watcher_contexts('working'):
with context as (app, make_static):
client = app.test_client()
with closing(client.get('/static/foo')) as response:
self.assertEqual(response.status_code, 200)
with closing(client.get('/static/spam')) as response:
self.assertEqual(response.status_code, 200)
with closing(client.get('/static/bar')) as response:
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data, b'abc\ndef\n')
with closing(client.get('/static/baz')) as response:
self.assertEqual(response.status_code, 404)
with closing(client.get('/static/eggs.css')) as response:
self.assertEqual(response.status_code, 200)
with catch_warnings(record=True): # silence RuleMissing warning
with catch_stdout() as stdout:
filename = os.path.basename(get_temporary_filename(
directory=make_static.assets_folder
))
time.sleep(0.05)
self.assertEqual(
stdout.getvalue(),
'Flask-MakeStatic: detected new asset %s, compiling\n' %
filename
)
with closing(client.get('/static/' + filename)) as response:
self.assertEqual(response.status_code, 404)
with catch_stdout() as stdout:
bump_modification_time(
os.path.join(make_static.assets_folder, 'foo')
)
time.sleep(0.05)
self.assertEqual(
stdout.getvalue(),
'Flask-MakeStatic: detected change in foo, compiling\n'
)
with closing(client.get('/static/foo')) as response:
self.assertEqual(response.status_code, 200)
def test_static_view_globbing(self):
config = {'MAKESTATIC_FILEPATTERN_FORMAT': 'globbing'}
for context in self.get_watcher_contexts('working_globbing', config):
with context as (app, make_static):
client = app.test_client()
with closing(client.get('/static/foo')) as response:
self.assertEqual(response.status_code, 200)
with closing(client.get('/static/spam')) as response:
self.assertEqual(response.status_code, 200)
with closing(client.get('/static/bar')) as response:
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data, b'abc\ndef\n')
with closing(client.get('/static/baz')) as response:
self.assertEqual(response.status_code, 404)
with closing(client.get('/static/eggs.css')) as response:
self.assertEqual(response.status_code, 200)
with catch_warnings(record=True): # silence RuleMissing warning
with catch_stdout() as stdout:
filename = os.path.basename(get_temporary_filename(
directory=make_static.assets_folder
))
time.sleep(0.05)
self.assertEqual(
stdout.getvalue(),
'Flask-MakeStatic: detected new asset %s, compiling\n' %
filename
)
with closing(client.get('/static/' + filename)) as response:
self.assertEqual(response.status_code, 404)
with catch_stdout() as stdout:
bump_modification_time(
os.path.join(make_static.assets_folder, 'foo')
)
time.sleep(0.05)
self.assertEqual(
stdout.getvalue(),
'Flask-MakeStatic: detected change in foo, compiling\n'
)
with closing(client.get('/static/foo')) as response:
self.assertEqual(response.status_code, 200)
def test_compile(self):
app = Flask('working')
make_static = MakeStatic(app)
make_static.compile()
# ensure that app is not patched and assets are not compiled on demand
app = Flask('working')
client = app.test_client()
with closing(client.get('/static/foo')) as response:
self.assertEqual(response.status_code, 200)
with closing(client.get('/static/spam')) as response:
self.assertEqual(response.status_code, 200)
with closing(client.get('/static/bar')) as response:
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data, b'abc\ndef\n')
with closing(client.get('/static/eggs.css')) as response:
self.assertEqual(response.status_code, 200)
def test_compile_in_app_context(self):
app = Flask('working')
make_static = MakeStatic()
make_static.init_app(app)
with app.app_context():
make_static.compile()
# ensure that app is not patched and assets are not compiled on demand
app = Flask('working')
client = app.test_client()
with closing(client.get('/static/foo')) as response:
self.assertEqual(response.status_code, 200)
with closing(client.get('/static/spam')) as response:
self.assertEqual(response.status_code, 200)
with closing(client.get('/static/bar')) as response:
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data, b'abc\ndef\n')
with closing(client.get('/static/eggs.css')) as response:
self.assertEqual(response.status_code, 200)
def test_compile_globbing(self):
app = Flask('working_globbing')
app.config['MAKESTATIC_FILEPATTERN_FORMAT'] = 'globbing'
make_static = MakeStatic(app)
make_static.compile()
# ensure that app is not patched and assets are not compiled on demand
app = Flask('working_globbing')
client = app.test_client()
with closing(client.get('/static/foo')) as response:
self.assertEqual(response.status_code, 200)
with closing(client.get('/static/spam')) as response:
self.assertEqual(response.status_code, 200)
with closing(client.get('/static/bar')) as response:
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data, b'abc\ndef\n')
with closing(client.get('/static/eggs.css')) as response:
self.assertEqual(response.status_code, 200)
def test_compile_warns_on_missing_rule(self):
app = Flask('missing_rule')
make_static = MakeStatic(app)
with catch_warnings(record=True) as warnings:
make_static.compile()
self.assertEqual(len(warnings), 1)
self.assertTrue(issubclass(warnings[-1].category, RuleMissing))
self.assertEqual(str(warnings[-1].message),
'cannot find a rule for foo.css')
def test_compile_factory_pattern_without_app_context(self):
app = Flask('working')
make_static = MakeStatic()
make_static.init_app(app)
self.assertRaises(RuntimeError, make_static.compile)
class WatcherTestCase(unittest.TestCase):
def assert_(self, **kwargs):
self.assertEqual(self.added_files, kwargs.pop('added_files', []))
del self.added_files[:]
self.assertEqual(self.modified_files, kwargs.pop('modified_files', []))
del self.modified_files[:]
self.assertEqual(self.removed_files, kwargs.pop('removed_files', []))
del self.removed_files[:]
self.assertEqual(self.added_directories,
kwargs.pop('added_directories', []))
del self.added_directories[:]
self.assertEqual(self.modified_directories,
kwargs.pop('modified_directories', []))
del self.modified_directories[:]
self.assertEqual(self.removed_directories,
kwargs.pop('removed_directories', []))
del self.removed_directories[:]
if kwargs:
raise TypeError(
'unexpected keyword argument: %s' % kwargs.popitem()[0]
)
def test(self):
watcher = ThreadedWatcher()
self.added_files = []
self.modified_files = []
self.removed_files = []
self.added_directories = []
self.modified_directories = []
self.removed_directories = []
watcher.file_added.connect(self.added_files.append)
watcher.file_modified.connect(self.modified_files.append)
watcher.file_removed.connect(self.removed_files.append)
watcher.directory_added.connect(self.added_directories.append)
watcher.directory_modified.connect(self.modified_directories.append)
watcher.directory_removed.connect(self.removed_directories.append)
directory = get_temporary_directory()
watcher.add_directory(directory)
watcher.watch(sleep=0.01)
foo = os.path.join(directory, 'foo')
open(foo, 'w').close()
time.sleep(0.05) # reasonable amount of reaction time
self.assert_(added_files=[foo], modified_directories=[directory])
bump_modification_time(foo)
time.sleep(0.05)
self.assert_(modified_files=[foo], modified_directories=[directory])
os.remove(foo)
time.sleep(0.05)
self.assert_(removed_files=[foo], modified_directories=[directory])
bar = os.path.join(directory, 'bar')
os.mkdir(bar)
time.sleep(0.05)
self.assert_(added_directories=[bar], modified_directories=[directory])
baz = os.path.join(bar, 'baz')
open(baz, 'w').close()
time.sleep(0.05)
self.assert_(added_files=[baz], modified_directories=[bar])
bump_modification_time(baz)
time.sleep(0.05)
self.assert_(modified_files=[baz], modified_directories=[bar])
os.remove(baz)
time.sleep(0.05)
self.assert_(removed_files=[baz], modified_directories=[bar])
os.rmdir(bar)
time.sleep(0.05)
self.assert_(removed_directories=[bar],
modified_directories=[directory])
watcher.stop()
time.sleep(0.05)
self.assertEqual(threading.active_count(), 1)
def suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(MakeStaticTestCase))
suite.addTest(unittest.makeSuite(WatcherTestCase))
return suite
if __name__ == '__main__':
unittest.main(defaultTest='suite')