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.
This commit is contained in:
Pavel Raiskup 2024-10-03 20:13:45 +02:00 committed by Jakub Kadlčík
parent 6feb4f2d21
commit 8ce108266b
12 changed files with 76 additions and 27 deletions

1
.gitignore vendored
View file

@ -14,6 +14,7 @@ dist
build
.build
.coverage
.test-titodir
titorc.5
titorc.5.xml
tito.8

View file

@ -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[@]}"

View file

@ -0,0 +1,5 @@
"""
Tito testsuite - config for test/functional.
"""
from unit import titodirpatch # noqa: F401

View file

@ -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
)

View file

@ -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')))

View file

@ -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()

View file

@ -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,

View file

@ -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):

View file

@ -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,6 +221,7 @@ class CommonTests(unittest.TestCase):
mock_user_conf.return_value = {}
stream = StringIO()
with patch("blessed.terminal.os.isatty") as isatty:
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

View file

@ -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})

View file

@ -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)

View file

@ -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/*