Skip to content

Preserve line endings that libsass hands us #161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions sass.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import collections
import functools
import inspect
from io import open
import io
import os
import os.path
import re
Expand Down Expand Up @@ -236,8 +236,9 @@ def compile_dirname(
if s:
v = v.decode('UTF-8')
mkdirp(os.path.dirname(output_filename))
with open(output_filename, 'w',
encoding="UTF-8") as output_file:
with io.open(
output_filename, 'w', encoding='UTF-8', newline='',
) as output_file:
output_file.write(v)
else:
return False, v
Expand Down
6 changes: 4 additions & 2 deletions sassc.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,15 @@ def main(argv=sys.argv, stdout=sys.stdout, stderr=sys.stderr):
if len(args) < 2:
print(css, file=stdout)
else:
with io.open(args[1], 'w', encoding='utf-8') as f:
with io.open(args[1], 'w', encoding='utf-8', newline='') as f:
f.write(css)
if options.watch:
print(filename, 'is just compiled to', args[1],
file=stdout)
if source_map_filename:
with io.open(source_map_filename, 'w', encoding='utf-8') as f:
with io.open(
source_map_filename, 'w', encoding='utf-8', newline='',
) as f:
f.write(source_map)
if options.watch: # pragma: no cover
# FIXME: we should utilize inotify on Linux, and FSEvents on Mac
Expand Down
15 changes: 5 additions & 10 deletions sasstests.py
Original file line number Diff line number Diff line change
Expand Up @@ -643,25 +643,20 @@ def test_wsgi_sass_middleware(self):
client = Client(app, Response)
r = client.get('/asdf')
assert r.status_code == 200
self.assert_bytes_equal(b'/asdf', r.data)
self.assertEqual(b'/asdf', r.data)
assert r.mimetype == 'text/plain'
r = client.get('/static/a.scss.css')
assert r.status_code == 200
self.assert_bytes_equal(
self.assertEqual(
b(A_EXPECTED_CSS_WITH_MAP),
r.data
r.data,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a comma of imperfection :)

Copy link
Member Author

@asottile asottile Aug 24, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing commas are a best practice :D

)
assert r.mimetype == 'text/css'
r = client.get('/static/not-exists.sass.css')
assert r.status_code == 200
self.assert_bytes_equal(b'/static/not-exists.sass.css', r.data)
self.assertEqual(b'/static/not-exists.sass.css', r.data)
assert r.mimetype == 'text/plain'

def assert_bytes_equal(self, expected, actual, *args):
self.assertEqual(expected.replace(b'\r\n', b'\n'),
actual.replace(b'\r\n', b'\n'),
*args)


class DistutilsTestCase(BaseTestCase):

Expand Down Expand Up @@ -749,7 +744,7 @@ def test_sassc_output(self):
assert exit_code == 0
assert self.err.getvalue() == ''
assert self.out.getvalue() == ''
with open(tmp) as f:
with io.open(tmp, encoding='UTF-8', newline='') as f:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this acts as a regression test

assert A_EXPECTED_CSS.strip() == f.read().strip()
finally:
os.remove(tmp)
Expand Down
10 changes: 7 additions & 3 deletions sassutils/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ def build_directory(sass_path, css_path, output_style='nested',
css = compile(filename=sass_fullname,
output_style=output_style,
include_paths=[_root_sass])
with io.open(css_fullname, 'w', encoding='utf-8') as css_file:
with io.open(
css_fullname, 'w', encoding='utf-8', newline='',
) as css_file:
css_file.write(css)
result[os.path.relpath(sass_fullname, _root_sass)] = \
os.path.relpath(css_fullname, _root_css)
Expand Down Expand Up @@ -215,10 +217,12 @@ def build_one(self, package_dir, filename, source_map=False):
css_folder = os.path.dirname(css_path)
if not os.path.exists(css_folder):
os.makedirs(css_folder)
with io.open(css_path, 'w', encoding='utf-8') as f:
with io.open(css_path, 'w', encoding='utf-8', newline='') as f:
f.write(css)
if source_map:
# Source maps are JSON, and JSON has to be UTF-8 encoded
with io.open(source_map_path, 'w', encoding='utf-8') as f:
with io.open(
source_map_path, 'w', encoding='utf-8', newline='',
) as f:
f.write(source_map)
return css_filename