aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/component/rpm/test_rpm_ch.py
blob: 3fa5436345437edd57f1699436dd0893de72f85c (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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# vim: set fileencoding=utf-8 :
#
# (C) 2013-2015 Intel Corporation <markus.lehtonen@linux.intel.com>
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, please see
#    <http://www.gnu.org/licenses/>
"""Tests for the gbp-rpm-ch tool"""

import os
import re
from nose.tools import assert_raises, eq_, ok_  # pylint: disable=E0611

from gbp.scripts.rpm_ch import main as rpm_ch
from gbp.git import GitRepository
from tests.testutils import capture_stderr

from tests.component.rpm import RpmRepoTestBase

# Disable "Method could be a function warning"
# pylint: disable=R0201


def mock_ch(args):
    """Wrapper for gbp-rpm-ch"""
    with capture_stderr():
        return rpm_ch(['arg0', '--packaging-branch=master',
                       '--spawn-editor=never'] + args)


class TestRpmCh(RpmRepoTestBase):
    """Basic tests for gbp-rpm-ch"""

    def setUp(self):
        """Test case setup"""
        super(TestRpmCh, self).setUp()
        # Set environment so that commits succeed without git config
        os.environ['GIT_AUTHOR_NAME'] = 'My Name'
        os.environ['GIT_COMMITTER_NAME'] = 'My Name'
        os.environ['EMAIL'] = 'me@example.com'

    @staticmethod
    def read_file(filename):
        """Read file to a list"""
        with open(filename) as fobj:
            return fobj.readlines()

    def test_invalid_args(self):
        """See that gbp-rpm-ch fails gracefully when called with invalid args"""
        GitRepository.create('.')

        with assert_raises(SystemExit):
            mock_ch(['--invalid-opt'])

    def test_import_outside_repo(self):
        """Run gbp-rpm-ch when not in a git repository"""
        eq_(mock_ch([]), 1)
        self._check_log(0, 'gbp:error: No Git repository at ')

    def test_update_spec_changelog(self):
        """Test updating changelog in spec"""
        repo = self.init_test_repo('gbp-test')
        eq_(mock_ch([]), 0)
        eq_(repo.status(), {' M': ['gbp-test.spec']})

    def test_update_changes_file(self):
        """Test updating a separate changes file"""
        repo = self.init_test_repo('gbp-test-native')
        eq_(mock_ch([]), 0)
        eq_(repo.status(), {' M': ['packaging/gbp-test-native.changes']})

    def test_create_spec_changelog(self):
        """Test creating changelog in spec file"""
        repo = self.init_test_repo('gbp-test2')
        orig_content = self.read_file('packaging/gbp-test2.spec')

        # Fails if no starting point is given
        eq_(mock_ch([]), 1)
        self._check_log(-1, "gbp:error: Couldn't determine starting point")

        # Give starting point
        eq_(mock_ch(['--since=HEAD^']), 0)
        eq_(repo.status(), {' M': ['packaging/gbp-test2.spec']})
        content = self.read_file('packaging/gbp-test2.spec')
        # Should contain 4 lines (%changelog, header, 1 entry and an empty line)
        eq_(len(content), len(orig_content) + 4)

    def test_create_changes_file(self):
        """Test creating a separate changes file"""
        repo = self.init_test_repo('gbp-test2')

        # Fails if no starting point is given
        eq_(mock_ch(['--changelog-file=CHANGES']), 1)
        self._check_log(-1, "gbp:error: Couldn't determine starting point")

        # Give starting point
        eq_(mock_ch(['--since=HEAD^', '--changelog-file=CHANGES']), 0)
        eq_(repo.status(), {'??': ['packaging/gbp-test2.changes']})
        content = self.read_file('packaging/gbp-test2.changes')
        # Should contain 3 lines (header, 1 entry and an empty line)
        eq_(len(content), 3)

    def test_option_changelog_file(self):
        """Test the --changelog-file cmdline option"""
        repo = self.init_test_repo('gbp-test-native')

        # Guess changelog file
        eq_(mock_ch(['--changelog-file=CHANGES']), 0)
        eq_(repo.status(), {' M': ['packaging/gbp-test-native.changes']})

        # Use spec file as changelog
        eq_(mock_ch(['--changelog-file=SPEC', '--since=HEAD^']), 0)
        eq_(repo.status(), {' M': ['packaging/gbp-test-native.changes',
                                   'packaging/gbp-test-native.spec']})

        # Arbitrary name
        eq_(mock_ch(['--changelog-file=foo.changes', '--since=HEAD^']), 0)
        eq_(repo.status(), {' M': ['packaging/gbp-test-native.changes',
                                   'packaging/gbp-test-native.spec'],
                            '??': ['foo.changes']})

    def test_option_spec_file(self):
        """Test the --spec-file cmdline option"""
        repo = self.init_test_repo('gbp-test2')

        eq_(mock_ch(['--spec-file=foo.spec']), 1)
        self._check_log(-1, "gbp:error: Unable to read spec file")

        eq_(mock_ch(['--spec-file=']), 1)
        self._check_log(-1, "gbp:error: Multiple spec files found")

        eq_(mock_ch(['--spec-file=packaging/gbp-test2.spec', '--since=HEAD^']),
            0)
        eq_(repo.status(), {' M': ['packaging/gbp-test2.spec']})

    def test_option_packaging_dir(self):
        """Test the --packaging-dir cmdline option"""
        repo = self.init_test_repo('gbp-test-native')

        eq_(mock_ch(['--packaging-dir=foo']), 1)
        self._check_log(-1, "gbp:error: No spec file found")

        # Packaging dir should be taken from spec file if it is defined
        eq_(mock_ch(['--packaging-dir', 'foo', '--spec-file',
                     'packaging/gbp-test-native.spec']), 0)
        eq_(repo.status(), {' M': ['packaging/gbp-test-native.changes']})

    def test_branch_options(self):
        """Test the --packaging-branch and --ignore-branch cmdline options"""
        self.init_test_repo('gbp-test-native')

        eq_(mock_ch(['--packaging-branch=foo']), 1)
        self._check_log(-2, "gbp:error: You are not on branch 'foo'")

        eq_(mock_ch(['--packaging-branch=foo', '--ignore-branch']), 0)

    def test_option_no_release(self):
        """Test the --no-release cmdline option"""
        self.init_test_repo('gbp-test-native')
        orig_content = self.read_file('packaging/gbp-test-native.changes')

        eq_(mock_ch(['--no-release']), 0)
        content = self.read_file('packaging/gbp-test-native.changes')
        # Only one line (entry) added
        eq_(len(content), len(orig_content) + 1)

    def test_author(self):
        """Test determining the author name/email"""
        repo = self.init_test_repo('gbp-test-native')

        # Test taking email address from env
        os.environ['EMAIL'] = 'user@host.com'
        eq_(mock_ch([]), 0)
        header = self.read_file('packaging/gbp-test-native.changes')[0]
        ok_(re.match(r'.+ <user@host\.com> .+', header))

        # Missing git config setting should not cause a failure
        del os.environ['EMAIL']
        del os.environ['GIT_AUTHOR_NAME']
        os.environ['GIT_CONFIG_NOSYSTEM'] = '1'
        os.environ['HOME'] = os.path.abspath('.')
        eq_(mock_ch(['--git-author', '--since=HEAD^1']), 0)

        # Test the --git-author option
        saved_author = os.environ.get('GIT_AUTHOR_NAME')
        saved_email = os.environ.get('GIT_AUTHOR_EMAIL')
        os.environ['GIT_AUTHOR_NAME'] = 'John Doe'
        os.environ['GIT_AUTHOR_EMAIL'] = 'jd@host.com'
        with open(os.path.join(repo.git_dir, 'config'), 'a') as fobj:
            fobj.write('[user]\n  name=John Doe\n  email=jd@host.com\n')
        eq_(mock_ch(['--git-author', '--since=HEAD^']), 0)
        header = self.read_file('packaging/gbp-test-native.changes')[0]
        ok_(re.match(r'.+ John Doe <jd@host\.com> .+', header), header)
        if saved_author:
            os.environ['GIT_AUTHOR_NAME'] = saved_author
        if saved_email:
            os.environ['GIT_AUTHOR_EMAIL'] = saved_email

    def test_option_full(self):
        """Test the --full cmdline option"""
        repo = self.init_test_repo('gbp-test-native')
        orig_content = self.read_file('packaging/gbp-test-native.changes')

        eq_(mock_ch(['--full', '--since=HEAD^']), 0)
        commit_msg_body = repo.get_commit_info('HEAD')['body']
        full_msg = [line for line in commit_msg_body.splitlines() if line]
        content = self.read_file('packaging/gbp-test-native.changes')
        # New lines: header, 1 entry "header", entry "body" from commit message
        # and one empty line
        eq_(len(content), len(orig_content) + 3 + len(full_msg))

    def test_option_ignore_regex(self):
        """Test the --ignore-regex cmdline option"""
        repo = self.init_test_repo('gbp-test-native')
        orig_content = self.read_file('packaging/gbp-test-native.changes')

        eq_(mock_ch(['--full', '--since', 'HEAD^', '--ignore-regex',
                     'Signed-off-by:.*']), 0)
        commit_msg_body = repo.get_commit_info('HEAD')['body']
        full_msg = [line for line in commit_msg_body.splitlines() if
                    (line and not line.startswith('Signed-off-by:'))]
        content = self.read_file('packaging/gbp-test-native.changes')
        # New lines: header, 1 entry "header", filtered entry "body" from
        # commit message and one empty line
        eq_(len(content), len(orig_content) + 3 + len(full_msg))

    def test_option_id_len(self):
        """Test the --id-len cmdline option"""
        repo = self.init_test_repo('gbp-test-native')

        eq_(mock_ch(['--id-len=10']), 0)
        commit_id = repo.rev_parse('HEAD', 10)
        content = self.read_file('packaging/gbp-test-native.changes')
        ok_(content[1].startswith('- [%s] ' % commit_id))

    def test_option_changelog_revision(self):
        """Test the --id-len cmdline option"""
        self.init_test_repo('gbp-test-native')

        # Test invalid format (unknown field)
        eq_(mock_ch(['--changelog-revision=%(unknown_field)s']), 1)
        self._check_log(-1, 'gbp:error: Unable to construct revision field')

        # Test acceptable format
        eq_(mock_ch(['--changelog-revision=foobar']), 0)
        header = self.read_file('packaging/gbp-test-native.changes')[0]
        ok_(re.match(r'.+ foobar$', header))

    def test_option_editor_cmd(self):
        """Test the --editor-cmd and --spawn-editor cmdline options"""
        repo = self.init_test_repo('gbp-test-native')
        eq_(mock_ch(['--spawn-editor=release', '--editor-cmd=rm']), 0)
        eq_(repo.status(), {' D': ['packaging/gbp-test-native.changes']})

        repo.force_head('HEAD', hard=True)
        ok_(repo.is_clean())

        os.environ['EDITOR'] = 'rm'
        eq_(mock_ch(['--spawn-editor=always', '--editor-cmd=']),
            0)

    def test_user_customizations(self):
        """Test the user customizations"""
        repo = self.init_test_repo('gbp-test-native')

        # Non-existent customization file
        eq_(mock_ch(['--customizations=customizations.py']), 1)

        # Create user customizations file
        with open('customizations.py', 'w') as fobj:
            fobj.write("class ChangelogEntryFormatter(object):\n")
            fobj.write("    @classmethod\n")
            fobj.write("    def compose(cls, commit_info, **kwargs):\n")
            fobj.write("        return ['- %s' % commit_info['id']]\n")

        eq_(mock_ch(['--customizations=customizations.py']), 0)
        entry = self.read_file('packaging/gbp-test-native.changes')[1]
        sha = repo.rev_parse('HEAD')
        eq_(entry, '- %s\n' % sha)

    def test_paths(self):
        """Test tracking of certain paths only"""
        repo = self.init_test_repo('gbp-test-native')
        orig_content = self.read_file('packaging/gbp-test-native.changes')

        # Add new commit with known content
        with open('new-file.txt', 'w') as fobj:
            fobj.write('this is new content\n')
        repo.add_files('new-file.txt')
        repo.commit_staged('Add new file')

        # Only track a non-existent file
        eq_(mock_ch(['--since=HEAD^', 'non-existent-path']), 0)
        content = self.read_file('packaging/gbp-test-native.changes')
        # New lines: header and one empty line, no entries
        eq_(len(content), len(orig_content) + 2)

        # Track existing file
        repo.force_head('HEAD', hard=True)
        eq_(mock_ch(['--since=HEAD^', 'new-file.txt']), 0)
        content = self.read_file('packaging/gbp-test-native.changes')
        # New lines: header, one entry line and one empty line
        eq_(len(content), len(orig_content) + 3)

    def test_commit_guessing(self):
        """Basic tests for guessing the starting point"""
        repo = self.init_test_repo('gbp-test-native')

        # Check 'tagname' that is not found
        eq_(mock_ch(['--changelog-revision=%(tagname)s']), 0)
        self._check_log(0, 'gbp:warning: Changelog points to tagname')

        # Check 'upstreamversion' and 'release' fields
        repo.force_head('HEAD', hard=True)
        eq_(mock_ch(['--changelog-revision=%(upstreamversion)s-%(release)s']),
            0)

    def test_commit_guessing_fail(self):
        """Test for failure of start commit guessing"""
        self.init_test_repo('gbp-test-native')

        # Add "very old" header to changelog
        with open('packaging/gbp-test-native.changes', 'w') as ch_fp:
            ch_fp.write('* Sat Jan 01 2000 User <user@host.com> 123\n- foo\n')
        # rpm-ch should fail by not being able to find any commits before the
        # last changelog section
        eq_(mock_ch([]), 1)
        self._check_log(-1, "gbp:error: Couldn't determine starting point")