Skip to content

Commit

Permalink
Collect pytest logs on CI (#32)
Browse files Browse the repository at this point in the history
This features collects pytest log files into log directory of the
current virtualenv, so we can collect them on CI.
  • Loading branch information
ssbarnea authored Mar 26, 2024
1 parent 174d255 commit 6569e74
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,8 @@ jobs:
- name: Archive logs
uses: actions/upload-artifact@v4
with:
name: logs.zip
name: logs-${{ matrix.name }}.zip
path: .tox/**/log/
# https://github.com/actions/upload-artifact/issues/123
continue-on-error: true

- name: Report failure if git reports dirty status
run: |
Expand Down
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,21 @@ You can disable regex check by defining `PYTEST_CHECK_TEST_ID_REGEX=0`.

You can disable the length check by defining `PYTEST_MAX_TEST_ID_LENGTH=0`.

## Prepare pytest log files for collection on CI

As pytest log files are created on temp directory and some CI systems refuse
to collect files from outside the current project, we do copy these files inside
`$VIRTUAL_ENV/log`, same directory used by tox itself. To collect the logs on
Github Actions, you only need a step like:

```yaml
- name: Archive logs
uses: actions/upload-artifact@v4
with:
name: logs-${{ matrix.name }}.zip
path: .tox/**/log/
```
## Release process
Releases are triggered from [GitHub Releases](https://github.com/pytest-dev/pytest-plus/releases)
Expand Down
11 changes: 11 additions & 0 deletions src/pytest_plus/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import os
import re
import shutil

import pytest
from _pytest.terminal import TerminalReporter
Expand Down Expand Up @@ -45,6 +46,16 @@ def pytest_sessionfinish(session: pytest.Session) -> None:
)
session.exitstatus = 1

# If running under CI (Github Actions included) and inside a venv, do
# copy all pytest own logs inside $VIRTUAL_ENV/log, for collection.
venv = os.environ.get("VIRTUAL_ENV", "")
if os.environ.get("CI", "0") != "0" and venv:
shutil.copytree(
src=session.config._tmp_path_factory.getbasetemp(), # type: ignore[attr-defined] # noqa: SLF001
dst=venv + "/log",
dirs_exist_ok=True,
)


@pytest.hookimpl(tryfirst=True) # type: ignore[misc,unused-ignore]
def pytest_collection_modifyitems(items: list[pytest.Item]) -> None:
Expand Down

0 comments on commit 6569e74

Please sign in to comment.