Skip to content

Commit

Permalink
tests: add tests for poetry
Browse files Browse the repository at this point in the history
  • Loading branch information
gmargaritis committed Jan 31, 2024
1 parent cb289dd commit b11ea2d
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 1 deletion.
53 changes: 53 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: CI

on:
push:
branches:
- main
pull_request:

env:
POETRY_VERSION: 1.7.1
GHCR_IMAGE_REPOSITORY: ghcr.io/${{ github.repository_owner }}/poetry

jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
python:
- "3.8"
- "3.9"
- "3.10"
- "3.11"
- "3.12"
variant:
- bookworm
- slim
steps:
- uses: actions/checkout@v3
- name: Build Docker Image
run: |
docker build --target test -t test-image \
--build-arg POETRY_VERSION=${{ env.POETRY_VERSION }} \
.
- name: Run Tests in Docker
run: |
docker run --rm \
-e POETRY_VERSION=${{ env.POETRY_VERSION }} \
test-image python3 /usr/src/app/tests.py
- name: Log in to GitHub Container Registry
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and Push Docker Image
uses: docker/build-push-action@v3.1.0
with:
context: .
push: true
tags: ${{ env.GHCR_IMAGE_REPOSITORY }}:${{ github.sha }}-${{ matrix.python }}-${{ matrix.variant }}
build-args: |
POETRY_VERSION=${{ env.POETRY_VERSION }}
6 changes: 5 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
ARG PYTHON_VERSION=3.12
ARG VARIANT=bookworm

FROM python:${PYTHON_VERSION}-${VARIANT}
FROM python:${PYTHON_VERSION}-${VARIANT} as base

ENV PYTHONUNBUFFERED=1

Expand All @@ -16,4 +16,8 @@ RUN python3 -m venv ${POETRY_HOME} &&\
${POETRY_HOME}/bin/pip install poetry==${POETRY_VERSION} &&\
poetry config virtualenvs.create false

FROM base as test
COPY ./tests/tests.py /usr/src/app/tests.py

FROM base
WORKDIR /usr/src/app
49 changes: 49 additions & 0 deletions tests/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import os
import re
import subprocess
import unittest


class TestPoetrySetup(unittest.TestCase):
def test_poetry_binary_available(self):
"""
Test if Poetry binary is available.
"""

result = subprocess.run(["which", "poetry"], capture_output=True, text=True)
self.assertEqual(result.returncode, 0, "Poetry binary is not available in PATH")

def test_poetry_version(self):
"""
Test if the installed Poetry version is correct.
"""

expected_version = os.environ.get("POETRY_VERSION")
result = subprocess.run(["poetry", "--version"], capture_output=True, text=True)
self.assertEqual(result.returncode, 0, "Error executing Poetry")
version_match = re.search(r"Poetry \(version (\S+)\)", result.stdout)
self.assertIsNotNone(version_match, "Unable to parse Poetry version")
self.assertEqual(
version_match.group(1),
expected_version,
f"Expected Poetry version {expected_version}, got {version_match.group(1)}",
)

def test_poetry_without_virtualenvs(self):
"""
Test if Poetry is configured to work without virtual environments.
"""

result = subprocess.run(
["poetry", "config", "virtualenvs.create"], capture_output=True, text=True
)
self.assertEqual(result.returncode, 0, "Error executing Poetry config command")
self.assertEqual(
result.stdout.strip(),
"false",
"Poetry is not configured to work without virtual environments",
)


if __name__ == "__main__":
unittest.main()

0 comments on commit b11ea2d

Please sign in to comment.