|
| 1 | +# https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python |
| 2 | +# https://github.com/actions/setup-python/blob/main/docs/advanced-usage.md#caching-packages |
| 3 | +# https://www.peterbe.com/plog/install-python-poetry-github-actions-faster |
| 4 | + |
| 5 | +name: python_test |
| 6 | + |
| 7 | +on: |
| 8 | + workflow_dispatch: |
| 9 | + # push: |
| 10 | + # branches: |
| 11 | + # - main |
| 12 | + # pull_request: |
| 13 | + |
| 14 | +jobs: |
| 15 | + ci: |
| 16 | + strategy: |
| 17 | + matrix: |
| 18 | + python-version: ["3.10", "3.11"] |
| 19 | + os: ["ubuntu-latest"] |
| 20 | + arch: ['x64'] |
| 21 | + |
| 22 | + runs-on: ${{ matrix.os }} |
| 23 | + |
| 24 | + steps: |
| 25 | + - name: checkout repo content |
| 26 | + uses: actions/checkout@v3 |
| 27 | + # TODO: debug 'cache not found for input keys dotlocal-Linux-8e1cebf...' |
| 28 | + # - name: load cached $HOME/.local |
| 29 | + # uses: actions/cache@v2.1.6 |
| 30 | + # id: cash-money |
| 31 | + # with: |
| 32 | + # path: ~/.local |
| 33 | + # key: dotlocal-${{ runner.os }}-${{ hashFiles('.github/workflows/python_test.yml') }} |
| 34 | + # - run: echo '${{ steps.cash-money.outputs.cache-hit }}' # true if cache-hit occured on the primary key |
| 35 | + # TODO: use `git config --global --add safe.directory` to find working dir (may need to set under checkout step) |
| 36 | + - name: set cwd and change to directory |
| 37 | + run: | |
| 38 | + CWD=$(basename $(git rev-parse --show-toplevel)) |
| 39 | + BASE_DIR=$(cat BASE_DIR) |
| 40 | + echo "BASE_DIR=$BASE_DIR" >> $GITHUB_ENV |
| 41 | + cd ${{ env.BASE_DIR }} || unset BASE_DIR; \ |
| 42 | + BASE_DIR="$(pwd)/work/${CWD}/${CWD}" && echo "BASE_DIR=$BASE_DIR" >> $GITHUB_ENV; \ |
| 43 | + cd ${{ env.BASE_DIR }} |
| 44 | + # - run: ls -la ${{ env.BASE_DIR }} |
| 45 | + - name: Read .tool-versions # dynamic versions |
| 46 | + uses: marocchino/tool-versions-action@v1 |
| 47 | + id: versions |
| 48 | + with: |
| 49 | + path: ${{ env.BASE_DIR }}/.tool-versions |
| 50 | + - name: shuffle files |
| 51 | + run: | |
| 52 | + cat ${{ env.BASE_DIR }}/.tool-versions | awk '/python/ {print $NF}' >> ${{ env.BASE_DIR }}/.python-version |
| 53 | + # [[ -e "$(pwd)/pyproject.toml" ]] && mv "$(pwd)/pyproject.toml" "$(pwd)/pyproject.toml.bak" |
| 54 | + # cp "${{ env.BASE_DIR }}/pyproject.toml" $(pwd)/ |
| 55 | + - name: setup python |
| 56 | + uses: actions/setup-python@v4 |
| 57 | + with: |
| 58 | + python-version-file: ${{ env.BASE_DIR }}/.python-version |
| 59 | + cache: 'pip' |
| 60 | + - name: setup poetry |
| 61 | + uses: snok/install-poetry@v1 |
| 62 | + id: cp310 |
| 63 | + with: |
| 64 | + version: ${{ steps.versions.outputs.poetry }} |
| 65 | + virtualenvs-create: true |
| 66 | + virtualenvs-in-project: true |
| 67 | + virtualenvs-path: ${{ env.BASE_DIR }} |
| 68 | + installer-parallel: true |
| 69 | + - name: load cached venv |
| 70 | + id: cached-poetry-dependencies |
| 71 | + uses: actions/cache@v2.1.6 |
| 72 | + with: |
| 73 | + path: ${{ env.BASE_DIR }}/.venv |
| 74 | + key: venv-${{ runner.os }}-${{ hashFiles('**/poetry.lock') }}-${{ hashFiles('.github/workflows/python_test.yml') }} |
| 75 | + - name: install virtualenv |
| 76 | + run: | |
| 77 | + cd ${{ env.BASE_DIR }} |
| 78 | + poetry install |
| 79 | + if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true' |
| 80 | + - name: Lint with flake8 |
| 81 | + run: | |
| 82 | + # stop the build if there are Python syntax errors or undefined names |
| 83 | + poetry run flake8 ${{ env.BASE_DIR }} --count --select=E9,F63,F7,F82 --show-source --statistics |
| 84 | + # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide |
| 85 | + poetry run flake8 ${{ env.BASE_DIR }} --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics |
| 86 | + # TODO: write tests |
| 87 | + - name: Test with pytest |
| 88 | + run: | |
| 89 | + # exit code 5 == 'collected 0 items' |
| 90 | + poetry run pytest ${{ env.BASE_DIR }} |
0 commit comments