tests: make the fedora-tox.yml working

This is tricky.  Note the sys.path hack in test/unit/__init__.py where
we intentionally use the Fedora's default Python libraries with a
different Python version selected by Tox.  Right now it means that we
use, e.g., python3-rpm compiled for Python 3.12 (F39) with Python 3.7
(which probably works because Tox is executed as root in the tox
container, overwriting the pre-compiled *.pyc files in container).

Some tests need to be skipped in Tox, therefore the skip_if_tox()
method.

Also, the GitHub's action for 'git checkout' provides somewhat
non-standard environment for Tito to work, hence the fix_tox_env()
configuration method.
This commit is contained in:
Pavel Raiskup 2024-10-04 15:41:12 +02:00 committed by Jakub Kadlčík
parent 5f55271ca3
commit 167a91cb4f
6 changed files with 65 additions and 4 deletions

View file

@ -12,13 +12,28 @@ jobs:
tox_test: tox_test:
name: Tox test name: Tox test
steps: steps:
- uses: actions/checkout@v3 - uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Run Tox tests - name: Run Tox tests
id: test id: test
uses: fedora-python/tox-github-action@main uses: fedora-python/tox-github-action@main
with: with:
tox_env: ${{ matrix.tox_env }} tox_env: ${{ matrix.tox_env }}
dnf_install: python3-rpm dnf_install: >
asciidoc
createrepo_c
docbook-style-xsl
git
git
git-annex
libxslt
python3-bugzilla
python3-rpm
rpm-build
rpmdevtools
rsync
which
strategy: strategy:
matrix: matrix:
tox_env: tox_env:

View file

@ -1 +1,2 @@
blessed blessed
requests

View file

@ -13,6 +13,7 @@
import os import os
import sys import sys
import subprocess
from contextlib import contextmanager from contextlib import contextmanager
from unittest.mock import patch, MagicMock from unittest.mock import patch, MagicMock
@ -46,6 +47,34 @@ titodirpatch = patch("tito.cli.DEFAULT_BUILD_DIR", titodir)
titodirpatch.start() titodirpatch.start()
def fix_tox_env():
"""
If we run in the fedora-tox environment, we need to do some configuration
"""
if "TOX_WORK_DIR" not in os.environ:
return
dirs = subprocess.check_output(
"rpm -ql python3-libs | grep site-packages$", shell=True,
encoding="utf-8")
for site_dir in dirs.strip().split():
sys.path.append(site_dir)
if os.path.exists(os.path.expanduser("~/.gitconfig")):
return
gconf = ['git', 'config', '--global']
subprocess.call(gconf + ['user.email', 'you@example.com'], cwd="/tmp")
subprocess.call(gconf + ['user.name', 'Your Name'], cwd="/tmp")
subprocess.call(gconf + ['--add', 'safe.directory', '*'], cwd="/tmp")
subprocess.call(gconf + ['init.defaultBranch', 'main'], cwd="/tmp")
# tito tests need 'main' head, do it explicitly for github's checkout
subprocess.call(['git', 'branch', 'main', 'origin/main'])
fix_tox_env()
def skip_if_rpmbuild(): def skip_if_rpmbuild():
""" some tests can't work during rpmbuild """ """ some tests can't work during rpmbuild """
# don't do "isdir()", worktrees have .git as a plain file # don't do "isdir()", worktrees have .git as a plain file
@ -54,6 +83,12 @@ def skip_if_rpmbuild():
skip("not supported for rpmbuild") skip("not supported for rpmbuild")
def skip_if_tox():
""" some tests don't work nice with Tox """
if "TOX_WORK_DIR" in os.environ:
skip("doesn't work in tox")
class Capture(object): class Capture(object):
class Tee(object): class Tee(object):
def __init__(self, stream, silent): def __init__(self, stream, silent):

View file

@ -32,9 +32,11 @@ except ImportError:
import unittest import unittest
from unit.fixture import TitoUnitTestFixture, REPO_DIR
from unit import skip_if_tox
from tito.compat import * # NOQA from tito.compat import * # NOQA
from tito.compat import StringIO, redirect_stdout from tito.compat import StringIO, redirect_stdout
from unit.fixture import TitoUnitTestFixture, REPO_DIR
class TestPep8(TitoUnitTestFixture): class TestPep8(TitoUnitTestFixture):
@ -94,6 +96,8 @@ class TestPep8(TitoUnitTestFixture):
class UglyHackishTest(TitoUnitTestFixture): class UglyHackishTest(TitoUnitTestFixture):
def setUp(self): def setUp(self):
# These tests give false-positives for .tox/ directory
skip_if_tox()
TitoUnitTestFixture.setUp(self) TitoUnitTestFixture.setUp(self)
os.chdir(REPO_DIR) os.chdir(REPO_DIR)

View file

@ -1,10 +1,11 @@
import os
import unittest import unittest
from unittest import mock from unittest import mock
from datetime import datetime from datetime import datetime
from tito.tagger import VersionTagger from tito.tagger import VersionTagger
from tito.compat import PY2, RawConfigParser from tito.compat import PY2, RawConfigParser
from unit import is_epel6, skip_if_rpmbuild from unit import is_epel6, skip_if_rpmbuild, srcdir
if is_epel6: if is_epel6:
import unittest2 as unittest import unittest2 as unittest
else: else:
@ -22,6 +23,7 @@ def strftime_mock(s):
class TestVersionTagger(unittest.TestCase): class TestVersionTagger(unittest.TestCase):
def setUp(self): def setUp(self):
skip_if_rpmbuild() skip_if_rpmbuild()
os.chdir(srcdir)
self.config = RawConfigParser() self.config = RawConfigParser()
@mock.patch("tito.tagger.main.strftime", strftime_mock) @mock.patch("tito.tagger.main.strftime", strftime_mock)

View file

@ -20,7 +20,11 @@ skipsdist = True
deps = deps =
-rrequirements.txt -rrequirements.txt
coverage coverage
pycodestyle
pytest pytest
pytest-cov pytest-cov
commands = commands =
python -m pytest -v {posargs} --cov-report term-missing --cov-branch --cov python -m pytest -v {posargs} --cov-report term-missing --cov-branch --cov
setenv =
PYTHONPATH = ./src
syspaths = True