porting code to python3.x with os patch
[tools/git-buildpackage.git] / tests / component / rpm / test_pq_rpm.py
1 # vim: set fileencoding=utf-8 :
2 #
3 # (C) 2013 Intel Corporation <markus.lehtonen@linux.intel.com>
4 #    This program is free software; you can redistribute it and/or modify
5 #    it under the terms of the GNU General Public License as published by
6 #    the Free Software Foundation; either version 2 of the License, or
7 #    (at your option) any later version.
8 #
9 #    This program is distributed in the hope that it will be useful,
10 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
11 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 #    GNU General Public License for more details.
13 #
14 #    You should have received a copy of the GNU General Public License
15 #    along with this program; if not, write to the Free Software
16 #    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17 """Tests for the gbp pq-rpm tool"""
18
19 import os
20 import tempfile
21 from nose.tools import assert_raises, eq_, ok_ # pylint: disable=E0611
22
23 from gbp.scripts.pq_rpm import main as pq
24 from gbp.git import GitRepository
25 from gbp.command_wrappers import GitCommand
26
27 from tests.component.rpm import RpmRepoTestBase
28
29 # Disable "Method could be a function warning"
30 # pylint: disable=R0201
31
32
33 def mock_pq(args):
34     """Wrapper for pq"""
35     # Call pq-rpm with added arg0
36     return pq(['arg0'] + args)
37
38 class TestPqRpm(RpmRepoTestBase):
39     """Basic tests for gbp-pq-rpm"""
40
41     def test_invalid_args(self):
42         """See that pq-rpm fails gracefully when called with invalid args"""
43         GitRepository.create('.')
44         # Test empty args
45         eq_(mock_pq([]), 1)
46         self._check_log(0, 'gbp:error: No action given.')
47         self._clear_log()
48
49         # Test invalid command
50         eq_(mock_pq(['mycommand']), 1)
51         self._check_log(0, "gbp:error: Unknown action 'mycommand'")
52         self._clear_log()
53
54         # Test invalid cmdline options
55         assert_raises(SystemExit, mock_pq, ['--invalid-arg=123'])
56
57         """
58         with assert_raises(SystemExit):
59             mock_pq(['--invalid-arg=123'])
60         """
61
62     def test_import_outside_repo(self):
63         """Run pq-rpm when not in a git repository"""
64         eq_(mock_pq(['export']), 1)
65         self._check_log(0, 'gbp:error: %s is not a git repository' %
66                               os.path.abspath(os.getcwd()))
67
68     def test_invalid_config_file(self):
69         """Test invalid config file"""
70         # Create dummy invalid config file and run pq-rpm
71         GitRepository.create('.')
72         with open('.gbp.conf', 'w') as conffd:
73             conffd.write('foobar\n')
74         eq_(mock_pq(['foo']), 1)
75         self._check_log(0, 'gbp:error: Invalid config file: File contains no '
76                            'section headers.')
77
78     def test_import_export(self):
79         """Basic test for patch import and export"""
80         repo = self.init_test_repo('gbp-test')
81         branches = repo.get_local_branches() + ['development/master']
82         # Test import
83         eq_(mock_pq(['import']), 0)
84         files = ['AUTHORS', 'dummy.sh', 'Makefile', 'NEWS', 'README',
85                  'mydir/myfile.txt', '.gbp.conf']
86         branches.append('development/master')
87         self._check_repo_state(repo, 'development/master', branches, files)
88         eq_(repo.get_merge_base('upstream', 'development/master'),
89             repo.rev_parse('upstream'))
90         ok_(len(repo.get_commits('', 'upstream')) <
91             len(repo.get_commits('', 'development/master')))
92
93         # Test export
94         eq_(mock_pq(['export']), 0)
95         files = ['.gbp.conf', '.gitignore', 'bar.tar.gz', 'foo.txt',
96                  'gbp-test.spec', '0001-my-gz.patch', '0002-my-bzip2.patch',
97                  '0003-my2.patch', 'my.patch']
98         self._check_repo_state(repo, 'master', branches, files)
99         eq_(repo.status()[' M'], [b'gbp-test.spec'])
100
101         # Another export after removing some patches
102         os.unlink('0001-my-gz.patch')
103         eq_(mock_pq(['export']), 0)
104         self._check_repo_state(repo, 'master', branches, files)
105
106     def test_import_export2(self):
107         """Another test for import and export"""
108         repo = self.init_test_repo('gbp-test2')
109         branches = repo.get_local_branches() + ['development/master-orphan']
110         repo.set_branch('master-orphan')
111         # Import
112         eq_(mock_pq(['import']), 0)
113         files = ['dummy.sh', 'Makefile', 'README', 'mydir/myfile.txt',
114                  '.gbp.conf']
115         self._check_repo_state(repo, 'development/master-orphan', branches,
116                                files)
117
118         # Test export
119         eq_(mock_pq(['export']), 0)
120         self._check_repo_state(repo, 'master-orphan', branches)
121         eq_(repo.status()[' M'], [b'packaging/gbp-test2.spec'])
122
123     def test_import_in_subdir(self):
124         """Test running gbp-rpm-pq from a subdir in the git tree"""
125         repo = self.init_test_repo('gbp-test2')
126         repo.set_branch('master-orphan')
127         branches = repo.get_local_branches() + ['development/master-orphan']
128         os.chdir('packaging')
129
130         # Running from subdir should be ok
131         eq_(mock_pq(['import']), 0)
132         self._check_repo_state(repo, 'development/master-orphan', branches)
133
134
135     def test_rebase(self):
136         """Basic test for rebase action"""
137         repo = self.init_test_repo('gbp-test')
138         repo.rename_branch('pq/master', 'development/master')
139         repo.set_branch('development/master')
140         branches = repo.get_local_branches()
141         # Make development branch out-of-sync
142         GitCommand("rebase")(['--onto', 'upstream^', 'upstream'])
143         # Sanity check for our git rebase...
144         ok_(repo.get_merge_base('development/master', 'upstream') !=
145             repo.rev_parse('upstream'))
146
147         # Do rebase
148         eq_(mock_pq(['rebase']), 0)
149         self._check_repo_state(repo, 'development/master', branches)
150         ok_(repo.get_merge_base('development/master', 'upstream') ==
151             repo.rev_parse('upstream'))
152
153         # Get to out-of-sync, again, and try rebase from master branch
154         GitCommand("rebase")(['--onto', 'upstream^', 'upstream'])
155         eq_(mock_pq(['switch']), 0)
156         eq_(mock_pq(['rebase']), 0)
157         self._check_repo_state(repo, 'development/master', branches)
158         ok_(repo.get_merge_base('development/master', 'upstream') ==
159             repo.rev_parse('upstream'))
160
161     def test_switch(self):
162         """Basic test for switch action"""
163         repo = self.init_test_repo('gbp-test')
164         pkg_files = repo.list_files()
165         branches = repo.get_local_branches() + ['development/master']
166         # Switch to non-existent pq-branch should fail
167         eq_(mock_pq(['switch']), 1)
168         self._check_log(-1, ".*Branch 'development/master' does not exist")
169
170         # Import and switch to base branch and back to pq
171         eq_(mock_pq(['import']), 0)
172         eq_(mock_pq(['switch']), 0)
173         self._check_repo_state(repo, 'master', branches)
174         eq_(mock_pq(['switch']), 0)
175         self._check_repo_state(repo, 'development/master', branches)
176
177         # Switch back to packaging branch
178         eq_(mock_pq(['switch']), 0)
179         self._check_repo_state(repo, 'master', branches, pkg_files)
180
181     def test_switch_drop(self):
182         """Basic test for drop action"""
183         repo = self.init_test_repo('gbp-test')
184         repo.rename_branch('pq/master', 'development/master')
185         repo.set_branch('development/master')
186         branches = repo.get_local_branches()
187
188         # Drop pq should fail when on pq branch
189         eq_(mock_pq(['drop']), 1)
190         self._check_log(-1, "gbp:error: On a patch-queue branch, can't drop it")
191         self._check_repo_state(repo, 'development/master', branches)
192
193         # Switch to master
194         eq_(mock_pq(['switch']), 0)
195         self._check_repo_state(repo, 'master', branches)
196
197         # Drop should succeed when on master branch
198         eq_(mock_pq(['drop']), 0)
199         branches.remove('development/master')
200         self._check_repo_state(repo, 'master', branches)
201
202     def test_force_import(self):
203         """Test force import"""
204         repo = self.init_test_repo('gbp-test')
205         pkg_files = [f.decode() for f in repo.list_files()]
206         repo.rename_branch('pq/master', 'development/master')
207         repo.set_branch('development/master')
208         branches = repo.get_local_branches()
209         pq_files = [f.decode() for f in repo.list_files()]
210
211         # Re-import should fail
212         eq_(mock_pq(['import']), 1)
213         self._check_log(0, "gbp:error: Already on a patch-queue branch")
214         self._check_repo_state(repo, 'development/master', branches, pq_files)
215
216         # Mangle pq branch and try force import on top of that
217         repo.force_head('master', hard=True)
218         self._check_repo_state(repo, 'development/master', branches, pkg_files)
219         eq_(mock_pq(['import', '--force']), 0)
220         self._check_repo_state(repo, 'development/master', branches, pq_files)
221
222         # Switch back to master
223         eq_(mock_pq(['switch']), 0)
224         self._check_repo_state(repo, 'master', branches, pkg_files)
225
226         # Import should fail
227         eq_(mock_pq(['import']), 1)
228         self._check_log(-1, "gbp:error: Patch-queue branch .* already exists")
229         self._check_repo_state(repo, 'master', branches, pkg_files)
230
231         # Force import should succeed
232         eq_(mock_pq(['import', '--force']), 0)
233         self._check_repo_state(repo, 'development/master', branches, pq_files)
234
235     def test_apply(self):
236         """Basic test for apply action"""
237         repo = self.init_test_repo('gbp-test')
238         upstr_files = ['dummy.sh', 'Makefile', 'README']
239         branches = repo.get_local_branches() + ['development/master']
240
241         # No patch given
242         eq_(mock_pq(['apply']), 1)
243         self._check_log(-1, "gbp:error: No patch name given.")
244
245         # Create a pristine pq-branch
246         repo.create_branch('development/master', 'upstream')
247
248         # Apply patch
249         with tempfile.NamedTemporaryFile() as tmp_patch:
250             tmp_patch.write(repo.show('master:%s' % 'my.patch'))
251             tmp_patch.file.flush()
252             eq_(mock_pq(['apply', tmp_patch.name]), 0)
253             self._check_repo_state(repo, 'development/master', branches,
254                                    upstr_files)
255
256         # Apply another patch, now when already on pq branch
257         with tempfile.NamedTemporaryFile() as tmp_patch:
258             tmp_patch.write(repo.show('master:%s' % 'my2.patch'))
259             tmp_patch.file.flush()
260             eq_(mock_pq(['apply', tmp_patch.name]), 0)
261         self._check_repo_state(repo, 'development/master', branches,
262                                upstr_files + ['mydir/myfile.txt'])
263
264     def test_convert(self):
265         """Basic test for convert action"""
266         repo = self.init_test_repo('gbp-test2')
267         branches = repo.get_local_branches() + ['master-orphan']
268         files = ['packaging/bar.tar.gz', 'packaging/foo.txt',
269                  'packaging/gbp-test2.spec', 'packaging/gbp-test2-alt.spec',
270                  'packaging/my.patch', 'packaging/0001-My-addition.patch',
271                  '.gbp.conf']
272         # First should fail because 'master-orphan' branch already exists
273         eq_(mock_pq(['convert']), 1)
274         self._check_log(-1, "gbp:error: Branch 'master-orphan' already exists")
275
276         # Re-try with force
277         eq_(mock_pq(['convert', '--force']), 0)
278         self._check_repo_state(repo, 'master-orphan', branches, files)
279
280     def test_convert_fail(self):
281         """Tests for convert action error cases"""
282         repo = self.init_test_repo('gbp-test')
283         branches = repo.get_local_branches()
284
285         # Already on orphan packaging branch
286         eq_(mock_pq(['convert']), 1)
287         self._check_repo_state(repo, 'master', branches)
288         self._check_log(-1, ".*is not based on upstream version")
289
290         # Create a pq branch and try from there
291         eq_(mock_pq(['import']), 0)
292         eq_(mock_pq(['convert']), 1)
293         self._check_repo_state(repo, 'development/master',
294                                branches + ['development/master'])
295         self._check_log(-1, ".*you're on patch-queue branch")
296
297         # Switch back to orphan packaging branch and try again
298         eq_(mock_pq(['switch']), 0)
299         eq_(mock_pq(['convert']), 1)
300         self._check_repo_state(repo, 'master',
301                                branches + ['development/master'])
302         self._check_log(-1, r".*pq branch \S+ already exists")
303
304     def test_option_patch_numbers(self):
305         """Test the --patch-numbers cmdline option"""
306         repo = self.init_test_repo('gbp-test')
307         repo.rename_branch('pq/master', 'development/master')
308         branches = repo.get_local_branches()
309         # Export
310         eq_(mock_pq(['export', '--no-patch-numbers']), 0)
311         files = ['.gbp.conf', '.gitignore', 'bar.tar.gz', 'foo.txt',
312                  'gbp-test.spec', 'my-gz.patch', 'my-bzip2.patch', 'my2.patch',
313                  'my.patch']
314         self._check_repo_state(repo, 'master', branches, files)
315
316     def test_option_tmp_dir(self):
317         """Test the --tmp-dir cmdline option"""
318         self.init_test_repo('gbp-test')
319         eq_(mock_pq(['import', '--tmp-dir=foo/bar']), 0)
320         # Check that the tmp dir basedir was created
321         ok_(os.path.isdir('foo/bar'))
322
323     def test_option_upstream_tag(self):
324         """Test the --upstream-tag cmdline option"""
325         repo = self.init_test_repo('gbp-test')
326
327         # Non-existent upstream-tag -> failure
328         eq_(mock_pq(['import', '--upstream-tag=foobar/%(upstreamversion)s']), 1)
329         self._check_log(-1, "gbp:error: Couldn't find upstream version")
330
331         # Create tag -> import should succeed
332         repo.create_tag('foobar/1.1', msg="test tag", commit='upstream')
333         eq_(mock_pq(['import', '--upstream-tag=foobar/%(upstreamversion)s']), 0)
334
335     def test_option_spec_file(self):
336         """Test --spec-file commandline option"""
337         self.init_test_repo('gbp-test')
338
339         # Non-existent spec file should lead to failure
340         eq_(mock_pq(['import', '--spec-file=foo.spec']), 1)
341         self._check_log(-1, "gbp:error: Can't parse spec: Unable to read spec")
342         # Correct spec file
343         eq_(mock_pq(['import', '--spec-file=gbp-test.spec']), 0)
344
345         # Force import on top to test parsing spec from another branch
346         eq_(mock_pq(['import', '--spec-file=gbp-test.spec', '--force']), 0)
347
348         # Test with export, too
349         eq_(mock_pq(['export', '--spec-file=foo.spec']), 1)
350         self._check_log(-1, "gbp:error: Can't parse spec: Unable to read spec")
351         eq_(mock_pq(['export', '--spec-file=gbp-test.spec']), 0)
352
353     def test_option_packaging_dir(self):
354         """Test --packaging-dir command line option"""
355         self.init_test_repo('gbp-test')
356
357         # Wrong packaging dir should lead to failure
358         eq_(mock_pq(['import', '--packaging-dir=foo']), 1)
359         self._check_log(-1, "gbp:error: Can't parse spec: No spec file found")
360         # Use correct packaging dir
361         eq_(mock_pq(['import', '--packaging-dir=.']), 0)
362
363         # Test with export, --spec-file option should override packaging dir
364         eq_(mock_pq(['export', '--packaging-dir=foo',
365                      '--spec-file=gbp-test.spec']), 0)
366
367     def test_option_pq_branch(self):
368         """Test the --pq-branch and --packaging-branch options"""
369         repo = self.init_test_repo('gbp-test')
370         branches = repo.get_local_branches()
371
372         # Invalid branch name
373         eq_(mock_pq(['import', '--pq-branch=foo:']), 1)
374         self._check_log(-1, "gbp:error: Cannot create patch-queue branch")
375
376         # Try all possible keys in pq-branch format string
377         eq_(mock_pq(['import',
378                      '--pq-branch=dev/%(branch)s/%(upstreamversion)s']), 0)
379         branches.append('dev/master/1.1')
380         self._check_repo_state(repo, 'dev/master/1.1', branches)
381
382         # Switch to non-existent packaging branch should fail
383         eq_(mock_pq(['switch', '--pq-branch=dev/master/1.1',
384                      '--packaging-branch=foobar']), 1)
385         self._check_log(-1, "gbp:error: Git command failed: Error running git")
386         self._check_repo_state(repo, 'dev/master/1.1', branches)
387
388         # Export to existing packaging branch should be ok
389         eq_(mock_pq(['switch', '--pq-branch=dev/master/1.1',
390                      '--packaging-branch=master']), 0)
391         self._check_repo_state(repo, 'master', branches)
392
393     def test_option_export_rev(self):
394         """Test the --export-rev cmdline option"""
395         repo = self.init_test_repo('gbp-test')
396         repo.rename_branch('pq/master', 'development/master')
397         branches = repo.get_local_branches()
398         files = repo.list_files()
399
400         # Export directly from upstream -> no patches expected
401         eq_(mock_pq(['export', '--export-rev=upstream']), 0)
402         files = ['.gbp.conf', '.gitignore', 'bar.tar.gz', 'foo.txt',
403                  'gbp-test.spec', 'my.patch']
404         self._check_repo_state(repo, 'master', branches, files)
405
406         # Export another rev
407         eq_(mock_pq(['export', '--export-rev=development/master~2']), 0)
408         self._check_repo_state(repo, 'master', branches,
409                                files + ['0001-my-gz.patch'])
410
411         # Export from upstream..master should fail
412         eq_(mock_pq(['export', '--export-rev=master']), 1)
413         self._check_log(-1, "gbp:error: Start commit .* not an ancestor of end")
414         # Export invalid rev should fail
415         eq_(mock_pq(['export', '--export-rev=foobar']), 1)
416         self._check_log(-1, "gbp:error: Invalid treeish object foobar")
417
418         # Export plain treeish. Doesn't work in pq (at least) -
419         # just for testing exception handling here
420         content = repo.list_tree('development/master')
421         tree = repo.make_tree(content)
422         eq_(mock_pq(['export', '--export-rev=%s' % tree]), 1)
423         self._check_log(-1, "gbp:error: Start commit .* not an ancestor of end")
424
425     def test_option_patch_compress(self):
426         """Test the --patch-export-compress cmdline option"""
427         repo = self.init_test_repo('gbp-test')
428         repo.rename_branch('pq/master', 'development/master')
429         branches = repo.get_local_branches()
430
431         # Export, all generated patches should be compressed
432         eq_(mock_pq(['export', '--patch-export-compress=1']), 0)
433         files = ['.gbp.conf', '.gitignore', 'bar.tar.gz', 'foo.txt',
434                  'gbp-test.spec', '0001-my-gz.patch.gz',
435                  '0002-my-bzip2.patch.gz', '0003-my2.patch.gz', 'my.patch']
436         self._check_repo_state(repo, 'master', branches, files)
437
438     def test_option_patch_export_squash(self):
439         """Test the --patch-export-squash-until cmdline option"""
440         repo = self.init_test_repo('gbp-test')
441         repo.rename_branch('pq/master', 'development/master')
442         repo.set_branch('development/master')
443         branches = repo.get_local_branches()
444
445         # Non-existent squash point should fail
446         eq_(mock_pq(['export', '--patch-export-squash-until=foo']), 1)
447         self._check_log(-1, r"gbp:error: Git command failed: revision 'foo\^0'")
448
449         # Invalid squash point should fail
450         eq_(mock_pq(['export', '--patch-export-squash-until=master']), 1)
451         self._check_log(-1, "gbp:error: Given squash point 'master' not in the "
452                             "history of end commit 'development/master'")
453
454         # Squashing up to the second latest patch -> 1 "normal" patch
455         squash = 'development/master~1'
456         eq_(mock_pq(['export', '--patch-export-squash-until=%s' % squash]), 0)
457         squash += ':squash'
458         eq_(mock_pq(['export', '--patch-export-squash-until=%s' % squash]), 0)
459         files = ['.gbp.conf', '.gitignore', 'bar.tar.gz', 'foo.txt',
460                  'gbp-test.spec', 'my.patch', 'squash.diff', '0002-my2.patch']
461         self._check_repo_state(repo, 'master', branches, files)
462
463     def test_option_patch_export_ignore(self):
464         """Test the --patch-export-ignore-path cmdline option"""
465         repo = self.init_test_repo('gbp-test')
466         repo.rename_branch('pq/master', 'development/master')
467         branches = repo.get_local_branches()
468
469         # Export
470         eq_(mock_pq(['export', '--patch-export-ignore-path=mydir/.*']), 0)
471         files = ['.gbp.conf', '.gitignore', 'bar.tar.gz', 'foo.txt',
472                  'gbp-test.spec', '0001-my-gz.patch', '0002-my-bzip2.patch',
473                  'my.patch']
474         self._check_repo_state(repo, 'master', branches, files)
475
476     def test_export_with_merges(self):
477         """Test exporting pq-branch with merge commits"""
478         repo = self.init_test_repo('gbp-test')
479         repo.rename_branch('pq/master', 'development/master')
480         repo.set_branch('development/master')
481         branches = repo.get_local_branches()
482
483         # Create a merge commit in pq-branch
484         patches = repo.format_patches('HEAD^', 'HEAD', '.')
485         repo.force_head('HEAD^', hard=True)
486         repo.commit_dir('.', 'Merge with master', 'development/master',
487                         ['master'])
488         merge_rev = repo.rev_parse('HEAD', short=7)
489         eq_(mock_pq(['apply', patches[0].decode()]), 0)
490         upstr_rev = repo.rev_parse('upstream', short=7)
491         os.unlink(patches[0])
492
493         # Export should create diff up to the merge point and one "normal" patch
494         eq_(mock_pq(['export']), 0)
495         files = ['.gbp.conf', '.gitignore', 'bar.tar.gz', 'foo.txt',
496                  'gbp-test.spec', 'my.patch',
497                   '%s-to-%s.diff' % (upstr_rev, merge_rev), '0002-my2.patch']
498         self._check_repo_state(repo, 'master', branches, files)
499
500     def test_option_import_files(self):
501         """Test the --import-files cmdline option"""
502         repo = self.init_test_repo('gbp-test')
503         # Add new conf file
504         os.mkdir('debian')
505         with open('debian/gbp.conf', 'w') as conf_file:
506             conf_file.write('[DEFAULT]\npq-branch = my-pq-branch\n')
507         repo.add_files(['debian/gbp.conf'], force=True)
508         repo.commit_files(['debian/gbp.conf'], msg="Add conf file")
509
510         # Import with default settings (should import gbp conf files)
511         branches = repo.get_local_branches() + ['my-pq-branch']
512         eq_(mock_pq(['import']), 0)
513         self._check_repo_state(repo, 'my-pq-branch', branches)
514         ok_('debian/gbp.conf' in repo.list_files())
515         ok_('.gbp.conf' in repo.list_files())
516
517         # Re-import with user-defined files
518         eq_(mock_pq(['import', '--force', '--packaging-branch', 'master',
519                      '--import-files', 'foo.txt,my.patch']), 0)
520         self._check_repo_state(repo, 'my-pq-branch', branches)
521         ok_('foo.txt' in repo.list_files())
522         ok_('my.patch' in repo.list_files())
523
524         # Drop and re-import with no files
525         eq_(mock_pq(['switch', '--packaging-branch', 'master', '--pq-branch',
526                      'my-pq-branch']), 0)
527         eq_(mock_pq(['drop']), 0)
528         eq_(mock_pq(['import', '--packaging-branch', 'master',
529                      '--pq-branch', 'my-pq-branch', '--import-files=']), 0)
530         self._check_repo_state(repo, 'my-pq-branch', branches)
531         ok_('debian/gbp.conf' not in repo.list_files())
532         ok_('.gbp.conf' not in repo.list_files())
533
534     def test_option_new_packaging_dir(self):
535         """Test the --new-packaging-dir cmdline option"""
536         repo = self.init_test_repo('gbp-test2')
537         branches = repo.get_local_branches() + ['master-orphan']
538         files = ['rpm/bar.tar.gz', 'rpm/foo.txt', 'rpm/gbp-test2.spec',
539                  'rpm/gbp-test2-alt.spec', 'rpm/my.patch',
540                  'rpm/0001-My-addition.patch']
541         # Drop already-existing master-orphan branch
542         repo.delete_branch('master-orphan')
543         # Try convert
544         eq_(mock_pq(['convert', '--import-files=',
545                      '--new-packaging-dir=rpm']), 0)
546         self._check_repo_state(repo, 'master-orphan', branches, files)
547
548     def test_option_retain_history(self):
549         """Test the --retain-history cmdline option"""
550         repo = self.init_test_repo('gbp-test2')
551         branches = repo.get_local_branches() + ['master-orphan']
552         files = ['packaging/bar.tar.gz', 'packaging/foo.txt',
553                  'packaging/gbp-test2.spec', 'packaging/gbp-test2-alt.spec',
554                  'packaging/my.patch', 'packaging/0001-My-addition.patch',
555                  '.gbp.conf']
556         # Drop pre-existing master-orphan branch
557         repo.delete_branch('master-orphan')
558
559         # Convert with history
560         eq_(mock_pq(['convert', '--retain-history']), 0)
561         self._check_repo_state(repo, 'master-orphan', branches, files)
562         eq_(len(repo.get_commits('', 'master-orphan')), 7)
563
564     def test_import_unapplicable_patch(self):
565         """Test import when a patch does not apply"""
566         repo = self.init_test_repo('gbp-test')
567         branches = repo.get_local_branches()
568         # Mangle patch
569         with open('my2.patch', 'w') as patch_file:
570             patch_file.write('-this-does\n+not-apply\n')
571         eq_(mock_pq(['import']), 1)
572         self._check_log(-1, "("
573                              "Aborting|"
574                              "Please, commit your changes or stash them|"
575                              "gbp:error: Import failed.* You have local changes"
576                             ")")
577         self._check_repo_state(repo, 'master', branches)
578
579         # Now commit the changes to the patch and try again
580         repo.add_files(['my2.patch'], force=True)
581         repo.commit_files(['my2.patch'], msg="Mangle patch")
582         eq_(mock_pq(['import']), 1)
583         self._check_log(-1, "gbp:error: Import failed: Error running git apply")
584         self._check_repo_state(repo, 'master', branches)
585