jenkins-bot has submitted this change. (
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1181097?usp=email )
Change subject: Tests: Enable coverage for script_tests
......................................................................
Tests: Enable coverage for script_tests
Bug: T401124
Change-Id: Ib83f0d05ee92bf4fababc0d4f04b16362d585935
---
M .github/workflows/pywikibot-ci.yml
M tests/utils.py
2 files changed, 32 insertions(+), 9 deletions(-)
Approvals:
Xqt: Looks good to me, approved
jenkins-bot: Verified
diff --git a/.github/workflows/pywikibot-ci.yml
b/.github/workflows/pywikibot-ci.yml
index 114fb7b..dea4499 100644
--- a/.github/workflows/pywikibot-ci.yml
+++ b/.github/workflows/pywikibot-ci.yml
@@ -140,6 +140,7 @@
fi
- name: Show coverage statistics
run: |
+ coverage combine
coverage report
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v5
diff --git a/tests/utils.py b/tests/utils.py
index 8f5d342..30a9959 100644
--- a/tests/utils.py
+++ b/tests/utils.py
@@ -9,9 +9,11 @@
import inspect
import os
import sys
+import tempfile
import unittest
import warnings
from contextlib import contextmanager, suppress
+from pathlib import Path
from subprocess import PIPE, Popen, TimeoutExpired
from typing import Any, NoReturn
@@ -521,16 +523,36 @@
"""
command = [sys.executable]
- if overrides:
- command.append('-c')
- overrides = '; '.join(
- f'{key} = {value}' for key, value in overrides.items())
- command.append(
- f'import pwb; import pywikibot; {overrides}; pwb.main()')
- else:
- command.append(_pwb_py)
+ # Test running and coverage is installed, enable coverage with subprocess
+ if os.environ.get('PYWIKIBOT_TEST_RUNNING') == '1':
+ with suppress(ModuleNotFoundError):
+ import coverage # noqa: F401
+ command.extend(['-m', 'coverage', 'run', '--parallel-mode'])
- return execute(command=command + args, data_in=data_in, timeout=timeout)
+ tmp_path: Path | None = None
+ try:
+ if overrides:
+ # Write overrides in temporary file
+ with tempfile.NamedTemporaryFile(
+ 'w', suffix='.py', delete=False) as f:
+ f.write('import pwb\nimport pywikibot\n')
+ f.write('\n'.join(f'{k} = {v}' for k, v in overrides.items()))
+ f.write('\npwb.main()\n')
+ tmp_path = Path(f.name)
+ command.append(f.name)
+ else:
+ command.append(_pwb_py)
+
+ # Run subprocess
+ result = execute(
+ command=command + args, data_in=data_in, timeout=timeout)
+
+ finally:
+ # delete temporary file if created
+ if tmp_path and tmp_path.exists():
+ tmp_path.unlink()
+
+ return result
@contextmanager
--
To view, visit
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1181097?usp=email
To unsubscribe, or for help writing mail filters, visit
https://gerrit.wikimedia.org/r/settings?usp=email
Gerrit-MessageType: merged
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: Ib83f0d05ee92bf4fababc0d4f04b16362d585935
Gerrit-Change-Number: 1181097
Gerrit-PatchSet: 3
Gerrit-Owner: Xqt <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
Pywikibot-commits mailing list -- [email protected]
To unsubscribe send an email to [email protected]