Skip to content

Commit c61dc5b

Browse files
pinkie1378davehunt
authored andcommitted
Fix for including a screenshot bug on Windows (#136)
Fixes #124
1 parent 764ef3c commit c61dc5b

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

pytest_html/plugin.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from __future__ import absolute_import
66

77
from base64 import b64encode, b64decode
8+
from os.path import isfile
89
import datetime
910
import json
1011
import os
@@ -159,8 +160,14 @@ def append_extra_html(self, extra, extra_index, test_index):
159160
href = None
160161
if extra.get('format') == extras.FORMAT_IMAGE:
161162
content = extra.get('content')
162-
if content.startswith(('file', 'http')) or \
163-
os.path.isfile(content):
163+
try:
164+
is_uri_or_path = (content.startswith(('file', 'http')) or
165+
isfile(content))
166+
except ValueError:
167+
# On Windows, os.path.isfile throws this exception when
168+
# passed a b64 encoded image.
169+
is_uri_or_path = False
170+
if is_uri_or_path:
164171
if self.self_contained:
165172
warnings.warn('Self-contained HTML report '
166173
'includes link to external '

testing/test_pytest_html.py

+6
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,12 @@ def pytest_runtest_makereport(item, call):
326326
src = 'data:{0};base64,{1}'.format(mime_type, content)
327327
assert '<img src="{0}"/>'.format(src) in html
328328

329+
def test_extra_image_windows(self, mocker, testdir):
330+
mock_isfile = mocker.patch('pytest_html.plugin.isfile')
331+
mock_isfile.side_effect = ValueError('stat: path too long for Windows')
332+
self.test_extra_image(testdir, 'image/png', 'png')
333+
assert mock_isfile.call_count == 1
334+
329335
@pytest.mark.parametrize('content', [
330336
("u'\u0081'"),
331337
("'foo'"),

tox.ini

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ commands = pytest -v -r a {posargs}
1111
deps =
1212
pytest-xdist
1313
pytest-rerunfailures
14+
pytest-mock
1415
py{27,36,py,py3}-ansi2html: ansi2html
1516

1617
[testenv:flake8]

0 commit comments

Comments
 (0)