Skip to content

Commit

Permalink
Encode all non-binary assets as utf-8. Fixes pytest-dev#98
Browse files Browse the repository at this point in the history
  • Loading branch information
davehunt committed Feb 24, 2017
1 parent e0c147d commit 2817ac5
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 29 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Release Notes

**1.14.0 (unreleased)**

* Fix encoding for asset files
* Escape contents of log sections

**1.13.0 (2016-12-19)**
Expand Down
3 changes: 2 additions & 1 deletion pytest_html/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ def create_asset(self, content, extra_index,

relative_path = '{0}/{1}'.format('assets', asset_file_name)

with open(asset_path, mode) as f:
kwargs = {'encoding': 'utf-8'} if 'b' not in mode else {}
with open(asset_path, mode, **kwargs) as f:
f.write(content)
return relative_path

Expand Down
71 changes: 43 additions & 28 deletions testing/test_pytest_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,17 +327,37 @@ def pytest_runtest_makereport(item, call):
src = 'data:{0};base64,{1}'.format(mime_type, content)
assert '<img src="{0}"/>'.format(src) in html

@pytest.mark.parametrize('file_extension, extra_type, file_type',
[('png', 'image', 'image'),
('png', 'png', 'image'),
('svg', 'svg', 'image'),
('jpg', 'jpg', 'image'),
('json', 'json', 'json'),
('txt', 'text', 'text')])
def test_extra_separated(self, testdir, file_extension,
extra_type, file_type):
content = b64encode(str(random.random())
.encode('utf-8')).decode('ascii')
def test_extra_text_separated(self, testdir):
testdir.makeconftest("""
import pytest
@pytest.mark.hookwrapper
def pytest_runtest_makereport(item, call):
outcome = yield
report = outcome.get_result()
if report.when == 'call':
from pytest_html import extras
report.extra = [extras.text(u'\u0081')]
""")
testdir.makepyfile('def test_pass(): pass')
result, html = run(testdir)
hash_key = ('test_extra_text_separated.py::'
'test_pass01').encode('utf-8')
hash_generator = hashlib.md5()
hash_generator.update(hash_key)
assert result.ret == 0
src = '{0}/{1}'.format('assets', '{0}.txt'.
format(hash_generator.hexdigest()))
link = ('<a class="text" href="{0}" target="_blank">'.format(src))
assert link in html
assert os.path.exists(src)

@pytest.mark.parametrize('file_extension, extra_type', [
('png', 'image'),
('png', 'png'),
('svg', 'svg'),
('jpg', 'jpg')])
def test_extra_image_separated(self, testdir, file_extension, extra_type):
content = b64encode('foo'.encode('utf-8')).decode('ascii')
testdir.makeconftest("""
import pytest
@pytest.mark.hookwrapper
Expand All @@ -350,30 +370,26 @@ def pytest_runtest_makereport(item, call):
""".format(extra_type, content))
testdir.makepyfile('def test_pass(): pass')
result, html = run(testdir)
hash_key = ('test_extra_separated.py::'
hash_key = ('test_extra_image_separated.py::'
'test_pass01').encode('utf-8')
hash_generator = hashlib.md5()
hash_generator.update(hash_key)
assert result.ret == 0
src = '{0}/{1}'.format('assets', '{0}.{1}'.
format(hash_generator.hexdigest(),
file_extension))
link = ('<a class="{0}" href="{1}" '
'target="_blank">'.format(file_type, src))
link = ('<a class="image" href="{0}" target="_blank">'.format(src))
assert link in html
assert os.path.exists(src)

@pytest.mark.parametrize('file_extension, extra_type, file_type',
[('png', 'png', 'image'),
('png', 'image', 'image'),
('svg', 'svg', 'image'),
('jpg', 'jpg', 'image'),
('json', 'json', 'json'),
('txt', 'text', 'text')])
def test_extra_separated_rerun(self, testdir, file_extension,
extra_type, file_type):
content = b64encode(str(random.random())
.encode('utf-8')).decode('ascii')
@pytest.mark.parametrize('file_extension, extra_type', [
('png', 'image'),
('png', 'png'),
('svg', 'svg'),
('jpg', 'jpg')])
def test_extra_image_separated_rerun(self, testdir, file_extension,
extra_type):
content = b64encode('foo'.encode('utf-8')).decode('ascii')
testdir.makeconftest("""
import pytest
@pytest.mark.hookwrapper
Expand All @@ -392,14 +408,13 @@ def test_fail():
result, html = run(testdir)

for i in range(1, 4):
hash_key = ('test_extra_separated_rerun.py::'
hash_key = ('test_extra_image_separated_rerun.py::'
'test_fail0{0}'.format(i)).encode('utf-8')
hash_generator = hashlib.md5()
hash_generator.update(hash_key)
src = 'assets/{0}.{1}'.format(hash_generator.hexdigest(),
file_extension)
link = ('<a class="{0}" href="{1}" '
'target="_blank">'.format(file_type, src))
link = ('<a class="image" href="{0}" target="_blank">'.format(src))
assert result.ret
assert link in html
assert os.path.exists(src)
Expand Down

0 comments on commit 2817ac5

Please sign in to comment.