From 7d64ad9d91ec46d5e8feca20923e8ef6fca6d18d Mon Sep 17 00:00:00 2001 From: Joshua Tzucker Date: Sat, 14 Sep 2024 16:20:49 -0700 Subject: [PATCH] Integrate CI checks (linting, type-checking, tests) --- .github/workflows/ci-checks.yml | 69 +++++++++++++++++++++++ Taskfile.yml | 23 +++++--- django_utils_lib/testing/pytest_plugin.py | 3 + poetry.toml | 4 ++ 4 files changed, 92 insertions(+), 7 deletions(-) create mode 100644 .github/workflows/ci-checks.yml create mode 100644 poetry.toml diff --git a/.github/workflows/ci-checks.yml b/.github/workflows/ci-checks.yml new file mode 100644 index 0000000..32d1173 --- /dev/null +++ b/.github/workflows/ci-checks.yml @@ -0,0 +1,69 @@ +name: CI Checks + +on: [push] + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + lint-and-type-check: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Extract project's active Python version + id: python-version + shell: bash + run: echo "python-version=$(cat .tool-versions | grep python | cut -d ' ' -f 2)" >> "$GITHUB_OUTPUT" + working-directory: . + - uses: actions/setup-python@v4 + with: + python-version: ${{ steps.python-version.outputs.python-version }} + - uses: abatilo/actions-poetry@v3 + with: + poetry-version: 1.7.1 + - uses: actions/cache@v4 + name: Configure Python poetry cache + with: + path: ./.venv + key: ${{ runner.os }}-${{ runner.arch }}-poetry-venv-${{ hashFiles('poetry.lock') }} + restore-keys: | + ${{ runner.os }}-${{ runner.arch }}-poetry-venv- + - uses: arduino/setup-task@v2 + - name: Install dependencies + shell: bash + run: task install + - name: Lint with ruff + shell: bash + run: task lint:ruff + - name: Type check with mypy + shell: bash + run: task lint:types + + test: + runs-on: ubuntu-latest + strategy: + matrix: + python-version: ["3.9", "3.10", "3.11", "3.12"] + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-python@v4 + with: + python-version: ${{ matrix.python-version }} + - uses: abatilo/actions-poetry@v3 + with: + poetry-version: 1.7.1 + - uses: actions/cache@v4 + name: Configure Python poetry cache + with: + path: ./.venv + key: ${{ runner.os }}-${{ runner.arch }}-${{ matrix.python-version }}-poetry-venv-${{ hashFiles('poetry.lock') }} + restore-keys: | + ${{ runner.os }}-${{ runner.arch }}-${{ matrix.python-version }}-poetry-venv- + - uses: arduino/setup-task@v2 + - name: Install dependencies + shell: bash + run: task install + - name: Run tests + shell: bash + run: task test diff --git a/Taskfile.yml b/Taskfile.yml index f7516e6..f6e514e 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -30,6 +30,9 @@ tasks: #==================== Internal Use ==========================# #============================================================# _verify_python_venv: | + if [[ -n "$CI" ]]; then + exit 0 + fi DETECTED_PYTHON_PATH=$(which python) if [[ $DETECTED_PYTHON_PATH != *"/envs/"* && $DETECTED_PYTHON_PATH != $PWD/* ]]; then echo "Python path (${DETECTED_PYTHON_PATH}) is not scoped to project; please make sure you have activated your virtual environment." @@ -46,14 +49,20 @@ tasks: #============================================================# #================= Linting / Testing ========================# #============================================================# - lint: - deps: [_verify_python_venv] - cmd: | - poetry run ruff check "$PACKAGE_DIR" - mypy "$PACKAGE_DIR" + lint:ruff: + deps: [_verify_python_venv, install] + cmd: poetry run ruff check "$PACKAGE_DIR" + lint:types: + deps: [_verify_python_venv, install] + cmd: poetry run mypy "$PACKAGE_DIR" + lint:all: + deps: [_verify_python_venv, install] + cmds: + - task: lint:ruff + - task: lint:types test: - deps: [_verify_python_venv] - cmd: poetry run pytest -n 4 + deps: [_verify_python_venv, install] + cmd: poetry run pytest -n auto #============================================================# #================= SECTION_HEADING ==========================# #============================================================# diff --git a/django_utils_lib/testing/pytest_plugin.py b/django_utils_lib/testing/pytest_plugin.py index d070cb8..649ba21 100644 --- a/django_utils_lib/testing/pytest_plugin.py +++ b/django_utils_lib/testing/pytest_plugin.py @@ -205,6 +205,9 @@ def get_config_val(self, config_key: PluginConfigKey): @property def auto_debug(self) -> bool: + # Disable if CI is detected + if os.getenv("CI", "").lower() == "true": + return False return bool(self.get_config_val("auto_debug")) or bool(os.getenv(f"{PACKAGE_NAME}_AUTO_DEBUG", "")) @property diff --git a/poetry.toml b/poetry.toml new file mode 100644 index 0000000..8933118 --- /dev/null +++ b/poetry.toml @@ -0,0 +1,4 @@ +[virtualenvs] +create = true +in-project = true +path = ".venv"