Skip to content

Commit

Permalink
Add workarounds for pytest-xdist and pytest-django, add CI linting, f…
Browse files Browse the repository at this point in the history
…ix types, fix behavior (#32)

* Add workarounds for pytest-xdist and pytest-django
* type fixes
* Update line length for ruff to 120
* Add ptyme track secret to git ignore
* Add linting to CI
* Fix monkey patch
* Crudely fix off-by-one in jurigged logic
* Start postgres server on dev container start
* Add support for unregistering workarounds from libraries
* Update documentation
* Have client match status code
  • Loading branch information
JamesHutchison authored Jun 15, 2023
1 parent 7171adb commit 530ba90
Show file tree
Hide file tree
Showing 25 changed files with 565 additions and 88 deletions.
4 changes: 2 additions & 2 deletions .devcontainer/postStartCommand.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash

# run in the background at startup
nohup bash .devcontainer/postStartBackground.sh > ".dev_container_logs/postStartBackground.out" &
nohup "bash -c .devcontainer/postStartBackground.sh & > .dev_container_logs/postStartBackground.out"

sleep 1
docker compose -f tests/workarounds/pytest_django/docker-compose.yml up -d postgres
8 changes: 8 additions & 0 deletions .github/actions/test/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ description: Install dependencies and run tests
runs:
using: composite
steps:
- name: Start postgres for pytest-django test(s)
run: |
docker-compose -f tests/workarounds/pytest_django/docker-compose.yml up -d
shell: bash
- name: Install dependencies
run: |
poetry install --with=dev
Expand All @@ -15,3 +19,7 @@ runs:
run: |
poetry run pytest
shell: bash
- name: Rerun workaround tests to check for incompatibilities
run: |
poetry run pytest tests/workarounds
shell: bash
9 changes: 9 additions & 0 deletions .github/workflows/pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ permissions:
jobs:
test:
runs-on: ubuntu-latest
# services:
# postgres:
# image: postgres:12.2
# env:
# POSTGRES_HOST_AUTH_METHOD: trust
steps:
- uses: actions/checkout@v3
- name: Set up Python 3.10
Expand All @@ -26,3 +31,7 @@ jobs:
uses: Gr1N/setup-poetry@12c727a3dcf8c1a548d8d041c9d5ef5cebb3ba2e
- name: test
uses: ./.github/actions/test
- name: lint using ruff
run: poetry run ruff pytest_hot_reloading tests
- name: lint using mypy
run: poetry run mypy pytest_hot_reloading tests
5 changes: 5 additions & 0 deletions .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ jobs:
if: github.repository == 'JamesHutchison/pytest-hot-reloading'
runs-on: ubuntu-latest
environment: production
# services:
# postgres:
# image: postgres:12.2
# env:
# POSTGRES_HOST_AUTH_METHOD: trust
steps:
- uses: actions/checkout@v3
- name: Set up Python 3.10
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,6 @@ dmypy.json
*.pid

.dev_container_logs/*

# ptyme server secret
.ptyme.secret
11 changes: 11 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "Check",
"type": "shell",
"command": "poetry run dmypy check pytest_hot_reloading tests && poetry run ruff pytest_hot_reloading tests",
"problemMatcher": []
}
]
}
36 changes: 35 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ If it takes less than 5 seconds to do all of the imports
necessary to run a unit test, then you probably don't need this.

## Installation
TBD
Do not install in production code. This is exclusively for the developer environment.

pip: Add `pytest-hot-reloading` to your `dev-requirements.txt` file and `pip install -r dev-requirements.txt`
poetry: `poetry add --group=dev pytest-hot-reloading`

## Usage
Add the plugin to the pytest arguments. Example using pyproject.toml:
Expand Down Expand Up @@ -54,9 +57,40 @@ If the daemon is already running and you run pytest with `--daemon`, then the ol
and a new one will be started. Note that `pytest --daemon` is NOT how you run tests. It is only used to start
the daemon.

## Workarounds
Libraries that use mutated globals may need a workaround to work with this plugin. The preferred
route is to have the library update its code to not mutate globals in a test environment, or to
restore them after a test suite has ran. In some cases, that isn't possible, usually because
the person with the problem doesn't own the library and can't wait around for a fix.

To register a workaround, create a function that is decorated by the
`pytest_hot_reloading.workaround.register_workaround` decorator. It may optionally yield. If it does,
then code after the yield is executed after the test suite has ran.

Example:
```python
from pytest_hot_reloading.workaround import register_workaround

@register_workaround("my_library")
def my_library_workaround():
import my_library

yield

my_library.some_global = BackToOriginalValue()
```

If you are a library author, you can disable any workarounds for your library by creating an empty
module `_clear_hot_reload_workarounds.py`. If this is successfully imported, then workarounds for
the given module will not be executed.

## Known Issues
- This is early alpha
- The jurigged library is not perfect and sometimes it gets in a bad state
- Some libraries were not written with hot reloading in mind, and will not work without some changes.
There is going to be logic to work around other issues with other libraries, such as pytest-django's
mutation of the settings module that runs every session, but it hasn't been implemented yet.

## Notes
- pytest-xdist will have its logic disabled, even if args are passed in to enable it
- pytest-django will not create test database suffixes for multiworker runs such as tox.
Loading

0 comments on commit 530ba90

Please sign in to comment.