From 4010f15a80567b865b8fd15135111c86648c8d74 Mon Sep 17 00:00:00 2001 From: Philipp Rintz <13933258+p-rintz@users.noreply.github.com> Date: Mon, 27 Feb 2023 05:18:21 +0100 Subject: [PATCH] Pytest Github Action (#1) Run pytest on PR/push. Fix main run test. Add coverage/license/build pass badges to README Create pytest comment on PR --------- Co-authored-by: github-actions[bot] --- .github/workflows/linter.yml | 2 +- .github/workflows/pytest-pull.yml | 31 ++++++++++++++++++++++ .github/workflows/pytest-push.yml | 43 +++++++++++++++++++++++++++++++ .gitignore | 2 +- README.md | 6 ++++- data/.gitignore | 1 + data/pytest-coverage.txt | 21 +++++++++++++++ pytest.xml | 1 + requirements_dev.txt | 8 ++++-- tests/test_netbox-dnsmasq.py | 2 ++ 10 files changed, 112 insertions(+), 5 deletions(-) create mode 100644 .github/workflows/pytest-pull.yml create mode 100644 .github/workflows/pytest-push.yml create mode 100644 data/.gitignore create mode 100644 data/pytest-coverage.txt create mode 100644 pytest.xml diff --git a/.github/workflows/linter.yml b/.github/workflows/linter.yml index aa6f054..f949ecc 100644 --- a/.github/workflows/linter.yml +++ b/.github/workflows/linter.yml @@ -5,7 +5,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - python_version: ["3.7", "3.8", "3.9", "3.10", "3.11"] + python_version: ["3.8", "3.9", "3.10", "3.11"] steps: - uses: actions/checkout@v3 with: diff --git a/.github/workflows/pytest-pull.yml b/.github/workflows/pytest-pull.yml new file mode 100644 index 0000000..96f19e7 --- /dev/null +++ b/.github/workflows/pytest-pull.yml @@ -0,0 +1,31 @@ +name: pytest-coverage-comment +on: + pull_request: + +jobs: + build: + runs-on: ubuntu-latest + strategy: + matrix: + python_version: ["3.8", "3.9", "3.10", "3.11"] + steps: + - uses: actions/checkout@v3 + + - uses: actions/setup-python@v4 + with: + python-version: ${{ matrix.python_version }} + + - name: Install dependencies + run: | + python3 -m pip install --upgrade pip + if [ -f requirements_dev.txt ]; then pip3 install -r requirements_dev.txt; fi + + - name: Build coverage file + run: | + pytest --junitxml=pytest.xml --cov-report=term-missing:skip-covered | tee pytest-coverage.txt + + - name: Pytest coverage comment + uses: MishaKav/pytest-coverage-comment@main + with: + pytest-coverage-path: ./pytest-coverage.txt + junitxml-path: ./pytest.xml diff --git a/.github/workflows/pytest-push.yml b/.github/workflows/pytest-push.yml new file mode 100644 index 0000000..f1bfe1e --- /dev/null +++ b/.github/workflows/pytest-push.yml @@ -0,0 +1,43 @@ +# From: https://github.com/marketplace/actions/pytest-coverage-comment +name: Update Coverage in Readme +on: + push: +jobs: + update-coverage-on-readme: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + with: + persist-credentials: false # otherwise, the token used is the GITHUB_TOKEN, instead of your personal token + fetch-depth: 0 # otherwise, you will failed to push refs to dest repo + + - uses: actions/setup-python@v4 + with: + python-version: 3.11 + + - name: Install dependencies + run: | + python3 -m pip install --upgrade pip + if [ -f requirements_dev.txt ]; then pip3 install -r requirements_dev.txt; fi + + - name: Build coverage file + run: | + pytest --junitxml=pytest.xml --cov | tee data/pytest-coverage.txt + + - name: Pytest coverage comment + id: coverageComment + uses: MishaKav/pytest-coverage-comment@main + with: + hide-comment: true + pytest-coverage-path: ./data/pytest-coverage.txt + + - name: Update Readme with Coverage Html + run: | + sed -i '//,//c\![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/p-rintz/netbox-dnsmasq/pytest-push.yml)![GitHub](https://img.shields.io/github/license/p-rintz/netbox-dnsmasq?color=brightgreen&style=flat)\n\${{ steps.coverageComment.outputs.coverageHtml }}\n' ./README.md + + - name: Commit & Push changes to Readme + uses: actions-js/push@master + with: + message: Update coverage on Readme + branch: ${{ github.ref_name }} + github_token: ${{ secrets.GITHUB_TOKEN }} diff --git a/.gitignore b/.gitignore index 96d9d2b..3cb0ab3 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,4 @@ dhcphosts.conf dnsmasq.hosts /Dockerfile-* # Test Dockerfiles .coverage - +__pycache__ diff --git a/README.md b/README.md index fb4f78d..0b44c8c 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,8 @@ # Netbox-dnsmasq +![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/p-rintz/netbox-dnsmasq/pytest-push.yml)![GitHub](https://img.shields.io/github/license/p-rintz/netbox-dnsmasq?color=brightgreen&style=flat) +Coverage
Coverage Report
FileStmtsMissCoverMissing
netbox_dnsmasq.py18088 96%
tests
   __init__.py00100% 
   conftest.py290100% 
   test_netbox-dnsmasq.py760100% 
TOTAL285897% 
+ + Create dnsmasq DHCP/DNS configs using the Netbox API. ## Configuration @@ -10,7 +14,7 @@ python3 -m venv .venv source .venv/bin/activate pip install -r requirements.txt ``` -**Python 3.7 or higher is required.** +**Python 3.8 or higher is required.** ### Config locations diff --git a/data/.gitignore b/data/.gitignore new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/data/.gitignore @@ -0,0 +1 @@ + diff --git a/data/pytest-coverage.txt b/data/pytest-coverage.txt new file mode 100644 index 0000000..2892313 --- /dev/null +++ b/data/pytest-coverage.txt @@ -0,0 +1,21 @@ +============================= test session starts ============================== +platform linux -- Python 3.11.2, pytest-7.2.1, pluggy-1.0.0 +rootdir: /home/runner/work/netbox-dnsmasq/netbox-dnsmasq +plugins: cov-4.0.0, recording-0.12.2 +collected 14 items + +tests/test_netbox-dnsmasq.py .............. [100%] + +- generated xml file: /home/runner/work/netbox-dnsmasq/netbox-dnsmasq/pytest.xml - + +---------- coverage: platform linux, python 3.11.2-final-0 ----------- +Name Stmts Miss Cover +-------------------------------------------------- +netbox_dnsmasq.py 180 8 96% +tests/__init__.py 0 0 100% +tests/conftest.py 29 0 100% +tests/test_netbox-dnsmasq.py 76 0 100% +-------------------------------------------------- +TOTAL 285 8 97% + +============================== 14 passed in 1.36s ============================== diff --git a/pytest.xml b/pytest.xml new file mode 100644 index 0000000..dae31c5 --- /dev/null +++ b/pytest.xml @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/requirements_dev.txt b/requirements_dev.txt index 5f72087..325ebbe 100644 --- a/requirements_dev.txt +++ b/requirements_dev.txt @@ -1,6 +1,10 @@ pytest~=7.2.1 -coverage==7.2.1 +pytest-cov~=4.0.0 pynetbox~=7.0.1 vcrpy~=4.2.1 pytest-recording~=0.12.2 -loguru~=0.6.0 \ No newline at end of file +loguru~=0.6.0 +flake8~=6.0.0 +black~=23.1.0 +isort~=5.12.0 +mypy~=1.0.1 diff --git a/tests/test_netbox-dnsmasq.py b/tests/test_netbox-dnsmasq.py index 7e49b35..2d4c5dd 100644 --- a/tests/test_netbox-dnsmasq.py +++ b/tests/test_netbox-dnsmasq.py @@ -224,6 +224,8 @@ def test_main_help(capsys, option): ) +@pytest.mark.default_cassette("test_get_ip_data.yaml") +@pytest.mark.vcr def test_main_run(capsys, set_test_environment): """ Test main function for correctly starting the script.