Skip to content

Commit

Permalink
Fix coding error rise when non-ASCII chars passed
Browse files Browse the repository at this point in the history
  • Loading branch information
dahlia committed May 20, 2014
1 parent 8acbcf6 commit 4151323
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 4 deletions.
9 changes: 9 additions & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
Changelog
=========

Version 0.4.1
-------------

To be released.

- Fixed :exc:`UnicodeEncodeError` that rise when the input source contains
any non-ASCII Unicode characters.


Version 0.4.0
-------------

Expand Down
5 changes: 4 additions & 1 deletion sassc.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,10 @@ def main(argv=sys.argv, stdout=sys.stdout, stderr=sys.stderr):
print(css, file=stdout)
else:
with open(args[1], 'w') as f:
print(css, file=f)
try:
print(css, file=f)
except UnicodeEncodeError:
print(css.encode('utf-8'), file=f)
if options.watch:
print(filename, 'is just compiled to', args[1],
file=stdout)
Expand Down
46 changes: 44 additions & 2 deletions sasstests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
from __future__ import with_statement

import collections
Expand All @@ -9,7 +10,7 @@
import tempfile
import unittest

from six import StringIO, b
from six import StringIO, b, text_type
from werkzeug.test import Client
from werkzeug.wrappers import Response

Expand Down Expand Up @@ -57,6 +58,13 @@
color: green; }
'''

D_EXPECTED_CSS = '''\
body {
background-color: green; }
body a {
font: '나눔고딕', sans-serif; }
'''


class SassTestCase(unittest.TestCase):

Expand Down Expand Up @@ -141,6 +149,14 @@ def test_compile_string(self):
a b {
color: blue; }
'''
actual = sass.compile(string=u'a { color: blue; } /* 유니코드 */')
self.assertEqual(
u'''a {
color: blue; }
/* 유니코드 */''',
actual
)
self.assertRaises(sass.CompileError, sass.compile,
string='a { b { color: blue; }')
# sass.CompileError should be a subtype of ValueError
Expand All @@ -158,6 +174,11 @@ def test_compile_filename(self):
assert actual == A_EXPECTED_CSS
actual = sass.compile(filename='test/c.sass')
assert actual == C_EXPECTED_CSS
actual = sass.compile(filename='test/d.sass')
if text_type is str:
self.assertEqual(D_EXPECTED_CSS, actual)
else:
self.assertEqual(D_EXPECTED_CSS.decode('utf-8'), actual)
self.assertRaises(IOError, sass.compile,
filename='test/not-exist.sass')
self.assertRaises(TypeError, sass.compile, filename=1234)
Expand Down Expand Up @@ -205,7 +226,7 @@ def test_builder_build_directory(self):
css_path = os.path.join(temp_path, 'css')
shutil.copytree('test', sass_path)
result_files = build_directory(sass_path, css_path)
assert len(result_files) == 3
assert len(result_files) == 4
assert result_files['a.sass'] == 'a.sass.css'
with open(os.path.join(css_path, 'a.sass.css')) as f:
css = f.read()
Expand All @@ -218,6 +239,10 @@ def test_builder_build_directory(self):
with open(os.path.join(css_path, 'c.sass.css')) as f:
css = f.read()
assert css == C_EXPECTED_CSS
assert result_files['d.sass'] == 'd.sass.css'
with open(os.path.join(css_path, 'd.sass.css')) as f:
css = f.read()
self.assertEqual(D_EXPECTED_CSS, css)
shutil.rmtree(temp_path)


Expand Down Expand Up @@ -345,6 +370,23 @@ def test_sassc_output(self):
finally:
os.remove(tmp)

def test_sassc_output_unicode(self):
fd, tmp = tempfile.mkstemp('.css')
try:
os.close(fd)
exit_code = sassc.main(['sassc', 'test/d.sass', tmp],
self.out, self.err)
self.assertEqual(0, exit_code)
self.assertEqual('', self.err.getvalue())
self.assertEqual('', self.out.getvalue())
with open(tmp) as f:
self.assertEqual(
D_EXPECTED_CSS.strip(),
f.read().strip()
)
finally:
os.remove(tmp)

def test_sassc_source_map_without_css_filename(self):
exit_code = sassc.main(['sassc', '-m', 'a.scss'], self.out, self.err)
self.assertEqual(2, exit_code)
Expand Down
5 changes: 4 additions & 1 deletion sassutils/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ def build_directory(sass_path, css_path, _root_sass=None, _root_css=None):
css_fullname = os.path.join(css_path, name) + '.css'
css = compile(filename=sass_fullname, include_paths=[_root_sass])
with open(css_fullname, 'w') as css_file:
css_file.write(css)
try:
css_file.write(css)
except UnicodeEncodeError:
css_file.write(css.encode('utf-8'))
result[os.path.relpath(sass_fullname, _root_sass)] = \
os.path.relpath(css_fullname, _root_css)
elif os.path.isdir(sass_fullname):
Expand Down
11 changes: 11 additions & 0 deletions test/d.sass
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

@mixin mx {
background-color: green;
}

body {
@include mx;
a {
font: '나눔고딕', sans-serif;
}
}

0 comments on commit 4151323

Please sign in to comment.