File: test_dput_main.py

package info (click to toggle)
dput 1.2.4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 38,396 kB
  • sloc: python: 13,102; sh: 162; makefile: 42
file content (2467 lines) | stat: -rw-r--r-- 97,265 bytes parent folder | download
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
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
# test/test_dput_main.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 ‘dput’ module `main` function. """

import contextlib
import doctest
import itertools
import os
import sys
import tempfile
import textwrap
import types
import unittest.mock

import testscenarios
import testtools
import testtools.matchers

import dput.dput
from dput.helper import dputhelper

from .helper import (
        EXIT_STATUS_FAILURE,
        EXIT_STATUS_SUCCESS,
        FakeSystemExit,
        make_fake_distribution,
        make_options_namespace,
        make_unique_slug,
        patch_importlib_metadata_distribution,
        patch_os_environ,
        patch_os_getuid,
        patch_os_isatty,
        patch_pwd_getpwuid,
        patch_sys_argv,
        patch_system_interfaces,
        )
from .test_changesfile import (
        make_changes_file_path,
        set_changes_file_scenario,
        setup_changes_file_fixtures,
        )
from .test_configfile import (
        patch_active_config_files,
        patch_get_fqdn_for_host,
        patch_get_incoming_for_host,
        patch_get_login_for_host,
        patch_get_port_for_host,
        patch_get_progress_indicator_for_host,
        patch_get_upload_method_name_for_host,
        patch_print_default_upload_method,
        patch_print_host_list,
        patch_runtime_config_options,
        patch_verify_config_upload_methods,
        set_config,
        )
from .test_dputhelper import (
        patch_get_username_from_system,
        patch_getopt,
        )


def patch_parse_changes(testcase):
    """ Patch the `parse_changes` function for the test case. """
    func_patcher = unittest.mock.patch.object(
            dput.dput, "parse_changes", autospec=True)
    func_patcher.start()
    testcase.addCleanup(func_patcher.stop)


def patch_read_configs(testcase):
    """ Patch the `read_configs` function for the test case. """
    def fake_read_configs(*args, **kwargs):
        return testcase.runtime_config_parser

    func_patcher = unittest.mock.patch.object(
            dput.dput, "read_configs", autospec=True,
            side_effect=fake_read_configs)
    func_patcher.start()
    testcase.addCleanup(func_patcher.stop)


def patch_print_config(testcase):
    """ Patch the `print_config` function for the test case. """
    func_patcher = unittest.mock.patch.object(
            dput.dput, "print_config", autospec=True)
    func_patcher.start()
    testcase.addCleanup(func_patcher.stop)


def set_upload_methods(testcase):
    """ Set the `upload_methods` value for the test case. """
    if not hasattr(testcase, 'upload_methods'):
        upload_methods = dput.dput.import_upload_functions()
        testcase.upload_methods = {
                name: unittest.mock.MagicMock(method)
                for (name, method) in upload_methods.items()}


def patch_import_upload_functions(testcase):
    """ Patch the `import_upload_functions` function for the test case. """
    func_patcher = unittest.mock.patch.object(
            dput.dput, "import_upload_functions", autospec=True,
            return_value=testcase.upload_methods)
    func_patcher.start()
    testcase.addCleanup(func_patcher.stop)


def patch_distribution(testcase):
    """ Patch the “dput” installed distribution for the `testcase`. """
    testcase.fake_distribution = make_fake_distribution(
            name="dput",
            version=getattr(testcase, 'dput_version', None),
    )
    patch_importlib_metadata_distribution(
            testcase, fake_distribution=testcase.fake_distribution)


class make_usage_message_TestCase(testtools.TestCase):
    """ Test cases for `make_usage_message` function. """

    def test_returns_text_with_program_name(self):
        """ Should return text with expected program name. """
        result = dput.dput.make_usage_message()
        expected_result = textwrap.dedent("""\
                Usage: dput ...
                ...
                """)
        self.expectThat(
                result,
                testtools.matchers.DocTestMatches(
                    expected_result, flags=doctest.ELLIPSIS))


class make_delayed_queue_path_TestCase(
        testscenarios.WithScenarios,
        testtools.TestCase):
    """ Test cases for `make_delayed_queue_path` function. """

    scenarios = [
            ('days-unspecified', {
                'test_days': None,
                'expected_result': "",
                }),
            ('days-zero', {
                'test_days': 0,
                'expected_result': "DELAYED/0-day",
                }),
            ('days-five', {
                'test_days': 5,
                'expected_result': "DELAYED/5-day",
                }),
            ]

    def setUp(self):
        """ Set up test fixtures. """
        super().setUp()

        self.set_test_args()

    def set_test_args(self):
        """ Set the arguments for the test call to the function. """
        self.test_args = dict(
                days=self.test_days,
                )

    def test_returns_expected_result(self):
        """ Should return expected path value. """
        result = dput.dput.make_delayed_queue_path(**self.test_args)
        self.assertEqual(self.expected_result, result)


def patch_make_delayed_queue_path(testcase):
    """ Patch the `make_delayed_queue_path` function for `testcase`. """
    if not hasattr(testcase, 'delayed_queue_path'):
        testcase.delayed_queue_path = os.path.basename(tempfile.mktemp())
    func_patcher = unittest.mock.patch.object(
            dput.dput, 'make_delayed_queue_path', autospec=True,
            return_value=testcase.delayed_queue_path)
    func_patcher.start()
    testcase.addCleanup(func_patcher.stop)


class upload_files_via_BaseTestCase(testtools.TestCase):
    """ Base for test cases for `upload_files_via_*` functions. """

    placeholder_args = {
            'fqdn': object(),
            'login': object(),
            'incoming': object(),
            'files_to_upload': object(),
            'debug': object(),
            }

    def setUp(self):
        """ Set up test fixtures. """
        super().setUp()
        patch_system_interfaces(self)

        set_config(self, 'exist-simple')
        patch_runtime_config_options(self)

        set_upload_methods(self)
        self.test_method = self.runtime_config_parser.get(
               self.test_host, 'method')
        self.test_method_func = self.upload_methods[self.test_method]

        if not hasattr(self, 'omit_kwargs'):
            self.omit_kwargs = []
        self.set_test_args()
        self.set_expected_method_and_args()

        patch_get_port_for_host(self)

    def set_test_args(self):
        """ Set the arguments for the test call to the function. """
        raise NotImplementedError

    def set_expected_method_and_args(self):
        """ Set the expected upload method and its arguments. """
        self.expected_method_func = self.test_method_func
        self.expected_args_list = [
                self.placeholder_args['fqdn'],
                self.placeholder_args['login'],
                self.placeholder_args['incoming'],
                self.placeholder_args['files_to_upload'],
                self.placeholder_args['debug'],
                ]
        self.expected_args = tuple(self.expected_args_list)


def patch_upload_files_via(testcase):
    """ Patch the `upload_files_via_*` functions for `testcase`. """
    for func_name in [
            'upload_files_via_{}'.format(suffix)
            for suffix in [
                'simulate',
                'method', 'method_local', 'method_ftp', 'method_scp']
            ]:
        func_patcher = unittest.mock.patch.object(
                dput.dput, func_name, autospec=True)
        func_patcher.start()
        testcase.addCleanup(func_patcher.stop)


class upload_files_via_simulate_TestCase(
        testscenarios.WithScenarios,
        upload_files_via_BaseTestCase):
    """ Test cases for `upload_files_via_simulate` function. """

    function_to_test = staticmethod(dput.dput.upload_files_via_simulate)

    scenarios = [
            ('files-zero', {
                'files_to_upload': [],
                }),
            ('files-one', {
                'files_to_upload': [tempfile.mktemp()],
                }),
            ('files-three', {
                'files_to_upload': [tempfile.mktemp() for __ in range(3)],
                }),
            ]

    def set_test_args(self):
        """ Set the arguments for the test call to the function. """
        self.test_args = dict(self.placeholder_args)
        self.test_args.update({
                'method': make_unique_slug(self),
                'files_to_upload': self.files_to_upload,
                'config': self.runtime_config_parser,
                'host': self.test_host,
                })

    def set_expected_method_and_args(self):
        """ Set the expected upload method and its arguments. """
        # Upload method is called multiple times with different arguments.
        pass

    def test_emits_expected_information_message(self):
        """ Should emit expected information message. """
        self.function_to_test(**self.test_args)
        expected_stderr_output = (
                "Not performing upload: ‘simulate’ option specified.")
        self.assertIn(expected_stderr_output, sys.stderr.getvalue())

    def test_emits_diagnostic_message_for_each_file_to_upload(self):
        """ Should emit diagnostic message for each file to upload. """
        self.function_to_test(**self.test_args)
        for path in self.files_to_upload:
            expected_output = textwrap.dedent("""\
                    Uploading with {method}: {path} to {fqdn}:{incoming}
                    """).format(
                        method=self.test_args['method'],
                        path=path,
                        fqdn=self.test_args['fqdn'],
                        incoming=self.test_args['incoming'])
            self.expectThat(
                    sys.stderr.getvalue(),
                    testtools.matchers.Contains(expected_output))


def patch_upload_files_via_simulate(testcase):
    """ Patch the `upload_files_via_simulate` function for the `testcase`. """
    func_patcher = unittest.mock.patch.object(
            dput.dput, "upload_files_via_simulate", autospec=True)
    func_patcher.start()
    testcase.addCleanup(func_patcher.stop)


class upload_files_via_method_TestCase(
        testscenarios.WithScenarios,
        upload_files_via_BaseTestCase):
    """ Test cases for `upload_files_via_method` function. """

    placeholder_args = dict(upload_files_via_BaseTestCase.placeholder_args)
    placeholder_args.update({
            'files_to_upload': object(),
            'debug': object(),
            'dummy': object(),
            'progress': object(),
            })

    scenarios = [
            ('method-http', {
                'config_method': "http",
                'expected_kwargs': {
                    'dummy': placeholder_args['dummy'],
                    'progress': placeholder_args['progress'],
                    },
                }),
            ('method-http default-dummy', {
                'config_method': "http",
                'omit_kwargs': ['dummy'],
                'expected_kwargs': {
                    'dummy': 0,
                    'progress': placeholder_args['progress'],
                    },
                }),
            ('method-http default-progress', {
                'config_method': "http",
                'omit_kwargs': ['progress'],
                'expected_kwargs': {
                    'dummy': placeholder_args['dummy'],
                    'progress': 0,
                    },
                }),
            ('method-rsync', {
                'config_method': "rsync",
                'expected_kwargs': {
                    'dummy': placeholder_args['dummy'],
                    'progress': placeholder_args['progress'],
                    },
                }),
            ]

    def set_test_args(self):
        """ Set the arguments for the test call to the function. """
        self.test_args = dict(
                (name, self.placeholder_args[name])
                for name in [
                    'fqdn', 'login', 'incoming', 'files_to_upload', 'debug',
                    'dummy', 'progress']
                if name not in self.omit_kwargs)
        self.test_args.update({
                'method_name': self.test_method,
                'method': self.test_method_func,
                'config': self.runtime_config_parser,
                'host': self.test_host,
                })
        if hasattr(self, 'test_args_extra'):
            self.test_args.update(self.test_args_extra)

    def test_calls_upload_method_with_expected_args(self):
        """ Should call upload method function with expected args. """
        dput.dput.upload_files_via_method(**self.test_args)
        self.expected_method_func.assert_called_with(
                *self.expected_args, **self.expected_kwargs)


class upload_files_via_method_local_TestCase(
        testscenarios.WithScenarios,
        upload_files_via_BaseTestCase):
    """ Test cases for `upload_files_via_method_local` function. """

    placeholder_args = dict(upload_files_via_BaseTestCase.placeholder_args)
    placeholder_args.update({
            'files_to_upload': object(),
            'debug': object(),
            'compress': object(),
            'progress': object(),
            })

    scenarios = [
            ('method-local', {
                'config_method': "local",
                'expected_kwargs': {
                    'compress': placeholder_args['compress'],
                    'progress': placeholder_args['progress'],
                    },
                }),
            ('method-local default-compress', {
                'config_method': "local",
                'omit_kwargs': ['compress'],
                'expected_kwargs': {
                    'compress': False,
                    'progress': placeholder_args['progress'],
                    },
                }),
            ('method-local default-progress', {
                'config_method': "local",
                'omit_kwargs': ['progress'],
                'expected_kwargs': {
                    'compress': placeholder_args['compress'],
                    'progress': 0,
                    },
                }),
            ]

    def set_test_args(self):
        """ Set the arguments for the test call to the function. """
        self.test_args = dict(
                (name, self.placeholder_args[name])
                for name in [
                    'fqdn', 'login', 'incoming', 'files_to_upload', 'debug',
                    'compress', 'progress']
                if name not in self.omit_kwargs)
        self.test_args.update({
                'method_name': self.test_method,
                'method': self.test_method_func,
                'config': self.runtime_config_parser,
                'host': self.test_host,
                })
        if hasattr(self, 'test_args_extra'):
            self.test_args.update(self.test_args_extra)

    def test_calls_upload_method_with_expected_args(self):
        """ Should call upload method function with expected args. """
        dput.dput.upload_files_via_method_local(**self.test_args)
        self.expected_method_func.assert_called_with(
                *self.expected_args, **self.expected_kwargs)


class upload_files_via_method_ftp_TestCase(
        testscenarios.WithScenarios,
        upload_files_via_BaseTestCase):
    """ Test cases for `upload_files_via_method_ftp` function. """

    placeholder_args = dict(upload_files_via_BaseTestCase.placeholder_args)
    placeholder_args.update({
            'files_to_upload': object(),
            'debug': False,
            'commandline_passive': False,
            'progress': object(),
            })

    scenarios = [
            ('method-ftp', {
                'config_method': "ftp",
                'get_port_for_host_return_value': None,
                'expected_kwargs': {
                    'ftp_mode': False,
                    'progress': placeholder_args['progress'],
                    'port': 21,
                    },
                'expected_stdout_output': "\n".join([
                    "D: FTP port: 21",
                    "D: Using active ftp",
                    ]),
                }),
            ('method-ftp default-passive', {
                'config_method': "ftp",
                'omit_kwargs': ['commandline_passive'],
                'get_port_for_host_return_value': None,
                'expected_kwargs': {
                    'ftp_mode': False,
                    'progress': placeholder_args['progress'],
                    'port': 21,
                    },
                'expected_stdout_output': "\n".join([
                    "D: FTP port: 21",
                    "D: Using active ftp",
                    ]),
                }),
            ('method-ftp default-progress', {
                'config_method': "ftp",
                'omit_kwargs': ['progress'],
                'get_port_for_host_return_value': None,
                'expected_kwargs': {
                    'ftp_mode': False,
                    'progress': 0,
                    'port': 21,
                    },
                'expected_stdout_output': "\n".join([
                    "D: FTP port: 21",
                    "D: Using active ftp",
                    ]),
                }),
            ('method-ftp config-port-custom', {
                'config_method': "ftp",
                'get_port_for_host_return_value': 555,
                'expected_kwargs': {
                    'ftp_mode': False,
                    'progress': placeholder_args['progress'],
                    'port': 555,
                    },
                'expected_stdout_output': "\n".join([
                    "D: FTP port: 555",
                    "D: Using active ftp",
                    ]),
                }),
            ('method-ftp commandline-passive', {
                'config_method': "ftp",
                'get_port_for_host_return_value': None,
                'test_args_extra': {
                    'commandline_passive': True,
                    },
                'expected_kwargs': {
                    'ftp_mode': True,
                    'progress': placeholder_args['progress'],
                    'port': 21,
                    },
                'expected_stdout_output': "\n".join([
                    "D: FTP port: 21",
                    "D: Using passive ftp",
                    ]),
                }),
            ('method-ftp config-passive', {
                'config_method': "ftp",
                'config_extras': {
                    'host': {
                        'passive_ftp': "True",
                        },
                    },
                'omit_kwargs': ['commandline_passive'],
                'get_port_for_host_return_value': None,
                'expected_kwargs': {
                    'ftp_mode': True,
                    'progress': placeholder_args['progress'],
                    'port': 21,
                    },
                'expected_stdout_output': "\n".join([
                    "D: FTP port: 21",
                    "D: Using passive ftp",
                    ]),
                }),
            ('method-ftp config-passive-false commandline-passive', {
                'config_method': "ftp",
                'config_extras': {
                    'host': {
                        'passive_ftp': "False",
                        },
                    },
                'test_args_extra': {
                    'commandline_passive': True,
                    },
                'get_port_for_host_return_value': None,
                'expected_kwargs': {
                    'ftp_mode': True,
                    'progress': placeholder_args['progress'],
                    'port': 21,
                    },
                'expected_stdout_output': "\n".join([
                    "D: FTP port: 21",
                    "D: Using passive ftp",
                    ]),
                }),
            ('method-ftp config-passive commandline-passive-false', {
                'config_method': "ftp",
                'config_extras': {
                    'host': {
                        'passive_ftp': "True",
                        },
                    },
                'test_args_extra': {
                    'commandline_passive': False,
                    },
                'get_port_for_host_return_value': None,
                'expected_kwargs': {
                    'ftp_mode': False,
                    'progress': placeholder_args['progress'],
                    'port': 21,
                    },
                'expected_stdout_output': "\n".join([
                    "D: FTP port: 21",
                    "D: Using active ftp",
                    ]),
                }),
            ]

    def set_test_args(self):
        """ Set the arguments for the test call to the function. """
        self.test_args = dict(
                (name, self.placeholder_args[name])
                for name in [
                    'fqdn', 'login', 'incoming', 'files_to_upload', 'debug',
                    'commandline_passive', 'progress']
                if name not in self.omit_kwargs)
        self.test_args.update({
                'method_name': self.test_method,
                'method': self.test_method_func,
                'config': self.runtime_config_parser,
                'host': self.test_host,
                })
        if hasattr(self, 'test_args_extra'):
            self.test_args.update(self.test_args_extra)

    def test_emits_expected_debug_message(self):
        """ Should emit expected debug message. """
        self.test_args['debug'] = True
        dput.dput.upload_files_via_method_ftp(**self.test_args)
        if hasattr(self, 'expected_stdout_output'):
            self.assertIn(self.expected_stdout_output, sys.stdout.getvalue())

    def test_calls_upload_method_with_expected_args(self):
        """ Should call upload method function with expected args. """
        dput.dput.upload_files_via_method_ftp(**self.test_args)
        self.expected_method_func.assert_called_with(
                *self.expected_args, **self.expected_kwargs)


class upload_files_via_method_scp_TestCase(
        testscenarios.WithScenarios,
        upload_files_via_BaseTestCase):
    """ Test cases for `upload_files_via_method_scp` function. """

    placeholder_args = dict(upload_files_via_BaseTestCase.placeholder_args)
    placeholder_args.update({
            'files_to_upload': object(),
            'debug': False,
            'compress': False,
            'ssh_config_options': None,
            })

    scenarios = [
            ('method-scp', {
                'config_method': "scp",
                'expected_kwargs': {
                    'compress': placeholder_args['compress'],
                    'ssh_config_options': [],
                    },
                'expected_stdout_output': "\n".join([
                    "D: ssh config options:",
                    "  ",
                    ]),
                }),
            ('method-scp default-compress', {
                'config_method': "scp",
                'omit_kwargs': ['compress'],
                'expected_kwargs': {
                    'compress': False,
                    'ssh_config_options': [],
                    },
                'expected_stdout_output': "\n".join([
                    "D: ssh config options:",
                    "  ",
                    ]),
                }),
            ('method-scp default-ssh-options', {
                'config_method': "scp",
                'omit_kwargs': ['ssh_config_options'],
                'expected_kwargs': {
                    'compress': placeholder_args['compress'],
                    'ssh_config_options': [],
                    },
                'expected_stdout_output': "\n".join([
                    "D: ssh config options:",
                    "  ",
                    ]),
                }),
            ('method-scp scp-compress', {
                'config_method': "scp",
                'test_args_extra': {
                    'compress': True,
                    },
                'expected_kwargs': {
                    'compress': True,
                    'ssh_config_options': [],
                    },
                'expected_stdout_output': "\n".join([
                    "D: Setting compression for scp",
                    "D: ssh config options:",
                    "  ",
                    ]),
                }),
            ('method-scp ssh-options-one', {
                'config_method': "scp",
                'test_args_extra': {
                    'compress': placeholder_args['compress'],
                    'ssh_config_options': ["lorem ipsum"],
                    },
                'expected_kwargs': {
                    'compress': placeholder_args['compress'],
                    'ssh_config_options': ["lorem ipsum"],
                    },
                'expected_stdout_output': "\n".join([
                    "D: ssh config options:",
                    "  ",
                    "  lorem ipsum",
                    "  ",
                    ]),
                }),
            ('method-scp ssh-options-three', {
                'config_method': "scp",
                'test_args_extra': {
                    'compress': placeholder_args['compress'],
                    'ssh_config_options': ["spam", "eggs", "beans"],
                    },
                'expected_kwargs': {
                    'compress': placeholder_args['compress'],
                    'ssh_config_options': ["spam", "eggs", "beans"],
                    },
                'expected_stdout_output': "\n".join([
                    "D: ssh config options:",
                    "  ",
                    "  spam",
                    "  eggs",
                    "  beans",
                    "  ",
                    ]),
                }),
            ]

    def set_test_args(self):
        """ Set the arguments for the test call to the function. """
        self.test_args = dict(
                (name, self.placeholder_args[name])
                for name in [
                    'fqdn', 'login', 'incoming', 'files_to_upload', 'debug',
                    'compress', 'ssh_config_options']
                if name not in self.omit_kwargs)
        self.test_args.update({
                'method_name': self.test_method,
                'method': self.test_method_func,
                'config': self.runtime_config_parser,
                'host': self.test_host,
                })
        if hasattr(self, 'test_args_extra'):
            self.test_args.update(self.test_args_extra)

    def test_emits_expected_debug_message(self):
        """ Should emit expected debug message. """
        self.test_args['debug'] = True
        dput.dput.upload_files_via_method_scp(**self.test_args)
        if hasattr(self, 'expected_stdout_output'):
            self.assertIn(self.expected_stdout_output, sys.stdout.getvalue())

    def test_calls_upload_method_with_expected_args(self):
        """ Should call upload method function with expected args. """
        dput.dput.upload_files_via_method_scp(**self.test_args)
        self.expected_method_func.assert_called_with(
                *self.expected_args, **self.expected_kwargs)


class upload_files_TestCase(
        testscenarios.WithScenarios,
        testtools.TestCase):
    """ Test cases for `upload_files` function. """

    files_scenarios = [
            ('files-none', {
                'files_to_upload': [],
                }),
            ('files-one', {
                'files_to_upload': [tempfile.mktemp()],
                }),
            ('files-three', {
                'files_to_upload': [tempfile.mktemp() for __ in range(3)],
                }),
            ]

    method_scenarios = [
            ('method-http', {
                'method_name': "http",
                'config_method': "http",
                'get_progress_indicator_for_host_return_value': 23,
                'expected_func_name': 'upload_files_via_method',
                'expected_kwargs_extra': {
                    'dummy': 0,
                    'progress': 23,
                    },
                }),
            ('method-http', {
                'method_name': "https",
                'config_method': "https",
                'get_progress_indicator_for_host_return_value': 23,
                'expected_func_name': 'upload_files_via_method',
                'expected_kwargs_extra': {
                    'dummy': 0,
                    'progress': 23,
                    },
                }),
            ('method-http', {
                'method_name': "rsync",
                'config_method': "rsync",
                'get_progress_indicator_for_host_return_value': 23,
                'expected_func_name': 'upload_files_via_method',
                'expected_kwargs_extra': {
                    'dummy': 0,
                    'progress': 23,
                    },
                }),
            ('method-local', {
                'method_name': "local",
                'config_method': "local",
                'get_progress_indicator_for_host_return_value': 23,
                'expected_func_name': 'upload_files_via_method_local',
                'expected_kwargs_extra': {
                    'compress': 0,
                    'progress': 23,
                    },
                }),
            ('method-ftp', {
                'method_name': "ftp",
                'config_method': "ftp",
                'get_progress_indicator_for_host_return_value': 23,
                'test_kwargs_extra': {
                    'commandline_passive': True,
                    },
                'expected_func_name': 'upload_files_via_method_ftp',
                'expected_kwargs_extra': {
                    'commandline_passive': True,
                    'progress': 23,
                    },
                }),
            ('method-scp ssh-config-options-none', {
                'method_name': "scp",
                'config_method': "scp",
                'config_extras': {
                    'host': {
                        'scp_compress': "True",
                        'ssh_config_options': "",
                        },
                    },
                'get_progress_indicator_for_host_return_value': 23,
                'expected_func_name': 'upload_files_via_method_scp',
                'expected_kwargs_extra': {
                    'compress': True,
                    'ssh_config_options': [],
                    },
                }),
            ('method-scp ssh-config-options-three', {
                'method_name': "scp",
                'config_method': "scp",
                'config_extras': {
                    'host': {
                        'scp_compress': "True",
                        'ssh_config_options': "spam\n\teggs\n    beans\n",
                        },
                    },
                'get_progress_indicator_for_host_return_value': 23,
                'expected_func_name': 'upload_files_via_method_scp',
                'expected_kwargs_extra': {
                    'compress': True,
                    'ssh_config_options': ["spam", "eggs", "beans"],
                    },
                }),
            ]

    delayed_scenarios = [
            ('delayed-none', {
                'delayed_queue_path': "",
                'expected_queue_annotation': None,
                }),
            ('delayed-arbitrary', {
                'delayed_queue_path': "delayed-path",
                'expected_queue_annotation': "[delayed-path]",
                }),
            ]

    scenarios = testscenarios.multiply_scenarios(
            files_scenarios, method_scenarios, delayed_scenarios)

    for (scenario_name, scenario) in scenarios:
        incoming_root = tempfile.mktemp()
        scenario['get_incoming_for_host_return_value'] = incoming_root
        expected_incoming = incoming_root
        if scenario['delayed_queue_path'] is not None:
            expected_incoming = os.path.join(
                    incoming_root,
                    scenario['delayed_queue_path'])
        scenario['expected_incoming'] = expected_incoming
        del incoming_root, expected_incoming
    del scenario_name, scenario

    def setUp(self):
        """ Set up test fixtures. """
        super().setUp()
        patch_system_interfaces(self)

        set_config(self, 'exist-simple')
        patch_runtime_config_options(self)

        self.set_files_to_upload()
        set_upload_methods(self)

        self.set_test_args()

        patch_determine_delay_days(self)
        patch_make_delayed_queue_path(self)
        patch_get_login_for_host(self)
        self.expected_login = dput.dput.get_login_for_host.return_value
        patch_get_fqdn_for_host(self)
        self.expected_fqdn = dput.dput.get_fqdn_for_host.return_value

        patch_get_incoming_for_host(self)
        self.expected_incoming = dput.dput.get_incoming_for_host.return_value
        if dput.dput.make_delayed_queue_path.return_value:
            self.expected_incoming = os.path.join(
                    dput.dput.get_incoming_for_host.return_value,
                    dput.dput.make_delayed_queue_path.return_value)

        patch_get_progress_indicator_for_host(self)
        patch_upload_files_via(self)

    def set_files_to_upload(self):
        """ Set the `files_to_upload` collection for this instance. """
        if not hasattr(self, 'files_to_upload'):
            self.files_to_upload = []

    def set_test_args(self):
        """ Set the arguments for the test call to the function. """
        self.test_args = dict(
                upload_methods=self.upload_methods,
                config=self.runtime_config_parser,
                host=self.test_host,
                files_to_upload=self.files_to_upload,
                debug=False,
                )
        if hasattr(self, 'test_kwargs_extra'):
            self.test_args.update(self.test_kwargs_extra)
        if hasattr(self, 'test_simulate'):
            self.test_args['simulate'] = self.test_simulate

    def test_emits_expected_queue_message(self):
        """ Should emit expected message for the upload queue. """
        dput.dput.upload_files(**self.test_args)
        if self.expected_queue_annotation is not None:
            expected_destination_template = "{host} {annotation}"
        else:
            expected_destination_template = "{host}"
        expected_destination = expected_destination_template.format(
                host=self.test_host,
                annotation=self.expected_queue_annotation)
        expected_method_name = self.method_name
        expected_output = (
                "Uploading to {destination}"
                " (via {method} to {fqdn})"
                ).format(
                    destination=expected_destination,
                    method=expected_method_name, fqdn=self.expected_fqdn)
        self.assertIn(expected_output, sys.stdout.getvalue())

    def test_emits_expected_diagnostic_message(self):
        """ Should emit expected diagnostic debug message. """
        self.test_args['debug'] = True
        dput.dput.upload_files(**self.test_args)
        self.expected_fqdn = dput.dput.get_fqdn_for_host.return_value
        self.expected_login = dput.dput.get_login_for_host.return_value
        expected_output = textwrap.dedent("""\
                D: FQDN: {fqdn}
                D: Login: {login}
                D: Incoming: {incoming}
                """).format(
                    fqdn=self.expected_fqdn,
                    login=self.expected_login,
                    incoming=self.expected_incoming)
        self.assertIn(expected_output, sys.stdout.getvalue())

    def test_calls_expected_upload_function_with_expected_args(self):
        """ Should call expected upload function with expected args. """
        dput.dput.upload_files(**self.test_args)
        expected_func = getattr(dput.dput, self.expected_func_name)
        expected_method_func = self.upload_methods[self.method_name]
        expected_args_list = [
                self.method_name,
                expected_method_func,
                self.runtime_config_parser,
                self.test_host,
                self.expected_fqdn,
                self.expected_login,
                self.expected_incoming,
                self.files_to_upload,
                self.test_args['debug'],
                ]
        self.expected_args = tuple(expected_args_list)
        self.expected_kwargs = {}
        if hasattr(self, 'expected_kwargs_extra'):
            self.expected_kwargs.update(self.expected_kwargs_extra)
        expected_func.assert_called_with(
                *self.expected_args, **self.expected_kwargs)

    def test_omits_upload_method_function_when_simulate(self):
        """ Should omit call to upload method function when ‘simulate’. """
        self.test_args['simulate'] = True
        dput.dput.upload_files(**self.test_args)
        expected_func = getattr(dput.dput, self.expected_func_name)
        self.assertFalse(
                expected_func.called,
                "should not call {func}".format(func=expected_func))


def patch_upload_files(testcase):
    """ Patch the `upload_files` function for `testcase`. """
    func_patcher = unittest.mock.patch.object(
            dput.dput, "upload_files", autospec=True)
    func_patcher.start()
    testcase.addCleanup(func_patcher.stop)


class parse_commandline_BaseTestCase(
        testtools.TestCase):
    """ Base class for test cases for function `parse_commandline`. """

    function_to_test = staticmethod(dput.dput.parse_commandline)

    progname = "lorem"
    dput_version = "ipsum"
    dput_usage_message = "Lorem ipsum, dolor sit amet."

    def setUp(self):
        """ Set up test fixtures. """
        super().setUp()
        patch_system_interfaces(self)

        setup_changes_file_fixtures(self)
        set_changes_file_scenario(self, 'exist-minimal')
        self.set_files_to_upload()

        self.patch_make_usage_message()
        patch_distribution(self)
        patch_get_login_for_host(self)
        patch_verify_config_upload_methods(self)

        self.options_defaults = dict(self.function_to_test.options_defaults)
        self.patch_getopt()

        if not hasattr(self, 'test_argv'):
            self.test_argv = self.make_test_argv()
        self.test_args = dict(
                argv=self.test_argv,
                )
        if hasattr(self, 'expected_options'):
            self.set_expected_options_mapping()
            self.set_expected_args()

    def set_files_to_upload(self):
        """ Set the `files_to_upload` collection for this test case. """
        if not hasattr(self, 'files_to_upload'):
            self.files_to_upload = [self.changes_file_double]

    def make_test_argv(self):
        """ Make the `argv` value for this test case. """
        test_argv = [self.progname] + [
                fake_file.path for fake_file in self.files_to_upload]
        return test_argv

    def patch_make_usage_message(self):
        """ Patch the `make_usage_message` function. """
        if not hasattr(self, 'dput_usage_message'):
            self.dput_usage_message = self.getUniqueString()
        func_patcher = unittest.mock.patch.object(
                dput.dput, "make_usage_message", autospec=True,
                return_value=self.dput_usage_message)
        func_patcher.start()
        self.addCleanup(func_patcher.stop)

    def set_expected_args(self):
        """ Set the `expected_args` collection for this test case. """
        if not hasattr(self, 'expected_args'):
            self.expected_args = self.getopt_args

    def set_expected_options_mapping(self):
        """ Set the `expected_options_mapping` for this test case. """
        if not hasattr(self, 'expected_options_mapping'):
            self.expected_options_mapping = self.options_defaults.copy()
            self.expected_options_mapping.update(self.expected_options)

    def patch_getopt(self):
        """ Patch the `dputhelper.getopt` function. """
        if not hasattr(self, 'getopt_opts'):
            self.getopt_opts = []
        if not hasattr(self, 'getopt_args'):
            self.getopt_args = [
                    fake_file.path for fake_file in self.files_to_upload]

        patch_getopt(self)


class parse_commandline_TestCase(
        testscenarios.WithScenarios,
        parse_commandline_BaseTestCase):
    """ Test cases for function `parse_commandline`. """

    scenarios = [
            ('host-omitted files-one options-none', {
                'test_argv': ["commandname"] + [
                    make_changes_file_path(tempfile.mktemp())],
                'expected_options': {},
                }),
            ('host-omitted files-three options-none', {
                'test_argv': ["commandname"] + [
                    make_changes_file_path(tempfile.mktemp())
                    for __ in range(3)],
                'expected_options': {},
                }),
            ('host-named files-one options-none', {
                'test_argv': ["commandname"] + ["lorem"] + [
                    make_changes_file_path(tempfile.mktemp())],
                'expected_options': {},
                }),
            ('host-named files-three options-none', {
                'test_argv': ["commandname"] + ["lorem"] + [
                    make_changes_file_path(tempfile.mktemp())
                    for __ in range(3)],
                'expected_options': {},
                }),
            ]

    def test_calls_getopt_with_expected_args(self):
        """ Should call `getopt` with expected arguments. """
        self.function_to_test(**self.test_args)
        dputhelper.getopt.assert_called_with(
                self.test_argv[1:],
                unittest.mock.ANY, unittest.mock.ANY)

    def test_returns_expected_values(self):
        """ Should return expected 2-tuple of (`options`, `args`). """
        (options, args) = self.function_to_test(**self.test_args)
        options_mapping = {
                name: getattr(options, name)
                for name in dir(options)
                if not name.startswith("_")}
        self.assertEqual(self.expected_options_mapping, options_mapping)
        self.assertEqual(self.expected_args, args)

    @unittest.mock.patch.object(dput.dput, 'debug', new=object())
    def test_sets_debug_flag_when_debug_option(self):
        """ Should set `debug` when ‘--debug’ option. """
        self.getopt_opts = [("--debug", None)]
        self.function_to_test(**self.test_args)
        self.assertEqual(dput.dput.debug, True)


class parse_commandline_ValidOptionsTestCase(
        testscenarios.WithScenarios,
        parse_commandline_BaseTestCase):
    """ Test cases for function `parse_commandline`, valid options. """

    args_scenarios = [
            ('host-omitted files-one', {
                'test_argv_command': "commandname",
                'test_argv_args': [
                    make_changes_file_path(tempfile.mktemp())],
                }),
            ('host-omitted files-three', {
                'test_argv_command': "commandname",
                'test_argv_args': [
                    make_changes_file_path(tempfile.mktemp())
                    for __ in range(3)],
                }),
            ('host-named files-one', {
                'test_argv_command': "commandname",
                'test_argv_args': ["lorem"] + [
                    make_changes_file_path(tempfile.mktemp())],
                }),
            ('host-named files-three', {
                'test_argv_command': "commandname",
                'test_argv_args': ["lorem"] + [
                    make_changes_file_path(tempfile.mktemp())
                    for __ in range(3)],
                }),
            ]

    options_scenarios = [
            ('options-debug-short', {
                'test_argv_options': ["-d"],
                'expected_options': {
                    'debug': True,
                    },
                }),
            ('options-debug-long', {
                'test_argv_options': ["--debug"],
                'expected_options': {
                    'debug': True,
                    },
                }),
            ('options-dinstall-short', {
                'test_argv_options': ["-D"],
                'expected_options': {
                    'run_dinstall': True,
                    },
                }),
            ('options-dinstall-long', {
                'test_argv_options': ["--dinstall"],
                'expected_options': {
                    'run_dinstall': True,
                    },
                }),
            ('options-config-short', {
                'test_argv_options': ["-c", "/tmp/nonexistent"],
                'expected_options': {
                    'config_file_path': "/tmp/nonexistent",
                    },
                }),
            ('options-config-long', {
                'test_argv_options': ["--config", "/tmp/nonexistent"],
                'expected_options': {
                    'config_file_path': "/tmp/nonexistent",
                    },
                }),
            ('options-force-short', {
                'test_argv_options': ["-f"],
                'expected_options': {
                    'force_upload': True,
                    },
                }),
            ('options-force-long', {
                'test_argv_options': ["--force"],
                'expected_options': {
                    'force_upload': True,
                    },
                }),
            ('options-host-list-short', {
                'test_argv_options': ["-H"],
                'expected_options': {
                    'config_host_list': True,
                    },
                }),
            ('options-host-list-long', {
                'test_argv_options': ["--host-list"],
                'expected_options': {
                    'config_host_list': True,
                    },
                }),
            ('options-lintian-short', {
                'test_argv_options': ["-l"],
                'expected_options': {
                    'run_lintian': True,
                    },
                }),
            ('options-lintian-long', {
                'test_argv_options': ["--lintian"],
                'expected_options': {
                    'run_lintian': True,
                    },
                }),
            ('options-no-upload-log-short', {
                'test_argv_options': ["-U"],
                'expected_options': {
                    'upload_log': False,
                    },
                }),
            ('options-no-upload-log-long', {
                'test_argv_options': ["--no-upload-log"],
                'expected_options': {
                    'upload_log': False,
                    },
                }),
            ('options-check-only-short', {
                'test_argv_options': ["-o"],
                'expected_options': {
                    'check_only': True,
                    },
                }),
            ('options-check-only-long', {
                'test_argv_options': ["--check-only"],
                'expected_options': {
                    'check_only': True,
                    },
                }),
            ('options-print-short', {
                'test_argv_options': ["-p"],
                'expected_options': {
                    'config_print': True,
                    },
                }),
            ('options-print-long', {
                'test_argv_options': ["--print"],
                'expected_options': {
                    'config_print': True,
                    },
                }),
            ('options-passive-short', {
                'test_argv_options': ["-P"],
                'expected_options': {
                    'commandline_passive': True,
                    },
                }),
            ('options-passive-long', {
                'test_argv_options': ["--passive"],
                'expected_options': {
                    'commandline_passive': True,
                    },
                }),
            ('options-simulate-short', {
                'test_argv_options': ["-s"],
                'expected_options': {
                    'simulate': True,
                    },
                }),
            ('options-simulate-long', {
                'test_argv_options': ["--simulate"],
                'expected_options': {
                    'simulate': True,
                    },
                }),
            ('options-unchecked-short', {
                'test_argv_options': ["-u"],
                'expected_options': {
                    'allow_unsigned_uploads': True,
                    },
                }),
            ('options-unchecked-long', {
                'test_argv_options': ["--unchecked"],
                'expected_options': {
                    'allow_unsigned_uploads': True,
                    },
                }),
            ('options-delayed-short', {
                'test_argv_options': ["-e", "7"],
                'expected_options': {
                    'delay_days_text': "7",
                    },
                }),
            ('options-delayed-long', {
                'test_argv_options': ["--delayed", "7"],
                'expected_options': {
                    'delay_days_text': "7",
                    },
                }),
            ('options-check-version-short', {
                'test_argv_options': ["-V"],
                'expected_options': {
                    'check_version': True,
                    },
                }),
            ('options-check-version-long', {
                'test_argv_options': ["--check-version"],
                'expected_options': {
                    'check_version': True,
                    },
                }),
            ('options-check-version-no-upload-log-lintian-long', {
                'test_argv_options': [
                    "--check-version",
                    "--no-upload-log",
                    "--lintian",
                    ],
                'getopt_opts': [
                    ("--check-version", None),
                    ("--no-upload-log", None),
                    ("--lintian", None),
                    ],
                'expected_options': {
                    'check_version': True,
                    'run_lintian': True,
                    'upload_log': False,
                    },
                }),
            # Legacy mis-spelled option name.
            ('options-check_version-long-legacy', {
                'test_argv_options': ["--check_version"],
                'getopt_opts': [
                    # The parameters to `getopt` are such that the parsed
                    # options will never contain the option spelled this way.
                    # This scenario is designed only to exercise the legacy
                    # code.
                    ("--check_version", None),
                    ],
                'expected_options': {
                    'check_version': True,
                    },
                }),
            ('options-check_version-no-upload-log-lintian-long', {
                'test_argv_options': [
                    "--check_version",
                    "--no-upload-log",
                    "--lintian",
                    ],
                'getopt_opts': [
                    # The parameters to `getopt` are such that the parsed
                    # options will never contain the option spelled this way.
                    # This scenario is designed only to exercise the legacy
                    # code.
                    ("--check_version", None),
                    ("--no-upload-log", None),
                    ("--lintian", None),
                    ],
                'expected_options': {
                    'check_version': True,
                    'run_lintian': True,
                    'upload_log': False,
                    },
                }),
            ]

    scenarios = testscenarios.multiply_scenarios(
            args_scenarios, options_scenarios)
    for (scenario_name, scenario) in scenarios:
        scenario['test_argv'] = [scenario['test_argv_command']]
        scenario['test_argv'].extend(scenario['test_argv_options'])
        scenario['test_argv'].extend(scenario['test_argv_args'])
        scenario['expected_arguments'] = scenario['test_argv_args']
        scenario['getopt_args'] = scenario['test_argv_args']
        if 'getopt_opts' not in scenario:
            fake_parsed_option = scenario['test_argv_options'][0]
            fake_parsed_option_value = (
                    scenario['test_argv_options'][1] if len(
                        scenario['test_argv_options']) > 1
                    else None)
            scenario['getopt_opts'] = [
                    (fake_parsed_option, fake_parsed_option_value),
                    ]
    del scenario_name, scenario

    def test_calls_getopt_with_expected_args(self):
        """ Should call `getopt` with expected arguments. """
        self.function_to_test(**self.test_args)
        dputhelper.getopt.assert_called_with(
                self.test_argv[1:],
                unittest.mock.ANY, unittest.mock.ANY)

    def test_returns_expected_values(self):
        """ Should return expected 2-tuple of (`options`, `args`). """
        (options, args) = self.function_to_test(**self.test_args)
        options_mapping = {
                name: getattr(options, name)
                for name in dir(options)
                if not name.startswith("_")}
        self.assertEqual(self.expected_options_mapping, options_mapping)
        self.assertEqual(self.expected_args, args)


class parse_commandline_ErrorTestCase(parse_commandline_BaseTestCase):
    """ Test cases for `parse_commandline`, error conditions. """

    def test_calls_sys_exit_when_no_nonoption_args(self):
        """ Should call `sys.exit` when no non-option args. """
        self.getopt_args = []
        for option in [
                "--debug", "--force", "--simulate", "--lintian"]:
            self.getopt_opts = [(option, None)]
            with self.subTest(test_opts=self.getopt_opts):
                with testtools.ExpectedException(FakeSystemExit):
                    self.function_to_test(**self.test_args)
                sys.exit.assert_called_with(os.EX_USAGE)

    def test_calls_sys_exit_when_unknown_option(self):
        """ Should call `sys.exit` when an unknown option. """
        self.getopt_args.insert(1, "--b0gUs")
        dputhelper.getopt.side_effect = dputhelper.DputException
        with testtools.ExpectedException(FakeSystemExit):
            self.function_to_test(**self.test_args)
        sys.exit.assert_called_with(os.EX_USAGE)


class parse_commandline_PrintAndEndTestCase(parse_commandline_BaseTestCase):
    """ Test cases for `parse_commandline` that print and end. """

    def test_emits_usage_message_when_option_help(self):
        """ Should emit usage message when ‘--help’ option. """
        self.getopt_opts = [("--help", None)]
        with contextlib.suppress(FakeSystemExit):
            self.function_to_test(**self.test_args)
        expected_output = self.dput_usage_message
        self.assertIn(expected_output, sys.stdout.getvalue())

    def test_calls_sys_exit_when_option_help(self):
        """ Should call `sys.exit` when ‘--help’ option. """
        self.getopt_opts = [("--help", None)]
        with testtools.ExpectedException(FakeSystemExit):
            self.function_to_test(**self.test_args)
        sys.exit.assert_called_with(EXIT_STATUS_SUCCESS)

    def test_emits_version_message_when_option_version(self):
        """ Should emit version message when ‘--version’ option. """
        self.getopt_opts = [("--version", None)]
        with contextlib.suppress(FakeSystemExit):
            self.function_to_test(**self.test_args)
        expected_progname = self.progname
        expected_version = self.fake_distribution.version
        expected_output = "{progname} {version}".format(
                progname=expected_progname, version=expected_version)
        self.assertIn(expected_output, sys.stdout.getvalue())

    def test_calls_sys_exit_when_option_version(self):
        """ Should call `sys.exit` when ‘--version’ option. """
        self.getopt_opts = [("--version", None)]
        with testtools.ExpectedException(FakeSystemExit):
            self.function_to_test(**self.test_args)
        sys.exit.assert_called_with(EXIT_STATUS_SUCCESS)

    def test_returns_options_normally_when_option_print_config(self):
        """ Should set ‘config_print’ when option ‘--print’ is present. """
        self.getopt_opts = [("--print", None)]
        self.getopt_args = []
        (options, args) = self.function_to_test(**self.test_args)
        self.assertEqual(True, options.config_print)

    def test_returns_options_normally_when_option_host_list(self):
        """ Should set ‘config_host_list’ when option ‘--host-list’. """
        self.getopt_opts = [("--host-list", None)]
        self.getopt_args = []
        (options, args) = self.function_to_test(**self.test_args)
        self.assertEqual(True, options.config_host_list)


class determine_delay_days_TestCase(
        testscenarios.WithScenarios,
        testtools.TestCase):
    """ Test cases for `determine_delay_days` function. """

    function_to_test = staticmethod(dput.dput.determine_delay_days)

    commandline_scenarios = [
            ('commandline-unspecified', {}),
            ('commandline-specified-empty-string', {
                'test_commandline_text_value': "",
                'expected_exception': TypeError,
                }),
            ('commandline-specified-non-numeric-string', {
                'test_commandline_text_value': "DEADB33F",
                'expected_exception': TypeError,
                }),
            ('commandline-specified-numerals', {
                'test_commandline_text_value': "3",
                'expected_result': 3,
                }),
            ('commandline-specified-numerals-out-of-range', {
                'test_commandline_text_value': "16",
                'expected_exception': ValueError,
                }),
            ('commandline-specified-negative-number', {
                'test_commandline_text_value': "-3",
                'expected_exception': ValueError,
                }),
            ]

    config_scenarios = [
            ('config-unspecified', {
                'config_delayed': None,
                }),
            ('config-specified-empty-string', {
                'config_delayed': "",
                'expected_value_if_host_config_used': None,
                }),
            ('config-specified-numerals', {
                'config_delayed': "9",
                'expected_value_if_host_config_used': 9,
                }),
            ]

    config_default_scenarios = [
            ('config-default-unspecified', {
                'config_default_delayed': None,
                'expected_value_if_config_default_used': None,
                }),
            ('config-default-specified-empty-string', {
                'config_default_delayed': "",
                'expected_value_if_config_default_used': None,
                }),
            ('config-default-specified-numerals', {
                'config_default_delayed': "9",
                'expected_value_if_config_default_used': 9,
                }),
            ]

    scenarios = testscenarios.multiply_scenarios(
            commandline_scenarios, config_scenarios, config_default_scenarios)

    def setUp(self):
        """ Set up test fixtures. """
        super().setUp()

        set_config(self, 'exist-simple')
        patch_runtime_config_options(self)

        self.set_test_args()
        self.set_expected_result()

    def set_test_args(self):
        """ Set the ‘test_args’ value for this test case. """
        test_options = types.SimpleNamespace()
        if hasattr(self, 'test_commandline_text_value'):
            test_options.delay_days_text = (
                    self.test_commandline_text_value)
        test_config_params = self.config_scenario['configs_by_name']['runtime']
        test_config_parser = test_config_params['config']
        self.test_args = {
                'options': test_options,
                'host_config': test_config_parser[self.test_host],
                }

    def set_expected_result(self):
        """ Set the ‘expected_result’ value for this test case. """
        if not hasattr(self, 'expected_result'):
            if not hasattr(self, 'expected_value_if_host_config_used'):
                if not hasattr(self, 'expected_value_if_config_default_used'):
                    self.expected_result = None
                else:
                    self.expected_result = (
                            self.expected_value_if_config_default_used)
            else:
                self.expected_result = self.expected_value_if_host_config_used

    def test_returns_expected_result(self):
        """ Should give expected result for inputs. """
        if hasattr(self, 'expected_exception'):
            with testtools.ExpectedException(self.expected_exception):
                self.function_to_test(**self.test_args)
        else:
            result = self.function_to_test(**self.test_args)
            self.assertEqual(self.expected_result, result)


def patch_determine_delay_days(testcase):
    """ Patch the `determine_delay_days` function for the `testcase`. """
    if not hasattr(testcase, 'determine_delay_days_return_value'):
        testcase.determine_delay_days_return_value = None
    func_patcher = unittest.mock.patch.object(
            dput.dput, "determine_delay_days", autospec=True,
            return_value=testcase.determine_delay_days_return_value)
    func_patcher.start()
    testcase.addCleanup(func_patcher.stop)


class main_TestCase(
        testtools.TestCase):
    """ Base for test cases for `main` function. """

    function_to_test = staticmethod(dput.dput.main)

    def setUp(self):
        """ Set up test fixtures. """
        super().setUp()
        patch_system_interfaces(self)

        set_config(
                self,
                getattr(self, 'config_scenario_name', 'exist-simple'))
        patch_runtime_config_options(self)

        setup_changes_file_fixtures(self)
        set_changes_file_scenario(self, 'exist-minimal')

        patch_os_isatty(self)
        patch_os_environ(self)
        patch_os_getuid(self)
        patch_pwd_getpwuid(self)
        patch_sys_argv(self)

        self.set_files_to_upload()
        set_upload_methods(self)

        self.set_test_args()

        self.patch_make_usage_message()
        patch_distribution(self)

        self.options_defaults = dict(
                dput.dput.parse_commandline.options_defaults)
        self.patch_parse_commandline()
        patch_active_config_files(self)
        patch_read_configs(self)
        patch_print_config(self)
        patch_get_username_from_system(self)
        patch_get_login_for_host(self)
        patch_import_upload_functions(self)
        patch_get_upload_method_name_for_host(self)
        patch_make_delayed_queue_path(self)
        patch_upload_files(self)
        patch_get_progress_indicator_for_host(self)
        patch_get_fqdn_for_host(self)
        patch_get_incoming_for_host(self)
        patch_determine_delay_days(self)
        patch_print_default_upload_method(self)
        patch_print_host_list(self)
        self.patch_guess_upload_host()
        self.patch_check_upload_logfile()
        self.patch_verify_files()
        self.patch_run_lintian_test()
        self.patch_execute_command()
        self.patch_create_upload_file()
        self.patch_dinstall_caller()

    def set_files_to_upload(self):
        """ Set the `files_to_upload` collection for this instance. """
        if not hasattr(self, 'files_to_upload'):
            self.files_to_upload = []

    def set_test_args(self):
        """ Set the arguments for the test call to the function. """
        self.test_args = dict()

    def patch_make_usage_message(self):
        """ Patch the `make_usage_message` function. """
        if not hasattr(self, 'dput_usage_message'):
            self.dput_usage_message = self.getUniqueString()
        func_patcher = unittest.mock.patch.object(
                dput.dput, "make_usage_message", autospec=True,
                return_value=self.dput_usage_message)
        func_patcher.start()
        self.addCleanup(func_patcher.stop)

    def set_fake_options(self, options_mapping=None):
        """ Set the `fake_options` object, updated with `options_mapping`. """
        if options_mapping is None:
            options_mapping = self.fake_options_mapping
        full_options_mapping = dict(itertools.chain(
                self.options_defaults.items(),
                options_mapping.items(),
                ))
        self.fake_options = make_options_namespace(full_options_mapping)

    def patch_parse_commandline(self):
        """ Patch the `parse_commandline` function. """
        if not hasattr(self, 'fake_options_mapping'):
            self.fake_options_mapping = {}
        self.set_fake_options(self.fake_options_mapping)
        if not hasattr(self, 'fake_args'):
            self.fake_args = [self.changes_file_double.path]

        def fake_parse_commandline(*args, **kwargs):
            return (self.fake_options, self.fake_args)

        func_patcher = unittest.mock.patch.object(
                dput.dput, "parse_commandline",
                side_effect=fake_parse_commandline)
        func_patcher.start()
        self.addCleanup(func_patcher.stop)

    def patch_guess_upload_host(self):
        """ Patch the `guess_upload_host` function. """
        if not hasattr(self, 'guess_upload_host_return_value'):
            self.guess_upload_host_return_value = self.test_host
        func_patcher = unittest.mock.patch.object(
                dput.dput, "guess_upload_host", autospec=True,
                return_value=self.guess_upload_host_return_value)
        func_patcher.start()
        self.addCleanup(func_patcher.stop)

    def patch_check_upload_logfile(self):
        """ Patch the `check_upload_logfile` function. """
        func_patcher = unittest.mock.patch.object(
                dput.dput, "check_upload_logfile", autospec=True)
        func_patcher.start()
        self.addCleanup(func_patcher.stop)

    def patch_verify_files(self):
        """ Patch the `verify_files` function. """
        func_patcher = unittest.mock.patch.object(
                dput.dput, "verify_files", autospec=True,
                return_value=self.files_to_upload)
        func_patcher.start()
        self.addCleanup(func_patcher.stop)

    def patch_run_lintian_test(self):
        """ Patch the `run_lintian_test` function. """
        func_patcher = unittest.mock.patch.object(
                dput.dput, "run_lintian_test", autospec=True)
        func_patcher.start()
        self.addCleanup(func_patcher.stop)

    def patch_execute_command(self):
        """ Patch the `execute_command` function. """
        func_patcher = unittest.mock.patch.object(
                dput.dput, "execute_command", autospec=True)
        func_patcher.start()
        self.addCleanup(func_patcher.stop)

    def patch_create_upload_file(self):
        """ Patch the `create_upload_file` function. """
        func_patcher = unittest.mock.patch.object(
                dput.dput, "create_upload_file", autospec=True)
        func_patcher.start()
        self.addCleanup(func_patcher.stop)

    def patch_dinstall_caller(self):
        """ Patch the `dinstall_caller` function. """
        func_patcher = unittest.mock.patch.object(
                dput.dput, "dinstall_caller", autospec=True)
        func_patcher.start()
        self.addCleanup(func_patcher.stop)


class main_CommandLineProcessingTestCase(main_TestCase):
    """ Test cases for `main` command-line processing. """

    sys_argv = ["lorem"] + [
            make_changes_file_path(tempfile.mktemp())
            for __ in range(3)]

    def test_calls_parse_commandline_with_expected_args(self):
        """ Should call `parse_commandline` with expected arguments. """
        self.function_to_test(**self.test_args)
        dput.dput.parse_commandline.assert_called_with(self.sys_argv)

    def test_returns_exit_status_when_systemexit(self):
        """ Should return exit status from `SystemExit` exception. """
        exit_status = self.getUniqueInteger()
        dput.dput.parse_commandline.side_effect = SystemExit(exit_status)
        result = self.function_to_test(**self.test_args)
        self.assertEqual(result, exit_status)


class main_CommandLineProcessing_ErrorTestCase(main_TestCase):
    """ Test cases for `main` command-line processing, error conditions. """

    fake_args = [
            make_changes_file_path(tempfile.mktemp())
            for __ in range(3)]

    def test_emits_error_message_when_filename_not_match_glob(self):
        """ Should emit an error message when filename does not match glob. """
        self.fake_args[2] = tempfile.mktemp()
        self.patch_parse_commandline()
        with contextlib.suppress(FakeSystemExit):
            self.function_to_test(**self.test_args)
        self.expected_stderr_output = (
                "Filename does not match ‘*.changes’: {}".format(
                    self.fake_args[2]))
        self.assertIn(self.expected_stderr_output, sys.stderr.getvalue())

    def test_calls_sys_exit_when_filename_not_match_glob(self):
        """ Should call `sys.exit` when filename does not match glob. """
        self.fake_args[2] = tempfile.mktemp()
        self.patch_parse_commandline()
        with testtools.ExpectedException(FakeSystemExit):
            self.function_to_test(**self.test_args)

    def test_emits_error_when_delayed_invalid(self):
        """ Should emit error message when ‘--delayed’ invalid. """
        days_error = TypeError("wrong")
        dput.dput.determine_delay_days.side_effect = days_error
        with contextlib.suppress(FakeSystemExit):
            self.function_to_test(**self.test_args)
        expected_output = textwrap.dedent("""\
                Incorrect delayed argument: {exc_type}: ...
                """.format(
                    exc_type=type(days_error).__name__))
        self.assertThat(
                sys.stdout.getvalue(),
                testtools.matchers.DocTestMatches(
                    expected_output, doctest.ELLIPSIS))

    def test_emits_error_when_delayed_value_out_of_range(self):
        """ Should emit error message when ‘--delayed’ value out of range. """
        days_error = ValueError("bad")
        dput.dput.determine_delay_days.side_effect = days_error
        with contextlib.suppress(FakeSystemExit):
            self.function_to_test(**self.test_args)
        expected_output = textwrap.dedent("""\
                Incorrect delayed argument: {exc_type}: ...
                """.format(
                    exc_type=type(days_error).__name__))
        self.assertThat(
                sys.stdout.getvalue(),
                testtools.matchers.DocTestMatches(
                    expected_output, doctest.ELLIPSIS))

    def test_calls_sys_exit_when_invalid_delayed_value(self):
        """ Should call `sys.exit` when ‘--delayed’ invalid. """
        days_error = ValueError("bad")
        dput.dput.determine_delay_days.side_effect = days_error
        with testtools.ExpectedException(FakeSystemExit):
            self.function_to_test(**self.test_args)
        sys.exit.assert_called_with(os.EX_USAGE)


class main_DebugMessageTestCase(main_TestCase):
    """ Test cases for `main` debug messages. """

    progname = "lorem"
    dput_version = "ipsum"

    @unittest.mock.patch.object(dput.dput, 'debug', new=True)
    def test_emits_debug_message_with_dput_version(self):
        """ Should emit debug message showing Dput version string. """
        self.fake_options.debug = True
        self.function_to_test(**self.test_args)
        expected_progname = self.progname
        expected_version = self.fake_distribution.version
        expected_output = textwrap.dedent("""\
                D: {progname} {version}
                """).format(
                    progname=expected_progname,
                    version=expected_version)
        self.assertIn(expected_output, sys.stdout.getvalue())


class main_DefaultFunctionCallTestCase(main_TestCase):
    """ Test cases for `main` defaults calling other functions. """

    def test_calls_import_upload_functions_with_expected_args(self):
        """ Should call `import_upload_functions` with expected arguments. """
        self.function_to_test(**self.test_args)
        dput.dput.import_upload_functions.assert_called_with()


class main_ConfigFileTestCase(
        testscenarios.WithScenarios,
        main_TestCase):
    """ Test cases for `main` specification of configuration file. """

    scenarios = [
            ('default', {
                'expected_config_path': "",
                }),
            ('config-from-command-line', {
                'fake_options_mapping': {
                    'config_file_path': "lorem.conf",
                    },
                'expected_config_path': "lorem.conf",
                }),
            ]

    def test_calls_active_config_files_with_expected_args(self):
        """ Should call `active_config_files` with expected arguments. """
        expected_args = (self.expected_config_path, unittest.mock.ANY)
        self.function_to_test(**self.test_args)
        dput.configfile.active_config_files.assert_called_with(*expected_args)

    def test_calls_read_configs_with_expected_args(self):
        """ Should call `read_configs` with expected arguments. """
        expected_config_files = self.fake_config_files
        expected_args = (expected_config_files, unittest.mock.ANY)
        self.function_to_test(**self.test_args)
        dput.dput.read_configs.assert_called_with(*expected_args)

    def test_calls_sys_exit_if_configuration_error_when_opening_files(self):
        """
        Should call `sys.exit` if `ConfigurationError` from opening files.
        """
        set_config(self, 'all-not-exist')
        self.set_test_args()
        fake_error_text = "Could not open any configfile, tried ‘foo’, ‘bar’"
        fake_error = dput.configfile.ConfigurationError(fake_error_text)
        dput.configfile.active_config_files.side_effect = fake_error
        with testtools.ExpectedException(FakeSystemExit):
            self.function_to_test(**self.test_args)
        expected_output = textwrap.dedent("""\
                ...{error_text}...
                """.format(error_text=fake_error_text))
        self.assertThat(
                sys.stderr.getvalue(),
                testtools.matchers.DocTestMatches(
                    expected_output, flags=doctest.ELLIPSIS))
        sys.exit.assert_called_with(EXIT_STATUS_FAILURE)

    def test_calls_sys_exit_if_configuration_error_when_reading_files(self):
        """
        Should call `sys.exit` if `ConfigurationError` from reading files.
        """
        fake_error_text = "Hidden Illuminati messages in config file"
        fake_error = dput.configfile.ConfigurationError(fake_error_text)
        dput.dput.read_configs.side_effect = fake_error
        with testtools.ExpectedException(FakeSystemExit):
            self.function_to_test(**self.test_args)
        expected_output = textwrap.dedent("""\
                ...{error_text}...
                """.format(error_text=fake_error_text))
        self.assertThat(
                sys.stderr.getvalue(),
                testtools.matchers.DocTestMatches(
                    expected_output, flags=doctest.ELLIPSIS))
        sys.exit.assert_called_with(EXIT_STATUS_FAILURE)


class main_PrintAndEndTestCase(main_TestCase):
    """ Test cases for `main` that print and end. """

    progname = "lorem"
    dput_version = "ipsum"
    dput_usage_message = "Lorem ipsum, dolor sit amet."

    def test_print_config_when_option_print(self):
        """ Should call `print_config` when ‘--print’. """
        self.fake_options_mapping.update({'config_print': True})
        self.set_fake_options()
        with contextlib.suppress(FakeSystemExit):
            self.function_to_test(**self.test_args)
        dput.dput.print_config.assert_called_with(
                self.runtime_config_parser, dput.dput.debug)

    def test_print_config_then_sys_exit_when_option_print(self):
        """ Should call `print_config`, then `sys.exit`, when ‘--print’. """
        self.fake_options_mapping.update({'config_print': True})
        self.set_fake_options()
        with testtools.ExpectedException(FakeSystemExit):
            self.function_to_test(**self.test_args)
        sys.exit.assert_called_with(EXIT_STATUS_SUCCESS)

    def test_calls_print_default_upload_method_when_option_host_list(self):
        """ Should call `print_default_upload_method` when ‘--host-list’. """
        self.fake_options_mapping.update({'config_host_list': True})
        self.set_fake_options()
        with contextlib.suppress(FakeSystemExit):
            self.function_to_test(**self.test_args)
        dput.dput.print_default_upload_method.assert_called_with(
                self.runtime_config_parser)

    def test_calls_print_host_list_when_option_host_list(self):
        """ Should call `print_host_list` when option ‘--host-list’. """
        self.fake_options_mapping.update({'config_host_list': True})
        self.set_fake_options()
        with contextlib.suppress(FakeSystemExit):
            self.function_to_test(**self.test_args)
        dput.dput.print_host_list.assert_called_with(
                self.runtime_config_parser)

    def test_calls_sys_exit_when_option_host_list(self):
        """ Should call `sys.exit` when option ‘--host-list’. """
        self.fake_options_mapping.update({'config_host_list': True})
        self.set_fake_options()
        with testtools.ExpectedException(FakeSystemExit):
            self.function_to_test(**self.test_args)
        sys.exit.assert_called_with(EXIT_STATUS_SUCCESS)


class main_NamedHost_TestCase(
        testscenarios.WithScenarios,
        main_TestCase):
    """ Test cases for `main` function, named host processing. """

    scenarios = [
            ('host-from-command-line', {
                'config_scenario_name': "exist-simple-host-three",
                'fake_args': ["bar", "lorem.changes", "ipsum.changes"],
                'expected_host': "bar",
                'expected_debug_output': "D: Host bar found in config",
                }),
            ('host-from-command-line check-only', {
                'config_scenario_name': "exist-simple-host-three",
                'fake_options_mapping': {'check_only': True},
                'fake_args': ["bar", "lorem.changes", "ipsum.changes"],
                'expected_host': "bar",
                'expected_debug_output': "D: Host bar found in config",
                }),
            ('host-from-command-line force-upload', {
                'config_scenario_name': "exist-simple-host-three",
                'fake_options_mapping': {'force_upload': True},
                'fake_args': ["bar", "lorem.changes", "ipsum.changes"],
                'expected_host': "bar",
                'expected_debug_output': "D: Host bar found in config",
                }),
            ('only-changes-filenames', {
                'config_scenario_name': "exist-simple-host-three",
                'fake_args': ["lorem.changes", "ipsum.changes"],
                'expected_host': "foo",
                'expected_debug_output': "D: No host named on command line.",
                }),
            ('only-changes-filenames check-only', {
                'config_scenario_name': "exist-simple-host-three",
                'fake_options_mapping': {
                    'debug': True,
                    'check_only': True,
                },
                'fake_args': ["lorem.changes", "ipsum.changes"],
                'expected_host': "foo",
                'expected_debug_output': "D: No host named on command line.",
                'follow_the_goddamned_branches': True,
                }),
            ]

    def test_emits_expected_debug_message(self):
        """ Should emit expected debug message. """
        self.fake_options.debug = True
        with contextlib.suppress(FakeSystemExit):
            self.function_to_test(**self.test_args)
        expected_output = self.expected_debug_output.format(
                host=self.expected_host)
        self.assertIn(expected_output, sys.stdout.getvalue())

    def test_check_upload_logfile_called_with_expected_host(self):
        """ Should call `check_upload_logfile` with expected host. """
        with contextlib.suppress(FakeSystemExit):
            self.function_to_test(**self.test_args)
        expected_fqdn = dput.dput.get_fqdn_for_host.return_value
        expected_args = (
                unittest.mock.ANY, self.expected_host, expected_fqdn,
                unittest.mock.ANY, unittest.mock.ANY,
                unittest.mock.ANY)
        dput.dput.check_upload_logfile.assert_called_with(*expected_args)

    def test_verify_files_called_with_expected_host(self):
        """ Should call `verify_files` with expected host. """
        with contextlib.suppress(FakeSystemExit):
            self.function_to_test(**self.test_args)
        expected_args = (
                unittest.mock.ANY, unittest.mock.ANY, self.expected_host,
                unittest.mock.ANY, unittest.mock.ANY,
                unittest.mock.ANY, unittest.mock.ANY, unittest.mock.ANY)
        dput.dput.verify_files.assert_called_with(*expected_args)


class main_NamedHostNotInConfigTestCase(
        testscenarios.WithScenarios,
        main_TestCase):
    """ Test cases for `main` function, named host not in config. """

    scenarios = [
            ('host-from-command-line', {
                'config_scenario_name': "exist-simple-host-three",
                'fake_args': ["b0gUs", "lorem.changes", "ipsum.changes"],
                'expected_stderr_output': "No host b0gUs found in config.",
                'expected_exception': FakeSystemExit,
                'expected_exit_status': EXIT_STATUS_FAILURE,
                }),
            ('host-from-command-line check-only', {
                'config_scenario_name': "exist-simple-host-three",
                'fake_options_mapping': {'check_only': True},
                'fake_args': ["b0gUs", "lorem.changes", "ipsum.changes"],
                'expected_stderr_output': "No host b0gUs found in config.",
                }),
            ]

    def test_emits_expected_debug_message(self):
        """ Should emit expected debug message. """
        self.fake_options.debug = True
        with contextlib.suppress(FakeSystemExit):
            self.function_to_test(**self.test_args)
        if hasattr(self, 'expected_stdout_output'):
            self.assertIn(self.expected_stdout_output, sys.stdout.getvalue())
        if hasattr(self, 'expected_stderr_output'):
            self.assertIn(self.expected_stderr_output, sys.stderr.getvalue())

    def test_calls_sys_exit_when_host_not_in_config(self):
        """ Should call `sys.exit` when named host not in config. """
        if not hasattr(self, 'expected_exception'):
            self.skipTest("No expected exception for this scenario")
        with testtools.ExpectedException(self.expected_exception):
            self.function_to_test(**self.test_args)
        sys.exit.assert_called_with(self.expected_exit_status)


class main_check_upload_logfile_CallTestCase(
        testscenarios.WithScenarios,
        main_TestCase):
    """ Test cases for `main` function calling `check_upload_logfile`. """

    scenarios = [
            ('default', {}),
            ('command-line-option-check-only', {
                'fake_options_mapping': {'check_only': True},
                'expected_check_only': True,
                }),
            ('command-line-option-force', {
                'fake_options_mapping': {'force_upload': True},
                'expected_force_upload': True,
                }),
            ('command-line-options-two', {
                'fake_options_mapping': {
                    'force_upload': True,
                    'check_only': True,
                    },
                'expected_check_only': True,
                'expected_force_upload': True,
                }),
            ]

    def test_calls_check_upload_logfile_with_expected_args(self):
        """ Should invoke `check_upload_logfile` with expected args. """
        self.function_to_test(**self.test_args)
        expected_fqdn = dput.dput.get_fqdn_for_host.return_value
        expected_args = (
                self.changes_file_double.path,
                self.test_host, expected_fqdn,
                getattr(self, 'expected_check_only', False),
                getattr(self, 'expected_force_upload', False),
                unittest.mock.ANY)
        dput.dput.check_upload_logfile.assert_called_with(*expected_args)


class main_verify_files_CallTestCase(
        testscenarios.WithScenarios,
        main_TestCase):
    """ Test cases for `main` function calling `verify_files`. """

    scenarios = [
            ('default', {}),
            ('command-line-option-check-only', {
                'fake_options_mapping': {'check_only': True},
                'expected_check_only': True,
                }),
            ('command-line-option-check-version', {
                'fake_options_mapping': {'check_version': True},
                'expected_check_version': True,
                }),
            ('command-line-option-unchecked', {
                'fake_options_mapping': {'allow_unsigned_uploads': True},
                'expected_allow_unsigned_uploads': True,
                }),
            ('command-line-options-three', {
                'fake_options_mapping': {
                    'check_only': True,
                    'check_version': True,
                    'allow_unsigned_uploads': True,
                    },
                'expected_check_only': True,
                'expected_check_version': True,
                'expected_allow_unsigned_uploads': True,
                }),
            ]

    def test_calls_verify_files_with_expected_args(self):
        """ Should invoke `verify_files` with expected args. """
        self.function_to_test(**self.test_args)
        expected_args = (
                os.path.dirname(self.changes_file_double.path),
                os.path.basename(self.changes_file_double.path),
                self.test_host,
                self.runtime_config_parser,
                getattr(self, 'expected_check_only', False),
                getattr(self, 'expected_check_version', False),
                getattr(self, 'expected_allow_unsigned_uploads', False),
                unittest.mock.ANY)
        dput.dput.verify_files.assert_called_with(*expected_args)


class main_run_lintian_test_CallTestCase(
        testscenarios.WithScenarios,
        main_TestCase):
    """ Test cases for `main` function calling `run_lintian_test`. """

    scenarios = [
            ('option-from-command-line', {
                'fake_options_mapping': {'run_lintian': True},
                }),
            ('option-from-config', {
                'config_run_lintian': True,
                }),
            ]

    def test_calls_run_lintian_test_with_expected_args(self):
        """ Should invoke `run_lintian_test` with expected args. """
        self.function_to_test(**self.test_args)
        expected_args = (self.changes_file_double.path,)
        dput.dput.run_lintian_test.assert_called_with(*expected_args)


class main_run_lintian_test_NoCallTestCase(
        testscenarios.WithScenarios,
        main_TestCase):
    """ Test cases for `main` function omitting `run_lintian_test`. """

    def test_omits_run_lintian_test(self):
        """ Should omit `run_lintian_test` when not requested. """
        self.function_to_test(**self.test_args)
        self.assertFalse(dput.dput.run_lintian_test.called)


class main_UploadHookCommandsTestCase(main_TestCase):
    """ Test cases for `main` function upload hook commands. """

    def test_calls_execute_command_with_pre_upload_command(self):
        """ Should invoke `execute_command` when `pre_upload_command`. """
        test_command = self.getUniqueString()
        self.runtime_config_parser.set(
                self.test_host, 'pre_upload_command', test_command)
        self.function_to_test(**self.test_args)
        expected_command = test_command
        dput.dput.execute_command.assert_called_with(
                expected_command, "pre", unittest.mock.ANY)

    def test_calls_execute_command_with_post_upload_command(self):
        """ Should invoke `execute_command` when `post_upload_command`. """
        test_command = self.getUniqueString()
        self.runtime_config_parser.set(
                self.test_host, 'post_upload_command', test_command)
        self.function_to_test(**self.test_args)
        expected_command = test_command
        dput.dput.execute_command.assert_called_with(
                expected_command, "post", unittest.mock.ANY)


class main_UploadFilesTestCase(
        testscenarios.WithScenarios,
        main_TestCase):
    """ Test cases for `main` function, call `upload_files`. """

    debug_scenarios = [
            ('debug-default', {
                'expected_kwarg_debug': False,
                }),
            ('debug-true', {
                'fake_option_debug': True,
                'expected_kwarg_debug': True,
                }),
            ]

    simulate_scenarios = [
            ('simulate-default', {
                'expected_kwarg_simulate': False,
                }),
            ('simulate-true', {
                'fake_option_simulate': True,
                'expected_kwarg_simulate': True,
                }),
            ]

    passive_scenarios = [
            ('passive-default', {
                'expected_kwarg_commandline_passive': None,
                }),
            ('passive-true', {
                'fake_option_commandline_passive': True,
                'expected_kwarg_commandline_passive': True,
                }),
            ]

    delay_scenarios = [
            ('delay-unspecified', {
                'determine_delay_days_return_value': None,
                'expected_kwarg_delay_days': None,
                }),
            ('delay-specified-none', {
                'determine_delay_days_return_value': None,
                'expected_kwarg_delay_days': None,
                }),
            ('delay-specified-integer', {
                'determine_delay_days_return_value': 555,
                'expected_kwarg_delay_days': 555,
                }),
            ]

    scenarios = testscenarios.multiply_scenarios(
            debug_scenarios, simulate_scenarios,
            passive_scenarios, delay_scenarios)

    def setUp(self):
        """ Set up fixtures for this test case. """
        super().setUp()

        self.set_expected_kwargs()

    def set_fake_options(self, options_mapping=None):
        """ Set the `fake_option_mapping` based on scenario attributes. """
        for option_name in [
                'debug',
                'simulate',
                'commandline_passive',
                ]:
            scenario_attribute_name = "fake_option_{}".format(option_name)
            if hasattr(self, scenario_attribute_name):
                fake_option_value = getattr(self, scenario_attribute_name)
                options_mapping[option_name] = fake_option_value
        super().set_fake_options(options_mapping)

    def set_expected_kwargs(self):
        """ Set the `expected_kwargs` based on scenario attributes. """
        self.expected_kwargs = {}
        for arg_name in [
                'debug',
                'simulate',
                'commandline_passive',
                'delay_days',
                ]:
            scenario_attribute_name = "expected_kwarg_{}".format(arg_name)
            if hasattr(self, scenario_attribute_name):
                expected_kwarg_value = getattr(self, scenario_attribute_name)
                self.expected_kwargs[arg_name] = expected_kwarg_value

    def test_calls_upload_files_with_expected_args(self):
        """ Should call `upload_files` function with expected args. """
        self.expected_args = (
                self.upload_methods,
                self.runtime_config_parser,
                self.test_host,
                self.files_to_upload,
                )
        self.function_to_test(**self.test_args)
        dput.dput.upload_files.assert_called_with(
                *self.expected_args, **self.expected_kwargs)


class main_UploadLogTestCase(
        testscenarios.WithScenarios,
        main_TestCase):
    """ Test cases for `main` function, creating upload log file. """

    scenarios = [
            ('default', {
                'expect_call': True,
                }),
            ('command-line-disable-option', {
                'fake_options_mapping': {'upload_log': False},
                'expect_call': False,
                }),
            ('command-line-simulate-option', {
                'fake_options_mapping': {'simulate': True},
                'expect_call': False,
                }),
            ]

    def test_calls_create_upload_file_if_specified(self):
        """ Should call `create_upload_file` if specified. """
        self.function_to_test(**self.test_args)
        if self.expect_call:
            expected_args = (
                    os.path.basename(self.changes_file_double.path),
                    self.test_host, dput.dput.get_fqdn_for_host.return_value,
                    os.path.dirname(self.changes_file_double.path),
                    self.files_to_upload,
                    unittest.mock.ANY)
            dput.dput.create_upload_file.assert_called_with(*expected_args)
        else:
            self.assertFalse(dput.dput.create_upload_file.called)


class main_DinstallTestCase(
        testscenarios.WithScenarios,
        main_TestCase):
    """ Test cases for `main` function, invoking ‘dinstall’ command. """

    scenarios = [
            ('option-from-command-line', {
                'fake_options_mapping': {'run_dinstall': True},
                'expected_output': "D: run_dinstall: True",
                }),
            ('option-from-config', {
                'config_run_dinstall': True,
                'expected_output': "D: Host Config: True",
                }),
            ]

    def test_emits_debug_message_for_options(self):
        """ Should emit debug message for options. """
        self.fake_options.debug = True
        self.function_to_test(**self.test_args)
        self.assertIn(self.expected_output, sys.stdout.getvalue())

    def test_calls_dinstall_caller_with_expected_args(self):
        """ Should call `dinstall_caller` with expected args. """
        expected_args = (
                os.path.basename(self.changes_file_double.path),
                self.test_host, dput.dput.get_fqdn_for_host.return_value,
                unittest.mock.ANY, unittest.mock.ANY, unittest.mock.ANY)
        self.function_to_test(**self.test_args)
        dput.dput.dinstall_caller.assert_called_with(*expected_args)

    def test_emits_expected_information_message(self):
        """ Should emit expected information message. """
        self.fake_options.simulate = True
        self.function_to_test(**self.test_args)
        expected_stderr_output = (
                "Not running ‘dinstall’: ‘simulate’ option specified.")
        self.assertIn(expected_stderr_output, sys.stderr.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 :