pytest-html icon indicating copy to clipboard operation
pytest-html copied to clipboard

Fix: Crash on _hydrate_data

Open orgads opened this issue 2 years ago • 8 comments
trafficstars

Traceback (most recent call last):
  File "/usr/local/lib/python3.11/dist-packages/_pytest/main.py", line 271, in wrap_session
    session.exitstatus = doit(config, session) or 0
                         ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/dist-packages/_pytest/main.py", line 325, in _main
    config.hook.pytest_runtestloop(session=session)
  File "/usr/local/lib/python3.11/dist-packages/pluggy/_hooks.py", line 493, in __call__
    return self._hookexec(self.name, self._hookimpls, kwargs, firstresult)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/dist-packages/pluggy/_manager.py", line 115, in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/dist-packages/pluggy/_callers.py", line 152, in _multicall
    return outcome.get_result()
           ^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/dist-packages/pluggy/_result.py", line 114, in get_result
    raise exc.with_traceback(exc.__traceback__)
  File "/usr/local/lib/python3.11/dist-packages/pluggy/_callers.py", line 77, in _multicall
    res = hook_impl.function(*args)
          ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/dist-packages/_pytest/main.py", line 350, in pytest_runtestloop
    item.config.hook.pytest_runtest_protocol(item=item, nextitem=nextitem)
  File "/usr/local/lib/python3.11/dist-packages/pluggy/_hooks.py", line 493, in __call__
    return self._hookexec(self.name, self._hookimpls, kwargs, firstresult)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/dist-packages/pluggy/_manager.py", line 115, in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/dist-packages/pluggy/_callers.py", line 152, in _multicall
    return outcome.get_result()
           ^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/dist-packages/pluggy/_result.py", line 114, in get_result
    raise exc.with_traceback(exc.__traceback__)
  File "/usr/local/lib/python3.11/dist-packages/pluggy/_callers.py", line 77, in _multicall
    res = hook_impl.function(*args)
          ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/dist-packages/pytest_rerunfailures.py", line 622, in pytest_runtest_protocol
    item.ihook.pytest_runtest_logreport(report=report)
  File "/usr/local/lib/python3.11/dist-packages/pluggy/_hooks.py", line 493, in __call__
    return self._hookexec(self.name, self._hookimpls, kwargs, firstresult)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/dist-packages/pluggy/_manager.py", line 115, in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/dist-packages/pluggy/_callers.py", line 113, in _multicall
    raise exception.with_traceback(exception.__traceback__)
  File "/usr/local/lib/python3.11/dist-packages/pluggy/_callers.py", line 77, in _multicall
    res = hook_impl.function(*args)
          ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/dist-packages/pytest_html/basereport.py", line 242, in pytest_runtest_logreport
    self._hydrate_data(data, cells)
  File "/usr/local/lib/python3.11/dist-packages/pytest_html/basereport.py", line 159, in _hydrate_data
    if "sortable" in self._report.table_header[index]:
                     ~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^
IndexError: list index out of range

Amends commit abde929fc8.

orgads avatar Oct 10 '23 08:10 orgads

@BeyondEvil ping

orgads avatar Oct 11 '23 18:10 orgads

I need to understand more about why this is happening.

Could you create a minimal example and/or a test case?

BeyondEvil avatar Oct 31 '23 00:10 BeyondEvil

Ping @orgads

BeyondEvil avatar Nov 04 '23 17:11 BeyondEvil

Hi,

Our project is quite complex and uses some hooks.

I'll try to create a minimal project that reproduces, hopefully this week.

orgads avatar Nov 04 '23 18:11 orgads

Do you see a reason not to accept this fix anyway?

orgads avatar Nov 04 '23 18:11 orgads

Do you see a reason not to accept this fix anyway?

Yes, because I suspect it doesn't fix the root issue, but fixes a symptom where the hooks are incorrectly used.

This should only happen if there's a mismatch between the defined headers and rows inserted doesn't have the proper number of cells (the same number as headers).

BeyondEvil avatar Nov 04 '23 18:11 BeyondEvil

Hi,

I found the culprit.

In pytest_html_results_table_row, I insert a cell:

def pytest_html_results_table_row(report, cells):
    cells[3] = html.td(html.a('Open logs', href=report.logs_link))

Now in pytest-html v4, cells are plain strings, so I had to change this to:

def pytest_html_results_table_row(report, cells):
    cells[3] = str(html.td(html.a('Open logs', href=report.logs_link)))

Is this expected?

orgads avatar Dec 22 '23 13:12 orgads

Yes, that is expected as per the documentation@orgads

The py.html module is deprecated.

We try to handle it in code: https://github.com/pytest-dev/pytest-html/blob/master/src/pytest_html/basereport.py#L363

I'm not sure why that didn't work for you.

BeyondEvil avatar Jan 04 '24 23:01 BeyondEvil