From eac77bd2f9b9804db2917a86a4f0e46d4d02392b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jim=20Bra=CC=88nnlund?= Date: Tue, 7 Mar 2017 21:16:12 +0100 Subject: [PATCH 1/5] Test to deal with issue #106 --- testing/test_pytest_html.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/testing/test_pytest_html.py b/testing/test_pytest_html.py index 547aeed5..dbcc861c 100644 --- a/testing/test_pytest_html.py +++ b/testing/test_pytest_html.py @@ -418,6 +418,21 @@ def test_fail(): assert link in html assert os.path.exists(src) + def test_extra_text_encoding(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'\u2318')] + """) + testdir.makepyfile('def test_pass(): pass') + result, html = run(testdir, 'report.html', '--self-contained-html') + assert result.ret == 0 + def test_no_environment(self, testdir): testdir.makeconftest(""" def pytest_configure(config): From 64d7a2de3cf1e1bcedf40027c6660a403d550720 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jim=20Br=C3=A4nnlund?= Date: Mon, 8 May 2017 16:27:55 +0200 Subject: [PATCH 2/5] Add ability to add image via file and link --- .gitignore | 1 + README.rst | 2 ++ pytest_html/plugin.py | 17 +++++++++-------- testing/test_pytest_html.py | 18 ++++++++++++++++++ 4 files changed, 30 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index 7cf62bcd..a05244ee 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ __pycache__ .DS_Store .cache +.eggs .tox build dist diff --git a/README.rst b/README.rst index 3cb9cbc3..8aa86f1e 100644 --- a/README.rst +++ b/README.rst @@ -94,6 +94,8 @@ Raw HTML ``extra.html('
Additional HTML
')`` Plain text ``extra.text('Add some simple Text')`` URL ``extra.url('http://www.example.com/')`` Image ``extra.image(image, mime_type='image/gif', extension='gif')`` +Image ``extra.image('file:///path/to/file.png')`` +Image ``extra.image('http://some_image.png')`` ========== ============================================ There are also convenient types for several image formats: diff --git a/pytest_html/plugin.py b/pytest_html/plugin.py index afc6275a..554c6647 100644 --- a/pytest_html/plugin.py +++ b/pytest_html/plugin.py @@ -13,6 +13,7 @@ import time import bisect import hashlib +import re try: from ansi2html import Ansi2HTMLConverter, style @@ -157,14 +158,15 @@ def create_asset(self, content, extra_index, def append_extra_html(self, extra, extra_index, test_index): href = None if extra.get('format') == extras.FORMAT_IMAGE: - if self.self_contained: + content = extra.get('content') + if re.match('^file|^http', content): + html_div = html.img(src=content) + elif self.self_contained: src = 'data:{0};base64,{1}'.format( extra.get('mime_type'), - extra.get('content')) - self.additional_html.append(html.div( - html.img(src=src), class_='image')) + content) + html_div = html.img(src=src) else: - content = extra.get('content') if PY3: content = b64decode(content.encode('utf-8')) else: @@ -172,9 +174,8 @@ def append_extra_html(self, extra, extra_index, test_index): href = src = self.create_asset( content, extra_index, test_index, extra.get('extension'), 'wb') - self.additional_html.append(html.div( - html.a(html.img(src=src), href=href), - class_='image')) + html_div = html.a(html.img(src=src), href=href) + self.additional_html.append(html.div(html_div, class_='image')) elif extra.get('format') == extras.FORMAT_HTML: self.additional_html.append(html.div( diff --git a/testing/test_pytest_html.py b/testing/test_pytest_html.py index dbcc861c..dcdba047 100644 --- a/testing/test_pytest_html.py +++ b/testing/test_pytest_html.py @@ -418,6 +418,24 @@ def test_fail(): assert link in html assert os.path.exists(src) + @pytest.mark.parametrize('type', ["https://", "file://"]) + def test_extra_image_non_b64(self, testdir, type): + content = type + 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.image('{0}')] + """.format(content)) + testdir.makepyfile('def test_pass(): pass') + result, html = run(testdir, 'report.html') + assert result.ret == 0 + assert ''.format(content) in html + def test_extra_text_encoding(self, testdir): testdir.makeconftest(""" import pytest From edc27986028290b722fa7009d6df1e2567ba1422 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jim=20Br=C3=A4nnlund?= Date: Tue, 16 May 2017 11:06:21 +0200 Subject: [PATCH 3/5] Trying to please Master Dave --- README.rst | 2 ++ pytest_html/plugin.py | 9 +++++++-- testing/test_pytest_html.py | 6 +++--- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/README.rst b/README.rst index 8aa86f1e..903c76a0 100644 --- a/README.rst +++ b/README.rst @@ -98,6 +98,8 @@ Image ``extra.image('file:///path/to/file.png')`` Image ``extra.image('http://some_image.png')`` ========== ============================================ +**Note**: When using ``--self-contained-html``, images added as files or links may not work as expected. + There are also convenient types for several image formats: ============ ==================== diff --git a/pytest_html/plugin.py b/pytest_html/plugin.py index 554c6647..adf993a4 100644 --- a/pytest_html/plugin.py +++ b/pytest_html/plugin.py @@ -13,7 +13,7 @@ import time import bisect import hashlib -import re +import warnings try: from ansi2html import Ansi2HTMLConverter, style @@ -159,7 +159,12 @@ def append_extra_html(self, extra, extra_index, test_index): href = None if extra.get('format') == extras.FORMAT_IMAGE: content = extra.get('content') - if re.match('^file|^http', content): + if content.startswith(('file', 'http')) or \ + os.path.isfile(content): + if self.self_contained: + warnings.warn('Images added via file or link, ' + 'may not work as expected when ' + 'using --self-contained-html') html_div = html.img(src=content) elif self.self_contained: src = 'data:{0};base64,{1}'.format( diff --git a/testing/test_pytest_html.py b/testing/test_pytest_html.py index dcdba047..764c7937 100644 --- a/testing/test_pytest_html.py +++ b/testing/test_pytest_html.py @@ -418,9 +418,9 @@ def test_fail(): assert link in html assert os.path.exists(src) - @pytest.mark.parametrize('type', ["https://", "file://"]) - def test_extra_image_non_b64(self, testdir, type): - content = type + @pytest.mark.parametrize('src_type', ["https://", "file://"]) + def test_extra_image_non_b64(self, testdir, src_type): + content = src_type testdir.makeconftest(""" import pytest @pytest.mark.hookwrapper From 93230f111a357de45d93b18251108197db657098 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jim=20Br=C3=A4nnlund?= Date: Wed, 17 May 2017 16:06:32 +0200 Subject: [PATCH 4/5] Added test for extra image and some doc updates --- README.rst | 17 +++++++++++++++-- pytest_html/plugin.py | 6 +++--- testing/test_pytest_html.py | 4 +++- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/README.rst b/README.rst index 903c76a0..fd9e4578 100644 --- a/README.rst +++ b/README.rst @@ -52,6 +52,8 @@ of a less permissive license, this package is not included as a dependency. If you have this package installed, then ANSI codes will be converted to HTML in your report. +.. _self-contained: + Creating a self-contained report ---------------------------------- @@ -65,6 +67,12 @@ convenient when sharing your results. This can be done in the following way: $ pytest --html=report.html --self-contained-html +Images added as files or links are going to be linked as external resources, +meaning that the standalone report HTML-file may not display these images +as expected. + +The plugin will issue a warning when adding files or links to the standalone report. + Enhancing reports ----------------- @@ -94,11 +102,16 @@ Raw HTML ``extra.html('
Additional HTML
')`` Plain text ``extra.text('Add some simple Text')`` URL ``extra.url('http://www.example.com/')`` Image ``extra.image(image, mime_type='image/gif', extension='gif')`` -Image ``extra.image('file:///path/to/file.png')`` +Image ``extra.image('/path/to/file.png')`` Image ``extra.image('http://some_image.png')`` ========== ============================================ -**Note**: When using ``--self-contained-html``, images added as files or links may not work as expected. +**Note**: When adding an image from file, the path can be either absolute +or relative. + +**Note**: When using ``--self-contained-html``, images added as files or links +may not work as expected, see the +:ref:`Creating a self-contained report ` section for more info. There are also convenient types for several image formats: diff --git a/pytest_html/plugin.py b/pytest_html/plugin.py index adf993a4..5e1ded53 100644 --- a/pytest_html/plugin.py +++ b/pytest_html/plugin.py @@ -162,9 +162,9 @@ def append_extra_html(self, extra, extra_index, test_index): if content.startswith(('file', 'http')) or \ os.path.isfile(content): if self.self_contained: - warnings.warn('Images added via file or link, ' - 'may not work as expected when ' - 'using --self-contained-html') + warnings.warn('Self-contained HTML report ' + 'includes link to external ' + 'resource: {}'.format(content)) html_div = html.img(src=content) elif self.self_contained: src = 'data:{0};base64,{1}'.format( diff --git a/testing/test_pytest_html.py b/testing/test_pytest_html.py index 764c7937..cb07a737 100644 --- a/testing/test_pytest_html.py +++ b/testing/test_pytest_html.py @@ -418,7 +418,7 @@ def test_fail(): assert link in html assert os.path.exists(src) - @pytest.mark.parametrize('src_type', ["https://", "file://"]) + @pytest.mark.parametrize('src_type', ["https://", "file://", "image.png"]) def test_extra_image_non_b64(self, testdir, src_type): content = src_type testdir.makeconftest(""" @@ -432,6 +432,8 @@ def pytest_runtest_makereport(item, call): report.extra = [extras.image('{0}')] """.format(content)) testdir.makepyfile('def test_pass(): pass') + if src_type == "image.png": + testdir.makefile('.png', image='pretty picture') result, html = run(testdir, 'report.html') assert result.ret == 0 assert ''.format(content) in html From 7906504aa5ef92e98ef9ed1a8ff47f762866695f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jim=20Br=C3=A4nnlund?= Date: Mon, 29 May 2017 16:34:18 +0200 Subject: [PATCH 5/5] moar requested changes --- README.rst | 8 +++----- pytest_html/plugin.py | 2 +- testing/test_pytest_html.py | 17 +---------------- 3 files changed, 5 insertions(+), 22 deletions(-) diff --git a/README.rst b/README.rst index fd9e4578..128e18fe 100644 --- a/README.rst +++ b/README.rst @@ -52,10 +52,8 @@ of a less permissive license, this package is not included as a dependency. If you have this package installed, then ANSI codes will be converted to HTML in your report. -.. _self-contained: - Creating a self-contained report ----------------------------------- +-------------------------------- In order to respect the `Content Security Policy (CSP) `_, @@ -110,8 +108,8 @@ Image ``extra.image('http://some_image.png')`` or relative. **Note**: When using ``--self-contained-html``, images added as files or links -may not work as expected, see the -:ref:`Creating a self-contained report ` section for more info. +may not work as expected, see section `Creating a self-contained report`_ for +more info. There are also convenient types for several image formats: diff --git a/pytest_html/plugin.py b/pytest_html/plugin.py index 5e1ded53..c530ae63 100644 --- a/pytest_html/plugin.py +++ b/pytest_html/plugin.py @@ -165,7 +165,7 @@ def append_extra_html(self, extra, extra_index, test_index): warnings.warn('Self-contained HTML report ' 'includes link to external ' 'resource: {}'.format(content)) - html_div = html.img(src=content) + html_div = html.a(html.img(src=content), href=content) elif self.self_contained: src = 'data:{0};base64,{1}'.format( extra.get('mime_type'), diff --git a/testing/test_pytest_html.py b/testing/test_pytest_html.py index cb07a737..e4b8b929 100644 --- a/testing/test_pytest_html.py +++ b/testing/test_pytest_html.py @@ -436,22 +436,7 @@ def pytest_runtest_makereport(item, call): testdir.makefile('.png', image='pretty picture') result, html = run(testdir, 'report.html') assert result.ret == 0 - assert ''.format(content) in html - - def test_extra_text_encoding(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'\u2318')] - """) - testdir.makepyfile('def test_pass(): pass') - result, html = run(testdir, 'report.html', '--self-contained-html') - assert result.ret == 0 + assert ''.format(content) in html def test_no_environment(self, testdir): testdir.makeconftest("""