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 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400
|
# test/test_changesfile.py
# Part of ‘dput’, a Debian package upload toolkit.
#
# This is free software, and you are welcome to redistribute it under
# certain conditions; see the end of this file for copyright
# information, grant of license, and disclaimer of warranty.
""" Unit tests for Debian upload control (‘*.changes’) files. """
import collections
import doctest
import email.message
import io
import os
import os.path
import sys
import tempfile
import textwrap
import unittest.mock
import testscenarios
import testtools
import dput.dput
from dput.helper import dputhelper
from .helper import (
EXIT_STATUS_FAILURE,
FakeSystemExit,
FileDouble,
get_file_doubles_from_fake_file_scenarios,
make_fake_file_scenarios,
make_unique_slug,
patch_locale_getpreferredencoding,
patch_os_stat,
patch_system_interfaces,
setup_file_double_behaviour,
)
from .test_configfile import (
patch_runtime_config_options,
set_config,
)
class _FieldsMapping:
""" A mapping to stand in for the `dict` of an `email.message.Message`. """
def __init__(self, *args, **kwargs):
try:
self._message = kwargs.pop('_message')
except KeyError:
raise TypeError("no ‘_message’ specified for this mapping")
super().__init__(*args, **kwargs)
def __len__(self, *args, **kwargs):
return self._message.__len__(*args, **kwargs)
def __contains__(self, *args, **kwargs):
return self._message.__contains__(*args, **kwargs)
def __getitem__(self, *args, **kwargs):
return self._message.__getitem__(*args, **kwargs)
def __setitem__(self, *args, **kwargs):
self._message.__setitem__(*args, **kwargs)
def __delitem__(self, *args, **kwargs):
self._message.__delitem__(*args, **kwargs)
def keys(self, *args, **kwargs):
return self._message.keys(*args, **kwargs)
def values(self, *args, **kwargs):
return self._message.values(*args, **kwargs)
def items(self, *args, **kwargs):
return self._message.items(*args, **kwargs)
def get(self, *args, **kwargs):
return self._message.get(*args, **kwargs)
class FakeMessage(email.message.Message, object):
""" A fake RFC 2822 message that mocks the obsolete `rfc822.Message`. """
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.dict = _FieldsMapping(_message=self)
def make_fake_message(fields):
""" Make a fake message instance. """
message = FakeMessage()
for (name, value) in fields.items():
message.add_header(name, value)
return message
def make_files_field_value(params_by_name):
""" Make a value for “Files” field of a changes document. """
result = "\n".join(
" ".join(params)
for (file_name, params) in params_by_name.items())
return result
def make_upload_files_params(checksums_by_file_name, sizes_by_file_name):
""" Make a mapping of upload parameters for files. """
params_by_name = {
file_name: [
checksums_by_file_name[file_name],
str(sizes_by_file_name[file_name]),
"foo", "bar", file_name]
for file_name in checksums_by_file_name}
return params_by_name
def make_changes_document(fields, upload_params_by_name=None):
""" Make a changes document from field values.
:param fields: Sequence of (name, value) tuples for fields.
:param upload_params_by_name: Mapping from filename to upload
parameters for each file.
:return: The changes document as an RFC 822 formatted text.
"""
document_fields = fields.copy()
if upload_params_by_name is not None:
files_field_text = make_files_field_value(upload_params_by_name)
document_fields.update({'files': files_field_text})
document = make_fake_message(document_fields)
return document
def make_changes_file_scenarios():
""" Make fake Debian upload control (‘*.changes’) scenarios. """
file_path = make_changes_file_path()
fake_file_empty = io.StringIO()
fake_file_no_format = io.StringIO(textwrap.dedent("""\
FOO
BAR
Files:
Lorem ipsum dolor sit amet
"""))
fake_file_with_signature = io.StringIO(textwrap.dedent("""\
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
FOO
BAR
Files:
Lorem ipsum dolor sit amet
-----BEGIN PGP SIGNATURE-----
Version: 0.0
Comment: Proin ac massa at orci sagittis fermentum.
gibberishgibberishgibberishgibberishgibberishgibberish
gibberishgibberishgibberishgibberishgibberishgibberish
gibberishgibberishgibberishgibberishgibberishgibberish
-----END PGP SIGNATURE-----
"""))
fake_file_with_format = io.StringIO(textwrap.dedent("""\
Format: FOO
Changed-By: Dolor sit amet <dolor@example.org>
Files:
Lorem ipsum dolor sit amet
"""))
fake_file_accented_text = io.StringIO(textwrap.dedent("""\
Format: FOO
Changed-By: Dølòr sít ämêt <dolor@example.org>
Files:
Lorem ipsum dolor sit amet
"""))
fake_file_olditalic_text = io.StringIO(textwrap.dedent("""\
Format: FOO
Changed-By: 𐌃𐌏𐌋𐌏𐌓 𐌔𐌉𐌕 𐌀𐌌𐌄𐌕 <dolor@example.org>
Files:
Lorem ipsum dolor sit amet
"""))
fake_file_invalid = io.StringIO(textwrap.dedent("""\
Format: FOO
Files:
FOO BAR
"""))
fake_file_needs_utf8 = io.StringIO(textwrap.dedent("""\
# ქართული
# 你好
Files:
Lórem ipsüm dolôr sìt āmet
"""))
scenarios = [
('no-format', {
'file_double': FileDouble(
path=file_path,
fake_file=fake_file_no_format),
'expected_result': make_changes_document({
'files': "Lorem ipsum dolor sit amet",
}),
}),
('with-pgp-signature', {
'file_double': FileDouble(
path=file_path,
fake_file=fake_file_with_signature),
'expected_result': make_changes_document({
'files': "Lorem ipsum dolor sit amet",
}),
}),
('with-format', {
'file_double': FileDouble(
path=file_path,
fake_file=fake_file_with_format),
'expected_result': make_changes_document({
'changed-by': "Dolor sit amet <dolor@example.org>",
'files': "Lorem ipsum dolor sit amet",
}),
}),
('non-ascii', {
'file_double': FileDouble(
path=file_path,
fake_file=fake_file_accented_text),
'expected_result': make_changes_document({
'changed-by': "Dølòr sít ämêt <dolor@example.org>",
'files': "Lorem ipsum dolor sit amet",
}),
}),
('higher-character-plane', {
'file_double': FileDouble(
path=file_path,
fake_file=fake_file_olditalic_text),
'expected_result': make_changes_document({
'changed-by': "𐌃𐌏𐌋𐌏𐌓 𐌔𐌉𐌕 𐌀𐌌𐌄𐌕 <dolor@example.org>",
'files': "Lorem ipsum dolor sit amet",
}),
}),
('error empty', {
'file_double': FileDouble(
path=file_path, fake_file=fake_file_empty),
'expected_error': KeyError,
}),
('error invalid', {
'file_double': FileDouble(
path=file_path,
fake_file=fake_file_invalid),
'expected_error': FakeSystemExit,
}),
('encoding needs-utf8', {
'file_double': FileDouble(
path=file_path,
fake_file=fake_file_needs_utf8),
'expected_result': make_changes_document({
'files': "Lórem ipsüm dolôr sìt āmet",
}),
}),
]
for (scenario_name, scenario) in scenarios:
scenario['changes_file_scenario_name'] = scenario_name
return scenarios
def set_fake_upload_file_paths(testcase):
""" Set the fake upload file paths. """
testcase.fake_upload_file_paths = [
os.path.join(
os.path.dirname(testcase.changes_file_double.path),
os.path.basename(tempfile.mktemp()))
for __ in range(10)]
required_suffixes = [".dsc", ".tar.xz"]
suffixes = required_suffixes + getattr(
testcase, 'additional_file_suffixes', [])
file_path_base = testcase.fake_upload_file_paths.pop()
for suffix in suffixes:
file_path = file_path_base + suffix
testcase.fake_upload_file_paths.insert(0, file_path)
def set_file_checksums(testcase):
""" Set the fake file checksums for the test case. """
testcase.fake_checksum_by_file = {
os.path.basename(file_path): make_unique_slug(testcase)
for file_path in testcase.fake_upload_file_paths}
def set_file_sizes(testcase):
""" Set the fake file sizes for the test case. """
testcase.fake_size_by_file = {
os.path.basename(file_path): testcase.getUniqueInteger()
for file_path in testcase.fake_upload_file_paths}
def set_file_doubles(testcase):
""" Set the file doubles for the test case. """
for file_path in testcase.fake_upload_file_paths:
file_double = FileDouble(file_path)
file_double.set_os_stat_scenario('okay')
file_double.stat_result = file_double.stat_result._replace(
st_size=testcase.fake_size_by_file[
os.path.basename(file_path)],
)
file_double.register_for_testcase(testcase)
def setup_upload_file_fixtures(testcase):
""" Set fixtures for fake files to upload for the test case. """
set_fake_upload_file_paths(testcase)
set_file_checksums(testcase)
set_file_sizes(testcase)
set_file_doubles(testcase)
def make_changes_file_path(file_dir_path=None):
""" Make a filesystem path for the changes file. """
if file_dir_path is None:
file_dir_path = tempfile.mktemp()
file_name = os.path.basename(
"{base}.changes".format(base=tempfile.mktemp()))
file_path = os.path.join(file_dir_path, file_name)
return file_path
def setup_changes_file_fixtures(testcase):
""" Set up fixtures for changes file doubles. """
file_path = make_changes_file_path()
scenarios = make_fake_file_scenarios(file_path)
testcase.changes_file_scenarios = scenarios
file_doubles = get_file_doubles_from_fake_file_scenarios(
scenarios.values())
setup_file_double_behaviour(testcase, file_doubles)
def set_changes_file_scenario(testcase, name):
""" Set the changes file scenario for this test case. """
scenario = dict(testcase.changes_file_scenarios)[name]
testcase.changes_file_scenario = scenario
testcase.changes_file_double = scenario['file_double']
testcase.changes_file_double.register_for_testcase(testcase)
class parse_changes_TestCase(
testscenarios.WithScenarios,
testtools.TestCase):
""" Base for test cases for `parse_changes` function. """
scenarios = NotImplemented
def setUp(self):
""" Set up test fixtures. """
super().setUp()
patch_system_interfaces(self)
self.test_infile = io.StringIO()
class parse_changes_SuccessTestCase(parse_changes_TestCase):
""" Success test cases for `parse_changes` function. """
scenarios = list(
(name, scenario)
for (name, scenario) in make_changes_file_scenarios()
if not name.startswith('error'))
def test_gives_expected_result_for_infile(self):
""" Should give the expected result for specified input file. """
result = dput.dput.parse_changes(self.file_double.fake_file)
normalised_result_set = set(
(key.lower(), value.strip())
for (key, value) in result.items())
self.assertEqual(
set(self.expected_result.items()), normalised_result_set)
class parse_changes_ErrorTestCase(parse_changes_TestCase):
""" Error test cases for `parse_changes` function. """
scenarios = list(
(name, scenario)
for (name, scenario) in make_changes_file_scenarios()
if name.startswith('error'))
def test_raises_expected_exception_for_infile(self):
""" Should raise the expected exception for specified input file. """
with testtools.ExpectedException(self.expected_error):
dput.dput.parse_changes(self.file_double.fake_file)
class check_upload_variant_TestCase(
testscenarios.WithScenarios,
testtools.TestCase):
""" Test cases for `check_upload_variant` function. """
scenarios = [
('simple', {
'fields': {
'architecture': "foo bar baz",
},
'expected_result': True,
}),
('arch-missing', {
'fields': {
'spam': "Lorem ipsum dolor sit amet",
},
'expected_result': False,
}),
('source-only', {
'fields': {
'architecture': "source",
},
'expected_result': False,
}),
('source-and-others', {
'fields': {
'architecture': "foo source bar",
},
'expected_result': False,
}),
]
def setUp(self):
""" Set up test fixtures. """
super().setUp()
patch_system_interfaces(self)
self.set_changes_document(self.fields)
self.set_test_args()
def set_changes_document(self, fields):
""" Set the package changes document based on specified fields. """
self.test_changes_document = make_changes_document(fields)
def set_test_args(self):
""" Set the arguments for the test call to the function. """
self.test_args = {
'changes': self.test_changes_document,
'debug': False,
}
def test_returns_expected_result_for_changes_document(self):
""" Should return expected result for specified changes document. """
result = dput.dput.check_upload_variant(**self.test_args)
self.assertEqual(self.expected_result, result)
def test_emits_debug_message_showing_architecture(self):
""" Should emit a debug message for the specified architecture. """
if 'architecture' not in self.fields:
self.skipTest("Architecture field not in this scenario")
self.test_args['debug'] = True
dput.dput.check_upload_variant(**self.test_args)
expected_output = textwrap.dedent("""\
D: Architecture: {arch}
""").format(arch=self.fields['architecture'])
self.assertIn(expected_output, sys.stdout.getvalue())
def test_emits_debug_message_for_binary_upload(self):
""" Should emit a debug message for the specified architecture. """
triggers_binaryonly = bool(self.expected_result)
if not triggers_binaryonly:
self.skipTest("Scenario does not trigger binary-only upload")
self.test_args['debug'] = True
dput.dput.check_upload_variant(**self.test_args)
expected_output = textwrap.dedent("""\
D: Doing a binary upload only.
""")
self.assertIn(expected_output, sys.stdout.getvalue())
SourceCheckResult = collections.namedtuple(
'SourceCheckResult', ['include_orig', 'include_tar'])
class source_check_BaseTestCase(
testscenarios.WithScenarios,
testtools.TestCase):
""" Base for test cases for `source_check` function. """
def setUp(self):
""" Set up test fixtures. """
super().setUp()
patch_system_interfaces(self)
self.make_changes_document()
self.set_test_args()
def make_test_version_string(self):
""" Make the test version string from component parts. """
version_string = self.test_upstream_version
if hasattr(self, 'test_epoch'):
version_string = "{epoch}:{version}".format(
epoch=self.test_epoch,
version=version_string)
if hasattr(self, 'test_release'):
version_string = "{version}-{release}".format(
version=version_string,
release=self.test_release)
return version_string
def make_changes_document(self):
""" Make the fake document for upload changes control file. """
fields = {}
if not hasattr(self, 'test_version'):
if hasattr(self, 'test_upstream_version'):
self.test_version = self.make_test_version_string()
if hasattr(self, 'test_version'):
fields.update({'version': self.test_version})
self.changes_document = make_changes_document(fields)
def set_test_args(self):
self.test_args = {
'changes': self.changes_document,
'debug': False,
}
class source_check_SuccessTestCase(source_check_BaseTestCase):
""" Test cases for `source_check` function, success result. """
default_expected_result = SourceCheckResult(
include_orig=False, include_tar=False)
scenarios = [
('no-version', {
'expected_result': default_expected_result,
}),
('no-epoch native-version', {
'test_upstream_version': "1.2",
'expected_result': SourceCheckResult(
include_orig=False, include_tar=True),
}),
('epoch native-version', {
'test_epoch': "3",
'test_upstream_version': "1.2",
'expected_result': SourceCheckResult(
include_orig=False, include_tar=True),
}),
('no-epoch debian-release', {
'test_upstream_version': "1.2",
'test_release': "5",
'expected_result': SourceCheckResult(
include_orig=False, include_tar=True),
}),
('no-epoch version-with-hyphen debian-release', {
'test_upstream_version': "1.2-3",
'test_release': "5",
'expected_result': SourceCheckResult(
include_orig=False, include_tar=True),
}),
('epoch debian-release', {
'test_epoch': "3",
'test_upstream_version': "1.2",
'test_release': "5",
'expected_result': SourceCheckResult(
include_orig=False, include_tar=True),
}),
('no-epoch new-upstream-version', {
'test_upstream_version': "1.2",
'test_release': "1",
'expected_result': SourceCheckResult(
include_orig=True, include_tar=False),
}),
('no-epoch new-upstream-version-with-hyphen', {
'test_upstream_version': "1.2-3",
'test_release': "1",
'expected_result': SourceCheckResult(
include_orig=True, include_tar=False),
}),
('epoch new_upstream-version', {
'test_epoch': "3",
'test_upstream_version': "1.2",
'test_release': "1",
'expected_result': SourceCheckResult(
include_orig=True, include_tar=False),
}),
('no-epoch nmu', {
'test_upstream_version': "1.2",
'test_release': "4.5",
'expected_result': SourceCheckResult(
include_orig=False, include_tar=True),
}),
('epoch nmu', {
'test_epoch': "3",
'test_upstream_version': "1.2",
'test_release': "4.5",
'expected_result': SourceCheckResult(
include_orig=False, include_tar=True),
}),
('no-epoch nmu before-first-release', {
'test_upstream_version': "1.2",
'test_release': "0.1",
'expected_result': SourceCheckResult(
include_orig=True, include_tar=False),
}),
('epoch nmu before-first-release', {
'test_epoch': "3",
'test_upstream_version': "1.2",
'test_release': "0.1",
'expected_result': SourceCheckResult(
include_orig=True, include_tar=False),
}),
('no-epoch nmu after-first-release', {
'test_upstream_version': "1.2",
'test_release': "1.1",
'expected_result': SourceCheckResult(
include_orig=True, include_tar=False),
}),
('epoch nmu after-first-release', {
'test_epoch': "3",
'test_upstream_version': "1.2",
'test_release': "1.1",
'expected_result': SourceCheckResult(
include_orig=True, include_tar=False),
}),
]
def test_returns_expected_result_for_changes_document(self):
""" Should return expected result for specified changes document. """
result = dput.dput.source_check(**self.test_args)
self.assertEqual(self.expected_result, result)
def test_emits_version_string_debug_message_only_if_version(self):
""" Should emit message for version only if has version. """
self.test_args['debug'] = True
version = getattr(self, 'test_version', None)
message_lead = "D: Package Version:"
expected_output = textwrap.dedent("""\
{lead} {version}
""").format(
lead=message_lead, version=version)
dput.dput.source_check(**self.test_args)
if hasattr(self, 'test_version'):
self.assertIn(expected_output, sys.stdout.getvalue())
else:
self.assertNotIn(message_lead, sys.stdout.getvalue())
def test_emits_epoch_debug_message_only_if_epoch(self):
""" Should emit message for epoch only if has an epoch. """
self.test_args['debug'] = True
dput.dput.source_check(**self.test_args)
expected_output = textwrap.dedent("""\
D: Epoch found
""")
dput.dput.source_check(**self.test_args)
if (hasattr(self, 'test_epoch') and hasattr(self, 'test_release')):
self.assertIn(expected_output, sys.stdout.getvalue())
else:
self.assertNotIn(expected_output, sys.stdout.getvalue())
def test_emits_upstream_version_debug_message_only_if_nonnative(self):
""" Should emit message for upstream version only if non-native. """
self.test_args['debug'] = True
upstream_version = getattr(self, 'test_upstream_version', None)
message_lead = "D: Upstream Version:"
expected_output = textwrap.dedent("""\
{lead} {version}
""").format(
lead=message_lead, version=upstream_version)
dput.dput.source_check(**self.test_args)
if hasattr(self, 'test_release'):
self.assertIn(expected_output, sys.stdout.getvalue())
else:
self.assertNotIn(message_lead, sys.stdout.getvalue())
def test_emits_debian_release_debug_message_only_if_nonnative(self):
""" Should emit message for Debian release only if non-native. """
self.test_args['debug'] = True
debian_release = getattr(self, 'test_release', None)
message_lead = "D: Debian Version:"
expected_output = textwrap.dedent("""\
{lead} {version}
""").format(
lead=message_lead, version=debian_release)
dput.dput.source_check(**self.test_args)
if hasattr(self, 'test_release'):
self.assertIn(expected_output, sys.stdout.getvalue())
else:
self.assertNotIn(message_lead, sys.stdout.getvalue())
class source_check_ErrorTestCase(source_check_BaseTestCase):
""" Test cases for `source_check` function, error result. """
scenarios = [
('version-characters-invalid', {
'test_version': "inv@l!d",
'expected_error': (
dputhelper.UploadChangesInvalidVersionDputError),
}),
('version-epoch-characters-invalid', {
'test_version': "1%3:1.2.3-1",
'expected_error': (
dputhelper.UploadChangesInvalidVersionDputError),
}),
('version-debian-release-characters-invalid', {
'test_version': "3:1.2.3-1@5",
'expected_error': (
dputhelper.UploadChangesInvalidVersionDputError),
}),
('version-too-many-epochs', {
'test_version': "3:7:1.2.3-1",
'expected_error': (
dputhelper.UploadChangesInvalidVersionDputError),
}),
]
def test_raises_expected_exception_for_infile(self):
""" Should raise the expected exception for specified input file. """
with testtools.ExpectedException(self.expected_error):
dput.dput.source_check(**self.test_args)
class verify_files_BaseTestCase(testtools.TestCase):
""" Base for test cases for `verify_files` function. """
default_args = dict(
host="foo",
check_only=None,
check_version=None,
allow_unsigned_uploads=None,
debug=None,
)
def setUp(self):
""" Set up test fixtures. """
super().setUp()
patch_system_interfaces(self)
self.file_double_by_path = {}
set_config(self, 'exist-simple')
patch_runtime_config_options(self)
self.set_test_args()
setup_changes_file_fixtures(self)
set_changes_file_scenario(self, 'exist-minimal')
self.test_args.update(dict(
changes_file_directory=os.path.dirname(
self.changes_file_double.path),
changes_file_name=os.path.basename(
self.changes_file_double.path),
))
patch_os_stat(self)
setup_upload_file_fixtures(self)
self.set_expected_files_to_upload()
self.patch_checksum_test()
self.patch_parse_changes()
self.patch_check_upload_variant()
self.set_expected_binary_upload()
self.set_expected_source_control_file_path()
self.patch_version_check()
self.patch_verify_signature()
self.patch_source_check()
def set_expected_files_to_upload(self):
""" Set the expected `files_to_upload` result for this test case. """
self.expected_files_to_upload = set(
path for path in self.fake_upload_file_paths)
self.expected_files_to_upload.add(self.changes_file_double.path)
def patch_checksum_test(self):
""" Patch `checksum_test` function for this test case. """
func_patcher = unittest.mock.patch.object(
dput.dput, "checksum_test", autospec=True)
mock_func = func_patcher.start()
self.addCleanup(func_patcher.stop)
def get_checksum_for_file(path, hash_name):
return self.fake_checksum_by_file[os.path.basename(path)]
mock_func.side_effect = get_checksum_for_file
def set_changes_document(self):
""" Set the changes document for this test case. """
self.changes_document = make_changes_document(
fields={},
upload_params_by_name=self.upload_params_by_name)
self.test_distribution = getattr(self, 'test_distribution', "lorem")
if self.test_distribution is not None:
self.changes_document.add_header(
'distribution', self.test_distribution)
self.runtime_config_parser.set(
self.test_args['host'], 'allowed_distributions',
self.test_distribution)
dput.dput.parse_changes.return_value = self.changes_document
def set_upload_params(self):
""" Set the upload parameters for this test case. """
self.upload_params_by_name = make_upload_files_params(
self.fake_checksum_by_file,
self.fake_size_by_file)
def patch_parse_changes(self):
""" Patch `parse_changes` function for this test case. """
func_patcher = unittest.mock.patch.object(
dput.dput, "parse_changes", autospec=True)
func_patcher.start()
self.addCleanup(func_patcher.stop)
self.set_upload_params()
self.set_changes_document()
def patch_check_upload_variant(self):
""" Patch `check_upload_variant` function for this test case. """
if not hasattr(self, 'check_upload_variant_return_value'):
self.check_upload_variant_return_value = True
func_patcher = unittest.mock.patch.object(
dput.dput, "check_upload_variant", autospec=True,
return_value=self.check_upload_variant_return_value)
func_patcher.start()
self.addCleanup(func_patcher.stop)
def patch_version_check(self):
""" Patch `version_check` function for this test case. """
func_patcher = unittest.mock.patch.object(
dput.dput, "version_check", autospec=True)
func_patcher.start()
self.addCleanup(func_patcher.stop)
def patch_verify_signature(self):
""" Patch `verify_signature` function for this test case. """
func_patcher = unittest.mock.patch.object(
dput.dput, "verify_signature", autospec=True)
func_patcher.start()
self.addCleanup(func_patcher.stop)
def patch_source_check(self):
""" Patch `source_check` function for this test case. """
func_patcher = unittest.mock.patch.object(
dput.dput, "source_check", autospec=True)
mock_func = func_patcher.start()
self.addCleanup(func_patcher.stop)
source_check_result = getattr(
self, 'source_check_result', SourceCheckResult(
include_orig=False, include_tar=False))
mock_func.return_value = source_check_result
def set_test_args(self):
""" Set test args for this test case. """
extra_args = getattr(self, 'extra_args', {})
self.test_args = self.default_args.copy()
self.test_args['config'] = self.runtime_config_parser
self.test_args.update(extra_args)
def set_expected_binary_upload(self):
""" Set expected value for `binary_upload` flag. """
self.expected_binary_upload = self.check_upload_variant_return_value
def set_expected_source_control_file_path(self):
""" Set expected value for source control file path. """
file_name = next(
os.path.basename(file_path)
for file_path in self.fake_upload_file_paths
if file_path.endswith(".dsc"))
if not self.expected_binary_upload:
self.expected_source_control_file_path = os.path.join(
os.path.dirname(self.changes_file_double.path), file_name)
else:
self.expected_source_control_file_path = ""
class verify_files_TestCase(
testscenarios.WithScenarios,
verify_files_BaseTestCase):
""" Test cases for `verify_files` function. """
scenarios = [
('default', {}),
('binary-only', {
'check_upload_variant_return_value': False,
}),
('include foo.tar.gz', {
'additional_file_suffixes': [".tar.gz"],
'source_check_result': SourceCheckResult(
include_orig=False, include_tar=True),
}),
('include foo.orig.tar.gz', {
'additional_file_suffixes': [".orig.tar.gz"],
'source_check_result': SourceCheckResult(
include_orig=True, include_tar=False),
}),
('unexpected foo.tar.gz', {
'additional_file_suffixes': [".tar.gz"],
'expected_rejection_message': (
"Package includes a .tar.gz file although"),
}),
('unexpected foo.orig.tar.gz', {
'additional_file_suffixes': [".orig.tar.gz"],
'expected_rejection_message': (
"Package includes an .orig.tar.gz file although"),
}),
('no distribution', {
'test_distribution': None,
}),
]
def test_emits_changes_file_path_debug_message(self):
""" Should emit debug message for changes file path. """
self.test_args['debug'] = True
dput.dput.verify_files(**self.test_args)
expected_output = textwrap.dedent("""\
D: Validating contents of changes file {path}
""").format(path=self.changes_file_double.path)
self.assertIn(expected_output, sys.stdout.getvalue())
def test_calls_sys_exit_if_input_read_denied(self):
""" Should call `sys.exit` if input file read access is denied. """
set_changes_file_scenario(self, 'error-read-denied')
with testtools.ExpectedException(FakeSystemExit):
dput.dput.verify_files(**self.test_args)
expected_output = textwrap.dedent("""\
Can't open {path}
""").format(path=self.changes_file_double.path)
self.assertIn(expected_output, sys.stdout.getvalue())
sys.exit.assert_called_with(EXIT_STATUS_FAILURE)
def test_calls_parse_changes_with_changes_files(self):
""" Should call `parse_changes` with changes file. """
dput.dput.verify_files(**self.test_args)
file_double = self.changes_file_double
with open(file_double.path, encoding='UTF-8') as expected_file:
dput.dput.parse_changes.assert_called_with(expected_file)
def test_calls_check_upload_variant_with_changes_document(self):
""" Should call `check_upload_variant` with changes document. """
dput.dput.verify_files(**self.test_args)
dput.dput.check_upload_variant.assert_called_with(
self.changes_document, unittest.mock.ANY)
def test_emits_upload_dsc_file_debug_message(self):
""" Should emit debug message for ‘*.dsc’ file. """
if getattr(self, 'check_upload_variant_return_value', True):
self.skipTest("Binary package upload for this scenario")
self.test_args['debug'] = True
dput.dput.verify_files(**self.test_args)
dsc_file_path = next(
os.path.basename(file_path)
for file_path in self.fake_upload_file_paths
if file_path.endswith(".dsc"))
expected_output = textwrap.dedent("""\
D: dsc-File: {path}
""").format(path=dsc_file_path)
self.assertThat(
sys.stdout.getvalue(),
testtools.matchers.Contains(expected_output))
def test_calls_sys_exit_when_source_upload_omits_dsc_file(self):
""" Should call `sys.exit` when source upload omits ‘*.dsc’ file. """
if getattr(self, 'check_upload_variant_return_value', True):
self.skipTest("Binary package upload for this scenario")
self.fake_checksum_by_file = dict(
(file_path, checksum)
for (file_path, checksum)
in self.fake_checksum_by_file.items()
if not file_path.endswith(".dsc"))
self.set_upload_params()
self.set_changes_document()
with testtools.ExpectedException(FakeSystemExit):
dput.dput.verify_files(**self.test_args)
expected_output = textwrap.dedent("""\
Error: no dsc file found in sourceful upload
""")
self.assertThat(
sys.stderr.getvalue(),
testtools.matchers.Contains(expected_output))
sys.exit.assert_called_with(EXIT_STATUS_FAILURE)
def test_calls_version_check_when_specified_in_config(self):
""" Should call `version_check` when specified in config. """
self.runtime_config_parser.set(
self.test_args['host'], 'check_version', "true")
dput.dput.verify_files(**self.test_args)
dput.dput.version_check.assert_called_with(
os.path.dirname(self.changes_file_double.path),
self.changes_document,
self.test_args['debug'])
def test_calls_version_check_when_specified_in_args(self):
""" Should call `version_check` when specified in arguments. """
self.test_args['check_version'] = True
dput.dput.verify_files(**self.test_args)
dput.dput.version_check.assert_called_with(
os.path.dirname(self.changes_file_double.path),
self.changes_document,
self.test_args['debug'])
def test_calls_sys_exit_when_host_section_not_in_config(self):
""" Should call `sys.exit` when specified host not in config. """
self.runtime_config_parser.remove_section(self.test_args['host'])
with testtools.ExpectedException(FakeSystemExit):
dput.dput.verify_files(**self.test_args)
expected_output = textwrap.dedent("""\
Error in config file:
No section: ...
""")
self.assertThat(
sys.stderr.getvalue(),
testtools.matchers.DocTestMatches(
expected_output, doctest.ELLIPSIS))
sys.exit.assert_called_with(EXIT_STATUS_FAILURE)
def test_calls_verify_signature_with_expected_args(self):
""" Should call `verify_signature` with expected args. """
dput.dput.verify_files(**self.test_args)
dput.dput.verify_signature.assert_called_with(
self.test_args['host'],
self.changes_file_double.path,
self.expected_source_control_file_path,
self.runtime_config_parser,
self.test_args['check_only'],
self.test_args['allow_unsigned_uploads'],
unittest.mock.ANY,
self.test_args['debug'])
def test_calls_source_check_with_changes_document(self):
""" Should call `source_check` with changes document. """
dput.dput.verify_files(**self.test_args)
dput.dput.source_check.assert_called_with(
self.changes_document, self.test_args['debug'])
def test_emits_upload_file_path_debug_message(self):
""" Should emit debug message for each upload file path. """
self.test_args['debug'] = True
dput.dput.verify_files(**self.test_args)
for file_path in self.fake_upload_file_paths:
expected_output = textwrap.dedent("""\
D: File to upload: {path}
""").format(path=file_path)
self.expectThat(
sys.stdout.getvalue(),
testtools.matchers.Contains(expected_output))
def test_calls_checksum_test_with_upload_files(self):
""" Should call `checksum_test` with each upload file path. """
dput.dput.verify_files(**self.test_args)
expected_calls = [
unittest.mock.call(file_path, unittest.mock.ANY)
for file_path in self.fake_upload_file_paths]
dput.dput.checksum_test.assert_has_calls(
expected_calls, any_order=True)
def set_bogus_file_checksums(self):
""" Set bogus file checksums that will not match. """
self.fake_checksum_by_file = {
file_name: self.getUniqueString()
for file_name in self.fake_checksum_by_file}
def test_emits_checksum_okay_debug_message(self):
""" Should emit debug message checksum okay for each file. """
self.test_args['debug'] = True
dput.dput.verify_files(**self.test_args)
for file_path in self.fake_upload_file_paths:
expected_output = textwrap.dedent("""\
D: Checksum for {path} is fine
""").format(path=file_path)
self.expectThat(
sys.stdout.getvalue(),
testtools.matchers.Contains(expected_output))
def test_emits_checksum_mismatch_debug_message(self):
""" Should emit debug message when a checksum does not match. """
self.test_args['debug'] = True
self.set_bogus_file_checksums()
with testtools.ExpectedException(FakeSystemExit):
dput.dput.verify_files(**self.test_args)
expected_output = textwrap.dedent("""\
...
D: Checksum from .changes: ...
D: Generated Checksum: ...
...
""")
self.assertThat(
sys.stdout.getvalue(),
testtools.matchers.DocTestMatches(
expected_output, doctest.ELLIPSIS))
def test_calls_sys_exit_when_checksum_mismatch(self):
""" Should call `sys.exit` when a checksum does not match. """
specified_checksum_by_file = self.fake_checksum_by_file
self.set_bogus_file_checksums()
with testtools.ExpectedException(FakeSystemExit):
dput.dput.verify_files(**self.test_args)
expected_output_for_files = [
textwrap.dedent("""\
Checksum doesn't match for {file_name}
""").format(
file_name=os.path.join(
os.path.dirname(self.changes_file_double.path),
file_name),
specified_hash=specified_hash,
computed_hash=self.fake_checksum_by_file[file_name])
for (file_name, specified_hash)
in specified_checksum_by_file.items()]
self.assertThat(
sys.stdout.getvalue(),
testtools.matchers.MatchesAny(*[
testtools.matchers.Contains(expected_output)
for expected_output in expected_output_for_files]))
sys.exit.assert_called_with(EXIT_STATUS_FAILURE)
def test_calls_os_stat_with_upload_files(self):
""" Should call `os.stat` with each upload file path. """
dput.dput.verify_files(**self.test_args)
expected_calls = [
unittest.mock.call(file_path)
for file_path in self.fake_upload_file_paths]
os.stat.assert_has_calls(expected_calls, any_order=True)
def set_bogus_file_sizes(self):
""" Set bogus file sizes that will not match. """
file_double_registry = FileDouble.get_registry_for_testcase(self)
for file_name in self.fake_size_by_file:
bogus_size = self.getUniqueInteger()
self.fake_size_by_file[file_name] = bogus_size
file_path = os.path.join(
os.path.dirname(self.changes_file_double.path),
file_name)
file_double = file_double_registry[file_path]
file_double.stat_result = file_double.stat_result._replace(
st_size=bogus_size)
def test_emits_size_mismatch_debug_message(self):
""" Should emit debug message when a size does not match. """
self.test_args['debug'] = True
self.set_bogus_file_sizes()
dput.dput.verify_files(**self.test_args)
expected_output = textwrap.dedent("""\
...
D: size from .changes: ...
D: calculated size: ...
...
""")
self.assertThat(
sys.stdout.getvalue(),
testtools.matchers.DocTestMatches(
expected_output, doctest.ELLIPSIS))
def test_emits_size_mismatch_message_for_each_file(self):
""" Should emit error message for each file with size mismatch. """
self.set_bogus_file_sizes()
dput.dput.verify_files(**self.test_args)
for file_path in self.fake_upload_file_paths:
expected_output = textwrap.dedent("""\
size doesn't match for {path}
""").format(path=file_path)
self.expectThat(
sys.stdout.getvalue(),
testtools.matchers.Contains(expected_output))
def test_emits_rejection_warning_when_unexpected_tarball(self):
""" Should emit warning of rejection when unexpected tarball. """
if not hasattr(self, 'expected_rejection_message'):
self.skipTest("No rejection message expected")
dput.dput.verify_files(**self.test_args)
sys.stderr.write("calls: {calls!r}\n".format(
calls=sys.stdout.write.mock_calls))
self.assertThat(
sys.stdout.getvalue(),
testtools.matchers.Contains(self.expected_rejection_message))
def test_raises_error_when_distribution_mismatch(self):
""" Should raise error when distribution mismatch against allowed. """
if not getattr(self, 'test_distribution', None):
self.skipTest("No distribution set for this test case")
self.runtime_config_parser.set(
self.test_args['host'], 'allowed_distributions',
"dolor sit amet")
with testtools.ExpectedException(dputhelper.DputUploadFatalException):
dput.dput.verify_files(**self.test_args)
def test_emits_changes_file_upload_debug_message(self):
""" Should emit debug message for upload of changes file. """
self.test_args['debug'] = True
dput.dput.verify_files(**self.test_args)
expected_output = textwrap.dedent("""\
D: File to upload: {path}
""").format(path=self.changes_file_double.path)
self.assertIn(expected_output, sys.stdout.getvalue())
def test_returns_expected_files_to_upload_collection(self):
""" Should return expected `files_to_upload` collection value. """
result = dput.dput.verify_files(**self.test_args)
expected_result = self.expected_files_to_upload
self.assertEqual(expected_result, set(result))
class verify_files_UnusualPreferredEncodingTestCase(
testscenarios.WithScenarios,
verify_files_BaseTestCase):
"""
Test cases for `verify_files` function when preferred encoding is unusual.
"""
locale_getpreferredencoding_return_value = "ANSI_X3.4-1968"
scenarios = [
('default', {}),
]
def setUp(self):
""" Set up test fixtures. """
patch_locale_getpreferredencoding(self)
super().setUp()
set_changes_file_scenario(self, 'exist-needs-utf8')
self.test_args.update(dict(
changes_file_directory=os.path.dirname(
self.changes_file_double.path),
changes_file_name=os.path.basename(
self.changes_file_double.path),
))
def test_calls_parse_changes_with_changes_files(self):
""" Should call `parse_changes` with changes file. """
dput.dput.verify_files(**self.test_args)
file_double = self.changes_file_double
expected_file = open(file_double.path, encoding='UTF-8')
dput.dput.parse_changes.assert_called_with(expected_file)
class guess_upload_host_TestCase(
testscenarios.WithScenarios,
testtools.TestCase):
""" Test cases for `guess_upload_host` function. """
changes_file_scenarios = [
('no-distribution', {
'fake_file': io.StringIO(textwrap.dedent("""\
Files:
Lorem ipsum dolor sit amet
""")),
}),
('distribution-spam', {
'fake_file': io.StringIO(textwrap.dedent("""\
Distribution: spam
Files:
Lorem ipsum dolor sit amet
""")),
}),
('distribution-beans', {
'fake_file': io.StringIO(textwrap.dedent("""\
Distribution: beans
Files:
Lorem ipsum dolor sit amet
""")),
}),
('needs-utf8 no-distribution', {
'fake_file': io.StringIO(textwrap.dedent("""\
Files:
Lórem ipsüm dolôr sìt āmet
""")),
}),
]
scenarios = [
('distribution-found-of-one', {
'changes_file_scenario_name': "distribution-spam",
'test_distribution': "spam",
'config_scenario_name': "exist-distribution-one",
'expected_host': "foo",
}),
('distribution-notfound-of-one', {
'changes_file_scenario_name': "distribution-beans",
'test_distribution': "beans",
'config_scenario_name': "exist-distribution-one",
'expected_host': "ftp-master",
}),
('distribution-first-of-three', {
'changes_file_scenario_name': "distribution-spam",
'test_distribution': "spam",
'config_scenario_name': "exist-distribution-three",
'expected_host': "foo",
}),
('distribution-last-of-three', {
'changes_file_scenario_name': "distribution-beans",
'test_distribution': "beans",
'config_scenario_name': "exist-distribution-three",
'expected_host': "foo",
}),
('no-configured-distribution', {
'changes_file_scenario_name': "distribution-beans",
'config_scenario_name': "exist-distribution-none",
'expected_host': "ftp-master",
}),
('no-distribution', {
'changes_file_scenario_name': "no-distribution",
'config_scenario_name': "exist-simple",
'expected_host': "ftp-master",
}),
('default-distribution', {
'config_scenario_name': "exist-default-distribution-only",
'config_default_default_host_main': "consecteur",
'expected_host': "consecteur",
}),
('unusual-preferred-encoding no-distribution', {
'locale_preferredencoding_return_value': "ANSI_X3.4-1968",
'changes_file_scenario_name': "needs-utf8 no-distribution",
'config_scenario_name': "exist-simple",
'expected_host': "ftp-master",
}),
]
def setUp(self):
""" Set up test fixtures. """
super().setUp()
patch_system_interfaces(self)
if hasattr(self, 'locale_preferredencoding_return_value'):
patch_locale_getpreferredencoding(
self, encoding=self.locale_preferredencoding_return_value)
set_config(
self,
getattr(self, 'config_scenario_name', 'exist-minimal'))
patch_runtime_config_options(self)
self.setup_changes_file_fixtures()
set_changes_file_scenario(
self,
getattr(self, 'changes_file_scenario_name', 'no-distribution'))
self.set_test_args()
def set_test_args(self):
""" Set the arguments for the test call to the function. """
self.test_args = dict(
changes_file_directory=os.path.dirname(
self.changes_file_double.path),
changes_file_name=os.path.basename(
self.changes_file_double.path),
config=self.runtime_config_parser,
)
def setup_changes_file_fixtures(self):
""" Set up fixtures for fake changes file. """
file_path = make_changes_file_path()
scenarios = [s for (__, s) in self.changes_file_scenarios]
for scenario in scenarios:
scenario['file_double'] = FileDouble(
file_path, scenario['fake_file'])
setup_file_double_behaviour(
self,
get_file_doubles_from_fake_file_scenarios(scenarios))
def test_calls_sys_exit_if_read_denied(self):
""" Should call `sys.exit` if read permission denied. """
self.changes_file_double.set_os_access_scenario('denied')
self.changes_file_double.set_open_scenario('read_denied')
with testtools.ExpectedException(FakeSystemExit):
dput.dput.guess_upload_host(**self.test_args)
expected_output = textwrap.dedent("""\
Can't open {path}
""").format(path=self.changes_file_double.path)
self.assertIn(expected_output, sys.stdout.getvalue())
sys.exit.assert_called_with(EXIT_STATUS_FAILURE)
def test_returns_expected_host(self):
""" Should return expected host value. """
result = dput.dput.guess_upload_host(**self.test_args)
self.assertEqual(self.expected_host, result)
@unittest.mock.patch.object(dput.dput, 'debug', True)
def test_emits_debug_message_for_host(self):
""" Should emit a debug message for the discovered host. """
config_parser = self.runtime_config_parser
if not (
config_parser.has_section(self.expected_host)
and config_parser.get(self.expected_host, 'distributions')):
self.skipTest("No distributions specified")
dput.dput.guess_upload_host(**self.test_args)
expected_output = textwrap.dedent("""\
D: guessing host {host} based on distribution {dist}
""").format(
host=self.expected_host, dist=self.test_distribution)
self.assertIn(expected_output, sys.stdout.getvalue())
# Copyright © 2015–2024 Ben Finney <bignose@debian.org>
#
# This is free software: you may copy, modify, and/or distribute this work
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 3 of that license or any later version.
# No warranty expressed or implied. See the file ‘LICENSE.GPL-3’ for details.
# Local variables:
# coding: utf-8
# mode: python
# End:
# vim: fileencoding=utf-8 filetype=python :
|