forked from rpm-software-management/tito
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common_tests.py
689 lines (557 loc) · 25.8 KB
/
common_tests.py
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
#
# Copyright (c) 2008-2009 Red Hat, Inc.
#
# This software is licensed to you under the GNU General Public License,
# version 2 (GPLv2). There is NO WARRANTY for this software, express or
# implied, including the implied warranties of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2
# along with this software; if not, see
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
#
# Red Hat trademarks are not licensed under GPLv2. No permission is
# granted to use or replicate Red Hat trademarks that are incorporated
# in this software or its documentation.
""" Pure unit tests for tito's common module. """
from tito.common import (replace_version, find_spec_like_file, increase_version,
search_for, compare_version, run_command_print, find_wrote_in_rpmbuild_output,
render_cheetah, increase_zstream, reset_release, find_file_with_extension,
normalize_class_name, extract_sha1, BugzillaExtractor, DEFAULT_BUILD_DIR, munge_specfile,
munge_setup_macro,
_out)
from tito.compat import StringIO
from tito.tagger import CargoBump
import os
import re
import unittest
from mock import Mock, patch, call
from tempfile import NamedTemporaryFile
from textwrap import dedent
from unit import open_mock, Capture
from blessings import Terminal
class CommonTests(unittest.TestCase):
def setUp(self):
# Start in a known location to prevent problems with tests that
# end in a temp directory that is subsequently deleted.
os.chdir(DEFAULT_BUILD_DIR)
def test_normalize_class_name(self):
""" Test old spacewalk.releng namespace is converted to tito. """
self.assertEquals("tito.builder.Builder",
normalize_class_name("tito.builder.Builder"))
self.assertEquals("tito.builder.Builder",
normalize_class_name("spacewalk.releng.builder.Builder"))
self.assertEquals("tito.tagger.VersionTagger",
normalize_class_name("spacewalk.releng.tagger.VersionTagger"))
def test_replace_version_leading_whitespace(self):
line = " version='1.0'\n"
expected = " version='2.5.3'\n"
self.assertEquals(expected, replace_version(line, "2.5.3"))
def test_replace_version_no_whitespace(self):
line = "version='1.0'\n"
expected = "version='2.5.3'\n"
self.assertEquals(expected, replace_version(line, "2.5.3"))
def test_replace_version_some_whitespace(self):
line = "version = '1.0'\n"
expected = "version = '2.5.3'\n"
self.assertEquals(expected, replace_version(line, "2.5.3"))
def test_replace_version_double_quote(self):
line = 'version="1.0"\n'
expected = 'version="2.5.3"\n'
self.assertEquals(expected, replace_version(line, "2.5.3"))
def test_replace_version_trailing_chars(self):
line = "version = '1.0', blah blah blah\n"
expected = "version = '2.5.3', blah blah blah\n"
self.assertEquals(expected, replace_version(line, "2.5.3"))
def test_replace_version_crazy_old_version(self):
line = "version='1.0asjhd82371kjsdha98475h87asd7---asdai.**&'\n"
expected = "version='2.5.3'\n"
self.assertEquals(expected, replace_version(line, "2.5.3"))
def test_replace_version_crazy_new_version(self):
line = "version='1.0'\n"
expected = "version='91asj.;]][[a]sd[]'\n"
self.assertEquals(expected, replace_version(line,
"91asj.;]][[a]sd[]"))
def test_replace_version_uppercase(self):
line = "VERSION='1.0'\n"
expected = "VERSION='2.5.3'\n"
self.assertEquals(expected, replace_version(line, "2.5.3"))
def test_replace_version_no_match(self):
line = "this isn't a version fool.\n"
self.assertEquals(line, replace_version(line, "2.5.3"))
def test_extract_sha1(self):
ls_remote_output = "Could not chdir to home directory\n" + \
"fe87e2b75ed1850718d99c797cc171b88bfad5ca ref/origin/sometag"
self.assertEquals("fe87e2b75ed1850718d99c797cc171b88bfad5ca",
extract_sha1(ls_remote_output))
def test_compare_version(self):
self.assertEquals(0, compare_version("1", "1"))
self.assertTrue(compare_version("2.1", "2.2") < 0)
self.assertTrue(compare_version("3.0.4.10", "3.0.4.2") > 0)
self.assertTrue(compare_version("4.08", "4.08.01") < 0)
self.assertTrue(compare_version("3.2.1.9.8144", "3.2") > 0)
self.assertTrue(compare_version("3.2", "3.2.1.9.8144") < 0)
self.assertTrue(compare_version("1.2", "2.1") < 0)
self.assertTrue(compare_version("2.1", "1.2") > 0)
self.assertTrue(compare_version("1.0", "1.0.1") < 0)
self.assertTrue(compare_version("1.0.1", "1.0") > 0)
self.assertEquals(0, compare_version("5.6.7", "5.6.7"))
self.assertEquals(0, compare_version("1.01.1", "1.1.1"))
self.assertEquals(0, compare_version("1.1.1", "1.01.1"))
self.assertEquals(0, compare_version("1", "1.0"))
self.assertEquals(0, compare_version("1.0", "1"))
self.assertEquals(0, compare_version("1.0.2.0", "1.0.2"))
def test_run_command_print(self):
self.assertEquals('', run_command_print("sleep 0.1"))
def test_rpmbuild_claims_to_be_successful(self):
succeeded_result = "success"
output = "Wrote: %s" % succeeded_result
success_line = find_wrote_in_rpmbuild_output(output)
self.assertEquals(succeeded_result, success_line[0])
@patch("tito.common.error_out")
def test_rpmbuild_which_ended_with_error_is_described_with_the_analyzed_line(self, mock_error):
output = "some error output from rpmbuild\n" \
"next error line"
find_wrote_in_rpmbuild_output(output)
mock_error.assert_called_once_with("Unable to locate 'Wrote: ' lines in rpmbuild output: '%s'" % output)
@patch("tito.common.find_file_with_extension")
def test_find_spec_like_file_tmpl(self, mock_find):
mock_find.side_effect = [None, "result.spec.tmpl"]
result = find_spec_like_file()
self.assertEquals("result.spec.tmpl", result)
self.assertEquals(2, len(mock_find.mock_calls))
@patch("tito.common.find_file_with_extension")
def test_find_spec_like_file_spec(self, mock_find):
mock_find.side_effect = ["result.spec"]
result = find_spec_like_file()
self.assertEquals("result.spec", result)
self.assertEquals(1, len(mock_find.mock_calls))
@patch("tito.common.find_file_with_extension")
def test_find_spec_like_file_no_match(self, mock_find):
mock_find.side_effect = [None, None]
with Capture(silent=True):
self.assertRaises(SystemExit, find_spec_like_file)
self.assertEquals(2, len(mock_find.mock_calls))
@patch("os.listdir")
def test_find_file_with_extension(self, mock_listdir):
mock_listdir.return_value = ["hello.txt"]
result = find_file_with_extension("/tmp", ".txt")
self.assertEquals(mock_listdir.mock_calls[0], call("/tmp"))
self.assertEquals("/tmp/hello.txt", result)
@patch("os.listdir")
def test_find_file_with_extension_no_match(self, mock_listdir):
mock_listdir.return_value = ["hello.txt"]
result = find_file_with_extension("/tmp", ".foo")
self.assertEquals(mock_listdir.mock_calls[0], call("/tmp"))
self.assertEqual(None, result)
@patch("os.listdir")
def test_find_file_with_extension_duplicates(self, mock_listdir):
mock_listdir.return_value = ["hello.txt", "goodbye.txt"]
with Capture(silent=True):
self.assertRaises(SystemExit, find_file_with_extension, "/tmp", ".txt")
def test_search_for(self):
content = dedent("""
HelloWorld
Hello World
""")
with open_mock(content):
results = search_for("foo", r"(Hello\s+World)", r"(HelloWorld)")
self.assertEquals(("Hello World",), results[0])
self.assertEquals(("HelloWorld",), results[1])
def test_search_for_gets_first_match(self):
content = dedent("""
HelloWorld
Hello World
""")
with open_mock(content):
results = search_for("foo", r"(Hello.*)")
self.assertEquals(("HelloWorld",), results[0])
def test_search_for_no_match(self):
content = dedent("""
HelloWorld
Goodbye World
""")
with open_mock(content):
with Capture(silent=True):
self.assertRaises(SystemExit, search_for, "foo", r"(NoMatch)")
@patch("tito.common.read_user_config")
def test_turn_off_colors(self, mock_user_conf):
mock_user_conf.return_value = {'COLOR': '0'}
stream = StringIO()
_out('Hello world', None, Terminal().red, stream)
self.assertEquals('Hello world\n', stream.getvalue())
@patch("tito.common.read_user_config")
def test_colors(self, mock_user_conf):
mock_user_conf.return_value = {}
stream = StringIO()
_out('Hello world', None, Terminal().red, stream)
# RHEL 6 doesn't have self.assertRegexpMatches unfortunately
self.assertTrue(re.match('.+Hello world.+\n', stream.getvalue()))
class CheetahRenderTest(unittest.TestCase):
@patch("os.unlink")
@patch("glob.glob")
@patch("shutil.move")
@patch("tito.common.run_command")
@patch("tempfile.NamedTemporaryFile")
def test_renders_cheetah(self, mock_tempfile, mock_run_command, mock_move, mock_glob, mock_unlink):
mock_run_command.return_value = True
mock_tempfile.return_value.name = "temp_pickle"
mock_unlink.return_value = True
mock_glob.return_value = ["/tmp/foo.spec.cheetah"]
mock_move.return_value = True
render_cheetah("foo.spec.tmpl", "/tmp", {})
expected = "cheetah fill --flat --pickle=temp_pickle --odir=/tmp --oext=cheetah foo.spec.tmpl"
self.assertEquals(call(expected), mock_run_command.mock_calls[0])
self.assertEquals(call("/tmp/*.cheetah"), mock_glob.mock_calls[0])
self.assertEquals(call("/tmp/foo.spec.cheetah", "/tmp/foo.spec"), mock_move.mock_calls[0])
self.assertEquals(call("temp_pickle"), mock_unlink.mock_calls[0])
@patch("os.unlink")
@patch("glob.glob")
@patch("tito.common.run_command")
@patch("tempfile.NamedTemporaryFile")
def test_renders_cheetah_missing_result(self, mock_tempfile, mock_run_command, mock_glob, mock_unlink):
mock_run_command.return_value = True
mock_tempfile.return_value.name = "temp_pickle"
mock_unlink.return_value = True
mock_glob.return_value = []
with Capture(silent=True):
self.assertRaises(SystemExit, render_cheetah, "foo.spec.tmpl", "/tmp", {})
expected = "cheetah fill --flat --pickle=temp_pickle --odir=/tmp --oext=cheetah foo.spec.tmpl"
self.assertEquals(call(expected), mock_run_command.mock_calls[0])
self.assertEquals(call("/tmp/*.cheetah"), mock_glob.mock_calls[0])
self.assertEquals(call("temp_pickle"), mock_unlink.mock_calls[0])
class CargoTransformTest(unittest.TestCase):
def setUp(self):
pass
def tearDown(self):
pass
def test_simple_case(self):
input = ['[package]',
'name = "hello_world" # the name of the package',
'version = "0.1.0" # the current version, obeying semver',
'authors = ["you@example.com"]']
output = CargoBump.process_cargo_toml(input, "2.2.2")
self.assertEquals(4, len(output))
self.assertEquals("[package]", output[0])
self.assertEquals("name = \"hello_world\" # the name of the package", output[1])
self.assertEquals("version = \"2.2.2\" # the current version, obeying semver", output[2])
self.assertEquals("authors = [\"you@example.com\"]", output[3])
def test_complicated_case(self):
input = ['[package]',
'name = "hello_world"',
'version = "2.2.2"',
'authors = ["you@example.com"]',
'',
'[dependencies]',
'regex = "1.0.0"',
'',
'[dependencies.termion]',
'version = "0.1.0"']
output = CargoBump.process_cargo_toml(input, "3.3.3")
self.assertEquals(10, len(output))
self.assertEquals("version = \"3.3.3\"", output[2])
self.assertEquals("regex = \"1.0.0\"", output[6])
self.assertEquals("version = \"0.1.0\"", output[9])
class SpecTransformTest(unittest.TestCase):
def setUp(self):
self.spec_file = NamedTemporaryFile(delete=False).name
def tearDown(self):
os.unlink(self.spec_file)
def test_simple_transform(self):
simple_spec = dedent("""
Name: Hello
Version: 1.0.0
Release: 1%{?dist}
Source: hello-1.0.0.tar.gz
%prep
%setup -q
""")
with open(self.spec_file, 'w') as f:
f.write(simple_spec)
sha = "acecafe"
commit_count = 5
display_version = "git-%s.%s" % (commit_count, sha)
fullname = "hello-%s" % display_version
munge_specfile(self.spec_file, sha, commit_count, fullname, "%s.tar.gz" % fullname)
output = open(self.spec_file, 'r').readlines()
self.assertEquals(8, len(output))
self.assertEquals("Release: 1.git.%s.%s%%{?dist}\n" % (commit_count, sha), output[3])
self.assertEquals("Source: %s.tar.gz\n" % fullname, output[4])
self.assertEquals("%%setup -q -n %s\n" % fullname, output[7])
# Spot check some things that should not change
self.assertEquals("Name: Hello\n", output[1])
self.assertEqual("%prep\n", output[6])
def test_transform_release_only(self):
simple_spec = dedent("""
Release: 1%{?dist}
Source: hello-1.0.0.tar.gz
%setup -q
""")
with open(self.spec_file, 'w') as f:
f.write(simple_spec)
sha = "acecafe"
commit_count = 5
munge_specfile(self.spec_file, sha, commit_count)
output = open(self.spec_file, 'r').readlines()
self.assertEquals(4, len(output))
self.assertEquals("Release: 1.git.%s.%s%%{?dist}\n" % (commit_count, sha), output[1])
self.assertEquals("Source: hello-1.0.0.tar.gz\n", output[2])
self.assertEquals("%setup -q\n", output[3])
def test_transform_no_whitespace_modifications(self):
simple_spec = dedent("""
Release: 1%{?dist}
Source: hello-1.0.0.tar.gz
""")
with open(self.spec_file, 'w') as f:
f.write(simple_spec)
sha = "acecafe"
commit_count = 5
munge_specfile(self.spec_file, sha, commit_count)
output = open(self.spec_file, 'r').readlines()
self.assertEquals(3, len(output))
self.assertEquals("Release: 1.git.%s.%s%%{?dist}\n" % (commit_count, sha), output[1])
self.assertEquals("Source: hello-1.0.0.tar.gz\n", output[2])
def test_complex_setup_transform(self):
simple_spec = dedent("""
%setup -q -n hello-1
""")
with open(self.spec_file, 'w') as f:
f.write(simple_spec)
sha = "acecafe"
commit_count = 5
display_version = "git-%s.%s" % (commit_count, sha)
fullname = "hello-%s" % display_version
munge_specfile(self.spec_file, sha, commit_count, fullname, "%s.tar.gz" % fullname)
output = open(self.spec_file, 'r').readlines()
self.assertEquals("%%setup -q -n %s\n" % fullname, output[1])
class VersionMathTest(unittest.TestCase):
def test_increase_version_minor(self):
line = "1.0.0"
expected = "1.0.1"
self.assertEquals(expected, increase_version(line))
def test_increase_version_major(self):
line = "1.0"
expected = "1.1"
self.assertEquals(expected, increase_version(line))
def test_increase_release(self):
line = "1"
expected = "2"
self.assertEquals(expected, increase_version(line))
def test_underscore_release(self):
line = "1_PG5"
expected = "2_PG5"
self.assertEquals(expected, increase_version(line))
def test_increase_versionless(self):
line = "%{app_version}"
expected = "%{app_version}"
self.assertEquals(expected, increase_version(line))
def test_increase_release_with_rpm_cruft(self):
line = "1%{?dist}"
expected = "2%{?dist}"
self.assertEquals(expected, increase_version(line))
def test_increase_release_with_zstream(self):
line = "1%{?dist}.1"
expected = "1%{?dist}.2"
self.assertEquals(expected, increase_version(line))
def test_unknown_version(self):
line = "somethingstrange"
expected = "somethingstrange"
self.assertEquals(expected, increase_version(line))
def test_empty_string(self):
line = ""
expected = ""
self.assertEquals(expected, increase_version(line))
def test_increase_zstream(self):
line = "1%{?dist}"
expected = "1%{?dist}.1"
self.assertEquals(expected, increase_zstream(line))
def test_increase_zstream_already_appended(self):
line = "1%{?dist}.1"
expected = "1%{?dist}.2"
self.assertEquals(expected, increase_zstream(line))
def test_reset_release_with_rpm_cruft(self):
line = "2%{?dist}"
expected = "1%{?dist}"
self.assertEquals(expected, reset_release(line))
def test_reset_release_with_more_rpm_cruft(self):
line = "2.beta"
expected = "1.beta"
self.assertEquals(expected, reset_release(line))
def test_reset_release(self):
line = "2"
expected = "1"
self.assertEquals(expected, reset_release(line))
class ExtractBugzillasTest(unittest.TestCase):
def test_single_line(self):
commit_log = "- 123456: Did something interesting."
extractor = BugzillaExtractor(commit_log)
results = extractor.extract()
self.assertEquals(1, len(results))
self.assertEquals("Resolves: #123456 - Did something interesting.",
results[0])
def test_single_with_dash(self):
commit_log = "- 123456 - Did something interesting."
extractor = BugzillaExtractor(commit_log)
results = extractor.extract()
self.assertEquals(1, len(results))
self.assertEquals("Resolves: #123456 - Did something interesting.",
results[0])
def test_single_with_no_spaces(self):
commit_log = "- 123456-Did something interesting."
extractor = BugzillaExtractor(commit_log)
results = extractor.extract()
self.assertEquals(1, len(results))
self.assertEquals("Resolves: #123456 - Did something interesting.",
results[0])
def test_diff_format(self):
commit_log = "+- 123456: Did something interesting."
extractor = BugzillaExtractor(commit_log)
results = extractor.extract()
self.assertEquals(1, len(results))
self.assertEquals("Resolves: #123456 - Did something interesting.",
results[0])
def test_single_line_no_bz(self):
commit_log = "- Did something interesting."
extractor = BugzillaExtractor(commit_log)
results = extractor.extract()
self.assertEquals(0, len(results))
def test_multi_line(self):
commit_log = "- 123456: Did something interesting.\n- Another commit.\n" \
"- 456789: A third commit."
extractor = BugzillaExtractor(commit_log)
results = extractor.extract()
self.assertEquals(2, len(results))
self.assertEquals("Resolves: #123456 - Did something interesting.",
results[0])
self.assertEquals("Resolves: #456789 - A third commit.",
results[1])
def test_single_required_flag_found(self):
extractor = BugzillaExtractor("", required_flags=[
'myos-1.0+', 'pm_ack+'])
bug1 = ('123456', 'Did something interesting.')
extractor._extract_bzs = Mock(return_value=[
bug1])
extractor._check_for_bugzilla_creds = Mock()
extractor._load_bug = Mock(
return_value=MockBug(bug1[0], ['myos-1.0+', 'pm_ack+']))
results = extractor.extract()
self.assertEquals(1, len(extractor.bzs))
self.assertEquals(bug1[0], extractor.bzs[0][0])
self.assertEquals(bug1[1], extractor.bzs[0][1])
self.assertEquals(1, len(results))
self.assertEquals("Resolves: #123456 - Did something interesting.",
results[0])
def test_required_flags_found(self):
extractor = BugzillaExtractor("", required_flags=[
'myos-1.0+', 'pm_ack+'])
bug1 = ('123456', 'Did something interesting.')
bug2 = ('444555', 'Something else.')
bug3 = ('987654', 'Such amaze!')
extractor._extract_bzs = Mock(return_value=[
bug1, bug2, bug3])
extractor._check_for_bugzilla_creds = Mock()
bug_mocks = [
MockBug(bug1[0], ['myos-1.0+', 'pm_ack+']),
MockBug(bug2[0], ['myos-2.0?', 'pm_ack?']),
MockBug(bug3[0], ['myos-1.0+', 'pm_ack+'])]
def next_bug(*args):
return bug_mocks.pop(0)
extractor._load_bug = Mock(side_effect=next_bug)
results = extractor.extract()
self.assertEquals(2, len(extractor.bzs))
self.assertEquals(bug1[0], extractor.bzs[0][0])
self.assertEquals(bug1[1], extractor.bzs[0][1])
self.assertEquals(bug3[0], extractor.bzs[1][0])
self.assertEquals(bug3[1], extractor.bzs[1][1])
self.assertEquals(2, len(results))
self.assertEquals("Resolves: #123456 - Did something interesting.",
results[0])
self.assertEquals("Resolves: #987654 - Such amaze!",
results[1])
@patch("tito.common.error_out")
def test_required_flags_missing(self, mock_error):
required_flags = ['myos-2.0+']
extractor = BugzillaExtractor("", required_flags)
bug1 = ('123456', 'Did something interesting.')
bug2 = ('444555', 'Something else.')
bug3 = ('987654', 'Such amaze!')
extractor._extract_bzs = Mock(return_value=[
bug1, bug2, bug3])
extractor._check_for_bugzilla_creds = Mock()
bug_mocks = [
MockBug(bug1[0], ['myos-1.0+', 'pm_ack+']),
MockBug(bug2[0], ['myos-2.0?', 'pm_ack?']),
MockBug(bug3[0], ['myos-1.0+', 'pm_ack+'])]
def next_bug(*args):
return bug_mocks.pop(0)
extractor._load_bug = Mock(side_effect=next_bug)
results = extractor.extract()
self.assertEquals(0, len(extractor.bzs))
self.assertEquals(0, len(results))
mock_error.assert_called_once_with("No bugzillas found with required flags: %s" % required_flags)
def test_required_flags_missing_with_placeholder(self):
extractor = BugzillaExtractor("", required_flags=[
'myos-2.0+'], placeholder_bz="54321")
bug1 = ('123456', 'Did something interesting.')
extractor._extract_bzs = Mock(return_value=[
bug1])
extractor._check_for_bugzilla_creds = Mock()
extractor._load_bug = Mock(
return_value=MockBug(bug1[0], ['myos-1.0+', 'pm_ack+']))
results = extractor.extract()
self.assertEquals(0, len(extractor.bzs))
self.assertEquals(1, len(results))
self.assertEquals("Related: #54321", results[0])
def test_same_id_multiple_times(self):
extractor = BugzillaExtractor("", required_flags=[
'myos-1.0+', 'pm_ack+'])
bug1 = ('123456', 'Did something interesting.')
bug3 = ('123456', 'Oops, lets try again.')
extractor._extract_bzs = Mock(return_value=[
bug1, bug3])
extractor._check_for_bugzilla_creds = Mock()
extractor._load_bug = Mock(
return_value=MockBug(bug1[0], ['myos-1.0+', 'pm_ack+']))
results = extractor.extract()
self.assertEquals(2, len(extractor.bzs))
self.assertEquals(bug1[0], extractor.bzs[0][0])
self.assertEquals(bug1[1], extractor.bzs[0][1])
self.assertEquals(bug3[0], extractor.bzs[1][0])
self.assertEquals(bug3[1], extractor.bzs[1][1])
self.assertEquals(2, len(results))
self.assertEquals("Resolves: #123456 - Did something interesting.",
results[0])
self.assertEquals("Resolves: #123456 - Oops, lets try again.",
results[1])
@patch("tito.common.error_out")
def test_bug_doesnt_exist(self, mock_error):
required_flags = ['myos-1.0+', 'pm_ack+']
extractor = BugzillaExtractor("", required_flags)
bug1 = ('123456', 'Did something interesting.')
extractor._extract_bzs = Mock(return_value=[
bug1])
extractor._check_for_bugzilla_creds = Mock()
from tito.compat import xmlrpclib
extractor._load_bug = Mock(side_effect=xmlrpclib.Fault("", ""))
results = extractor.extract()
self.assertEquals(0, len(extractor.bzs))
self.assertEquals(0, len(results))
mock_error.assert_called_once_with("No bugzillas found with required flags: %s" % required_flags)
class MockBug(object):
def __init__(self, bug_id, flags):
self.flags = {}
for flag in flags:
self.flags[flag[0:-1]] = flag[-1]
def get_flag_status(self, flag):
if flag in self.flags:
return self.flags[flag]
else:
return None
class MungeSetupMacroTests(unittest.TestCase):
SOURCE = "tito-git-3.20362dd"
def test_setup(self):
line = "%setup -q -n tito-%{version}"
self.assertEqual("%setup -q -n " + self.SOURCE,
munge_setup_macro(self.SOURCE, line))
def test_autosetup(self):
line = "%autosetup -n tito-%{version}"
self.assertEqual("%autosetup -n " + self.SOURCE + " -p1",
munge_setup_macro(self.SOURCE, line))