Skip to content
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
3 changes: 2 additions & 1 deletion moban/plugins/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ def apply_template(self, template_abs_path, template, data, output_file):
template, data, output_file
)
rendered_content = utils.strip_off_trailing_new_lines(rendered_content)
rendered_content = rendered_content.encode("utf-8")
if not isinstance(rendered_content, bytes):
rendered_content = rendered_content.encode("utf-8")
try:
flag = HASH_STORE.is_file_changed(
output_file, rendered_content, template_abs_path
Expand Down
6 changes: 5 additions & 1 deletion moban/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ def file_permissions(afile):

def strip_off_trailing_new_lines(content):
if isinstance(content, bytes):
content = content.decode("utf-8")
try:
content = content.decode("utf-8")
return re.sub(r"(\n\s+)+$", r"\n", content)
except UnicodeDecodeError:
return content
return re.sub(r"(\n\s+)+$", r"\n", content)


Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/non-unicode.char
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
8 changes: 6 additions & 2 deletions tests/test_copy_encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ def setUp(self):

def test_encoding_of_template(self):
template_content = self.engine.get_template("coala_color.svg")
with open("tests/fixtures/coala_color.svg", "r") as expected:
with open("tests/fixtures/coala_color.svg", "rb") as expected:
expected = expected.read()
eq_(expected, template_content.decode("utf-8").replace("\r", ""))
eq_(expected, template_content)
template_content = self.engine.get_template("non-unicode.char")
with open("tests/fixtures/non-unicode.char", "rb") as expected:
expected = expected.read()
eq_(expected, template_content)