aboutsummaryrefslogtreecommitdiff
path: root/tests/05_test_detection.py
blob: 9b902e108c56e16e5c3dede7b63309941742d8ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# vim: set fileencoding=utf-8 :

"""Test tarball compression type detection"""

from . import context

import unittest

from gbp.scripts import buildpackage
from gbp.deb import (DebianPkgPolicy, orig_file)
from gbp.errors import GbpError

class MockGitRepository:
    def __init__(self, with_branch=False, subject=None):
        self.with_branch = with_branch
        self.subject = subject

    def has_pristine_tar_branch(self):
        return self.with_branch

    def pristine_tar_branch(self):
        'pristine-tar'

    def grep_log(self, regex, branch):
        return None

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

class TestDetection(unittest.TestCase):
    def setUp(self):
        self.tmpdir = context.new_tmpdir(__name__)
        self.cp = {'Source': 'source', 'Upstream-Version': '1.2'}

    def tearDown(self):
        context.teardown()

    def test_guess_comp_type_no_pristine_tar_no_orig(self):
        repo = MockGitRepository(with_branch=False)
        guessed = buildpackage.guess_comp_type(
            repo, 'auto', self.cp, str(self.tmpdir))
        self.assertEqual('gzip', guessed)

    def test_guess_comp_type_no_pristine_tar_with_orig(self):
        open(self.tmpdir.join('source_1.2.orig.tar.bz2'), "w").close()
        repo = MockGitRepository(with_branch=False)
        guessed = buildpackage.guess_comp_type(
            repo, 'auto', self.cp, str(self.tmpdir))
        self.assertEqual('bzip2', guessed)

    def test_guess_comp_type_no_pristine_tar_with_multiple_origs(self):
        open(self.tmpdir.join('source_1.2.orig.tar.gz'), "w").close()
        open(self.tmpdir.join('source_1.2.orig.tar.xz'), "w").close()
        repo = MockGitRepository(with_branch=False)
        self.assertRaises(
            GbpError,
            buildpackage.guess_comp_type,
            repo,
            'auto',
            self.cp,
            str(self.tmpdir))

    def test_guess_comp_type_auto_bzip2(self):
        subject = 'pristine-tar data for source_1.2-3.orig.tar.bz2'
        repo = MockGitRepository(with_branch=True, subject=subject)
        guessed = buildpackage.guess_comp_type(
            repo, 'auto', self.cp, str(self.tmpdir))
        self.assertEqual("bzip2", guessed)

    def test_has_orig_single_false(self):
        self.assertFalse(DebianPkgPolicy.has_origs([orig_file(self.cp, 'gzip')], str(self.tmpdir)))

    def test_has_orig_single_true(self):
        open(self.tmpdir.join('source_1.2.orig.tar.gz'), "w").close()
        self.assertTrue(DebianPkgPolicy.has_origs([orig_file(self.cp, 'gzip')], str(self.tmpdir)))

    def test_has_orig_multiple_false(self):
        orig_files = [orig_file(self.cp, 'gzip')] + \
                     [orig_file(self.cp, 'gzip', sub) for sub in ['foo', 'bar']]
        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()
        orig_files = [orig_file(self.cp, 'gzip')] + \
                     [orig_file(self.cp, 'gzip', sub) for sub in ['foo', 'bar']]
        self.assertTrue(DebianPkgPolicy.has_origs(orig_files, str(self.tmpdir)))

    def test_guess_comp_type_bzip2(self):
        repo = MockGitRepository(with_branch=False)
        guessed = buildpackage.guess_comp_type(
            repo, 'bzip2', self.cp, None)
        self.assertEqual("bzip2", guessed)

    def test_guess_comp_type_gzip(self):
        repo = MockGitRepository(with_branch=False)
        guessed = buildpackage.guess_comp_type(
            repo, 'gzip', self.cp, None)
        self.assertEqual("gzip", guessed)

    def test_guess_comp_type_bz(self):
        repo = MockGitRepository(with_branch=False)
        guessed = buildpackage.guess_comp_type(
            repo, 'xz', self.cp, None)
        self.assertEqual("xz", guessed)

    def test_guess_comp_type_lzma(self):
        repo = MockGitRepository(with_branch=False)
        guessed = buildpackage.guess_comp_type(
            repo, 'lzma', self.cp, None)
        self.assertEqual("lzma", guessed)

    def test_guess_comp_type_bz2(self):
        repo = MockGitRepository(with_branch=False)
        guessed = buildpackage.guess_comp_type(
            repo, 'bz2', self.cp, None)
        self.assertEqual("bzip2", guessed)

    def test_guess_comp_type_gz(self):
        repo = MockGitRepository(with_branch=False)
        guessed = buildpackage.guess_comp_type(
            repo, 'gz', self.cp, None)
        self.assertEqual("gzip", guessed)