-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathtest_scss.py
More file actions
439 lines (373 loc) · 18.3 KB
/
Copy pathtest_scss.py
File metadata and controls
439 lines (373 loc) · 18.3 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
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
# -*- coding: utf-8 -*-
from __future__ import with_statement
try:
import unittest2 as unittest
except ImportError:
import unittest # NOQA
import os
import os.path as op
import shutil
try:
from unittest.mock import Mock, patch
except ImportError:
from mock import Mock, patch
from flask import Flask
import flask_scss
import time
import pathlib
SCSS_CONTENT = "a { color: red; text-decoration: none; }"
SCSS_UNICODE_CONTENT = "a { content: \"ビール ሳድስ ๛\";}"
SCSS_CONTENT_WITH_PARTIAL = "@import \"test\";\na { color: red; text-decoration: none; }"
TEST_PARTIAL = ".test{color: white;}"
class ScssTest(unittest.TestCase):
def setUp(self):
self.test_data = op.join(os.getcwd(), 'test_data')
os.makedirs(self.test_data)
self.app = Mock()
self.app.root_path = self.test_data
self.app.config = {'SCSS_LOAD_PATHS': []}
self.app.static_folder = op.join(self.test_data, 'static')
def tearDown(self):
shutil.rmtree(self.test_data, ignore_errors=True)
def set_layout(self, base=None):
base = base or self.test_data
self.static_dir = op.join(base, 'static')
os.makedirs(self.static_dir)
self.asset_dir = op.join(base, 'assets')
os.makedirs(self.asset_dir)
def create_asset_file(self, filename, content=SCSS_CONTENT):
filepath = op.join(self.asset_dir, filename)
if not os.path.exists(os.path.dirname(filepath)):
os.makedirs(os.path.dirname(filepath))
with open(filepath, 'w') as f:
f.write(content)
return filepath
def create_static_file(self, filename):
filepath = op.join(self.static_dir, filename)
with open(filepath, 'w') as f:
f.write('nothing')
return filepath
def test_that_required_flask_app_attributes_exist(self):
flask_app = Flask('__main__')
for attr in ['static_folder', 'root_path', 'logger', 'before_request',
'testing', 'debug']:
assert hasattr(flask_app, attr)
def test_set_asset_dir_assets_scss(self):
asset_scss_dir = op.join(self.test_data, 'assets', 'scss')
os.makedirs(asset_scss_dir)
scss_inst = flask_scss.Scss(self.app)
self.assertEqual(scss_inst.asset_dir, asset_scss_dir)
def test_set_asset_dir_assets(self):
asset_dir = op.join(self.test_data, 'assets')
os.makedirs(asset_dir)
scss_inst = flask_scss.Scss(self.app)
self.assertEqual(scss_inst.asset_dir, asset_dir)
def test_set_asset_dir_none(self):
scss_inst = flask_scss.Scss(self.app)
self.assertIsNone(scss_inst.asset_dir)
def test_set_asset_dir_user_input(self):
asset_scss_dir = op.join(self.test_data, 'assets2', 'scss')
os.makedirs(asset_scss_dir)
scss_inst = flask_scss.Scss(self.app,
asset_dir=op.join(self.test_data, 'assets2'))
self.assertEqual(scss_inst.asset_dir, asset_scss_dir)
def test_set_asset_dir_from_app_conf(self):
asset_scss_dir = op.join(self.test_data, 'assets', 'bar')
self.app.config['SCSS_ASSET_DIR'] = asset_scss_dir
os.makedirs(asset_scss_dir)
scss_inst = flask_scss.Scss(self.app)
self.assertEqual(scss_inst.asset_dir, asset_scss_dir)
def test_local_asset_dir_must_override_app_conf(self):
asset_scss_dir = op.join(self.test_data, 'assets', 'bar')
asset_scss_dir2 = op.join(self.test_data, 'assets2', 'bar')
self.app.config['SCSS_ASSET_DIR'] = asset_scss_dir
os.makedirs(asset_scss_dir)
os.makedirs(asset_scss_dir2)
scss_inst = flask_scss.Scss(self.app, asset_dir=asset_scss_dir2)
self.assertEqual(scss_inst.asset_dir, asset_scss_dir2)
def test_set_static_dir_static_css(self):
static_css_dir = op.join(self.test_data, 'static', 'css')
os.makedirs(static_css_dir)
scss_inst = flask_scss.Scss(self.app)
self.assertEqual(scss_inst.static_dir, static_css_dir)
def test_set_static_dir_static(self):
static_dir = op.join(self.test_data, 'static')
os.makedirs(static_dir)
scss_inst = flask_scss.Scss(self.app)
self.assertEqual(scss_inst.static_dir, static_dir)
def test_set_static_dir_none(self):
scss_inst = flask_scss.Scss(self.app)
self.assertIsNone(scss_inst.static_dir)
def test_set_static_dir_user_input(self):
static_css_dir = op.join(self.test_data, 'static2', 'css')
os.makedirs(static_css_dir)
scss_inst = flask_scss.Scss(self.app,
static_dir=op.join(self.test_data, 'static2'))
self.assertEqual(scss_inst.static_dir, static_css_dir)
def test_set_static_dir_from_app_conf(self):
static_scss_dir = op.join(self.test_data, 'static', 'bar')
self.app.config['SCSS_STATIC_DIR'] = static_scss_dir
os.makedirs(static_scss_dir)
scss_inst = flask_scss.Scss(self.app)
self.assertEqual(scss_inst.static_dir, static_scss_dir)
def test_local_static_dir_must_override_app_conf(self):
static_scss_dir = op.join(self.test_data, 'static', 'bar')
static_scss_dir2 = op.join(self.test_data, 'static2', 'bar')
self.app.config['SCSS_STATIC_DIR'] = static_scss_dir
os.makedirs(static_scss_dir)
os.makedirs(static_scss_dir2)
scss_inst = flask_scss.Scss(self.app, static_dir=static_scss_dir2)
self.assertEqual(scss_inst.static_dir, static_scss_dir2)
def test_set_hooks_no_static_dir(self):
asset_dir = op.join(self.test_data, 'assets')
os.makedirs(asset_dir)
flask_scss.Scss(self.app)
self.assertFalse(self.app.before_request.called)
def test_set_hooks_no_asset_dir(self):
static_dir = op.join(self.test_data, 'static')
os.makedirs(static_dir)
flask_scss.Scss(self.app)
self.assertFalse(self.app.before_request.called)
def test_set_hooks_ok(self):
self.set_layout()
scss_inst = flask_scss.Scss(self.app)
self.assertTrue(self.app.before_request.called)
self.assertEqual(self.app.before_request.call_count, 1)
self.app.before_request.assert_called_with(scss_inst.update_scss)
def test_discover_scss(self):
self.set_layout()
self.create_asset_file('foo.scss')
self.create_asset_file('foo.txt')
scss_inst = flask_scss.Scss(self.app)
scss_inst.discover_scss()
self.assertIn(op.join(self.asset_dir, 'foo.scss'), scss_inst.assets)
self.assertNotIn(op.join(self.asset_dir, 'foo.txt'), scss_inst.assets)
def test_scss_discovery_is_recursive(self):
self.set_layout()
self.create_asset_file('foo.scss')
asset_scss_dir = op.join(self.test_data, 'assets', 'bar')
self.create_asset_file('bar/baz.scss')
scss_inst = flask_scss.Scss(self.app)
scss_inst.discover_scss()
self.assertIn(op.join(self.test_data, 'assets', 'bar', 'baz.scss'),
scss_inst.assets)
def test_partial_scss_are_not_considered_assets(self):
self.set_layout()
self.create_asset_file('_bar.scss')
scss_inst = flask_scss.Scss(self.app)
scss_inst.discover_scss()
self.assertNotIn(op.join(self.asset_dir, '_bar.scss'), scss_inst.assets)
def test_partial_scss_are_considered_partials(self):
self.set_layout()
self.create_asset_file('_bar.scss')
scss_inst = flask_scss.Scss(self.app)
scss_inst.discover_scss()
self.assertIn(op.join(self.asset_dir, '_bar.scss'), scss_inst.partials)
def test_asset_tree_is_kept_in_static_dir(self):
self.set_layout()
asset_scss_dir = op.join(self.test_data, 'assets', 'bar')
os.makedirs(asset_scss_dir)
self.create_asset_file('bar/baz.scss')
scss_inst = flask_scss.Scss(self.app)
scss_inst.discover_scss()
asset = op.join(self.test_data, 'assets', 'bar', 'baz.scss')
expected_dest = op.join(self.test_data, 'static', 'bar', 'baz.css')
self.assertEqual(expected_dest, scss_inst.assets[asset],
"css folder not kept")
def test_update_scss_asset_to_update(self):
self.set_layout()
css_path = self.create_static_file('foo.css')
scss_path = self.create_asset_file('foo.scss')
os.utime(css_path, (time.time() - 10, time.time() - 10))
os.utime(scss_path, (time.time() - 5, time.time() - 5))
scss_inst = flask_scss.Scss(self.app)
# check that the css file is older than the scss file
self.assertGreater(op.getmtime(scss_path), op.getmtime(css_path))
scss_inst.update_scss()
#verifies that css file has been modified
self.assertGreater(op.getmtime(css_path), op.getmtime(scss_path))
# verifies that the content of the css file has changed
with open(css_path) as css_file:
css_content = css_file.read()
self.assertNotEqual(css_content, "nothing")
def test_update_scss_nothing_to_update(self):
self.set_layout()
css_path = self.create_static_file('foo.css')
scss_path = self.create_asset_file('foo.scss')
os.utime(scss_path, (time.time() - 10, time.time() - 10))
os.utime(css_path, (time.time() - 5, time.time() - 5))
scss_inst = flask_scss.Scss(self.app)
# check that the css file is newer than the scss file
self.assertGreater(op.getmtime(css_path), op.getmtime(scss_path))
scss_inst.update_scss()
#verifies that css file has been modified
self.assertGreater(op.getmtime(css_path), op.getmtime(scss_path))
# verifies that the content of the css file has NOT changed
with open(css_path) as css_file:
css_content = css_file.read()
self.assertEqual(css_content, "nothing")
# might not be pertinent
def test_update_scss_update_only_changed_scss_files(self):
self.set_layout()
css_must_be_compiled_path = self.create_static_file('foo.css')
css_must_not_change_path = self.create_static_file('bar.css')
scss_newer_path = self.create_asset_file('foo.scss')
scss_older_path = self.create_asset_file('bar.scss')
os.utime(scss_older_path,
(time.time() - 10, time.time() - 10))
os.utime(css_must_not_change_path,
(time.time() - 5, time.time() - 5))
os.utime(scss_newer_path,
(time.time() - 5, time.time() - 5))
os.utime(css_must_be_compiled_path,
(time.time() - 10, time.time() - 10))
scss_inst = flask_scss.Scss(self.app)
self.assertGreater(op.getmtime(css_must_not_change_path),
op.getmtime(scss_older_path))
self.assertGreater(op.getmtime(scss_newer_path),
op.getmtime(css_must_be_compiled_path))
scss_inst.update_scss()
self.assertGreater(op.getmtime(css_must_not_change_path),
op.getmtime(scss_older_path))
self.assertGreater(op.getmtime(css_must_be_compiled_path),
op.getmtime(scss_newer_path))
with open(css_must_not_change_path) as file_in:
css_older_content = file_in.read()
self.assertEqual(css_older_content, "nothing")
with open(css_must_be_compiled_path) as file_in:
css_newer_content = file_in.read()
self.assertNotEqual(css_newer_content, "nothing")
def test_css_is_refreshed_if_partials_are_updated(self):
self.set_layout()
css_must_be_compiled_path = self.create_static_file('foo.css')
scss_path = self.create_asset_file('foo.scss')
scss_partial_path = self.create_asset_file('_bar.scss')
scss_inst = flask_scss.Scss(self.app)
os.utime(scss_partial_path,
(time.time() - 25, time.time() - 25))
scss_inst.discover_scss()
os.utime(scss_partial_path,
(time.time() - 5, time.time() - 5))
os.utime(scss_path,
(time.time() - 15, time.time() - 15))
os.utime(css_must_be_compiled_path,
(time.time() - 10, time.time() - 10))
self.assertGreater(op.getmtime(css_must_be_compiled_path),
op.getmtime(scss_path))
self.assertGreater(op.getmtime(scss_partial_path),
op.getmtime(css_must_be_compiled_path))
scss_inst.update_scss()
self.assertGreater(op.getmtime(css_must_be_compiled_path),
op.getmtime(scss_path))
self.assertGreater(op.getmtime(css_must_be_compiled_path),
op.getmtime(scss_partial_path), "not refreshed")
with open(css_must_be_compiled_path) as file_in:
css_newer_content = file_in.read()
self.assertNotEqual(css_newer_content, "nothing")
def test_it_sets_up_refresh_hooks_if_application_is_in_test_mode(self):
self.app.testing = True
self.app.debug = False
with patch.object(flask_scss.Scss, 'set_hooks') as mock_set_hooks:
flask_scss.Scss(self.app)
self.assertTrue(mock_set_hooks.called)
def test_it_sets_up_refresh_hooks_if_application_is_in_debug_mode(self):
self.app.testing = False
self.app.debug = True
with patch.object(flask_scss.Scss, 'set_hooks') as mock_set_hooks:
flask_scss.Scss(self.app)
self.assertTrue(mock_set_hooks.called)
def test_it_sets_up_refresh_hooks_if_application_is_in_debug_and_testing_mode(self):
self.app.testing = True
self.app.debug = True
with patch.object(flask_scss.Scss, 'set_hooks') as mock_set_hooks:
flask_scss.Scss(self.app)
self.assertTrue(mock_set_hooks.called)
def test_it_does_not_set_up_refresh_hooks_if_application_is_not_in_debug_or_testing_mode(self):
self.app.testing = False
self.app.debug = False
with patch.object(flask_scss.Scss, 'set_hooks') as mock_set_hooks:
flask_scss.Scss(self.app)
self.assertFalse(mock_set_hooks.called)
def test_it_looks_for_an_app_load_path_settings(self):
self.app.config['SCSS_LOAD_PATHS'].append('foo')
scss = flask_scss.Scss(self.app)
self.assertIn(pathlib.Path(os.getcwd(), 'foo'),
scss.compiler.search_path)
def test_it_looks_for_an_app_load_path_settings_with_multiple_paths(self):
paths = ['foo', 'bar']
for path in paths:
self.app.config['SCSS_LOAD_PATHS'].append(path)
scss = flask_scss.Scss(self.app)
for path in paths:
self.assertIn(pathlib.Path(os.getcwd(), path),
scss.compiler.search_path)
def test_app_config_is_overridden_by_local_conf(self):
self.app.config['SCSS_LOAD_PATHS'].append('foo')
scss = flask_scss.Scss(self.app, load_paths=['bar'])
self.assertIn(pathlib.Path(os.getcwd(), 'bar'),
scss.compiler.search_path)
def test_app_config_is_overridden_by_local_conf_with_multiple_paths(self):
self.app.config['SCSS_LOAD_PATHS'].append('foo')
scss = flask_scss.Scss(self.app, load_paths=['bar', 'baz'])
self.assertIn(pathlib.Path(os.getcwd(), 'bar'),
scss.compiler.search_path)
self.assertIn(pathlib.Path(os.getcwd(), 'baz'),
scss.compiler.search_path)
def test_the_asset_dir_is_in_the_load_path(self):
self.set_layout()
inst = flask_scss.Scss(self.app, load_paths=['bar', 'baz'])
self.assertIn(pathlib.Path(inst.asset_dir), inst.compiler.search_path)
def test_compile_scss_creates_subfolders_if_necessary(self):
self.set_layout()
asset_scss_dir = op.join(self.test_data, 'assets', 'bar')
os.makedirs(asset_scss_dir)
self.create_asset_file('bar/baz.scss')
self.create_asset_file('foo.scss')
op.join(self.test_data, 'assets', 'bar', 'baz.scss')
expected_dest = op.join(self.test_data, 'static', 'bar', 'baz.css')
scss_inst = flask_scss.Scss(self.app)
scss_inst.update_scss()
self.assertTrue(os.path.exists(expected_dest))
self.assertTrue(os.path.exists(os.path.join(self.test_data, 'static',
'foo.css')))
def test_update_scss_with_partial(self):
self.set_layout()
css_path = self.create_static_file('foo.css')
scss_path = self.create_asset_file('foo.scss', content=SCSS_CONTENT_WITH_PARTIAL)
scss_path = self.create_asset_file('_test.scss', content=TEST_PARTIAL)
os.utime(css_path, (time.time() - 10, time.time() - 10))
os.utime(scss_path, (time.time() - 5, time.time() - 5))
scss_inst = flask_scss.Scss(self.app)
# check that the css file is older than the scss file
self.assertGreater(op.getmtime(scss_path), op.getmtime(css_path))
scss_inst.update_scss()
#verifies that css file has been modified
self.assertGreater(op.getmtime(css_path), op.getmtime(scss_path))
# verifies that the content of the css file has changed
with open(css_path) as css_file:
css_content = css_file.read()
self.assertIn(".test", css_content)
def test_update_scss_with_unicode_content(self):
self.set_layout()
css_path = self.create_static_file('foo.css')
scss_path = self.create_asset_file('foo.scss', content=SCSS_UNICODE_CONTENT)
os.utime(css_path, (time.time() - 10, time.time() - 10))
os.utime(scss_path, (time.time() - 5, time.time() - 5))
scss_inst = flask_scss.Scss(self.app)
# check that the css file is older than the scss file
self.assertGreater(op.getmtime(scss_path), op.getmtime(css_path))
scss_inst.update_scss()
#verifies that css file has been modified
self.assertGreater(op.getmtime(css_path), op.getmtime(scss_path))
# verifies that the content of the css file has changed
with open(css_path) as css_file:
css_content = css_file.read()
self.assertIn("๛", css_content)
def test_import_scheme_new(self):
try:
from flask.ext.scss import Scss
except ImportError:
assert False, "'from flask.ext.scss import Scss' should work"
if __name__ == "__main__":
#import sys;sys.argv = ['', 'Test.testName']
unittest.main()