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 @@
7 changes: 7 additions & 0 deletions tests/regression_tests/regr-01-copy-binary-file/.moban.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
configuration:
template_dir:
- copy-source
targets:
- output: regression-test.png
template: image.png
template_type: copy
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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)
50 changes: 50 additions & 0 deletions tests/test_regression.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import os
import sys
from textwrap import dedent

from mock import patch
from nose.tools import eq_

from moban.main import main
import filecmp


def custom_dedent(long_texts):
refined = dedent(long_texts)
if refined.startswith("\n"):
refined = refined[1:]
return refined


class TestRegression:
def setUp(self):
self.current = os.getcwd()

def test_coping_binary_file(self):
folder = "regr-01-copy-binary-file"
args = ["moban"]
self._raw_moban(
args,
folder,
os.path.join("copy-source", "image.png"),
"regression-test.png")

def _raw_moban(self, args, folder, expected, output):
base_dir = os.path.join("tests", "regression_tests")
os.chdir(os.path.join(base_dir, folder))
with patch.object(sys, "argv", args):
main()
status = filecmp.cmp(output, expected)
os.unlink(output)
assert status

def tearDown(self):
if os.path.exists(".moban.hashes"):
os.unlink(".moban.hashes")
os.chdir(self.current)


def _verify_content(file_name, expected):
with open(file_name, "r") as f:
content = f.read()
eq_(content, expected)