14
14
import unittest
15
15
import warnings
16
16
17
- from six import PY3 , StringIO , b , text_type
17
+ from six import PY3 , StringIO , b , string_types , text_type
18
18
from werkzeug .test import Client
19
19
from werkzeug .wrappers import Response
20
20
28
28
def normalize_path (path ):
29
29
path = os .path .abspath (os .path .normpath (path ))
30
30
return path .replace (os .sep , os .altsep )
31
-
32
- def normalize_source_map_path (path ):
33
- """To workaround strange path separators made by libsass ---
34
- which seems a bug of libsass on win32.
35
-
36
- """
37
- return path .replace (os .altsep , '//' )
38
31
else :
39
32
def normalize_path (path ):
40
33
return path
41
34
42
- normalize_source_map_path = normalize_path
43
-
44
35
45
36
A_EXPECTED_CSS = '''\
46
37
body {
@@ -62,7 +53,7 @@ def normalize_path(path):
62
53
A_EXPECTED_MAP = {
63
54
'version' : 3 ,
64
55
'file' : 'test/a.css' ,
65
- 'sources' : [normalize_source_map_path ( 'test/a.scss' ) ],
56
+ 'sources' : ['test/a.scss' ],
66
57
'sourcesContent' : [],
67
58
'names' : [],
68
59
'mappings' : ';AAKA;EAHE,kBAAkB;;EAIpB,KAAK;IAED,OAAO'
@@ -120,7 +111,42 @@ def normalize_path(path):
120
111
utf8_if_py3 = {'encoding' : 'utf-8' } if PY3 else {}
121
112
122
113
123
- class SassTestCase (unittest .TestCase ):
114
+ class BaseTestCase (unittest .TestCase ):
115
+
116
+ def assert_json_file (self , expected , filename ):
117
+ with open (filename ) as f :
118
+ try :
119
+ tree = json .load (f )
120
+ except ValueError as e :
121
+ f .seek (0 )
122
+ msg = '{0!s}\n \n {1}:\n \n {2}' .format (e , filename , f .read ())
123
+ raise ValueError (msg )
124
+ self .assertEqual (expected , tree )
125
+
126
+ def assert_source_map_equal (self , expected , actual , * args , ** kwargs ):
127
+ if isinstance (expected , string_types ):
128
+ expected = json .loads (expected )
129
+ if isinstance (actual , string_types ):
130
+ actual = json .loads (actual )
131
+ if sys .platform == 'win32' :
132
+ # On Windows the result of "mappings" is strange;
133
+ # seems a bug of libsass itself
134
+ expected .pop ('mappings' , None )
135
+ actual .pop ('mappings' , None )
136
+ self .assertEqual (expected , actual , * args , ** kwargs )
137
+
138
+ def assert_source_map_file (self , expected , filename ):
139
+ with open (filename ) as f :
140
+ try :
141
+ tree = json .load (f )
142
+ except ValueError as e :
143
+ f .seek (0 )
144
+ msg = '{0!s}\n \n {1}:\n \n {2}' .format (e , filename , f .read ())
145
+ raise ValueError (msg )
146
+ self .assert_source_map_equal (expected , tree )
147
+
148
+
149
+ class SassTestCase (BaseTestCase ):
124
150
125
151
def test_version (self ):
126
152
assert re .match (r'^\d+\.\d+\.\d+$' , sass .__version__ )
@@ -143,7 +169,7 @@ def test_and_join(self):
143
169
self .assertEqual ('' , sass .and_join ([]))
144
170
145
171
146
- class CompileTestCase (unittest . TestCase ):
172
+ class CompileTestCase (BaseTestCase ):
147
173
148
174
def test_compile_required_arguments (self ):
149
175
self .assertRaises (TypeError , sass .compile )
@@ -258,10 +284,7 @@ def test_compile_source_map(self):
258
284
),
259
285
actual
260
286
)
261
- self .assertEqual (
262
- A_EXPECTED_MAP ,
263
- json .loads (source_map )
264
- )
287
+ self .assert_source_map_equal (A_EXPECTED_MAP , source_map )
265
288
266
289
def test_compile_source_map_deprecated_source_comments_map (self ):
267
290
filename = 'test/a.scss'
@@ -279,7 +302,7 @@ def test_compile_source_map_deprecated_source_comments_map(self):
279
302
self .assertEqual (1 , len (w ))
280
303
assert issubclass (w [- 1 ].category , DeprecationWarning )
281
304
self .assertEqual (expected , actual )
282
- self .assertEqual (expected_map , actual_map )
305
+ self .assert_source_map_equal (expected_map , actual_map )
283
306
284
307
def test_regression_issue_2 (self ):
285
308
actual = sass .compile (string = '''
@@ -303,7 +326,7 @@ def test_regression_issue_11(self):
303
326
assert normalized == '@media(max-width:3){body{color:black;}}'
304
327
305
328
306
- class BuilderTestCase (unittest . TestCase ):
329
+ class BuilderTestCase (BaseTestCase ):
307
330
308
331
def setUp (self ):
309
332
self .temp_path = tempfile .mkdtemp ()
@@ -359,7 +382,7 @@ def test_output_style(self):
359
382
css )
360
383
361
384
362
- class ManifestTestCase (unittest . TestCase ):
385
+ class ManifestTestCase (BaseTestCase ):
363
386
364
387
def test_normalize_manifests (self ):
365
388
manifests = Manifest .normalize_manifests ({
@@ -401,11 +424,11 @@ def test_build_one(self):
401
424
replace_source_path (B_EXPECTED_CSS_WITH_MAP , 'b.scss' ),
402
425
f .read ()
403
426
)
404
- self .assert_json_file (
427
+ self .assert_source_map_file (
405
428
{
406
429
'version' : 3 ,
407
430
'file' : '../test/b.css' ,
408
- 'sources' : [normalize_source_map_path ( '../test/b.scss' ) ],
431
+ 'sources' : ['../test/b.scss' ],
409
432
'sourcesContent' : [],
410
433
'names' : [],
411
434
'mappings' : ';AAAA,EAAE;EAEE,WAAW'
@@ -419,11 +442,11 @@ def test_build_one(self):
419
442
replace_source_path (D_EXPECTED_CSS_WITH_MAP , 'd.scss' ),
420
443
f .read ()
421
444
)
422
- self .assert_json_file (
445
+ self .assert_source_map_file (
423
446
{
424
447
'version' : 3 ,
425
448
'file' : '../test/d.css' ,
426
- 'sources' : [normalize_source_map_path ( '../test/d.scss' ) ],
449
+ 'sources' : ['../test/d.scss' ],
427
450
'sourcesContent' : [],
428
451
'names' : [],
429
452
'mappings' : ';AAKA;EAHE,kBAAkB;;EAIpB,KAAK;IAED,MAAM'
@@ -433,18 +456,8 @@ def test_build_one(self):
433
456
finally :
434
457
shutil .rmtree (d )
435
458
436
- def assert_json_file (self , expected , filename ):
437
- with open (filename ) as f :
438
- try :
439
- tree = json .load (f )
440
- except ValueError as e :
441
- f .seek (0 )
442
- msg = '{0!s}\n \n {1}:\n \n {2}' .format (e , filename , f .read ())
443
- raise ValueError (msg )
444
- self .assertEqual (expected , tree )
445
-
446
459
447
- class WsgiTestCase (unittest . TestCase ):
460
+ class WsgiTestCase (BaseTestCase ):
448
461
449
462
@staticmethod
450
463
def sample_wsgi_app (environ , start_response ):
@@ -485,7 +498,7 @@ def assert_bytes_equal(self, expected, actual, *args):
485
498
* args )
486
499
487
500
488
- class DistutilsTestCase (unittest . TestCase ):
501
+ class DistutilsTestCase (BaseTestCase ):
489
502
490
503
def tearDown (self ):
491
504
for filename in self .list_built_css ():
@@ -531,7 +544,7 @@ def test_output_style(self):
531
544
)
532
545
533
546
534
- class SasscTestCase (unittest . TestCase ):
547
+ class SasscTestCase (BaseTestCase ):
535
548
536
549
def setUp (self ):
537
550
self .out = StringIO ()
@@ -625,7 +638,7 @@ def test_sassc_sourcemap(self):
625
638
f .read ().strip ()
626
639
)
627
640
with open (out_filename + '.map' ) as f :
628
- self .assertEqual (
641
+ self .assert_source_map_equal (
629
642
dict (A_EXPECTED_MAP , sources = None ),
630
643
dict (json .load (f ), sources = None )
631
644
)
0 commit comments