From 614605becbfabae39dc19f02bd69415f31c64476 Mon Sep 17 00:00:00 2001 From: Karan Singh Kochar Date: Sun, 18 Aug 2024 14:01:49 -0500 Subject: [PATCH] chore: Update pytest workflow to use Poetry for dependency management --- .github/workflows/pytest.yml | 2 +- poetry.lock | 34 +++++++++++++++++++++++++++++++++- pyproject.toml | 1 + tests/conftest.py | 30 ++++++++++++++++++++++++++++-- 4 files changed, 63 insertions(+), 4 deletions(-) diff --git a/.github/workflows/pytest.yml b/.github/workflows/pytest.yml index 025893c..1465040 100644 --- a/.github/workflows/pytest.yml +++ b/.github/workflows/pytest.yml @@ -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 \ No newline at end of file + poetry run pytest --ci \ No newline at end of file diff --git a/poetry.lock b/poetry.lock index 8ebe144..f995e5d 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1051,6 +1051,24 @@ traitlets = ">=5.3" docs = ["myst-parser", "pydata-sphinx-theme", "sphinx-autodoc-typehints", "sphinxcontrib-github-alt", "sphinxcontrib-spelling", "traitlets"] test = ["ipykernel", "pre-commit", "pytest (<8)", "pytest-cov", "pytest-timeout"] +[[package]] +name = "loguru" +version = "0.7.2" +description = "Python logging made (stupidly) simple" +optional = false +python-versions = ">=3.5" +files = [ + {file = "loguru-0.7.2-py3-none-any.whl", hash = "sha256:003d71e3d3ed35f0f8984898359d65b79e5b21943f78af86aa5491210429b8eb"}, + {file = "loguru-0.7.2.tar.gz", hash = "sha256:e671a53522515f34fd406340ee968cb9ecafbc4b36c679da03c18fd8d0bd51ac"}, +] + +[package.dependencies] +colorama = {version = ">=0.3.4", markers = "sys_platform == \"win32\""} +win32-setctime = {version = ">=1.0.0", markers = "sys_platform == \"win32\""} + +[package.extras] +dev = ["Sphinx (==7.2.5)", "colorama (==0.4.5)", "colorama (==0.4.6)", "exceptiongroup (==1.1.3)", "freezegun (==1.1.0)", "freezegun (==1.2.2)", "mypy (==v0.910)", "mypy (==v0.971)", "mypy (==v1.4.1)", "mypy (==v1.5.1)", "pre-commit (==3.4.0)", "pytest (==6.1.2)", "pytest (==7.4.0)", "pytest-cov (==2.12.1)", "pytest-cov (==4.1.0)", "pytest-mypy-plugins (==1.9.3)", "pytest-mypy-plugins (==3.0.0)", "sphinx-autobuild (==2021.3.14)", "sphinx-rtd-theme (==1.3.0)", "tox (==3.27.1)", "tox (==4.11.0)"] + [[package]] name = "markdown-it-py" version = "3.0.0" @@ -2367,6 +2385,20 @@ files = [ {file = "wcwidth-0.2.13.tar.gz", hash = "sha256:72ea0c06399eb286d978fdedb6923a9eb47e1c486ce63e9b4e64fc18303972b5"}, ] +[[package]] +name = "win32-setctime" +version = "1.1.0" +description = "A small Python utility to set file creation time on Windows" +optional = false +python-versions = ">=3.5" +files = [ + {file = "win32_setctime-1.1.0-py3-none-any.whl", hash = "sha256:231db239e959c2fe7eb1d7dc129f11172354f98361c4fa2d6d2d7e278baa8aad"}, + {file = "win32_setctime-1.1.0.tar.gz", hash = "sha256:15cf5750465118d6929ae4de4eb46e8edae9a5634350c01ba582df868e932cb2"}, +] + +[package.extras] +dev = ["black (>=19.3b0)", "pytest (>=4.6.2)"] + [[package]] name = "wrapt" version = "1.16.0" @@ -2552,4 +2584,4 @@ multidict = ">=4.0" [metadata] lock-version = "2.0" python-versions = "^3.11" -content-hash = "268fda3a4ded03f64dff5bb9340c043552cb8dc9fd41abbae3d1bdb8502ebc9e" +content-hash = "1ec3e6931b9194f342952a430a2eed0ce22fc8308ee895dfaaffd25dcea498a8" diff --git a/pyproject.toml b/pyproject.toml index a72df15..c57023c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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 diff --git a/tests/conftest.py b/tests/conftest.py index 56aa97a..6dfde94 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -2,8 +2,10 @@ 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 @@ -11,6 +13,14 @@ # 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 @@ -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