Skip to content

Commit e91dc57

Browse files
authored
DGS-16859: Enable SonarQube code coverage reporting (confluentinc#1887)
1 parent 1cf37de commit e91dc57

File tree

7 files changed

+60
-4
lines changed

7 files changed

+60
-4
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,6 @@ tmp-KafkaCluster
3030
venv_test
3131
venv_examples
3232
*Zone.Identifier
33+
.coverage
34+
**/coverage.xml
35+
**/test-report.xml

.semaphore/semaphore.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,22 @@ global_job_config:
1414
- checkout
1515
- mkdir artifacts
1616
blocks:
17+
- name: Test
18+
dependencies: []
19+
run:
20+
# don't run the tests on non-functional changes...
21+
when: "change_in('/', {exclude: ['.github/']})"
22+
task:
23+
jobs:
24+
- name: Test
25+
commands:
26+
- sem-version python 3.9
27+
- pip install tox
28+
- tox -e cover
29+
- mkdir test-output
30+
- cp test-report.xml test-output
31+
- test-results publish test-output
32+
- artifact push workflow coverage.xml
1733
- name: "Wheels: OSX x64 - Python 3.6-3.12"
1834
run:
1935
when: "tag =~ '.*'"
@@ -285,3 +301,16 @@ blocks:
285301
- cd ..
286302
- artifact push project artifacts/confluent-kafka-python-wheels-${SEMAPHORE_GIT_TAG_NAME}-${SEMAPHORE_WORKFLOW_ID}.tgz --destination confluent-kafka-python-wheels-${SEMAPHORE_GIT_TAG_NAME}-${SEMAPHORE_WORKFLOW_ID}.tgz
287303
- echo Thank you
304+
305+
after_pipeline:
306+
task:
307+
agent:
308+
machine:
309+
type: s1-prod-ubuntu20-04-amd64-1
310+
jobs:
311+
- name: SonarQube
312+
commands:
313+
- checkout
314+
- sem-version java 11
315+
- artifact pull workflow coverage.xml
316+
- emit-sonarqube-data --run_only_sonar_scan

requirements/requirements-tests.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ pytest
66
pytest-timeout
77
requests-mock
88
respx
9+
pytest_cov

service.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ git:
66
github:
77
enable: true
88
repo_name: confluentinc/confluent-kafka-python
9+
sonarqube:
10+
enable: true
11+
coverage_exclusions:
12+
- "**/*.pb.*"
13+
- "**/mk-include/**/*"
14+
- "examples/**"
915
semaphore:
1016
enable: true
1117
pipeline_enable: false

sonar-project.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
### service-bot sonarqube plugin managed file
22
sonar.coverage.exclusions=**/test/**/*,**/tests/**/*,**/mock/**/*,**/mocks/**/*,**/*mock*,**/*test*
33
sonar.cpd.exclusions=**/test/**/*,**/tests/**/*,**/mock/**/*,**/mocks/**/*,**/*mock*,**/*test*
4-
sonar.exclusions=**/*.pb.*,**/mk-include/**/*
4+
sonar.exclusions=**/*.pb.*,**/mk-include/**/*,examples/**
55
sonar.language=python
66
sonar.projectKey=confluent-kafka-python
77
sonar.python.coverage.reportPaths=**/coverage.xml

tests/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,10 @@ Tox can be used to test against various supported Python versions (py27, py36, p
123123

124124
```$ ./tests/run.sh tox```
125125

126+
127+
### Running with test coverage
128+
From top-level directory:
129+
```
130+
pip install tox
131+
./tests/run.sh tox -e cover
132+
```

tox.ini

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
envlist = flake8,py37,py38,py39,py310,py311,py312,py313
33

44
[testenv]
5+
deps =
6+
-r requirements/requirements-tests-install.txt
7+
.
58
passenv =
69
#http://tox.readthedocs.io/en/latest/config.html#confval-passenv=SPACE-SEPARATED-GLOBNAMES
710
*
811
commands =
9-
# Install main package and all sub-packages
10-
pip install -r requirements/requirements-tests-install.txt
11-
pip install .
1212
# Early verification that module is loadable
1313
python -c 'import confluent_kafka ; print(confluent_kafka.version())'
1414
# Run tests (large timeout to allow docker image downloads)
@@ -20,6 +20,16 @@ commands =
2020
deps = flake8
2121
commands = flake8
2222

23+
[testenv:cover]
24+
commands =
25+
# Install source with editable flag (-e) so that SonarQube can parse the coverage report.
26+
# Otherwise, the report shows source files located in site-packages, which SonarQube cannot find.
27+
# Example: ".tox/cover/lib/python3.11/site-packages/confluent_kafka/__init__.py"
28+
# instead of "src/confluent_kafka/__init__.py"
29+
pip install -e .
30+
pytest {env:PYTESTARGS:} --cov confluent_kafka --cov-report term --cov-report html --cov-report xml \
31+
--cov-branch --junitxml=test-report.xml tests/ {posargs}
32+
2333
[pytest]
2434
python_files = test_*
2535
testpaths = tests

0 commit comments

Comments
 (0)