From 0672e7a153a6b88e3de57038366dd35856c5108b Mon Sep 17 00:00:00 2001 From: "Morten B. Rasmussen" Date: Wed, 11 Dec 2024 09:40:16 +0100 Subject: [PATCH] feat(coverage): Add coverage to tests * Add coveragerc file, explicitly including cijoe modules * Add coverage execution for examples Signed-off-by: Morten B. Rasmussen --- .coveragerc | 9 + .github/workflows/cijoe_packages.yml | 171 +++++++++++++++++- .github/workflows/selftest.yml | 105 ----------- Makefile | 10 +- .../scripts/dockerimage_from_diskimage.py | 2 +- 5 files changed, 179 insertions(+), 118 deletions(-) create mode 100644 .coveragerc delete mode 100644 .github/workflows/selftest.yml diff --git a/.coveragerc b/.coveragerc new file mode 100644 index 00000000..84ba417b --- /dev/null +++ b/.coveragerc @@ -0,0 +1,9 @@ +[run] +source = + cijoe.core + cijoe.cli + cijoe.fio + cijoe.gha + cijoe.linux + cijoe.qemu + cijoe.system_imaging diff --git a/.github/workflows/cijoe_packages.yml b/.github/workflows/cijoe_packages.yml index 6ef270f0..ebdea958 100644 --- a/.github/workflows/cijoe_packages.yml +++ b/.github/workflows/cijoe_packages.yml @@ -13,10 +13,85 @@ on: env: LIBGUESTFS_BACKEND: direct + +defaults: + run: + shell: bash + jobs: + format: + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest] + python-version: ['3.12'] + + steps: + - name: Grab source + uses: actions/checkout@v4.1.7 + + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v5.2.0 + with: + python-version: ${{ matrix.python-version }} + + - name: Python dependencies and their versions + run: | + brew update || true + brew upgrade pipx || true + make deps + make info + echo $HOME + echo "$HOME/.local/bin" >> $GITHUB_PATH + + - name: Install pre-commit + run: | + pipx install pre-commit --python ${{ matrix.python-version }} + + - name: Run pre-commit + run: | + make format-all + + selftest: + needs: format + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest] + python-version: ['3.10', '3.11', '3.12'] + + steps: + - name: Grab source + uses: actions/checkout@v4.1.7 + + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v5.2.0 + with: + python-version: ${{ matrix.python-version }} + + - name: Python dependencies and their versions + run: | + pip install pipx + pipx environment + pipx ensurepath + brew update || true + brew upgrade pipx || true + make deps + make info + echo "$HOME/.local/bin" >> $GITHUB_PATH + + - name: Run make all + run: | + make all + ls -lha + cat .coverage + cijoe -r examples: runs-on: ubuntu-latest + needs: format container: image: ghcr.io/refenv/cijoe-docker:latest @@ -26,14 +101,12 @@ jobs: fail-fast: false matrix: usage_example: - - core.default - fio.default + - core.default - linux.default - - qemu.bootimage - qemu.build - - qemu.cloudinit - system_imaging.default - python-version: ['3.12'] + python-version: ['3.11'] steps: - name: Grab source @@ -56,16 +129,100 @@ jobs: - name: Emit the example configuration file and workflow run: | cijoe --example ${{ matrix.usage_example }} + ls -lha + pwd + which cijoe + which coverage - name: Run it! run: | - cd cijoe-example-${{ matrix.usage_example }} - cijoe --monitor -l + cd cijoe-example-${{ matrix.usage_example}} + coverage run \ + --data-file=.coverage \ + --rcfile=../repository/.coveragerc \ + $(which cijoe) --monitor + coverage report - name: Upload report if: always() uses: actions/upload-artifact@v4.3.0 with: name: report-cijoe_packages-${{ matrix.usage_example }} - path: cijoe-example-${{ matrix.usage_example }}/cijoe-output/* + path: cijoe-example-${{ matrix.usage_example}}/cijoe-output/* if-no-files-found: error + + - name: Upload coverage + if: always() + uses: actions/upload-artifact@v4.3.0 + with: + name: report-coverage-${{ matrix.usage_example }} + path: cijoe-example-${{ matrix.usage_example}}/.coverage + if-no-files-found: error + + collect-coverage: + runs-on: ubuntu-latest + needs: [examples, selftest] + container: + image: ghcr.io/refenv/cijoe-docker:latest + options: --privileged + + strategy: + fail-fast: false + + steps: + - name: Grab source + uses: actions/checkout@v4.2.2 + with: + path: repository + + - name: Build and install cijoe from source + run: | + pipx uninstall cijoe + cd repository + make deps info build install + echo "$HOME/.local/bin" >> $GITHUB_PATH + + - name: Download all artifacts + uses: actions/download-artifact@v4.1.8 + with: + # If you omit 'name', all artifacts from previous jobs are downloaded. + path: ./all_artifacts + + - name: Show downloaded artifacts + run: | + coverage combine \ + --rcfile=repository/.coveragerc \ + all_artifacts/**/.coverage + coverage report \ + --debug=trace,config \ + --rcfile=repository/.coveragerc + + - name: Coveralls upload + uses: coverallsapp/github-action@v2 + + + publish: + if: startsWith(github.ref, 'refs/tags/v') + needs: selftest + + runs-on: ubuntu-latest + steps: + - name: Grab source + uses: actions/checkout@v4.1.7 + - name: Set up Python + uses: actions/setup-python@v5.2.0 + with: + python-version: '3.9' + + - name: Python dependencies and their versions + run: | + make deps + make info + echo "$HOME/.local/bin" >> $GITHUB_PATH + + - name: Build and publish + env: + TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }} + TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }} + run: |- + make release diff --git a/.github/workflows/selftest.yml b/.github/workflows/selftest.yml deleted file mode 100644 index 44444ea5..00000000 --- a/.github/workflows/selftest.yml +++ /dev/null @@ -1,105 +0,0 @@ ---- -name: selftest - -on: - workflow_dispatch: - pull_request: - push: - branches: - - "main" - tags: - - "v*" - -defaults: - run: - shell: bash - -jobs: - format: - runs-on: ${{ matrix.os }} - strategy: - fail-fast: false - matrix: - os: [ubuntu-latest] - python-version: ['3.12'] - - steps: - - name: Grab source - uses: actions/checkout@v4.1.7 - - - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v5.2.0 - with: - python-version: ${{ matrix.python-version }} - - - name: Python dependencies and their versions - run: | - brew update || true - brew upgrade pipx || true - make deps - make info - echo "$HOME/.local/bin" >> $GITHUB_PATH - - - name: Install pre-commit - run: | - pipx install pre-commit --python ${{ matrix.python-version }} - - - name: Run pre-commit - run: | - make format-all - - selftest: - needs: format - runs-on: ${{ matrix.os }} - strategy: - fail-fast: false - matrix: - os: [ubuntu-latest, macos-latest] - python-version: ['3.9', '3.10', '3.11', '3.12'] - - steps: - - name: Grab source - uses: actions/checkout@v4.1.7 - - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v5.2.0 - with: - python-version: ${{ matrix.python-version }} - - - name: Python dependencies and their versions - run: | - brew update || true - brew upgrade pipx || true - make deps - make info - echo "$HOME/.local/bin" >> $GITHUB_PATH - - - name: All - run: | - make all - cijoe -r - - publish: - if: startsWith(github.ref, 'refs/tags/v') - needs: selftest - - runs-on: ubuntu-latest - steps: - - name: Grab source - uses: actions/checkout@v4.1.7 - - name: Set up Python - uses: actions/setup-python@v5.2.0 - with: - python-version: '3.9' - - - name: Python dependencies and their versions - run: | - make deps - make info - echo "$HOME/.local/bin" >> $GITHUB_PATH - - - name: Build and publish - env: - TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }} - TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }} - run: | - make release diff --git a/Makefile b/Makefile index 1baa6e9d..18a8409b 100644 --- a/Makefile +++ b/Makefile @@ -4,9 +4,9 @@ PROJECT_NAME=cijoe BUILD=pyproject-build PIPX=pipx -PYTEST="$(shell pipx environment -V PIPX_LOCAL_VENVS)/${PROJECT_NAME}/bin/pytest" +PYTEST="$(shell pipx environment --value PIPX_LOCAL_VENVS)/${PROJECT_NAME}/bin/pytest" PYTHON_SYS=python3 -PYTHON_VENV="$(shell pipx environment -V PIPX_LOCAL_VENVS)/${PROJECT_NAME}/bin/python3" +#PYTHON_VENV="$(shell pipx environment --value PIPX_LOCAL_VENVS)/${PROJECT_NAME}/bin/python3" TWINE=twine CIJOE_VERSION=$(shell cd src; python3 -c "from cijoe import core;print(core.__version__)") @@ -42,7 +42,6 @@ info: ${PIPX} --version || true ${PYTEST} --version || true ${PYTHON_SYS} --version || true - ${PYTHON_VENV} --version || true ${TWINE} --version || true @echo "## ${PROJECT_NAME}: make info [DONE]" @@ -123,7 +122,8 @@ endef install: @echo "## ${PROJECT_NAME}: make install" @${PIPX} install dist/*.tar.gz --include-deps --force --python python3 - @${PIPX} inject cijoe coverage --include-apps --force + @${PIPX} inject cijoe coverage --include-apps --include-deps --force + @${PIPX} inject cijoe pytest-cov --force @echo "## ${PROJECT_NAME}: make install [DONE]" define uninstall-help @@ -143,7 +143,7 @@ endef .PHONY: test test: @echo "## ${PROJECT_NAME}: make test" - ${PYTEST} --pyargs cijoe.core.selftest --config src/cijoe/core/configs/example_config_default.toml -v -s + ${PYTEST} --cov --pyargs cijoe.core.selftest --config src/cijoe/core/configs/example_config_default.toml -v -s @echo "## ${PROJECT_NAME}: make test [DONE]" define examples-help diff --git a/src/cijoe/system_imaging/scripts/dockerimage_from_diskimage.py b/src/cijoe/system_imaging/scripts/dockerimage_from_diskimage.py index 17c3227d..6664c2c4 100644 --- a/src/cijoe/system_imaging/scripts/dockerimage_from_diskimage.py +++ b/src/cijoe/system_imaging/scripts/dockerimage_from_diskimage.py @@ -67,7 +67,7 @@ def dockerimage_from_diskimage(cijoe, image): cijoe.run_local(f'echo "Needs cleanup!" && find {workdir}') cijoe.run_local( - f'echo "Run with: docker run -it {image['docker']['name']}:{image['docker']['tag']} bash"' + f'echo "Run with: docker run -it {image["docker"]["name"]}:{image["docker"]["tag"]} bash"' ) return 0