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
.build .build
.coverage .coverage
.test-titodir
titorc.5 titorc.5
titorc.5.xml titorc.5.xml
tito.8 tito.8

View file

@ -7,4 +7,4 @@ case $arg in
*) args+=( "$arg" ) *) args+=( "$arg" )
esac esac
done 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 glob import glob
from os.path import join from os.path import join
from unit import skip_if_rpmbuild
class BuildTitoTests(unittest.TestCase): class BuildTitoTests(unittest.TestCase):
@classmethod @classmethod
def setUpClass(self): def setUpClass(self):
'Run tito build before _all_ tests in this class.' 'Run tito build before _all_ tests in this class.'
skip_if_rpmbuild()
self.output_dir = tempfile.mkdtemp("-titotestoutput") self.output_dir = tempfile.mkdtemp("-titotestoutput")
os.chdir(os.path.abspath(join(__file__, '..', '..', '..'))) os.chdir(os.path.abspath(join(__file__, '..', '..', '..')))
self.artifacts = tito( 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 self.output_dir
) )

View file

@ -26,7 +26,7 @@ from os.path import join
from tito.common import run_command from tito.common import run_command
from tito.compat import * # NOQA from tito.compat import * # NOQA
from functional.fixture import TitoGitTestFixture, tito 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: if is_epel6:
import unittest2 as unittest import unittest2 as unittest
@ -83,6 +83,8 @@ class FetchBuilderTests(TitoGitTestFixture):
def test_simple_build_no_tag(self): def test_simple_build_no_tag(self):
# We have not tagged here. Build --rpm should just work: # We have not tagged here. Build --rpm should just work:
skip_if_rpmbuild()
self.assertFalse(os.path.exists( self.assertFalse(os.path.exists(
join(self.pkg_dir, '.tito/packages/extsrc'))) join(self.pkg_dir, '.tito/packages/extsrc')))

View file

@ -19,6 +19,8 @@ from unittest import mock
from functional.fixture import TitoGitTestFixture from functional.fixture import TitoGitTestFixture
from unit import titodir
from tito.compat import * # NOQA from tito.compat import * # NOQA
from tito.release import CoprReleaser from tito.release import CoprReleaser
@ -58,7 +60,7 @@ class CoprReleaserTests(TitoGitTestFixture):
'http://example.com/~someuser/my_srpm/') 'http://example.com/~someuser/my_srpm/')
def test_with_releaser(self): 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, self.config, {}, 'test', self.releaser_config, False,
False, False, **{'offline': True}) False, False, **{'offline': True})
releaser.release(dry_run=True) releaser.release(dry_run=True)
@ -67,7 +69,7 @@ class CoprReleaserTests(TitoGitTestFixture):
def test_with_remote_defined_in_user_conf(self): def test_with_remote_defined_in_user_conf(self):
self.releaser_config.remove_option("test", "remote_location") self.releaser_config.remove_option("test", "remote_location")
user_config = {'COPR_REMOTE_LOCATION': 'http://example.com/~otheruser/'} 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, self.config, user_config, 'test', self.releaser_config, False,
False, False, **{'offline': True}) False, False, **{'offline': True})
releaser.release(dry_run=True) releaser.release(dry_run=True)
@ -77,7 +79,7 @@ class CoprReleaserTests(TitoGitTestFixture):
@mock.patch("tito.release.CoprReleaser._upload") @mock.patch("tito.release.CoprReleaser._upload")
def test_no_remote_defined(self, upload, submit): def test_no_remote_defined(self, upload, submit):
self.releaser_config.remove_option("test", "remote_location") 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, self.config, {}, 'test', self.releaser_config, False,
False, False, **{'offline': True}) False, False, **{'offline': True})
releaser.release(dry_run=True) releaser.release(dry_run=True)
@ -90,7 +92,7 @@ class CoprReleaserTests(TitoGitTestFixture):
self.releaser_config.remove_option("test", "remote_location") self.releaser_config.remove_option("test", "remote_location")
self.releaser_config.set('test', 'project_name', "%s %s" % (PKG_NAME, self.releaser_config.set('test', 'project_name', "%s %s" % (PKG_NAME,
PKG_NAME)) PKG_NAME))
releaser = CoprReleaser(PKG_NAME, None, '/tmp/tito/', releaser = CoprReleaser(PKG_NAME, None, titodir,
self.config, {}, 'test', self.releaser_config, False, self.config, {}, 'test', self.releaser_config, False,
False, False, **{'offline': True}) False, False, **{'offline': True})
releaser.release() releaser.release()

View file

@ -19,7 +19,7 @@ from tito.release import Releaser
from tito.compat import getoutput from tito.compat import getoutput
from functional.fixture import TitoGitTestFixture, tito from functional.fixture import TitoGitTestFixture, tito
from tito.compat import RawConfigParser from tito.compat import RawConfigParser
from unit import Capture from unit import Capture, titodir, skip_if_rpmbuild
PKG_NAME = "titotestpkg" PKG_NAME = "titotestpkg"
@ -93,6 +93,8 @@ class SingleProjectTests(TitoGitTestFixture):
self.assertTrue('- Fake' in changelog) self.assertTrue('- Fake' in changelog)
def test_undo_tag(self): def test_undo_tag(self):
skip_if_rpmbuild()
os.chdir(self.repo_dir) os.chdir(self.repo_dir)
original_head = getoutput('git show-ref -s refs/heads/main') original_head = getoutput('git show-ref -s refs/heads/main')
@ -162,7 +164,7 @@ class SingleProjectTests(TitoGitTestFixture):
self.repo_dir)) self.repo_dir))
def test_release(self): def test_release(self):
releaser = Releaser(PKG_NAME, None, '/tmp/tito/', releaser = Releaser(PKG_NAME, None, titodir,
self.config, {}, 'test', self.releaser_config, False, self.config, {}, 'test', self.releaser_config, False,
False, False, **{'offline': True}) False, False, **{'offline': True})
self.assertTrue(isinstance(releaser.builder, Builder)) self.assertTrue(isinstance(releaser.builder, Builder))
@ -171,7 +173,7 @@ class SingleProjectTests(TitoGitTestFixture):
def test_release_override_builder(self): def test_release_override_builder(self):
self.releaser_config.set('test', 'builder', self.releaser_config.set('test', 'builder',
'tito.builder.UpstreamBuilder') 'tito.builder.UpstreamBuilder')
releaser = Releaser(PKG_NAME, None, '/tmp/tito/', releaser = Releaser(PKG_NAME, None, titodir,
self.config, {}, 'test', self.releaser_config, False, self.config, {}, 'test', self.releaser_config, False,
False, False, **{'offline': True}) False, False, **{'offline': True})
self.assertTrue(isinstance(releaser.builder, self.assertTrue(isinstance(releaser.builder,

View file

@ -11,10 +11,13 @@
# granted to use or replicate Red Hat trademarks that are incorporated # granted to use or replicate Red Hat trademarks that are incorporated
# in this software or its documentation. # in this software or its documentation.
import os
import sys import sys
from contextlib import contextmanager from contextlib import contextmanager
from unittest.mock import patch, MagicMock from unittest.mock import patch, MagicMock
from pytest import skip
from tito.compat import PY2, StringIO from tito.compat import PY2, StringIO
@ -37,6 +40,19 @@ else:
file_spec = None 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 Capture(object):
class Tee(object): class Tee(object):

View file

@ -20,14 +20,14 @@ import unittest
from unittest.mock import patch, call from unittest.mock import patch, call
from tempfile import NamedTemporaryFile from tempfile import NamedTemporaryFile
from textwrap import dedent from textwrap import dedent
from unit import open_mock, Capture from unit import open_mock, Capture, titodir
from blessed import Terminal from blessed import Terminal
# Pure unit tests for tito's common module # Pure unit tests for tito's common module
from tito.common import (replace_version, find_spec_like_file, increase_version, from tito.common import (replace_version, find_spec_like_file, increase_version,
search_for, compare_version, run_command_print, find_wrote_in_rpmbuild_output, search_for, compare_version, run_command_print, find_wrote_in_rpmbuild_output,
render_cheetah, increase_zstream, reset_release, find_file_with_extension, 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, munge_setup_macro, get_project_name,
_out) _out)
@ -40,7 +40,7 @@ class CommonTests(unittest.TestCase):
def setUp(self): def setUp(self):
# Start in a known location to prevent problems with tests that # Start in a known location to prevent problems with tests that
# end in a temp directory that is subsequently deleted. # end in a temp directory that is subsequently deleted.
os.chdir(DEFAULT_BUILD_DIR) os.chdir(titodir)
def test_normalize_class_name(self): def test_normalize_class_name(self):
""" Test old spacewalk.releng namespace is converted to tito. """ """ Test old spacewalk.releng namespace is converted to tito. """
@ -221,10 +221,11 @@ class CommonTests(unittest.TestCase):
mock_user_conf.return_value = {} mock_user_conf.return_value = {}
stream = StringIO() stream = StringIO()
with patch("blessed.terminal.os.isatty") as isatty: with patch("blessed.terminal.os.isatty") as isatty:
_out('Hello world', None, Terminal().red, stream) with patch.dict(os.environ, {"TERM": "xterm"}):
isatty.return_value = True _out('Hello world', None, Terminal().red, stream)
# RHEL 6 doesn't have self.assertRegexpMatches unfortunately isatty.return_value = True
self.assertTrue(re.match('.+Hello world.+\n', stream.getvalue())) # RHEL 6 doesn't have self.assertRegexpMatches unfortunately
self.assertTrue(re.match('.+Hello world.+\n', stream.getvalue()))
def test_get_project_name(self): def test_get_project_name(self):
TAGS = [ TAGS = [

View file

@ -2,7 +2,7 @@ import unittest
from unittest import mock from unittest import mock
from tito.compat import PY2, RawConfigParser from tito.compat import PY2, RawConfigParser
from tito.release import Releaser from tito.release import Releaser
from unit import builtins_input from unit import builtins_input, titodir, skip_if_rpmbuild
class ReleaserTests(unittest.TestCase): class ReleaserTests(unittest.TestCase):
@ -10,6 +10,7 @@ class ReleaserTests(unittest.TestCase):
@mock.patch("tito.release.main.create_builder") @mock.patch("tito.release.main.create_builder")
@mock.patch("tito.release.main.mkdtemp") @mock.patch("tito.release.main.mkdtemp")
def setUp(self, mkdtemp, create_builder): def setUp(self, mkdtemp, create_builder):
skip_if_rpmbuild()
self.config = RawConfigParser() self.config = RawConfigParser()
self.releaser_config = RawConfigParser() self.releaser_config = RawConfigParser()
@ -17,7 +18,7 @@ class ReleaserTests(unittest.TestCase):
self.releaser_config.set('test', "releaser", self.releaser_config.set('test', "releaser",
"tito.release.Releaser") "tito.release.Releaser")
self.releaser = Releaser("titotestpkg", None, "/tmp/tito/", self.releaser = Releaser("titotestpkg", None, titodir,
self.config, {}, "test", self.releaser_config, False, self.config, {}, "test", self.releaser_config, False,
False, False, **{"offline": True}) False, False, **{"offline": True})

View file

@ -4,7 +4,7 @@ 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 from unit import is_epel6, skip_if_rpmbuild
if is_epel6: if is_epel6:
import unittest2 as unittest import unittest2 as unittest
else: else:
@ -21,6 +21,7 @@ def strftime_mock(s):
class TestVersionTagger(unittest.TestCase): class TestVersionTagger(unittest.TestCase):
def setUp(self): def setUp(self):
skip_if_rpmbuild()
self.config = RawConfigParser() self.config = RawConfigParser()
@mock.patch("tito.tagger.main.strftime", strftime_mock) @mock.patch("tito.tagger.main.strftime", strftime_mock)

View file

@ -15,6 +15,12 @@
%endif %endif
%endif %endif
%if 0%{?rhel} == 7
%bcond_with check
%else
%bcond_without check
%endif
Name: tito Name: tito
Version: 0.6.26 Version: 0.6.26
Release: 1%{?dist} Release: 1%{?dist}
@ -53,15 +59,15 @@ BuildRequires: rpm-build
BuildRequires: tar BuildRequires: tar
BuildRequires: which BuildRequires: which
%if 0%{?fedora} %if %{with check}
# todo: add %%check to spec file in accordance with BuildRequires: createrepo_c
# https://fedoraproject.org/wiki/QA/Testing_in_check
BuildRequires: git BuildRequires: git
BuildRequires: python-bugzilla BuildRequires: rsync
BuildRequires: python3-devel BuildRequires: python3-blessed
BuildRequires: python3-setuptools
BuildRequires: python3-bugzilla BuildRequires: python3-bugzilla
BuildRequires: rpm-python3 BuildRequires: python3-pycodestyle
BuildRequires: python3-pytest
BuildRequires: python3-rpm
%endif %endif
Requires: rpm-build 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 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 %files
%doc AUTHORS COPYING %doc AUTHORS COPYING
%doc doc/* %doc doc/*