summaryrefslogtreecommitdiffstats
path: root/tests/test_debhelper.py
blob: 285440c4fc91da87fb815b961ac7521c08f40554 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
from tempfile import TemporaryDirectory
import unittest
import os

from dhpython.debhelper import DebHelper, build_options


class DebHelperTestCase(unittest.TestCase):
    impl = 'cpython3'
    control = []
    options = {}
    parse_control = True

    def build_options(self):
        return build_options(**self.options)

    def setUp(self):
        self.tempdir = TemporaryDirectory()
        self.addCleanup(self.tempdir.cleanup)

        old_wd = os.getcwd()
        os.chdir(self.tempdir.name)
        self.addCleanup(os.chdir, old_wd)

        os.mkdir('debian')
        with open('debian/control', 'w') as f:
            f.write('\n'.join(self.control))
        if self.parse_control:
            self.dh = DebHelper(self.build_options(), impl=self.impl)


CONTROL = [
    'Source: foo-src',
    'Build-Depends: python3-all,',
    ' python-all,',
    ' bar (<< 2) [amd64],',
    ' baz (>= 1.0)',
    'X-Python3-Version: >= 3.1, << 3.10',
    '',
    'Architecture: all',
    'Package: python3-foo',
    'Depends: ${python3:Depends}',
    '',
    'Package: python3-foo-ext',
    'Architecture: any',
    'Depends: ${python3:Depends}, '
    '# COMMENT',
    ' ${shlibs:Depends},',
    '',
    'Package: python-foo',
    'Architecture: all',
    'Depends: ${python:Depends}',
    '',
    '',
    'Package: foo',
    'Architecture: all',
    'Depends: ${python3:Depends}',
    '',
    '',
    'Package: recfoo',
    'Architecture: all',
    'Recommends: ${python3:Depends}',
    '',
    '',
]

class TestControlBlockParsing(DebHelperTestCase):
    control = CONTROL

    def test_parses_source(self):
        self.assertEqual(self.dh.source_name, 'foo-src')

    def test_parses_build_depends(self):
        self.assertEqual(self.dh.build_depends, {
            'python3-all': {None: None},
            'python-all': {None: None},
            'bar': {'amd64': '<< 2'},
            'baz': {None: '>= 1.0'},
        })

    def test_parses_XPV(self):
        self.assertEqual(self.dh.python_version, '>= 3.1, << 3.10')

    def test_parses_packages(self):
        self.assertEqual(list(self.dh.packages.keys()),
                         ['python3-foo', 'python3-foo-ext', 'foo', 'recfoo'])

    def test_parses_arch(self):
        self.assertEqual(self.dh.packages['python3-foo-ext']['arch'], 'any')

    def test_parses_arch_all(self):
        self.assertEqual(self.dh.packages['python3-foo']['arch'], 'all')


class TestControlSkipIndep(DebHelperTestCase):
    control = CONTROL
    options = {
        'arch': True,
    }

    def test_skip_indep(self):
        self.assertEqual(list(self.dh.packages.keys()), ['python3-foo-ext'])


class TestControlSkipArch(DebHelperTestCase):
    control = CONTROL
    options = {
        'arch': False,
    }

    def test_skip_arch(self):
        self.assertEqual(list(self.dh.packages.keys()),
                         ['python3-foo', 'foo', 'recfoo'])


class TestControlSinglePkg(DebHelperTestCase):
    control = CONTROL
    options = {
        'package': ['python3-foo'],
    }

    def test_parses_packages(self):
        self.assertEqual(list(self.dh.packages.keys()), ['python3-foo'])


class TestControlSkipSinglePkg(DebHelperTestCase):
    control = CONTROL
    options = {
        'no_package': ['python3-foo'],
    }

    def test_parses_packages(self):
        self.assertEqual(list(self.dh.packages.keys()),
                         ['python3-foo-ext', 'foo', 'recfoo'])


class TestControlBlockParsingPy2(DebHelperTestCase):
    control = CONTROL
    impl = 'cpython2'

    def test_parses_packages(self):
        self.assertEqual(list(self.dh.packages.keys()), ['python-foo'])


class TestControlNoBinaryPackages(DebHelperTestCase):
    control = [
        'Source: foo-src',
        'Build-Depends: python3-all',
        '',
    ]
    parse_control = False

    def test_throws_error(self):
        msg = ('Unable to parse debian/control, found less than 2 paragraphs')
        with self.assertRaisesRegex(Exception, msg):
            DebHelper(self.build_options())


class TestControlMissingPackage(DebHelperTestCase):
    control = [
        'Source: foo-src',
        'Build-Depends: python3-all',
        '',
        'Architecture: all',
    ]
    parse_control = False

    def test_parses_packages(self):
        msg = ('Unable to parse debian/control, paragraph 2 missing Package '
               'field')
        with self.assertRaisesRegex(Exception, msg):
            DebHelper(self.build_options())


class TestRemainingPackages(DebHelperTestCase):
    control = CONTROL
    options = {
        'remaining_packages': True,
    }
    parse_control = False

    def setUp(self):
        super().setUp()
        with open('debian/python3-foo.debhelper.log', 'w') as f:
            f.write('dh_python3\n')
        with open('debian/python3-foo-ext.debhelper.log', 'w') as f:
            f.write('dh_foobar\n')
        self.dh = DebHelper(self.build_options(), impl=self.impl)

    def test_skips_logged_packages(self):
        self.assertEqual(list(self.dh.packages.keys()),
                         ['python3-foo-ext', 'foo', 'recfoo'])