From 48502a51204106b1ef0ea1b55c40357377ee2989 Mon Sep 17 00:00:00 2001 From: Mathijs de Bruin Date: Mon, 2 Sep 2024 19:38:12 +0100 Subject: [PATCH] Various improvements in tests/CI (#1271) * Run unit tests with all supported Python versions. * Caching of Python deps in CI. Special care taken of https://github.com/python-poetry/poetry/issues/2117 * Local action for DRY Python/Poetry install with caching. --- .../actions/poetry-python-install/action.yaml | 46 +++++++++++++++++++ .github/workflows/e2e-tests.yaml | 18 ++++---- .github/workflows/mypy.yaml | 20 ++++---- .github/workflows/pytest.yaml | 25 +++++----- 4 files changed, 77 insertions(+), 32 deletions(-) create mode 100644 .github/actions/poetry-python-install/action.yaml diff --git a/.github/actions/poetry-python-install/action.yaml b/.github/actions/poetry-python-install/action.yaml new file mode 100644 index 0000000000..c65fb3f791 --- /dev/null +++ b/.github/actions/poetry-python-install/action.yaml @@ -0,0 +1,46 @@ +name: 'Install Python, poetry and dependencies.' +description: 'Install Python, Poetry and poetry dependencies using cache' + +inputs: + python-version: + description: 'Python version' + required: true + poetry-version: + description: 'Poetry version' + required: true + poetry-working-directory: + description: 'Working directory for poetry command.' + required: false + default: '.' + poetry-install-args: + description: 'Extra arguments for poetry install, e.g. --with tests.' + required: false + +defaults: + run: + shell: bash + +runs: + using: 'composite' + steps: + - name: Cache poetry install + uses: actions/cache@v2 + with: + path: ~/.local + key: poetry-${{ runner.os }}-${{ inputs.poetry-version }}-0 + - name: Install Poetry + run: pipx install 'poetry==${{ inputs.poetry-version }}' + shell: bash + - name: Set up Python ${{ inputs.python-version }} + id: setup_python + uses: actions/setup-python@v4 + with: + python-version: ${{ inputs.python-version }} + cache: poetry + cache-dependency-path: ${{ inputs.poetry-working-directory }}/poetry.lock + - name: Set Poetry environment + run: poetry -C '${{ inputs.poetry-working-directory }}' env use '${{ steps.setup_python.outputs.python-path }}' + shell: bash + - name: Install Python dependencies + run: poetry -C '${{ inputs.poetry-working-directory }}' install ${{ inputs.poetry-install-args }} + shell: bash diff --git a/.github/workflows/e2e-tests.yaml b/.github/workflows/e2e-tests.yaml index f6d26de166..ad83e94f54 100644 --- a/.github/workflows/e2e-tests.yaml +++ b/.github/workflows/e2e-tests.yaml @@ -8,31 +8,31 @@ jobs: strategy: matrix: os: [ubuntu-latest, windows-latest] + env: + BACKEND_DIR: ./backend steps: - uses: actions/checkout@v3 - uses: pnpm/action-setup@v2 with: version: 8.6.9 + - uses: ./.github/actions/poetry-python-install + name: Install Python, poetry and Python dependencies + with: + python-version: 3.9 + poetry-version: 1.8.3 + poetry-working-directory: ${{ env.BACKEND_DIR }} + poetry-install-args: --with tests - name: Use Node.js 16.15.0 uses: actions/setup-node@v3 with: node-version: '16.15.0' cache: 'pnpm' - - name: Set up Python - uses: actions/setup-python@v4 - with: - python-version: '3.9' - cache: 'pip' - - name: Install Poetry - run: pip install poetry - name: Install JS dependencies run: pnpm install --no-frozen-lockfile - name: Build UI run: pnpm run buildUi - name: Lint UI run: pnpm run lintUi - - name: Install Python dependencies - run: poetry install -C ./backend --with tests - name: Run tests env: CYPRESS_RECORD_KEY: ${{ secrets.CYPRESS_RECORD_KEY }} diff --git a/.github/workflows/mypy.yaml b/.github/workflows/mypy.yaml index a0cd334b27..a33b2f4bd8 100644 --- a/.github/workflows/mypy.yaml +++ b/.github/workflows/mypy.yaml @@ -5,19 +5,17 @@ on: [workflow_call] jobs: mypy: runs-on: ubuntu-latest - defaults: - run: - working-directory: ./backend + env: + BACKEND_DIR: ./backend steps: - uses: actions/checkout@v3 - - name: Set up Python - uses: actions/setup-python@v4 + - uses: ./.github/actions/poetry-python-install + name: Install Python, poetry and Python dependencies with: - python-version: '3.9' - cache: 'pip' - - name: Install Poetry - run: pip install poetry - - name: Install dependencies - run: poetry install --with tests --with mypy --with custom-data + python-version: 3.9 + poetry-version: 1.8.3 + poetry-install-args: --with tests --with mypy --with custom-data + poetry-working-directory: ${{ env.BACKEND_DIR }} - name: Run Mypy run: poetry run mypy chainlit/ + working-directory: ${{ env.BACKEND_DIR }} diff --git a/.github/workflows/pytest.yaml b/.github/workflows/pytest.yaml index d8b68c7f05..3b9851a279 100644 --- a/.github/workflows/pytest.yaml +++ b/.github/workflows/pytest.yaml @@ -3,21 +3,22 @@ name: Pytest on: [workflow_call] jobs: - mypy: + pytest: runs-on: ubuntu-latest - defaults: - run: - working-directory: ./backend + strategy: + matrix: + python-version: ['3.9', '3.10', '3.11', '3.12'] + env: + BACKEND_DIR: ./backend steps: - uses: actions/checkout@v3 - - name: Set up Python - uses: actions/setup-python@v4 + - uses: ./.github/actions/poetry-python-install + name: Install Python, poetry and Python dependencies with: - python-version: '3.9' - cache: 'pip' - - name: Install Poetry - run: pip install poetry - - name: Install dependencies - run: poetry install --with tests --with mypy --with custom-data + python-version: ${{ matrix.python-version }} + poetry-version: 1.8.3 + poetry-install-args: --with tests --with mypy --with custom-data + poetry-working-directory: ${{ env.BACKEND_DIR }} - name: Run Pytest run: poetry run pytest --cov=chainlit/ + working-directory: ${{ env.BACKEND_DIR }}