From ba4f5264ce4dd95025272d43df1a34dd3110087a Mon Sep 17 00:00:00 2001 From: Guido Günther Date: Fri, 20 Jan 2017 17:12:03 +0100 Subject: component test: Use more fixtures --- tests/component/deb/fixtures.py | 60 ++++++++++++++--------- tests/component/deb/test_buildpackage.py | 14 +++--- tests/component/deb/test_clone.py | 5 +- tests/component/deb/test_import_orig.py | 84 ++++++++++---------------------- tests/component/deb/test_pull.py | 4 +- 5 files changed, 72 insertions(+), 95 deletions(-) diff --git a/tests/component/deb/fixtures.py b/tests/component/deb/fixtures.py index 78dbb31b..5f14778a 100644 --- a/tests/component/deb/fixtures.py +++ b/tests/component/deb/fixtures.py @@ -38,39 +38,51 @@ DEFAULT_QUILT30 = os.path.join(DEB_TEST_DATA_DIR, class RepoFixtures(object): @classmethod - def native(cls, fn, dsc=DEFAULT_NATIVE): - """Debian native test fixture""" - @wraps(fn) - def _native_repo(*args): - repo = cls.import_native(dsc) - return fn(*args, repo=repo) - return _native_repo + def native(cls, dsc=DEFAULT_NATIVE, opts=None): + """Docorator to be used as Debian native test fixture""" + def wrapper(fn): + @wraps(fn) + def _native_repo(*args): + repo = cls.import_native(dsc, opts) + return fn(*args, repo=repo) + return _native_repo + return wrapper @classmethod - def quilt30(cls, fn, dsc=DEFAULT_QUILT30): - @wraps(fn) - def _quilt30_repo(*args): - repo = cls.import_quilt30(dsc) - return fn(*args, repo=repo) - return _quilt30_repo + def quilt30(cls, dsc=DEFAULT_QUILT30, opts=None): + """Decorator to be used as 3.0 (quilt) test fixture""" + def wrapper(fn): + @wraps(fn) + def _quilt30_repo(*args): + repo = cls.import_quilt30(dsc, opts) + return fn(*args, repo=repo) + return _quilt30_repo + return wrapper @classmethod - def import_native(cls, dsc=DEFAULT_NATIVE): - assert import_dsc(['arg0', dsc]) == 0 + def _import_one(cls, dsc, opts): + opts = opts or [] + assert import_dsc(['arg0'] + opts + [dsc]) == 0 parsed = DscFile(dsc) - repo = ComponentTestGitRepository(parsed.pkg) + return ComponentTestGitRepository(parsed.pkg) + + @classmethod + def import_native(cls, dsc=DEFAULT_NATIVE, opts=None): + """Import a Debian native package, verify and change into repo""" + repo = cls._import_one(dsc, opts) ComponentTestBase._check_repo_state(repo, 'master', ['master']) eq_(len(repo.get_commits()), 1) + os.chdir(repo.path) return repo @classmethod - def import_quilt30(cls, dsc=DEFAULT_QUILT30): - assert import_dsc(['arg0', dsc]) == 0 - parsed = DscFile(dsc) - repo = ComponentTestGitRepository(parsed.pkg) - ComponentTestBase._check_repo_state(repo, 'master', ['master', - 'upstream']) + def import_quilt30(cls, dsc=DEFAULT_QUILT30, opts=None): + """Import a 3.0 (quilt) package, verify and change into repo""" + repo = cls._import_one(dsc, opts) + expected_branches = ['master', 'upstream'] + if opts and '--pristine-tar' in opts: + expected_branches.append('pristine-tar') + ComponentTestBase._check_repo_state(repo, 'master', expected_branches) eq_(len(repo.get_commits()), 2) - return repo - assert eq_(len(repo.get_commits()), 2) + os.chdir(repo.path) return repo diff --git a/tests/component/deb/test_buildpackage.py b/tests/component/deb/test_buildpackage.py index a9ba5395..b7ee795e 100644 --- a/tests/component/deb/test_buildpackage.py +++ b/tests/component/deb/test_buildpackage.py @@ -63,20 +63,19 @@ class TestBuildpackage(ComponentTestBase): "GBP_CHANGES_FILE", "GBP_BUILD_DIR"]) - @RepoFixtures.native + @RepoFixtures.native() def test_debian_buildpackage(self, repo): """Test that building a native debian package works""" self._test_buildpackage(repo) - @RepoFixtures.quilt30 + @RepoFixtures.quilt30() def test_non_native_buildpackage(self, repo): """Test that building a source 3.0 debian package works""" self._test_buildpackage(repo) - @RepoFixtures.native + @RepoFixtures.native() def test_tag_only(self, repo): """Test that only tagging a native debian package works""" - os.chdir('git-buildpackage') repo.delete_tag('debian/0.4.14') # make sure we can tag again ret = buildpackage(['arg0', '--git-tag-only', @@ -138,16 +137,15 @@ class TestBuildpackage(ComponentTestBase): for t in tarballs: self.assertTrue(os.path.exists(t), "Tarball %s not found" % t) - @RepoFixtures.quilt30 + @RepoFixtures.quilt30() def test_export_dir_buildpackage(self, repo): """Test that building with a export dir works""" self._test_buildpackage(repo, ['--git-export-dir=../foo/bar']) ok_(os.path.exists('../foo/bar')) - @RepoFixtures.native + @RepoFixtures.native() def test_argument_quoting(self, repo): """Test that we quote arguments to builder (#)""" - os.chdir('git-buildpackage') with open('../arg with spaces', 'w'): pass # We use ls as builder to look for a file with spaces. This @@ -159,7 +157,7 @@ class TestBuildpackage(ComponentTestBase): '../arg with spaces']) ok_(ret == 0, "Building the package failed") - @RepoFixtures.quilt30 + @RepoFixtures.quilt30() def test_tarball_default_compression(self, repo): """Test that we use defaults for compression if not given (#820846)""" self._test_buildpackage(repo, ['--git-no-pristine-tar']) diff --git a/tests/component/deb/test_clone.py b/tests/component/deb/test_clone.py index 9beea644..d1b70f51 100644 --- a/tests/component/deb/test_clone.py +++ b/tests/component/deb/test_clone.py @@ -30,9 +30,10 @@ from gbp.scripts.clone import main as clone class TestClone(ComponentTestBase): """Test cloning from a remote""" - @RepoFixtures.native + @RepoFixtures.native() def test_clone_nonempty(self, repo): """Test that cloning into an existing dir fails""" + os.chdir('..') ok_(clone(['arg0', repo.path]) == 1, "Cloning did no fail as expected") self._check_log(-2, @@ -41,7 +42,7 @@ class TestClone(ComponentTestBase): "'git-buildpackage' already exists and is not " "an empty directory.") - @RepoFixtures.native + @RepoFixtures.native() def test_clone_native(self, repo): """Test that cloning of debian native packages works""" dest = os.path.join(self._tmpdir, diff --git a/tests/component/deb/test_import_orig.py b/tests/component/deb/test_import_orig.py index d9e171e5..5b9ec9bb 100644 --- a/tests/component/deb/test_import_orig.py +++ b/tests/component/deb/test_import_orig.py @@ -22,12 +22,10 @@ import tarfile from mock import patch, DEFAULT -from tests.component import (ComponentTestBase, - ComponentTestGitRepository, - skipUnless) +from tests.component import (ComponentTestBase, skipUnless) from tests.component.deb import DEB_TEST_DATA_DIR, DEB_TEST_DOWNLOAD_URL +from tests.component.deb.fixtures import RepoFixtures -from gbp.scripts.import_dsc import main as import_dsc from gbp.scripts.import_orig import main as import_orig from gbp.deb.pristinetar import DebianPristineTar from gbp.deb.dscfile import DscFile @@ -44,6 +42,13 @@ def raise_if_tag_match(match): return wrapped +def _dsc_file(pkg, version, dir='dsc-3.0'): + return os.path.join(DEB_TEST_DATA_DIR, dir, '%s_%s.dsc' % (pkg, version)) + + +DEFAULT_DSC = _dsc_file('hello-debhelper', '2.6-2') + + class TestImportOrig(ComponentTestBase): """Test importing of new upstream versions""" pkg = "hello-debhelper" @@ -59,11 +64,6 @@ class TestImportOrig(ComponentTestBase): dir, '%s_%s.orig.tar.gz' % (self.pkg, version)) - def _dsc(self, version, dir='dsc-3.0'): - return os.path.join(DEB_TEST_DATA_DIR, - dir, - '%s_%s.dsc' % (self.pkg, version)) - def test_initial_import(self): """Test that importing into an empty repo works""" repo = GitRepository.create(self.pkg) @@ -90,16 +90,11 @@ class TestImportOrig(ComponentTestBase): ok_(file in repo.ls_tree('upstream'), "Could not find component tarball file %s in %s" % (file, repo.ls_tree('upstream'))) - def test_update(self): + @RepoFixtures.quilt30(DEFAULT_DSC, opts=['--pristine-tar']) + def test_update(self, repo): """ Test that importing a new version works """ - dsc = self._dsc('2.6-2') - ok_(import_dsc(['arg0', '--pristine-tar', dsc]) == 0) - repo = ComponentTestGitRepository(self.pkg) - os.chdir(self.pkg) - self._check_repo_state(repo, 'master', ['master', 'upstream', 'pristine-tar']) - orig = self._orig('2.8') ok_(import_orig(['arg0', '--postimport=printenv > postimport.out', @@ -112,16 +107,11 @@ class TestImportOrig(ComponentTestBase): ("GBP_UPSTREAM_VERSION", "2.8"), ("GBP_DEBIAN_VERSION", "2.8-1")]) - def test_update_component_tarballs(self): + @RepoFixtures.quilt30(DEFAULT_DSC, opts=['--pristine-tar']) + def test_update_component_tarballs(self, repo): """ Test importing new version with additional tarballs works """ - dsc = self._dsc('2.6-2') - ok_(import_dsc(['arg0', '--pristine-tar', dsc]) == 0) - repo = ComponentTestGitRepository(self.pkg) - os.chdir(self.pkg) - self._check_repo_state(repo, 'master', ['master', 'upstream', 'pristine-tar']) - # Import 2.8 orig = self._orig('2.8', dir='dsc-3.0-additional-tarballs') ok_(import_orig(['arg0', '--component=foo', '--no-interactive', '--pristine-tar', orig]) == 0) @@ -129,7 +119,7 @@ class TestImportOrig(ComponentTestBase): tags=['debian/2.6-2', 'upstream/2.6', 'upstream/2.8']) self._check_component_tarballs(repo, ['foo/test1', 'foo/test2']) - dsc = DscFile.parse(self._dsc('2.8-1', dir='dsc-3.0-additional-tarballs')) + dsc = DscFile.parse(_dsc_file(self.pkg, '2.8-1', dir='dsc-3.0-additional-tarballs')) # Check if we can rebuild the upstream tarball and additional tarball ptars = [('hello-debhelper_2.8.orig.tar.gz', 'pristine-tar', '', dsc.tgz), ('hello-debhelper_2.8.orig-foo.tar.gz', 'pristine-tar^', 'foo', dsc.additional_tarballs['foo'])] @@ -153,7 +143,7 @@ class TestImportOrig(ComponentTestBase): tags=['debian/2.6-2', 'upstream/2.6', 'upstream/2.8', 'upstream/2.9']) self._check_component_tarballs(repo, ['foo/test1', 'foo/test2', 'foo/test3']) - dsc = DscFile.parse(self._dsc('2.9-1', dir='dsc-3.0-additional-tarballs')) + dsc = DscFile.parse(_dsc_file(self.pkg, '2.9-1', dir='dsc-3.0-additional-tarballs')) # Check if we can rebuild the upstream tarball and additional tarball ptars = [('hello-debhelper_2.9.orig.tar.gz', 'pristine-tar', '', dsc.tgz), ('hello-debhelper_2.9.orig-foo.tar.gz', 'pristine-tar^', 'foo', dsc.additional_tarballs['foo'])] @@ -171,7 +161,7 @@ class TestImportOrig(ComponentTestBase): def test_tag_exists(self): """Test that importing an already imported version fails""" repo = GitRepository.create(self.pkg) - os.chdir(self.pkg) + os.chdir(repo.path) orig = self._orig('2.6') # First import ok_(import_orig(['arg0', '--no-interactive', '--pristine-tar', orig]) == 0) @@ -182,20 +172,13 @@ class TestImportOrig(ComponentTestBase): # Check that the second import didn't change any refs self.check_refs(repo, heads) - def test_update_fail_create_upstream_tag(self): + @RepoFixtures.quilt30(DEFAULT_DSC, opts=['--pristine-tar']) + def test_update_fail_create_upstream_tag(self, repo): """ Test that we can recover from a failure to create the upstream tag """ - repo = GitRepository.create(self.pkg) - os.chdir(self.pkg) - - dsc = self._dsc('2.6-2') - ok_(import_dsc(['arg0', '--pristine-tar', dsc]) == 0) - self._check_repo_state(repo, 'master', ['master', 'upstream', 'pristine-tar']) - heads = self.rem_refs(repo, self.def_branches) - orig = self._orig('2.8') with patch('gbp.git.repository.GitRepository.create_tag', side_effect=GitRepositoryError('this is a create tag error mock')): @@ -204,19 +187,12 @@ class TestImportOrig(ComponentTestBase): tags=['debian/2.6-2', 'upstream/2.6']) self.check_refs(repo, heads) - def test_update_fail_merge(self): + @RepoFixtures.quilt30(DEFAULT_DSC, opts=['--pristine-tar']) + def test_update_fail_merge(self, repo): """ Test that we can recover from a failed merge """ - repo = GitRepository.create(self.pkg) - os.chdir(self.pkg) - - dsc = self._dsc('2.6-2') - ok_(import_dsc(['arg0', '--pristine-tar', dsc]) == 0) - self._check_repo_state(repo, 'master', ['master', 'upstream', 'pristine-tar']) - heads = self.rem_refs(repo, self.def_branches) - orig = self._orig('2.8') with patch('gbp.scripts.import_orig.debian_branch_merge', side_effect=GitRepositoryError('this is a fail merge error mock')): @@ -233,7 +209,7 @@ class TestImportOrig(ComponentTestBase): tag on initial import """ repo = GitRepository.create(self.pkg) - os.chdir(self.pkg) + os.chdir(repo.path) orig = self._orig('2.6') ok_(import_orig(['arg0', '--no-interactive', orig]) == 1) @@ -254,16 +230,11 @@ class TestImportOrig(ComponentTestBase): self._check_repo_state(repo, None, [], tags=[]) - def test_filter_with_component_tarballs(self): + @RepoFixtures.quilt30(DEFAULT_DSC, opts=['--pristine-tar']) + def test_filter_with_component_tarballs(self, repo): """ Test that using a filter works even with component tarballs (#840602) """ - dsc = self._dsc('2.6-2') - ok_(import_dsc(['arg0', '--pristine-tar', dsc]) == 0) - repo = ComponentTestGitRepository(self.pkg) - os.chdir(self.pkg) - self._check_repo_state(repo, 'master', ['master', 'upstream', 'pristine-tar']) - # copy data since we don't want the repacked tarball to end up in DEB_TEST_DATA_DIR os.mkdir('../tarballs') for f in ['hello-debhelper_2.8.orig-foo.tar.gz', 'hello-debhelper_2.8.orig.tar.gz']: @@ -296,17 +267,12 @@ class TestImportOrig(ComponentTestBase): t.getmember(f) t.close() - def test_filter_with_orig_tarball(self): + @RepoFixtures.quilt30(DEFAULT_DSC, opts=['--pristine-tar']) + def test_filter_with_orig_tarball(self, repo): """ Test that using a filter works even with an upstream tarball that has already the correct name (#558777) """ - dsc = self._dsc('2.6-2') - ok_(import_dsc(['arg0', '--pristine-tar', dsc]) == 0) - repo = ComponentTestGitRepository(self.pkg) - os.chdir(self.pkg) - self._check_repo_state(repo, 'master', ['master', 'upstream', 'pristine-tar']) - f = 'hello-debhelper_2.8.orig.tar.gz' src = os.path.join(DEB_TEST_DATA_DIR, 'dsc-3.0', f) shutil.copy(src, '..') diff --git a/tests/component/deb/test_pull.py b/tests/component/deb/test_pull.py index 80f6fe66..266f1c52 100644 --- a/tests/component/deb/test_pull.py +++ b/tests/component/deb/test_pull.py @@ -31,7 +31,7 @@ from gbp.scripts.pull import main as pull class TestPull(ComponentTestBase): """Test cloning from a remote""" - @RepoFixtures.native + @RepoFixtures.native() def test_pull_explicit_remote(self, repo): """Test that pulling of debian native packages works""" dest = os.path.join(self._tmpdir, 'cloned_repo') @@ -41,7 +41,7 @@ class TestPull(ComponentTestBase): eq_(pull(['argv0', 'origin']), 0) assert len(repo.get_commits()) == 1 - @RepoFixtures.native + @RepoFixtures.native() def test_pull_default_remote(self, repo): """Test that pulling of debian native packages works""" dest = os.path.join(self._tmpdir, 'cloned_repo') -- cgit v1.2.3