05_test_detection.py 4.8 KB
Newer Older
1 2 3 4
# vim: set fileencoding=utf-8 :

"""Test tarball compression type detection"""

5 6
from . import context

7 8
import unittest

9 10 11 12
from gbp.deb.source import DebianSource

from gbp.scripts import export_orig
from gbp.deb import DebianPkgPolicy
13
from gbp.errors import GbpError
14

Guido Günther's avatar
Guido Günther committed
15

16 17 18 19 20
class MockGitRepository:
    def __init__(self, with_branch=False, subject=None):
        self.with_branch = with_branch
        self.subject = subject

21
    def has_pristine_tar_branch(self):
22 23
        return self.with_branch

24 25 26
    def pristine_tar_branch(self):
        'pristine-tar'

27
    def grep_log(self, regex, branch, merges=True):
28 29
        return None

30 31
    def get_commit_info(self, commit):
        return {'subject': self.subject}
32

Guido Günther's avatar
Guido Günther committed
33

34 35 36 37 38 39 40 41 42 43
class MockedSource(DebianSource):
    def __init__(self):
        pass

    def is_native(self):
        return False

    @property
    def name(self):
        return 'source'
44

45 46 47 48 49 50
    @property
    def upstream_version(self):
        return '1.2'


class TestDetection(unittest.TestCase):
51
    def setUp(self):
52
        self.source = MockedSource()
53
        self.tmpdir = context.new_tmpdir(__name__)
54 55

    def tearDown(self):
56
        context.teardown()
57

58
    def test_guess_comp_type_no_pristine_tar_no_orig(self):
59
        repo = MockGitRepository(with_branch=False)
60
        guessed = export_orig.guess_comp_type(
61
            'auto', self.source, repo, str(self.tmpdir))
62 63
        self.assertEqual('gzip', guessed)

64
    def test_guess_comp_type_no_pristine_tar_with_orig(self):
65
        open(self.tmpdir.join('source_1.2.orig.tar.bz2'), "w").close()
66
        repo = MockGitRepository(with_branch=False)
67
        guessed = export_orig.guess_comp_type(
68
            'auto', self.source, repo, str(self.tmpdir))
69 70 71
        self.assertEqual('bzip2', guessed)

    def test_guess_comp_type_no_pristine_tar_with_multiple_origs(self):
72 73
        open(self.tmpdir.join('source_1.2.orig.tar.gz'), "w").close()
        open(self.tmpdir.join('source_1.2.orig.tar.xz'), "w").close()
74
        repo = MockGitRepository(with_branch=False)
75 76
        with self.assertRaises(GbpError):
            export_orig.guess_comp_type('auto', self.source, repo, str(self.tmpdir))
77

78
    def test_guess_comp_type_auto_bzip2(self):
79 80
        subject = 'pristine-tar data for source_1.2-3.orig.tar.bz2'
        repo = MockGitRepository(with_branch=True, subject=subject)
81
        guessed = export_orig.guess_comp_type(
82
            'auto', self.source, repo, str(self.tmpdir))
83 84
        self.assertEqual("bzip2", guessed)

85
    def test_has_orig_single_false(self):
86
        self.assertFalse(DebianPkgPolicy.has_origs([self.source.upstream_tarball_name('gzip')], str(self.tmpdir)))
87

88
    def test_has_orig_single_true(self):
89
        open(self.tmpdir.join('source_1.2.orig.tar.gz'), "w").close()
90
        self.assertTrue(DebianPkgPolicy.has_origs([self.source.upstream_tarball_name('gzip')], str(self.tmpdir)))
91 92

    def test_has_orig_multiple_false(self):
93 94
        orig_files = [self.source.upstream_tarball_name('gzip')] + \
                     [self.source.upstream_tarball_name('gzip', sub) for sub in ['foo', 'bar']]
95 96 97 98 99
        self.assertFalse(DebianPkgPolicy.has_origs(orig_files, str(self.tmpdir)))

    def test_has_orig_multiple_true(self):
        for ext in ['', '-foo', '-bar']:
            open(self.tmpdir.join('source_1.2.orig%s.tar.gz' % ext), "w").close()
100 101
        orig_files = [self.source.upstream_tarball_name('gzip')] + \
                     [self.source.upstream_tarball_name('gzip', sub) for sub in ['foo', 'bar']]
102
        self.assertTrue(DebianPkgPolicy.has_origs(orig_files, str(self.tmpdir)))
103

104 105
    def test_guess_comp_type_bzip2(self):
        repo = MockGitRepository(with_branch=False)
106
        guessed = export_orig.guess_comp_type(
107
            'bzip2', self.source, repo, None)
108 109 110 111
        self.assertEqual("bzip2", guessed)

    def test_guess_comp_type_gzip(self):
        repo = MockGitRepository(with_branch=False)
112
        guessed = export_orig.guess_comp_type(
113
            'gzip', self.source, repo, None)
114 115 116 117
        self.assertEqual("gzip", guessed)

    def test_guess_comp_type_bz(self):
        repo = MockGitRepository(with_branch=False)
118
        guessed = export_orig.guess_comp_type(
119
            'xz', self.source, repo, None)
120 121 122 123
        self.assertEqual("xz", guessed)

    def test_guess_comp_type_lzma(self):
        repo = MockGitRepository(with_branch=False)
124
        guessed = export_orig.guess_comp_type(
125
            'lzma', self.source, repo, None)
126
        self.assertEqual("lzma", guessed)
127 128 129

    def test_guess_comp_type_bz2(self):
        repo = MockGitRepository(with_branch=False)
130
        guessed = export_orig.guess_comp_type(
131
            'bz2', self.source, repo, None)
132 133 134 135
        self.assertEqual("bzip2", guessed)

    def test_guess_comp_type_gz(self):
        repo = MockGitRepository(with_branch=False)
136
        guessed = export_orig.guess_comp_type(
137
            'gz', self.source, repo, None)
138
        self.assertEqual("gzip", guessed)