Skip to content

Conversation

BeyondEvil
Copy link
Contributor

Addresses issue #77 ping @davehunt

@BeyondEvil
Copy link
Contributor Author

haha, I just can't get a win. Not sure why there's two commits in there. However, I do recognize the additional commit as one of my old PRs (encoding test issue).

content = extra.get('content')
if re.match('^file|^http', content):
html_div = html.img(src=content)
elif self.self_contained:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm... so this means if the --self-contained-html flag is passed, these images will not be displayed in the report at all. Whilst this is possibly acceptable (I don't think we should be downloading/reading these files) it could be confusing to users. Perhaps we could add a warning to the documentation that standalone reports may still include assets referenced by URL? We could also log a warning for each URL based asset when we're in standalone mode.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add a warning to the docs and a logging warning (using pytest warning?).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@davehunt I need some guidance on how to add the pytest warning. Too many options. Do I get the terminal reporter via getplugin()? Do I use one of the hooks? etc.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would use:

import warnings
warnings.warn('blah blah blah')

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just FYI, pytest-3.1 (to be released this week) automatically shows warnings such as these in a summary at the end of the test suite, so this is the best suggestion at the moment. 👍

Copy link
Contributor Author

@BeyondEvil BeyondEvil May 15, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wording OK with you, @davehunt ?

    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:```

Copy link
Contributor Author

@BeyondEvil BeyondEvil May 15, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"so this means if the --self-contained-html flag is passed, these images will not be displayed in the report at all. "

Hmm... they should be, whatever html.img() we create gets added after the if/elifs.

I tested with all types with and w/o --self-contained, and it worked.

Tested it again with the image gallery PR.

EDIT: I think I get what you mean now, the report will show all images as long as they are local to the report, sharing the report will break the images.


@pytest.mark.parametrize('type', ["https://", "file://"])
def test_extra_image_non_b64(self, testdir, type):
content = type
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

type is reserved, so we should pick another name for this parameter.

if extra.get('format') == extras.FORMAT_IMAGE:
if self.self_contained:
content = extra.get('content')
if re.match('^file|^http', content):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this won't cover absolute or relative paths.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what you mean... What won't cover absolute or relative paths? What other types are there? While testing this I used an image with an absolute path.

Do you want to cover relative paths as well?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean if I passed /absolute/path/to/file, C:\absolute\path\to\file, or ../relative/path/to/file then this would not work. Perhaps simply using os.path to check if the content is a valid path, or we could leave this as a future enhancement.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, I can give it a try, and if it gets to complex we'll leave it for the future.

if extra.get('format') == extras.FORMAT_IMAGE:
if self.self_contained:
content = extra.get('content')
if re.match('^file|^http', content):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean if I passed /absolute/path/to/file, C:\absolute\path\to\file, or ../relative/path/to/file then this would not work. Perhaps simply using os.path to check if the content is a valid path, or we could leave this as a future enhancement.

if extra.get('format') == extras.FORMAT_IMAGE:
if self.self_contained:
content = extra.get('content')
if re.match('^file|^http', content):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than importing re you could just use startswith.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha, I started with that but thought that re was better looking :P
However, with the abs/rel path change, I'll revert back to startswith 👍

README.rst Outdated
Image ``extra.image('http://some_image.png')``
========== ============================================

**Note**: When using ``--self-contained-html``, images added as files or links may not work as expected.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we expand this a little more. These images are going to be linked as external resources even when --self-contained-html is specified. The expectation is that this command line option means there's a standalone HTML file that contains all resources.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also think we should move this to the 'Creating a self-contained report' section, and reference/link to that section here.

if self.self_contained:
warnings.warn('Images added via file or link, '
'may not work as expected when '
'using --self-contained-html')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer something like "Self-contained HTML report includes link to external resource: /foo/bar"

assert os.path.exists(src)

@pytest.mark.parametrize('src_type', ["https://", "file://"])
def test_extra_image_non_b64(self, testdir, src_type):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For coverage, we should also include a test that creates a file in the testdir and references it as an image path.

assert result.ret == 0
assert '<img src="{0}"/>'.format(content) in html

def test_extra_text_encoding(self, testdir):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this test has sneaked into your patch somehow. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wrote this way back when you were having encoding issues. I can remove it if you don't want it.

#107

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ping @davehunt

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've started looking at the image/slider PR @davehunt, I've got most of your requests in place, I just need to fine tune the CSS.

However, I'd prefer to get this PR out of the way first, since they hit more or less exactly the same code.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I landed a change to the tests that covered this in https://github.com/pytest-dev/pytest-html/pull/108/files so this test isn't required. You can leave it here and I'll remove it before merging, if you prefer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed.

README.rst Outdated

**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 <self-contained>` section for more info.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need to create a reference as all titles can already be referenced. This would work as:

**Note**: When using ``--self-contained-html``, images added as files or links
may not work as expected, see section `Creating a self-contained report`_ for
more info.

README.rst Outdated
you have this package installed, then ANSI codes will be converted to HTML in
your report.

.. _self-contained:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't needed, as the title can already be referenced.

README.rst Outdated
.. _self-contained:

Creating a self-contained report
----------------------------------
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mind trimming the underline here so it doesn't exceed the title length? 👼

extra.append(pytest_html.extras.text('some string', name='Different title'))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this came in through a merge/rebase? I can fix this when we come to merge if you like.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, please. I have no idea if it belongs or not.

warnings.warn('Self-contained HTML report '
'includes link to external '
'resource: {}'.format(content))
html_div = html.img(src=content)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no link here to the image. We should set href to the value of content and include it in the html_div.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll fix that here, but that is taken care of in the image gallery PR. 👍

testdir.makefile('.png', image='pretty picture')
result, html = run(testdir, 'report.html')
assert result.ret == 0
assert '<img src="{0}"/>'.format(content) in html
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's also assert the href attribute.

@BeyondEvil
Copy link
Contributor Author

Ping @davehunt New PR, hope it is to your satisfaction. :)

@davehunt
Copy link
Collaborator

davehunt commented Jun 6, 2017

This looks great. I'll do a bit of testing locally with it before merging.

@davehunt
Copy link
Collaborator

davehunt commented Jun 6, 2017

Merged in f19a86d

@davehunt davehunt closed this Jun 6, 2017
@BeyondEvil BeyondEvil deleted the ss_as_file branch June 6, 2017 19:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants