Skip to content

Commit

Permalink
added pipenv setup for runner, and Github Actions to test runner
Browse files Browse the repository at this point in the history
  • Loading branch information
AlanZhang2002 committed May 15, 2024
1 parent 0aec97b commit 413923e
Show file tree
Hide file tree
Showing 7 changed files with 216 additions and 27 deletions.
53 changes: 53 additions & 0 deletions .github/workflows/python-test-backend.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: Python Lint and Test - Backend

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

permissions:
contents: read
env:
FIREBASE_KEY: ${{ secrets.FIREBASE_KEY }}
CONTACT_MONGODB_AT: ${{ secrets.CONTACT_MONGODB_AT }}
BACKEND_PORT: ${{ secrets.BACKEND_PORT }}
MONGODB_PORT: ${{ secrets.MONGODB_PORT }}
jobs:
Lint_And_Test:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Set up Python 3.8
uses: actions/setup-python@v3
with:
python-version: "3.8"
- name: Install dependencies
working-directory: ./apps/backend
run: |
python -m pip install --upgrade pip
python -m pip install coverage
pip install pipenv
pipenv install --dev --system --deploy --ignore-pipfile
- name: Analyze backend with pylint
working-directory: ./apps/backend
# https://pylint.readthedocs.io/en/v2.16.1/user_guide/configuration/all-options.html#fail-on
# Uses the .pylintrc rules file in the working directory
# Fail if any Error-level notices were produced
# Fail if the code quality score is below 9.0
# Recursively search the specified directories for files to analyze
# Check the file app.py
run: pylint --fail-on=E --fail-under=9.0 --recursive=y app.py
- name: Test backend with pytest + record coverage
working-directory: ./apps/backend
run: bash coverage.sh
- name: Zip backend coverage report
working-directory: ./apps/backend
run: zip -r codeCoverage.zip coverageReport/
- name: Upload backend coverage report
uses: actions/upload-artifact@v3
with:
name: code-coverage-report-backend
path: ./apps/backend/codeCoverage.zip
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Python Lint and Test
name: Python Lint and Test - Runner

on:
push:
Expand All @@ -25,21 +25,12 @@ jobs:
with:
python-version: "3.8"
- name: Install dependencies
working-directory: ./apps/backend
working-directory: ./apps/runner
run: |
python -m pip install --upgrade pip
python -m pip install coverage
pip install pipenv
pipenv install --dev --system --deploy --ignore-pipfile
- name: Analyze backend with pylint
working-directory: ./apps/backend
# https://pylint.readthedocs.io/en/v2.16.1/user_guide/configuration/all-options.html#fail-on
# Uses the .pylintrc rules file in the working directory
# Fail if any Error-level notices were produced
# Fail if the code quality score is below 9.0
# Recursively search the specified directories for files to analyze
# Check the file app.py
run: pylint --fail-on=E --fail-under=9.0 --recursive=y app.py
- name: Analyze runner with pylint
working-directory: ./apps/runner
# https://pylint.readthedocs.io/en/v2.16.1/user_guide/configuration/all-options.html#fail-on
Expand All @@ -50,23 +41,12 @@ jobs:
# Check the file runner.py
# Check the ./modules and ./tests directory
run: pylint --fail-on=E --fail-under=9.0 --recursive=y runner.py ./modules/ ./tests/
- name: Test backend with pytest + record coverage
working-directory: ./apps/backend
run: bash coverage.sh
- name: Zip backend coverage report
working-directory: ./apps/backend
run: zip -r codeCoverage.zip coverageReport/
- name: Test runner with pytest + record coverage
working-directory: ./apps/runner
run: bash coverage.sh
- name: Zip runner coverage report
working-directory: ./apps/runner
run: zip -r codeCoverage.zip coverageReport/
- name: Upload backend coverage report
uses: actions/upload-artifact@v3
with:
name: code-coverage-report-backend
path: ./apps/backend/codeCoverage.zip
- name: Upload runner coverage report
uses: actions/upload-artifact@v3
with:
Expand Down
1 change: 1 addition & 0 deletions apps/backend/Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ flask = "*"
requests = "*"
kubernetes = "*"
flask-cors = "*"
pytest-cov = "*"

[dev-packages]
# libmagic dlls for windows hosts https://pypi.org/project/python-magic/ (when missing, vague errors and it crashes)
Expand Down
126 changes: 122 additions & 4 deletions apps/backend/Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions apps/runner/coverage.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/bash
# This script is used by
# - the VSCode task 'Backend Code Coverage Report' to produce a report that can be displayed in the editor
# - the CI to execute the tests and produce a report that gets uploaded as an artifact for download

# Avoid pollution from past runs
rm -f .coverage
rm -f coverage.xml
rm -rf coverageReport/

# Running `python -m pytest` and not `pytest` because we need the current dir on sys.path, see https://docs.pytest.org/en/6.2.x/usage.html#calling-pytest-through-python-m-pytest
# -rA flag controls printed report details: https://docs.pytest.org/en/6.2.x/usage.html#detailed-summary-report
# --cov flags from pytest-cov plugin control contents and file output of the report https://pytest-cov.readthedocs.io/en/latest/readme.html
python -m pytest \
-rA \
--cov-report xml:coverage.xml \
--cov-report html:coverageReport \
--cov-branch \
--cov . \
--cov modules/

# Print information in the console for the user as well
# -m also prints Missed lines
coverage report -m

echo "▶ Use the VSCode action 'Coverage Gutters: Display Coverage' to see coverage in the editor"
2 changes: 1 addition & 1 deletion dev-install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@ source "${DIR}/setup/node.sh"
# Done (hopefully)

echo "🚀 Environment setup/update completed (probably)"
echo "▶ Before you begin working in Python in each new terminal shell, run \`source apps/backend/.venv/Scripts/activate\` from the repo root to enter the Virtual Env. You do NOT need to use \`pipenv shell\` like pipenv suggests. You should see \`(backend)\` on your console line when you are in a virtual env. To exit the venv, just close the shell or send the command \`deactivate\`."
echo "▶ Before you begin working in Python in each new terminal shell, activate the respective pyenv by running \`source <apps/backend,apps/runner,docs>/.venv/Scripts/activate\` from the repo root to enter the Virtual Env. You do NOT need to use \`pipenv shell\` like pipenv suggests. You should see \`(backend)\` on your console line when you are in a virtual env. To exit the venv, just close the shell or send the command \`deactivate\`."
echo "▶ Note that VSCode might automatically do this for you in new terminal windows."
source setup/exit_await_input.sh 0
11 changes: 11 additions & 0 deletions setup/python.sh
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,17 @@ if ! pipenv install --dev; then
source setup/exit_await_input.sh 1
fi

# Install runner dependencies
if ! cd ../runner; then
echo "🛑 Failed to change dir to docs directory?"
source setup/exit_await_input.sh 1
fi

if ! pipenv install --dev; then
echo "🛑 Failed to install or update backend dependencies, check above error for more details"
source setup/exit_await_input.sh 1
fi

# Install documentation dependencies
if ! cd ../../docs; then
echo "🛑 Failed to change dir to docs directory?"
Expand Down

0 comments on commit 413923e

Please sign in to comment.