From 8ce108266bc3cc27681ac008a9760cdbb3bf38b5 Mon Sep 17 00:00:00 2001 From: Pavel Raiskup Date: Thu, 3 Oct 2024 20:13:45 +0200 Subject: [PATCH] tests: enable tests at RPM build time - Some tests can not work at rpmbuild time, because we can not run rpmbuild in rpmbuild, and there's no .git/ directory, skip. - Use 'titodir' below 'srcdir', to not pollute the /tmp/tito dir. - The test_colors() test requires color-friendly TERM env. - EL7 is EOL, do not enable tests there. --- .gitignore | 1 + runtests.sh | 2 +- test/functional/__init__.py | 5 +++++ test/functional/build_tito_tests.py | 6 +++++- test/functional/fetch_tests.py | 4 +++- test/functional/release_copr_tests.py | 10 +++++---- test/functional/singleproject_tests.py | 8 +++++--- test/unit/__init__.py | 16 +++++++++++++++ test/unit/common_tests.py | 15 +++++++------- test/unit/releaser_tests.py | 5 +++-- test/unit/tagger_tests.py | 3 ++- tito.spec | 28 +++++++++++++++++++------- 12 files changed, 76 insertions(+), 27 deletions(-) diff --git a/.gitignore b/.gitignore index 2fa2b44..b74bb5f 100644 --- a/.gitignore +++ b/.gitignore @@ -14,6 +14,7 @@ dist build .build .coverage +.test-titodir titorc.5 titorc.5.xml tito.8 diff --git a/runtests.sh b/runtests.sh index c2eb37f..d06a050 100755 --- a/runtests.sh +++ b/runtests.sh @@ -7,4 +7,4 @@ case $arg in *) args+=( "$arg" ) esac done -exec python3 -m pytest -vv $cov "${args[@]}" +PYTHONPATH=$PWD/src exec python3 -m pytest -vv $cov "${args[@]}" diff --git a/test/functional/__init__.py b/test/functional/__init__.py index e69de29..c004750 100644 --- a/test/functional/__init__.py +++ b/test/functional/__init__.py @@ -0,0 +1,5 @@ +""" +Tito testsuite - config for test/functional. +""" + +from unit import titodirpatch # noqa: F401 diff --git a/test/functional/build_tito_tests.py b/test/functional/build_tito_tests.py index e471f23..bfce725 100644 --- a/test/functional/build_tito_tests.py +++ b/test/functional/build_tito_tests.py @@ -25,16 +25,20 @@ from functional.fixture import tito from glob import glob from os.path import join +from unit import skip_if_rpmbuild + class BuildTitoTests(unittest.TestCase): @classmethod def setUpClass(self): 'Run tito build before _all_ tests in this class.' + skip_if_rpmbuild() + self.output_dir = tempfile.mkdtemp("-titotestoutput") os.chdir(os.path.abspath(join(__file__, '..', '..', '..'))) self.artifacts = tito( - 'build --rpm --test --output=%s --offline --no-cleanup --debug' % + 'build --rpm --rpmbuild-options=--without=check --output=%s --offline --no-cleanup --debug' % self.output_dir ) diff --git a/test/functional/fetch_tests.py b/test/functional/fetch_tests.py index f1dc680..709d541 100644 --- a/test/functional/fetch_tests.py +++ b/test/functional/fetch_tests.py @@ -26,7 +26,7 @@ from os.path import join from tito.common import run_command from tito.compat import * # NOQA from functional.fixture import TitoGitTestFixture, tito -from unit import Capture, is_epel6, is_rawhide +from unit import Capture, is_epel6, is_rawhide, skip_if_rpmbuild if is_epel6: import unittest2 as unittest @@ -83,6 +83,8 @@ class FetchBuilderTests(TitoGitTestFixture): def test_simple_build_no_tag(self): # We have not tagged here. Build --rpm should just work: + skip_if_rpmbuild() + self.assertFalse(os.path.exists( join(self.pkg_dir, '.tito/packages/extsrc'))) diff --git a/test/functional/release_copr_tests.py b/test/functional/release_copr_tests.py index a89e986..7086c80 100644 --- a/test/functional/release_copr_tests.py +++ b/test/functional/release_copr_tests.py @@ -19,6 +19,8 @@ from unittest import mock from functional.fixture import TitoGitTestFixture +from unit import titodir + from tito.compat import * # NOQA from tito.release import CoprReleaser @@ -58,7 +60,7 @@ class CoprReleaserTests(TitoGitTestFixture): 'http://example.com/~someuser/my_srpm/') def test_with_releaser(self): - releaser = CoprReleaser(PKG_NAME, None, '/tmp/tito/', + releaser = CoprReleaser(PKG_NAME, None, titodir, self.config, {}, 'test', self.releaser_config, False, False, False, **{'offline': True}) releaser.release(dry_run=True) @@ -67,7 +69,7 @@ class CoprReleaserTests(TitoGitTestFixture): def test_with_remote_defined_in_user_conf(self): self.releaser_config.remove_option("test", "remote_location") user_config = {'COPR_REMOTE_LOCATION': 'http://example.com/~otheruser/'} - releaser = CoprReleaser(PKG_NAME, None, '/tmp/tito/', + releaser = CoprReleaser(PKG_NAME, None, titodir, self.config, user_config, 'test', self.releaser_config, False, False, False, **{'offline': True}) releaser.release(dry_run=True) @@ -77,7 +79,7 @@ class CoprReleaserTests(TitoGitTestFixture): @mock.patch("tito.release.CoprReleaser._upload") def test_no_remote_defined(self, upload, submit): self.releaser_config.remove_option("test", "remote_location") - releaser = CoprReleaser(PKG_NAME, None, '/tmp/tito/', + releaser = CoprReleaser(PKG_NAME, None, titodir, self.config, {}, 'test', self.releaser_config, False, False, False, **{'offline': True}) releaser.release(dry_run=True) @@ -90,7 +92,7 @@ class CoprReleaserTests(TitoGitTestFixture): self.releaser_config.remove_option("test", "remote_location") self.releaser_config.set('test', 'project_name', "%s %s" % (PKG_NAME, PKG_NAME)) - releaser = CoprReleaser(PKG_NAME, None, '/tmp/tito/', + releaser = CoprReleaser(PKG_NAME, None, titodir, self.config, {}, 'test', self.releaser_config, False, False, False, **{'offline': True}) releaser.release() diff --git a/test/functional/singleproject_tests.py b/test/functional/singleproject_tests.py index 4d50b3c..ff8c9d0 100644 --- a/test/functional/singleproject_tests.py +++ b/test/functional/singleproject_tests.py @@ -19,7 +19,7 @@ from tito.release import Releaser from tito.compat import getoutput from functional.fixture import TitoGitTestFixture, tito from tito.compat import RawConfigParser -from unit import Capture +from unit import Capture, titodir, skip_if_rpmbuild PKG_NAME = "titotestpkg" @@ -93,6 +93,8 @@ class SingleProjectTests(TitoGitTestFixture): self.assertTrue('- Fake' in changelog) def test_undo_tag(self): + skip_if_rpmbuild() + os.chdir(self.repo_dir) original_head = getoutput('git show-ref -s refs/heads/main') @@ -162,7 +164,7 @@ class SingleProjectTests(TitoGitTestFixture): self.repo_dir)) def test_release(self): - releaser = Releaser(PKG_NAME, None, '/tmp/tito/', + releaser = Releaser(PKG_NAME, None, titodir, self.config, {}, 'test', self.releaser_config, False, False, False, **{'offline': True}) self.assertTrue(isinstance(releaser.builder, Builder)) @@ -171,7 +173,7 @@ class SingleProjectTests(TitoGitTestFixture): def test_release_override_builder(self): self.releaser_config.set('test', 'builder', 'tito.builder.UpstreamBuilder') - releaser = Releaser(PKG_NAME, None, '/tmp/tito/', + releaser = Releaser(PKG_NAME, None, titodir, self.config, {}, 'test', self.releaser_config, False, False, False, **{'offline': True}) self.assertTrue(isinstance(releaser.builder, diff --git a/test/unit/__init__.py b/test/unit/__init__.py index 9e9ffa7..f3e7f74 100644 --- a/test/unit/__init__.py +++ b/test/unit/__init__.py @@ -11,10 +11,13 @@ # granted to use or replicate Red Hat trademarks that are incorporated # in this software or its documentation. +import os import sys from contextlib import contextmanager from unittest.mock import patch, MagicMock +from pytest import skip + from tito.compat import PY2, StringIO @@ -37,6 +40,19 @@ else: file_spec = None +srcdir = os.path.join(os.path.dirname(__file__), '..', '..') +titodir = os.path.join(srcdir, '.test-titodir') +titodirpatch = patch("tito.cli.DEFAULT_BUILD_DIR", titodir) +titodirpatch.start() + + +def skip_if_rpmbuild(): + """ some tests can't work during rpmbuild """ + # don't do "isdir()", worktrees have .git as a plain file + if os.path.exists(os.path.join(srcdir, ".git")): + return + skip("not supported for rpmbuild") + class Capture(object): class Tee(object): diff --git a/test/unit/common_tests.py b/test/unit/common_tests.py index 6a91510..4d76443 100644 --- a/test/unit/common_tests.py +++ b/test/unit/common_tests.py @@ -20,14 +20,14 @@ import unittest from unittest.mock import patch, call from tempfile import NamedTemporaryFile from textwrap import dedent -from unit import open_mock, Capture +from unit import open_mock, Capture, titodir from blessed import Terminal # Pure unit tests for tito's common module from tito.common import (replace_version, find_spec_like_file, increase_version, search_for, compare_version, run_command_print, find_wrote_in_rpmbuild_output, render_cheetah, increase_zstream, reset_release, find_file_with_extension, - normalize_class_name, extract_sha1, DEFAULT_BUILD_DIR, munge_specfile, + normalize_class_name, extract_sha1, munge_specfile, munge_setup_macro, get_project_name, _out) @@ -40,7 +40,7 @@ class CommonTests(unittest.TestCase): def setUp(self): # Start in a known location to prevent problems with tests that # end in a temp directory that is subsequently deleted. - os.chdir(DEFAULT_BUILD_DIR) + os.chdir(titodir) def test_normalize_class_name(self): """ Test old spacewalk.releng namespace is converted to tito. """ @@ -221,10 +221,11 @@ class CommonTests(unittest.TestCase): mock_user_conf.return_value = {} stream = StringIO() with patch("blessed.terminal.os.isatty") as isatty: - _out('Hello world', None, Terminal().red, stream) - isatty.return_value = True - # RHEL 6 doesn't have self.assertRegexpMatches unfortunately - self.assertTrue(re.match('.+Hello world.+\n', stream.getvalue())) + with patch.dict(os.environ, {"TERM": "xterm"}): + _out('Hello world', None, Terminal().red, stream) + isatty.return_value = True + # RHEL 6 doesn't have self.assertRegexpMatches unfortunately + self.assertTrue(re.match('.+Hello world.+\n', stream.getvalue())) def test_get_project_name(self): TAGS = [ diff --git a/test/unit/releaser_tests.py b/test/unit/releaser_tests.py index 919854d..59a9ff0 100644 --- a/test/unit/releaser_tests.py +++ b/test/unit/releaser_tests.py @@ -2,7 +2,7 @@ import unittest from unittest import mock from tito.compat import PY2, RawConfigParser from tito.release import Releaser -from unit import builtins_input +from unit import builtins_input, titodir, skip_if_rpmbuild class ReleaserTests(unittest.TestCase): @@ -10,6 +10,7 @@ class ReleaserTests(unittest.TestCase): @mock.patch("tito.release.main.create_builder") @mock.patch("tito.release.main.mkdtemp") def setUp(self, mkdtemp, create_builder): + skip_if_rpmbuild() self.config = RawConfigParser() self.releaser_config = RawConfigParser() @@ -17,7 +18,7 @@ class ReleaserTests(unittest.TestCase): self.releaser_config.set('test', "releaser", "tito.release.Releaser") - self.releaser = Releaser("titotestpkg", None, "/tmp/tito/", + self.releaser = Releaser("titotestpkg", None, titodir, self.config, {}, "test", self.releaser_config, False, False, False, **{"offline": True}) diff --git a/test/unit/tagger_tests.py b/test/unit/tagger_tests.py index 3448881..18dd94b 100644 --- a/test/unit/tagger_tests.py +++ b/test/unit/tagger_tests.py @@ -4,7 +4,7 @@ from datetime import datetime from tito.tagger import VersionTagger from tito.compat import PY2, RawConfigParser -from unit import is_epel6 +from unit import is_epel6, skip_if_rpmbuild if is_epel6: import unittest2 as unittest else: @@ -21,6 +21,7 @@ def strftime_mock(s): class TestVersionTagger(unittest.TestCase): def setUp(self): + skip_if_rpmbuild() self.config = RawConfigParser() @mock.patch("tito.tagger.main.strftime", strftime_mock) diff --git a/tito.spec b/tito.spec index f835614..b7016db 100644 --- a/tito.spec +++ b/tito.spec @@ -15,6 +15,12 @@ %endif %endif +%if 0%{?rhel} == 7 +%bcond_with check +%else +%bcond_without check +%endif + Name: tito Version: 0.6.26 Release: 1%{?dist} @@ -53,15 +59,15 @@ BuildRequires: rpm-build BuildRequires: tar BuildRequires: which -%if 0%{?fedora} -# todo: add %%check to spec file in accordance with -# https://fedoraproject.org/wiki/QA/Testing_in_check +%if %{with check} +BuildRequires: createrepo_c BuildRequires: git -BuildRequires: python-bugzilla -BuildRequires: python3-devel -BuildRequires: python3-setuptools +BuildRequires: rsync +BuildRequires: python3-blessed BuildRequires: python3-bugzilla -BuildRequires: rpm-python3 +BuildRequires: python3-pycodestyle +BuildRequires: python3-pytest +BuildRequires: python3-rpm %endif Requires: rpm-build @@ -103,6 +109,14 @@ cp -a tito.8 %{buildroot}/%{_mandir}/man8/ install -Dp -m 0644 share/tito_completion.sh %{buildroot}%{_datadir}/bash-completion/completions/tito +%if %{with check} +%check +git config --global user.email "you@example.com" +git config --global user.name "Your Name" +./runtests.sh --no-cov +%endif + + %files %doc AUTHORS COPYING %doc doc/*