From 167a91cb4f9c852628542f8e611e37a003aa90de Mon Sep 17 00:00:00 2001 From: Pavel Raiskup Date: Fri, 4 Oct 2024 15:41:12 +0200 Subject: [PATCH] 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. --- .github/workflows/fedora-tox.yml | 19 +++++++++++++++-- requirements.txt | 1 + test/unit/__init__.py | 35 ++++++++++++++++++++++++++++++++ test/unit/pep8_tests.py | 6 +++++- test/unit/tagger_tests.py | 4 +++- tox.ini | 4 ++++ 6 files changed, 65 insertions(+), 4 deletions(-) diff --git a/.github/workflows/fedora-tox.yml b/.github/workflows/fedora-tox.yml index 823af2d5..efab8749 100644 --- a/.github/workflows/fedora-tox.yml +++ b/.github/workflows/fedora-tox.yml @@ -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: diff --git a/requirements.txt b/requirements.txt index d43de1b8..a69c6bc6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ blessed +requests diff --git a/test/unit/__init__.py b/test/unit/__init__.py index f3e7f74f..4d1b797f 100644 --- a/test/unit/__init__.py +++ b/test/unit/__init__.py @@ -13,6 +13,7 @@ import os import sys +import subprocess from contextlib import contextmanager from unittest.mock import patch, MagicMock @@ -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', '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(): """ some tests can't work during rpmbuild """ # don't do "isdir()", worktrees have .git as a plain file @@ -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): diff --git a/test/unit/pep8_tests.py b/test/unit/pep8_tests.py index a844c340..cffbc414 100644 --- a/test/unit/pep8_tests.py +++ b/test/unit/pep8_tests.py @@ -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): @@ -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) diff --git a/test/unit/tagger_tests.py b/test/unit/tagger_tests.py index 18dd94bc..dfb39435 100644 --- a/test/unit/tagger_tests.py +++ b/test/unit/tagger_tests.py @@ -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: @@ -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) diff --git a/tox.ini b/tox.ini index 438fb94e..c965e8b4 100644 --- a/tox.ini +++ b/tox.ini @@ -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