Skip to content

Commit

Permalink
chore: Update pytest workflow to use Poetry for dependency management
Browse files Browse the repository at this point in the history
  • Loading branch information
pyrotank41 committed Aug 18, 2024
1 parent f94c2c8 commit 614605b
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ jobs:
VCR_RECORD_MODE: 'none' # Ensure we're only using existing cassettes
CI: 'true' # Set CI environment variable
run: |
poetry run pytest
poetry run pytest --ci
34 changes: 33 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ tiktoken = "^0.7.0"
ipykernel = "^6.29.5"
poetry-dynamic-versioning = "^1.4.0"
vcrpy = "^6.0.1"
loguru = "^0.7.2"

[tool.poetry-dynamic-versioning]
enable = true
Expand Down
30 changes: 28 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,25 @@
import sys

import pytest
import urllib3
import vcr
from pathlib import Path
from loguru import logger

# Get the parent directory of the directory containing this file
parent_dir = Path(__file__).resolve().parent.parent

# Add the parent directory to sys.path
sys.path.insert(0, str(parent_dir))

def pytest_addoption(parser):
parser.addoption(
"--ci",
action="store_true",
default=False,
help="Run tests as if in a CI environment",
)


def pytest_configure(config):
# Ensure we're using VCR
Expand All @@ -27,17 +37,33 @@ def vcr_config():

@pytest.fixture(autouse=True)
def no_http_requests(monkeypatch):
"""Raise an error if any HTTP request is made."""
"""Raise an error if any HTTP request is made, except for tiktoken encodings."""
original_urlopen = urllib3.connectionpool.HTTPConnectionPool.urlopen
logger.info("no_http_requests fixture setup")

def urlopen_mock(self, method, url, *args, **kwargs):
logger.debug(f"Intercepted request: {method} {url}")
if "tiktoken" in url:
logger.debug(f"Allowing tiktoken request: {method} {url}")
return original_urlopen(self, method, url, *args, **kwargs)
logger.debug(f"Blocking HTTP request: {method} {url}")
raise RuntimeError(
f"The test was about to make a real HTTP request to {method} {url}"
)

if os.environ.get("CI"): # Only activate in CI environment
if os.environ.get("CI"):
logger.debug("CI environment detected, activating no_http_requests fixture")
monkeypatch.setattr(
"urllib3.connectionpool.HTTPConnectionPool.urlopen", urlopen_mock
)
else:
logger.debug(
"Non-CI environment detected, no_http_requests fixture not activated"
)

yield

logger.debug("no_http_requests fixture cleanup")


# Ensure all tests use VCR
Expand Down

0 comments on commit 614605b

Please sign in to comment.