Skip to content

Commit

Permalink
tests: make the fedora-tox.yml working
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
praiskup authored and FrostyX committed Nov 6, 2024
1 parent 5f55271 commit 167a91c
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 4 deletions.
19 changes: 17 additions & 2 deletions .github/workflows/fedora-tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,28 @@ jobs:
tox_test:
name: Tox test
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Run Tox tests
id: test
uses: fedora-python/tox-github-action@main
with:
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:
matrix:
tox_env:
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
blessed
requests
35 changes: 35 additions & 0 deletions test/unit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import os
import sys
import subprocess

from contextlib import contextmanager
from unittest.mock import patch, MagicMock
Expand Down Expand Up @@ -46,6 +47,34 @@
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', '[email protected]'], 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():
""" some tests can't work during rpmbuild """
# don't do "isdir()", worktrees have .git as a plain file
Expand All @@ -54,6 +83,12 @@ def skip_if_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 Tee(object):
def __init__(self, stream, silent):
Expand Down
6 changes: 5 additions & 1 deletion test/unit/pep8_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@

import unittest

from unit.fixture import TitoUnitTestFixture, REPO_DIR
from unit import skip_if_tox

from tito.compat import * # NOQA
from tito.compat import StringIO, redirect_stdout
from unit.fixture import TitoUnitTestFixture, REPO_DIR


class TestPep8(TitoUnitTestFixture):
Expand Down Expand Up @@ -94,6 +96,8 @@ def test_conformance(self):

class UglyHackishTest(TitoUnitTestFixture):
def setUp(self):
# These tests give false-positives for .tox/ directory
skip_if_tox()
TitoUnitTestFixture.setUp(self)
os.chdir(REPO_DIR)

Expand Down
4 changes: 3 additions & 1 deletion test/unit/tagger_tests.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import os
import unittest
from unittest import mock
from datetime import datetime
from tito.tagger import VersionTagger
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:
import unittest2 as unittest
else:
Expand All @@ -22,6 +23,7 @@ def strftime_mock(s):
class TestVersionTagger(unittest.TestCase):
def setUp(self):
skip_if_rpmbuild()
os.chdir(srcdir)
self.config = RawConfigParser()

@mock.patch("tito.tagger.main.strftime", strftime_mock)
Expand Down
4 changes: 4 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ skipsdist = True
deps =
-rrequirements.txt
coverage
pycodestyle
pytest
pytest-cov
commands =
python -m pytest -v {posargs} --cov-report term-missing --cov-branch --cov
setenv =
PYTHONPATH = ./src
syspaths = True

0 comments on commit 167a91c

Please sign in to comment.