Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: set utf-8 encoding #595

Closed
wants to merge 2 commits into from
Closed

Conversation

NextAlone
Copy link

@NextAlone NextAlone commented Mar 15, 2023

In Python 3, the default encoding is UTF-8. However, on Windows with Chinese (CP936/GBK), conflicts in encoding may occur since UTF-8 is not the default. This can cause HTML files written in GBK using the io module to be read as UTF-8 in browsers, resulting in garbled signals.

Signed-off-by: Next Alone [email protected]

NextAlone and others added 2 commits March 15, 2023 10:13
Cause utf-8 is default at python3, the texts transfered among python are
conflict in encoding on Windows with Chinese(cp936, GBK), which is not
use utf-8 as default. This may cause a fact that html files write in
GBK, accoding to io module chooice, will be read as utf-8 in browsers,
which cause grabled signal.

Signed-off-by: Next Alone <[email protected]>
@BeyondEvil
Copy link
Contributor

Can you test 4.0.0-rc0?

https://pypi.org/project/pytest-html/4.0.0rc0/

@NextAlone
Copy link
Author

Can you test 4.0.0-rc0?

https://pypi.org/project/pytest-html/4.0.0rc0/

OK, I'd love to, and I'll let you know the results.

@NextAlone
Copy link
Author

NextAlone commented Mar 15, 2023

Run with py.test test_1.py --html report.html

image
none of logs grabbed in html file

import logging

class Test1:
    def test_1(self):
        logging.info("test_1")

with pytest.ini

[pytest]
log_cli = 1
log_cli_level = INFO
log_cli_date_format = %Y-%m-%d %H:%M:%S
log_cli_format = %(asctime)s %(levelname)s: %(message)s
log_file_level = DEBUG
log_file_date_format = %Y-%m-%d %H:%M:%S
log_file_format = %(asctime)s %(filename)s.%(module)s.%(funcName)s %(lineno)d %(levelname)s: %(message)s

@BeyondEvil
Copy link
Contributor

BeyondEvil commented Mar 15, 2023 via email

@BeyondEvil
Copy link
Contributor

BeyondEvil commented Mar 15, 2023 via email

@NextAlone
Copy link
Author

NextAlone commented Mar 15, 2023

py.test test_1.py --html report.html and py.test test_1.py --html=report.html tried

Can you share what command and flags you’re using?

On 15 Mar 2023 at 12:45 +0100, NextAlone @.>, wrote: none of logs grabbed in html file import logging class Test1: def test_1(self): logging.info("test_1") — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.>

@BeyondEvil
Copy link
Contributor

Can you try adding the -s flag?

@BeyondEvil
Copy link
Contributor

I also think you can drop the dot py.test -> pytest 😊

@NextAlone
Copy link
Author

the same, using pytest -s --html=report.html test_1.py

@BeyondEvil
Copy link
Contributor

Still working on this. It was a little more involved that I anticipated.

But I'm going to, as far as possible at least, adhere to what pytest does depending on the scenario.

@NextAlone
Copy link
Author

NextAlone commented Mar 23, 2023 via email

@BeyondEvil
Copy link
Contributor

BeyondEvil commented Mar 25, 2023

Got that, thanks

PR with the fix:
#603

@NextAlone
Copy link
Author

NextAlone commented Mar 26, 2023

Got that, thanks

PR with the fix: #603

I will test this fix soon to see if it helps solve the problem of Chinese character garbling.

@NextAlone
Copy link
Author

NextAlone commented Mar 27, 2023

INTERNALERROR> Traceback (most recent call last):
INTERNALERROR>   File "/private/tmp/test/.venv/lib/python3.11/site-packages/_pytest/main.py", line 268, in wrap_session
INTERNALERROR>     config.hook.pytest_sessionstart(session=session)
INTERNALERROR>   File "/private/tmp/test/.venv/lib/python3.11/site-packages/pluggy/_hooks.py", line 265, in __call__
INTERNALERROR>     return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult)
INTERNALERROR>            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
INTERNALERROR>   File "/private/tmp/test/.venv/lib/python3.11/site-packages/pluggy/_manager.py", line 80, in _hookexec
INTERNALERROR>     return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
INTERNALERROR>            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
INTERNALERROR>   File "/private/tmp/test/.venv/lib/python3.11/site-packages/pluggy/_callers.py", line 60, in _multicall
INTERNALERROR>     return outcome.get_result()
INTERNALERROR>            ^^^^^^^^^^^^^^^^^^^^
INTERNALERROR>   File "/private/tmp/test/.venv/lib/python3.11/site-packages/pluggy/_result.py", line 60, in get_result
INTERNALERROR>     raise ex[1].with_traceback(ex[2])
INTERNALERROR>   File "/private/tmp/test/.venv/lib/python3.11/site-packages/pluggy/_callers.py", line 39, in _multicall
INTERNALERROR>     res = hook_impl.function(*args)
INTERNALERROR>           ^^^^^^^^^^^^^^^^^^^^^^^^^
INTERNALERROR>   File "/private/tmp/test/pytest-html/src/pytest_html/nextgen.py", line 262, in pytest_sessionstart
INTERNALERROR>     self._generate_report()
INTERNALERROR>   File "/private/tmp/test/pytest-html/src/pytest_html/nextgen.py", line 148, in _generate_report
INTERNALERROR>     rendered_report = self._render_html(
INTERNALERROR>                       ^^^^^^^^^^^^^^^^^^
INTERNALERROR>   File "/private/tmp/test/pytest-html/src/pytest_html/nextgen.py", line 232, in _render_html
INTERNALERROR>     return self._template.render(
INTERNALERROR>            ^^^^^^^^^^^^^^^^^^^^^^
INTERNALERROR>   File "/private/tmp/test/.venv/lib/python3.11/site-packages/jinja2/environment.py", line 1301, in render
INTERNALERROR>     self.environment.handle_exception()
INTERNALERROR>   File "/private/tmp/test/.venv/lib/python3.11/site-packages/jinja2/environment.py", line 936, in handle_exception
INTERNALERROR>     raise rewrite_traceback_stack(source=source)
INTERNALERROR>   File "/private/tmp/test/pytest-html/src/pytest_html/resources/index.jinja2", line 126, in top-level template code
INTERNALERROR>     {% include "app.js" %}
INTERNALERROR>   File "/private/tmp/test/.venv/lib/python3.11/site-packages/jinja2/loaders.py", line 218, in get_source
INTERNALERROR>     raise TemplateNotFound(template)
INTERNALERROR> jinja2.exceptions.TemplateNotFound: app.js

use pip install -e . locally

@BeyondEvil
Copy link
Contributor

BeyondEvil commented Mar 27, 2023

INTERNALERROR> Traceback (most recent call last):
INTERNALERROR>   File "/private/tmp/test/.venv/lib/python3.11/site-packages/_pytest/main.py", line 268, in wrap_session
INTERNALERROR>     config.hook.pytest_sessionstart(session=session)
INTERNALERROR>   File "/private/tmp/test/.venv/lib/python3.11/site-packages/pluggy/_hooks.py", line 265, in __call__
INTERNALERROR>     return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult)
INTERNALERROR>            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
INTERNALERROR>   File "/private/tmp/test/.venv/lib/python3.11/site-packages/pluggy/_manager.py", line 80, in _hookexec
INTERNALERROR>     return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
INTERNALERROR>            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
INTERNALERROR>   File "/private/tmp/test/.venv/lib/python3.11/site-packages/pluggy/_callers.py", line 60, in _multicall
INTERNALERROR>     return outcome.get_result()
INTERNALERROR>            ^^^^^^^^^^^^^^^^^^^^
INTERNALERROR>   File "/private/tmp/test/.venv/lib/python3.11/site-packages/pluggy/_result.py", line 60, in get_result
INTERNALERROR>     raise ex[1].with_traceback(ex[2])
INTERNALERROR>   File "/private/tmp/test/.venv/lib/python3.11/site-packages/pluggy/_callers.py", line 39, in _multicall
INTERNALERROR>     res = hook_impl.function(*args)
INTERNALERROR>           ^^^^^^^^^^^^^^^^^^^^^^^^^
INTERNALERROR>   File "/private/tmp/test/pytest-html/src/pytest_html/nextgen.py", line 262, in pytest_sessionstart
INTERNALERROR>     self._generate_report()
INTERNALERROR>   File "/private/tmp/test/pytest-html/src/pytest_html/nextgen.py", line 148, in _generate_report
INTERNALERROR>     rendered_report = self._render_html(
INTERNALERROR>                       ^^^^^^^^^^^^^^^^^^
INTERNALERROR>   File "/private/tmp/test/pytest-html/src/pytest_html/nextgen.py", line 232, in _render_html
INTERNALERROR>     return self._template.render(
INTERNALERROR>            ^^^^^^^^^^^^^^^^^^^^^^
INTERNALERROR>   File "/private/tmp/test/.venv/lib/python3.11/site-packages/jinja2/environment.py", line 1301, in render
INTERNALERROR>     self.environment.handle_exception()
INTERNALERROR>   File "/private/tmp/test/.venv/lib/python3.11/site-packages/jinja2/environment.py", line 936, in handle_exception
INTERNALERROR>     raise rewrite_traceback_stack(source=source)
INTERNALERROR>   File "/private/tmp/test/pytest-html/src/pytest_html/resources/index.jinja2", line 126, in top-level template code
INTERNALERROR>     {% include "app.js" %}
INTERNALERROR>   File "/private/tmp/test/.venv/lib/python3.11/site-packages/jinja2/loaders.py", line 218, in get_source
INTERNALERROR>     raise TemplateNotFound(template)
INTERNALERROR> jinja2.exceptions.TemplateNotFound: app.js

use pip install -e . locally

Right.

When doing a local install you have to build the javascript:

$ npm ci && npm run build

If you don't want to install Node/npm you can just wait a couple of days. I will release RC1 shortly.

@NextAlone
Copy link
Author

Hello, everything works well, except for a few issues:

  1. The pop method seems to be missing from the Class Cells in BaseReport, which I previously used to modify tables.
  2. The table sort toggle button appears to be malfunctioning. I need to click it twice to reverse the sort order, and the icon does not reverse as it should, resulting in the following sequence: upside (in order) -> downside (in order) -> upside (inverted order).
  3. The sort icon in the Tests column has disappeared.

Thank you for your efforts.

@BeyondEvil
Copy link
Contributor

Hello, everything works well, except for a few issues:

  1. The pop method seems to be missing from the Class Cells in BaseReport, which I previously used to modify tables.
  2. The table sort toggle button appears to be malfunctioning. I need to click it twice to reverse the sort order, and the icon does not reverse as it should, resulting in the following sequence: upside (in order) -> downside (in order) -> upside (inverted order).
  3. The sort icon in the Tests column has disappeared.

Thank you for your efforts.

This is great feedback! Thank you!

  1. The pop method should not be needed any more. Please let me know if something is not working as you expect. A proper use case description would be great!

  2. Could you perhaps record a short video showing the issue?

  3. I see, will investigate. Thank you!

Thank you for your beta-testing (😅) It's greatly appreciated!

@NextAlone
Copy link
Author

NextAlone commented Mar 28, 2023

This is great feedback! Thank you!

  1. The pop method should not be needed any more. Please let me know if something is not working as you expect. A proper use case description would be great!
  2. Could you perhaps record a short video showing the issue?
  3. I see, will investigate. Thank you!

Thank you for your beta-testing (😅) It's greatly appreciated!

  1. I would like to remove the "Links" section and add "Time" and "Message" instead, as the "Links" sections are consistently empty, and I am unsure of their purpose.
    image

  2. I will record a video for you.

@NextAlone
Copy link
Author

2023-03-28.17.26.07.mov

@BeyondEvil
Copy link
Contributor

This is great feedback! Thank you!

  1. The pop method should not be needed any more. Please let me know if something is not working as you expect. A proper use case description would be great!
  2. Could you perhaps record a short video showing the issue?
  3. I see, will investigate. Thank you!

Thank you for your beta-testing (😅) It's greatly appreciated!

  1. I would like to remove the "Links" section and add "Time" and "Message" instead, as the "Links" sections are consistently empty, and I am unsure of their purpose.
    image
  2. I will record a video for you.

Aha, I see.

Ok, I will think of something to make the pop functionality work again.

FYI: Adding (some) of these is what the Links column is used for.

@BeyondEvil
Copy link
Contributor

Nice, thank you!

Can you share the code/configuration for the extra columns?

@BeyondEvil
Copy link
Contributor

So, bad news and good news.

The pop function won't be supported in v4. With the redesign it's going to be extremely hacky to support it.

The good news is that I'm going to remove columns that are empty. Meaning "Links" will not be shown if it's empty.

Does that work for you? Would you prefer a different solution?

@NextAlone
Copy link
Author

So, bad news and good news.

The pop function won't be supported in v4. With the redesign it's going to be extremely hacky to support it.

The good news is that I'm going to remove columns that are empty. Meaning "Links" will not be shown if it's empty.

Does that work for you? Would you prefer a different solution?

That sounds greet, that's enough.

@NextAlone
Copy link
Author

@pytest.hookimpl(optionalhook=True)
def pytest_html_results_table_header(cells):
    """
    Add timestamp to test result table header.
    """
    cells.insert(2, html.th("Time", class_="sortable time", col="time"))
    # cells.pop()
    cells.insert(4, html.th("Message", col="message"))


@pytest.hookimpl(optionalhook=True)
def pytest_html_results_table_row(report, cells):
    """
    Add timestamp to test result table row.
    """
    cells.insert(2, html.td(datetime.now(), class_="col-time"))
    # cells.pop()

    def get_short_message(report_):
        if report_.longrepr:
            res = ""
            ...
            return res
        else:
            return ""

    cells.insert(4, html.td(get_short_message(report), class_="col-message", style="white-space: pre-wrap;"))

@BeyondEvil
Copy link
Contributor

Just released 4.0.0rc1 please give it a try! Would very much appreciate your feedback. 🙇

@NextAlone
Copy link
Author

Just released 4.0.0rc1 please give it a try! Would very much appreciate your feedback. 🙇

Sorry for the delayed response, I will try it soon.

@BeyondEvil
Copy link
Contributor

Latest is now rc4 😅

@BeyondEvil
Copy link
Contributor

Btw. We support ”pop” again (for the time being.

@NextAlone
Copy link
Author

Btw. We support ”pop” again (for the time being.

It works fine, thanks.

@BeyondEvil
Copy link
Contributor

If everything is to your satisfaction, maybe we can close this issue?

@NextAlone
Copy link
Author

If everything is to your satisfaction, maybe we can close this issue?

Of course, thanks.

@NextAlone NextAlone closed this Apr 24, 2023
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.

2 participants