~bzr-pqm/bzr/bzr.dev

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
# Copyright (C) 2005, 2006, 2007, 2008, 2009 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

"""Tests for the test framework."""

from cStringIO import StringIO
import os
import sys
import time
import unittest
import warnings

import bzrlib
from bzrlib import (
    branchbuilder,
    bzrdir,
    debug,
    errors,
    lockdir,
    memorytree,
    osutils,
    progress,
    remote,
    repository,
    symbol_versioning,
    tests,
    workingtree,
    )
from bzrlib.repofmt import (
    pack_repo,
    weaverepo,
    )
from bzrlib.symbol_versioning import (
    deprecated_function,
    deprecated_in,
    deprecated_method,
    )
from bzrlib.tests import (
    test_lsprof,
    test_sftp_transport,
    TestUtil,
    )
from bzrlib.trace import note
from bzrlib.transport.memory import MemoryServer, MemoryTransport
from bzrlib.version import _get_bzr_source_tree


def _test_ids(test_suite):
    """Get the ids for the tests in a test suite."""
    return [t.id() for t in tests.iter_suite_tests(test_suite)]


class SelftestTests(tests.TestCase):

    def test_import_tests(self):
        mod = TestUtil._load_module_by_name('bzrlib.tests.test_selftest')
        self.assertEqual(mod.SelftestTests, SelftestTests)

    def test_import_test_failure(self):
        self.assertRaises(ImportError,
                          TestUtil._load_module_by_name,
                          'bzrlib.no-name-yet')

class MetaTestLog(tests.TestCase):

    def test_logging(self):
        """Test logs are captured when a test fails."""
        self.log('a test message')
        self._log_file.flush()
        self.assertContainsRe(self._get_log(keep_log_file=True),
                              'a test message\n')


class TestUnicodeFilename(tests.TestCase):

    def test_probe_passes(self):
        """UnicodeFilename._probe passes."""
        # We can't test much more than that because the behaviour depends
        # on the platform.
        tests.UnicodeFilename._probe()


class TestTreeShape(tests.TestCaseInTempDir):

    def test_unicode_paths(self):
        self.requireFeature(tests.UnicodeFilename)

        filename = u'hell\u00d8'
        self.build_tree_contents([(filename, 'contents of hello')])
        self.failUnlessExists(filename)


class TestTransportScenarios(tests.TestCase):
    """A group of tests that test the transport implementation adaption core.

    This is a meta test that the tests are applied to all available
    transports.

    This will be generalised in the future which is why it is in this
    test file even though it is specific to transport tests at the moment.
    """

    def test_get_transport_permutations(self):
        # this checks that get_test_permutations defined by the module is
        # called by the get_transport_test_permutations function.
        class MockModule(object):
            def get_test_permutations(self):
                return sample_permutation
        sample_permutation = [(1,2), (3,4)]
        from bzrlib.tests.per_transport import get_transport_test_permutations
        self.assertEqual(sample_permutation,
                         get_transport_test_permutations(MockModule()))

    def test_scenarios_include_all_modules(self):
        # this checks that the scenario generator returns as many permutations
        # as there are in all the registered transport modules - we assume if
        # this matches its probably doing the right thing especially in
        # combination with the tests for setting the right classes below.
        from bzrlib.tests.per_transport import transport_test_permutations
        from bzrlib.transport import _get_transport_modules
        modules = _get_transport_modules()
        permutation_count = 0
        for module in modules:
            try:
                permutation_count += len(reduce(getattr,
                    (module + ".get_test_permutations").split('.')[1:],
                     __import__(module))())
            except errors.DependencyNotPresent:
                pass
        scenarios = transport_test_permutations()
        self.assertEqual(permutation_count, len(scenarios))

    def test_scenarios_include_transport_class(self):
        # This test used to know about all the possible transports and the
        # order they were returned but that seems overly brittle (mbp
        # 20060307)
        from bzrlib.tests.per_transport import transport_test_permutations
        scenarios = transport_test_permutations()
        # there are at least that many builtin transports
        self.assertTrue(len(scenarios) > 6)
        one_scenario = scenarios[0]
        self.assertIsInstance(one_scenario[0], str)
        self.assertTrue(issubclass(one_scenario[1]["transport_class"],
                                   bzrlib.transport.Transport))
        self.assertTrue(issubclass(one_scenario[1]["transport_server"],
                                   bzrlib.transport.Server))


class TestBranchScenarios(tests.TestCase):

    def test_scenarios(self):
        # check that constructor parameters are passed through to the adapted
        # test.
        from bzrlib.tests.per_branch import make_scenarios
        server1 = "a"
        server2 = "b"
        formats = [("c", "C"), ("d", "D")]
        scenarios = make_scenarios(server1, server2, formats)
        self.assertEqual(2, len(scenarios))
        self.assertEqual([
            ('str',
             {'branch_format': 'c',
              'bzrdir_format': 'C',
              'transport_readonly_server': 'b',
              'transport_server': 'a'}),
            ('str',
             {'branch_format': 'd',
              'bzrdir_format': 'D',
              'transport_readonly_server': 'b',
              'transport_server': 'a'})],
            scenarios)


class TestBzrDirScenarios(tests.TestCase):

    def test_scenarios(self):
        # check that constructor parameters are passed through to the adapted
        # test.
        from bzrlib.tests.per_bzrdir import make_scenarios
        vfs_factory = "v"
        server1 = "a"
        server2 = "b"
        formats = ["c", "d"]
        scenarios = make_scenarios(vfs_factory, server1, server2, formats)
        self.assertEqual([
            ('str',
             {'bzrdir_format': 'c',
              'transport_readonly_server': 'b',
              'transport_server': 'a',
              'vfs_transport_factory': 'v'}),
            ('str',
             {'bzrdir_format': 'd',
              'transport_readonly_server': 'b',
              'transport_server': 'a',
              'vfs_transport_factory': 'v'})],
            scenarios)


class TestRepositoryScenarios(tests.TestCase):

    def test_formats_to_scenarios(self):
        from bzrlib.tests.per_repository import formats_to_scenarios
        formats = [("(c)", remote.RemoteRepositoryFormat()),
                   ("(d)", repository.format_registry.get(
                        'Bazaar pack repository format 1 (needs bzr 0.92)\n'))]
        no_vfs_scenarios = formats_to_scenarios(formats, "server", "readonly",
            None)
        vfs_scenarios = formats_to_scenarios(formats, "server", "readonly",
            vfs_transport_factory="vfs")
        # no_vfs generate scenarios without vfs_transport_factory
        self.assertEqual([
            ('RemoteRepositoryFormat(c)',
             {'bzrdir_format': remote.RemoteBzrDirFormat(),
              'repository_format': remote.RemoteRepositoryFormat(),
              'transport_readonly_server': 'readonly',
              'transport_server': 'server'}),
            ('RepositoryFormatKnitPack1(d)',
             {'bzrdir_format': bzrdir.BzrDirMetaFormat1(),
              'repository_format': pack_repo.RepositoryFormatKnitPack1(),
              'transport_readonly_server': 'readonly',
              'transport_server': 'server'})],
            no_vfs_scenarios)
        self.assertEqual([
            ('RemoteRepositoryFormat(c)',
             {'bzrdir_format': remote.RemoteBzrDirFormat(),
              'repository_format': remote.RemoteRepositoryFormat(),
              'transport_readonly_server': 'readonly',
              'transport_server': 'server',
              'vfs_transport_factory': 'vfs'}),
            ('RepositoryFormatKnitPack1(d)',
             {'bzrdir_format': bzrdir.BzrDirMetaFormat1(),
              'repository_format': pack_repo.RepositoryFormatKnitPack1(),
              'transport_readonly_server': 'readonly',
              'transport_server': 'server',
              'vfs_transport_factory': 'vfs'})],
            vfs_scenarios)


class TestTestScenarioApplication(tests.TestCase):
    """Tests for the test adaption facilities."""

    def test_apply_scenario(self):
        from bzrlib.tests import apply_scenario
        input_test = TestTestScenarioApplication("test_apply_scenario")
        # setup two adapted tests
        adapted_test1 = apply_scenario(input_test,
            ("new id",
            {"bzrdir_format":"bzr_format",
             "repository_format":"repo_fmt",
             "transport_server":"transport_server",
             "transport_readonly_server":"readonly-server"}))
        adapted_test2 = apply_scenario(input_test,
            ("new id 2", {"bzrdir_format":None}))
        # input_test should have been altered.
        self.assertRaises(AttributeError, getattr, input_test, "bzrdir_format")
        # the new tests are mutually incompatible, ensuring it has
        # made new ones, and unspecified elements in the scenario
        # should not have been altered.
        self.assertEqual("bzr_format", adapted_test1.bzrdir_format)
        self.assertEqual("repo_fmt", adapted_test1.repository_format)
        self.assertEqual("transport_server", adapted_test1.transport_server)
        self.assertEqual("readonly-server",
            adapted_test1.transport_readonly_server)
        self.assertEqual(
            "bzrlib.tests.test_selftest.TestTestScenarioApplication."
            "test_apply_scenario(new id)",
            adapted_test1.id())
        self.assertEqual(None, adapted_test2.bzrdir_format)
        self.assertEqual(
            "bzrlib.tests.test_selftest.TestTestScenarioApplication."
            "test_apply_scenario(new id 2)",
            adapted_test2.id())


class TestInterRepositoryScenarios(tests.TestCase):

    def test_scenarios(self):
        # check that constructor parameters are passed through to the adapted
        # test.
        from bzrlib.tests.per_interrepository import make_scenarios
        server1 = "a"
        server2 = "b"
        formats = [("C0", "C1", "C2"), ("D0", "D1", "D2")]
        scenarios = make_scenarios(server1, server2, formats)
        self.assertEqual([
            ('C0,str,str',
             {'repository_format': 'C1',
              'repository_format_to': 'C2',
              'transport_readonly_server': 'b',
              'transport_server': 'a'}),
            ('D0,str,str',
             {'repository_format': 'D1',
              'repository_format_to': 'D2',
              'transport_readonly_server': 'b',
              'transport_server': 'a'})],
            scenarios)


class TestWorkingTreeScenarios(tests.TestCase):

    def test_scenarios(self):
        # check that constructor parameters are passed through to the adapted
        # test.
        from bzrlib.tests.per_workingtree import make_scenarios
        server1 = "a"
        server2 = "b"
        formats = [workingtree.WorkingTreeFormat2(),
                   workingtree.WorkingTreeFormat3(),]
        scenarios = make_scenarios(server1, server2, formats)
        self.assertEqual([
            ('WorkingTreeFormat2',
             {'bzrdir_format': formats[0]._matchingbzrdir,
              'transport_readonly_server': 'b',
              'transport_server': 'a',
              'workingtree_format': formats[0]}),
            ('WorkingTreeFormat3',
             {'bzrdir_format': formats[1]._matchingbzrdir,
              'transport_readonly_server': 'b',
              'transport_server': 'a',
              'workingtree_format': formats[1]})],
            scenarios)


class TestTreeScenarios(tests.TestCase):

    def test_scenarios(self):
        # the tree implementation scenario generator is meant to setup one
        # instance for each working tree format, and one additional instance
        # that will use the default wt format, but create a revision tree for
        # the tests.  this means that the wt ones should have the
        # workingtree_to_test_tree attribute set to 'return_parameter' and the
        # revision one set to revision_tree_from_workingtree.

        from bzrlib.tests.per_tree import (
            _dirstate_tree_from_workingtree,
            make_scenarios,
            preview_tree_pre,
            preview_tree_post,
            return_parameter,
            revision_tree_from_workingtree
            )
        server1 = "a"
        server2 = "b"
        formats = [workingtree.WorkingTreeFormat2(),
                   workingtree.WorkingTreeFormat3(),]
        scenarios = make_scenarios(server1, server2, formats)
        self.assertEqual(7, len(scenarios))
        default_wt_format = workingtree.WorkingTreeFormat4._default_format
        wt4_format = workingtree.WorkingTreeFormat4()
        wt5_format = workingtree.WorkingTreeFormat5()
        expected_scenarios = [
            ('WorkingTreeFormat2',
             {'bzrdir_format': formats[0]._matchingbzrdir,
              'transport_readonly_server': 'b',
              'transport_server': 'a',
              'workingtree_format': formats[0],
              '_workingtree_to_test_tree': return_parameter,
              }),
            ('WorkingTreeFormat3',
             {'bzrdir_format': formats[1]._matchingbzrdir,
              'transport_readonly_server': 'b',
              'transport_server': 'a',
              'workingtree_format': formats[1],
              '_workingtree_to_test_tree': return_parameter,
             }),
            ('RevisionTree',
             {'_workingtree_to_test_tree': revision_tree_from_workingtree,
              'bzrdir_format': default_wt_format._matchingbzrdir,
              'transport_readonly_server': 'b',
              'transport_server': 'a',
              'workingtree_format': default_wt_format,
             }),
            ('DirStateRevisionTree,WT4',
             {'_workingtree_to_test_tree': _dirstate_tree_from_workingtree,
              'bzrdir_format': wt4_format._matchingbzrdir,
              'transport_readonly_server': 'b',
              'transport_server': 'a',
              'workingtree_format': wt4_format,
             }),
            ('DirStateRevisionTree,WT5',
             {'_workingtree_to_test_tree': _dirstate_tree_from_workingtree,
              'bzrdir_format': wt5_format._matchingbzrdir,
              'transport_readonly_server': 'b',
              'transport_server': 'a',
              'workingtree_format': wt5_format,
             }),
            ('PreviewTree',
             {'_workingtree_to_test_tree': preview_tree_pre,
              'bzrdir_format': default_wt_format._matchingbzrdir,
              'transport_readonly_server': 'b',
              'transport_server': 'a',
              'workingtree_format': default_wt_format}),
            ('PreviewTreePost',
             {'_workingtree_to_test_tree': preview_tree_post,
              'bzrdir_format': default_wt_format._matchingbzrdir,
              'transport_readonly_server': 'b',
              'transport_server': 'a',
              'workingtree_format': default_wt_format}),
             ]
        self.assertEqual(expected_scenarios, scenarios)


class TestInterTreeScenarios(tests.TestCase):
    """A group of tests that test the InterTreeTestAdapter."""

    def test_scenarios(self):
        # check that constructor parameters are passed through to the adapted
        # test.
        # for InterTree tests we want the machinery to bring up two trees in
        # each instance: the base one, and the one we are interacting with.
        # because each optimiser can be direction specific, we need to test
        # each optimiser in its chosen direction.
        # unlike the TestProviderAdapter we dont want to automatically add a
        # parameterized one for WorkingTree - the optimisers will tell us what
        # ones to add.
        from bzrlib.tests.per_tree import (
            return_parameter,
            revision_tree_from_workingtree
            )
        from bzrlib.tests.per_intertree import (
            make_scenarios,
            )
        from bzrlib.workingtree import WorkingTreeFormat2, WorkingTreeFormat3
        input_test = TestInterTreeScenarios(
            "test_scenarios")
        server1 = "a"
        server2 = "b"
        format1 = WorkingTreeFormat2()
        format2 = WorkingTreeFormat3()
        formats = [("1", str, format1, format2, "converter1"),
            ("2", int, format2, format1, "converter2")]
        scenarios = make_scenarios(server1, server2, formats)
        self.assertEqual(2, len(scenarios))
        expected_scenarios = [
            ("1", {
                "bzrdir_format": format1._matchingbzrdir,
                "intertree_class": formats[0][1],
                "workingtree_format": formats[0][2],
                "workingtree_format_to": formats[0][3],
                "mutable_trees_to_test_trees": formats[0][4],
                "_workingtree_to_test_tree": return_parameter,
                "transport_server": server1,
                "transport_readonly_server": server2,
                }),
            ("2", {
                "bzrdir_format": format2._matchingbzrdir,
                "intertree_class": formats[1][1],
                "workingtree_format": formats[1][2],
                "workingtree_format_to": formats[1][3],
                "mutable_trees_to_test_trees": formats[1][4],
                "_workingtree_to_test_tree": return_parameter,
                "transport_server": server1,
                "transport_readonly_server": server2,
                }),
            ]
        self.assertEqual(scenarios, expected_scenarios)


class TestTestCaseInTempDir(tests.TestCaseInTempDir):

    def test_home_is_not_working(self):
        self.assertNotEqual(self.test_dir, self.test_home_dir)
        cwd = osutils.getcwd()
        self.assertIsSameRealPath(self.test_dir, cwd)
        self.assertIsSameRealPath(self.test_home_dir, os.environ['HOME'])

    def test_assertEqualStat_equal(self):
        from bzrlib.tests.test_dirstate import _FakeStat
        self.build_tree(["foo"])
        real = os.lstat("foo")
        fake = _FakeStat(real.st_size, real.st_mtime, real.st_ctime,
            real.st_dev, real.st_ino, real.st_mode)
        self.assertEqualStat(real, fake)

    def test_assertEqualStat_notequal(self):
        self.build_tree(["foo", "bar"])
        self.assertRaises(AssertionError, self.assertEqualStat,
            os.lstat("foo"), os.lstat("bar"))


class TestTestCaseWithMemoryTransport(tests.TestCaseWithMemoryTransport):

    def test_home_is_non_existant_dir_under_root(self):
        """The test_home_dir for TestCaseWithMemoryTransport is missing.

        This is because TestCaseWithMemoryTransport is for tests that do not
        need any disk resources: they should be hooked into bzrlib in such a
        way that no global settings are being changed by the test (only a
        few tests should need to do that), and having a missing dir as home is
        an effective way to ensure that this is the case.
        """
        self.assertIsSameRealPath(
            self.TEST_ROOT + "/MemoryTransportMissingHomeDir",
            self.test_home_dir)
        self.assertIsSameRealPath(self.test_home_dir, os.environ['HOME'])

    def test_cwd_is_TEST_ROOT(self):
        self.assertIsSameRealPath(self.test_dir, self.TEST_ROOT)
        cwd = osutils.getcwd()
        self.assertIsSameRealPath(self.test_dir, cwd)

    def test_make_branch_and_memory_tree(self):
        """In TestCaseWithMemoryTransport we should not make the branch on disk.

        This is hard to comprehensively robustly test, so we settle for making
        a branch and checking no directory was created at its relpath.
        """
        tree = self.make_branch_and_memory_tree('dir')
        # Guard against regression into MemoryTransport leaking
        # files to disk instead of keeping them in memory.
        self.failIf(osutils.lexists('dir'))
        self.assertIsInstance(tree, memorytree.MemoryTree)

    def test_make_branch_and_memory_tree_with_format(self):
        """make_branch_and_memory_tree should accept a format option."""
        format = bzrdir.BzrDirMetaFormat1()
        format.repository_format = weaverepo.RepositoryFormat7()
        tree = self.make_branch_and_memory_tree('dir', format=format)
        # Guard against regression into MemoryTransport leaking
        # files to disk instead of keeping them in memory.
        self.failIf(osutils.lexists('dir'))
        self.assertIsInstance(tree, memorytree.MemoryTree)
        self.assertEqual(format.repository_format.__class__,
            tree.branch.repository._format.__class__)

    def test_make_branch_builder(self):
        builder = self.make_branch_builder('dir')
        self.assertIsInstance(builder, branchbuilder.BranchBuilder)
        # Guard against regression into MemoryTransport leaking
        # files to disk instead of keeping them in memory.
        self.failIf(osutils.lexists('dir'))

    def test_make_branch_builder_with_format(self):
        # Use a repo layout that doesn't conform to a 'named' layout, to ensure
        # that the format objects are used.
        format = bzrdir.BzrDirMetaFormat1()
        repo_format = weaverepo.RepositoryFormat7()
        format.repository_format = repo_format
        builder = self.make_branch_builder('dir', format=format)
        the_branch = builder.get_branch()
        # Guard against regression into MemoryTransport leaking
        # files to disk instead of keeping them in memory.
        self.failIf(osutils.lexists('dir'))
        self.assertEqual(format.repository_format.__class__,
                         the_branch.repository._format.__class__)
        self.assertEqual(repo_format.get_format_string(),
                         self.get_transport().get_bytes(
                            'dir/.bzr/repository/format'))

    def test_make_branch_builder_with_format_name(self):
        builder = self.make_branch_builder('dir', format='knit')
        the_branch = builder.get_branch()
        # Guard against regression into MemoryTransport leaking
        # files to disk instead of keeping them in memory.
        self.failIf(osutils.lexists('dir'))
        dir_format = bzrdir.format_registry.make_bzrdir('knit')
        self.assertEqual(dir_format.repository_format.__class__,
                         the_branch.repository._format.__class__)
        self.assertEqual('Bazaar-NG Knit Repository Format 1',
                         self.get_transport().get_bytes(
                            'dir/.bzr/repository/format'))

    def test_safety_net(self):
        """No test should modify the safety .bzr directory.

        We just test that the _check_safety_net private method raises
        AssertionError, it's easier than building a test suite with the same
        test.
        """
        # Oops, a commit in the current directory (i.e. without local .bzr
        # directory) will crawl up the hierarchy to find a .bzr directory.
        self.run_bzr(['commit', '-mfoo', '--unchanged'])
        # But we have a safety net in place.
        self.assertRaises(AssertionError, self._check_safety_net)

    def test_dangling_locks_cause_failures(self):
        class TestDanglingLock(tests.TestCaseWithMemoryTransport):
            def test_function(self):
                t = self.get_transport('.')
                l = lockdir.LockDir(t, 'lock')
                l.create()
                l.attempt_lock()
        test = TestDanglingLock('test_function')
        result = test.run()
        if self._lock_check_thorough:
            self.assertEqual(1, len(result.errors))
        else:
            # When _lock_check_thorough is disabled, then we don't trigger a
            # failure
            self.assertEqual(0, len(result.errors))


class TestTestCaseWithTransport(tests.TestCaseWithTransport):
    """Tests for the convenience functions TestCaseWithTransport introduces."""

    def test_get_readonly_url_none(self):
        from bzrlib.transport import get_transport
        from bzrlib.transport.memory import MemoryServer
        from bzrlib.transport.readonly import ReadonlyTransportDecorator
        self.vfs_transport_factory = MemoryServer
        self.transport_readonly_server = None
        # calling get_readonly_transport() constructs a decorator on the url
        # for the server
        url = self.get_readonly_url()
        url2 = self.get_readonly_url('foo/bar')
        t = get_transport(url)
        t2 = get_transport(url2)
        self.failUnless(isinstance(t, ReadonlyTransportDecorator))
        self.failUnless(isinstance(t2, ReadonlyTransportDecorator))
        self.assertEqual(t2.base[:-1], t.abspath('foo/bar'))

    def test_get_readonly_url_http(self):
        from bzrlib.tests.http_server import HttpServer
        from bzrlib.transport import get_transport
        from bzrlib.transport.local import LocalURLServer
        from bzrlib.transport.http import HttpTransportBase
        self.transport_server = LocalURLServer
        self.transport_readonly_server = HttpServer
        # calling get_readonly_transport() gives us a HTTP server instance.
        url = self.get_readonly_url()
        url2 = self.get_readonly_url('foo/bar')
        # the transport returned may be any HttpTransportBase subclass
        t = get_transport(url)
        t2 = get_transport(url2)
        self.failUnless(isinstance(t, HttpTransportBase))
        self.failUnless(isinstance(t2, HttpTransportBase))
        self.assertEqual(t2.base[:-1], t.abspath('foo/bar'))

    def test_is_directory(self):
        """Test assertIsDirectory assertion"""
        t = self.get_transport()
        self.build_tree(['a_dir/', 'a_file'], transport=t)
        self.assertIsDirectory('a_dir', t)
        self.assertRaises(AssertionError, self.assertIsDirectory, 'a_file', t)
        self.assertRaises(AssertionError, self.assertIsDirectory, 'not_here', t)

    def test_make_branch_builder(self):
        builder = self.make_branch_builder('dir')
        rev_id = builder.build_commit()
        self.failUnlessExists('dir')
        a_dir = bzrdir.BzrDir.open('dir')
        self.assertRaises(errors.NoWorkingTree, a_dir.open_workingtree)
        a_branch = a_dir.open_branch()
        builder_branch = builder.get_branch()
        self.assertEqual(a_branch.base, builder_branch.base)
        self.assertEqual((1, rev_id), builder_branch.last_revision_info())
        self.assertEqual((1, rev_id), a_branch.last_revision_info())


class TestTestCaseTransports(tests.TestCaseWithTransport):

    def setUp(self):
        super(TestTestCaseTransports, self).setUp()
        self.vfs_transport_factory = MemoryServer

    def test_make_bzrdir_preserves_transport(self):
        t = self.get_transport()
        result_bzrdir = self.make_bzrdir('subdir')
        self.assertIsInstance(result_bzrdir.transport,
                              MemoryTransport)
        # should not be on disk, should only be in memory
        self.failIfExists('subdir')


class TestChrootedTest(tests.ChrootedTestCase):

    def test_root_is_root(self):
        from bzrlib.transport import get_transport
        t = get_transport(self.get_readonly_url())
        url = t.base
        self.assertEqual(url, t.clone('..').base)


class TestTestResult(tests.TestCase):

    def check_timing(self, test_case, expected_re):
        result = bzrlib.tests.TextTestResult(self._log_file,
                descriptions=0,
                verbosity=1,
                )
        test_case.run(result)
        timed_string = result._testTimeString(test_case)
        self.assertContainsRe(timed_string, expected_re)

    def test_test_reporting(self):
        class ShortDelayTestCase(tests.TestCase):
            def test_short_delay(self):
                time.sleep(0.003)
            def test_short_benchmark(self):
                self.time(time.sleep, 0.003)
        self.check_timing(ShortDelayTestCase('test_short_delay'),
                          r"^ +[0-9]+ms$")
        # if a benchmark time is given, we now show just that time followed by
        # a star
        self.check_timing(ShortDelayTestCase('test_short_benchmark'),
                          r"^ +[0-9]+ms\*$")

    def test_unittest_reporting_unittest_class(self):
        # getting the time from a non-bzrlib test works ok
        class ShortDelayTestCase(unittest.TestCase):
            def test_short_delay(self):
                time.sleep(0.003)
        self.check_timing(ShortDelayTestCase('test_short_delay'),
                          r"^ +[0-9]+ms$")

    def test_assigned_benchmark_file_stores_date(self):
        output = StringIO()
        result = bzrlib.tests.TextTestResult(self._log_file,
                                        descriptions=0,
                                        verbosity=1,
                                        bench_history=output
                                        )
        output_string = output.getvalue()
        # if you are wondering about the regexp please read the comment in
        # test_bench_history (bzrlib.tests.test_selftest.TestRunner)
        # XXX: what comment?  -- Andrew Bennetts
        self.assertContainsRe(output_string, "--date [0-9.]+")

    def test_benchhistory_records_test_times(self):
        result_stream = StringIO()
        result = bzrlib.tests.TextTestResult(
            self._log_file,
            descriptions=0,
            verbosity=1,
            bench_history=result_stream
            )

        # we want profile a call and check that its test duration is recorded
        # make a new test instance that when run will generate a benchmark
        example_test_case = TestTestResult("_time_hello_world_encoding")
        # execute the test, which should succeed and record times
        example_test_case.run(result)
        lines = result_stream.getvalue().splitlines()
        self.assertEqual(2, len(lines))
        self.assertContainsRe(lines[1],
            " *[0-9]+ms bzrlib.tests.test_selftest.TestTestResult"
            "._time_hello_world_encoding")

    def _time_hello_world_encoding(self):
        """Profile two sleep calls

        This is used to exercise the test framework.
        """
        self.time(unicode, 'hello', errors='replace')
        self.time(unicode, 'world', errors='replace')

    def test_lsprofiling(self):
        """Verbose test result prints lsprof statistics from test cases."""
        self.requireFeature(test_lsprof.LSProfFeature)
        result_stream = StringIO()
        result = bzrlib.tests.VerboseTestResult(
            unittest._WritelnDecorator(result_stream),
            descriptions=0,
            verbosity=2,
            )
        # we want profile a call of some sort and check it is output by
        # addSuccess. We dont care about addError or addFailure as they
        # are not that interesting for performance tuning.
        # make a new test instance that when run will generate a profile
        example_test_case = TestTestResult("_time_hello_world_encoding")
        example_test_case._gather_lsprof_in_benchmarks = True
        # execute the test, which should succeed and record profiles
        example_test_case.run(result)
        # lsprofile_something()
        # if this worked we want
        # LSProf output for <built in function unicode> (['hello'], {'errors': 'replace'})
        #    CallCount    Recursive    Total(ms)   Inline(ms) module:lineno(function)
        # (the lsprof header)
        # ... an arbitrary number of lines
        # and the function call which is time.sleep.
        #           1        0            ???         ???       ???(sleep)
        # and then repeated but with 'world', rather than 'hello'.
        # this should appear in the output stream of our test result.
        output = result_stream.getvalue()
        self.assertContainsRe(output,
            r"LSProf output for <type 'unicode'>\(\('hello',\), {'errors': 'replace'}\)")
        self.assertContainsRe(output,
            r" *CallCount *Recursive *Total\(ms\) *Inline\(ms\) *module:lineno\(function\)\n")
        self.assertContainsRe(output,
            r"( +1 +0 +0\.\d+ +0\.\d+ +<method 'disable' of '_lsprof\.Profiler' objects>\n)?")
        self.assertContainsRe(output,
            r"LSProf output for <type 'unicode'>\(\('world',\), {'errors': 'replace'}\)\n")

    def test_known_failure(self):
        """A KnownFailure being raised should trigger several result actions."""
        class InstrumentedTestResult(tests.ExtendedTestResult):
            def done(self): pass
            def startTests(self): pass
            def report_test_start(self, test): pass
            def report_known_failure(self, test, err):
                self._call = test, err
        result = InstrumentedTestResult(None, None, None, None)
        def test_function():
            raise tests.KnownFailure('failed!')
        test = unittest.FunctionTestCase(test_function)
        test.run(result)
        # it should invoke 'report_known_failure'.
        self.assertEqual(2, len(result._call))
        self.assertEqual(test, result._call[0])
        self.assertEqual(tests.KnownFailure, result._call[1][0])
        self.assertIsInstance(result._call[1][1], tests.KnownFailure)
        # we dont introspec the traceback, if the rest is ok, it would be
        # exceptional for it not to be.
        # it should update the known_failure_count on the object.
        self.assertEqual(1, result.known_failure_count)
        # the result should be successful.
        self.assertTrue(result.wasSuccessful())

    def test_verbose_report_known_failure(self):
        # verbose test output formatting
        result_stream = StringIO()
        result = bzrlib.tests.VerboseTestResult(
            unittest._WritelnDecorator(result_stream),
            descriptions=0,
            verbosity=2,
            )
        test = self.get_passing_test()
        result.startTest(test)
        prefix = len(result_stream.getvalue())
        # the err parameter has the shape:
        # (class, exception object, traceback)
        # KnownFailures dont get their tracebacks shown though, so we
        # can skip that.
        err = (tests.KnownFailure, tests.KnownFailure('foo'), None)
        result.report_known_failure(test, err)
        output = result_stream.getvalue()[prefix:]
        lines = output.splitlines()
        self.assertContainsRe(lines[0], r'XFAIL *\d+ms$')
        self.assertEqual(lines[1], '    foo')
        self.assertEqual(2, len(lines))

    def get_passing_test(self):
        """Return a test object that can't be run usefully."""
        def passing_test():
            pass
        return unittest.FunctionTestCase(passing_test)

    def test_add_not_supported(self):
        """Test the behaviour of invoking addNotSupported."""
        class InstrumentedTestResult(tests.ExtendedTestResult):
            def done(self): pass
            def startTests(self): pass
            def report_test_start(self, test): pass
            def report_unsupported(self, test, feature):
                self._call = test, feature
        result = InstrumentedTestResult(None, None, None, None)
        test = SampleTestCase('_test_pass')
        feature = tests.Feature()
        result.startTest(test)
        result.addNotSupported(test, feature)
        # it should invoke 'report_unsupported'.
        self.assertEqual(2, len(result._call))
        self.assertEqual(test, result._call[0])
        self.assertEqual(feature, result._call[1])
        # the result should be successful.
        self.assertTrue(result.wasSuccessful())
        # it should record the test against a count of tests not run due to
        # this feature.
        self.assertEqual(1, result.unsupported['Feature'])
        # and invoking it again should increment that counter
        result.addNotSupported(test, feature)
        self.assertEqual(2, result.unsupported['Feature'])

    def test_verbose_report_unsupported(self):
        # verbose test output formatting
        result_stream = StringIO()
        result = bzrlib.tests.VerboseTestResult(
            unittest._WritelnDecorator(result_stream),
            descriptions=0,
            verbosity=2,
            )
        test = self.get_passing_test()
        feature = tests.Feature()
        result.startTest(test)
        prefix = len(result_stream.getvalue())
        result.report_unsupported(test, feature)
        output = result_stream.getvalue()[prefix:]
        lines = output.splitlines()
        self.assertEqual(lines, ['NODEP        0ms',
                                 "    The feature 'Feature' is not available."])

    def test_unavailable_exception(self):
        """An UnavailableFeature being raised should invoke addNotSupported."""
        class InstrumentedTestResult(tests.ExtendedTestResult):
            def done(self): pass
            def startTests(self): pass
            def report_test_start(self, test): pass
            def addNotSupported(self, test, feature):
                self._call = test, feature
        result = InstrumentedTestResult(None, None, None, None)
        feature = tests.Feature()
        def test_function():
            raise tests.UnavailableFeature(feature)
        test = unittest.FunctionTestCase(test_function)
        test.run(result)
        # it should invoke 'addNotSupported'.
        self.assertEqual(2, len(result._call))
        self.assertEqual(test, result._call[0])
        self.assertEqual(feature, result._call[1])
        # and not count as an error
        self.assertEqual(0, result.error_count)

    def test_strict_with_unsupported_feature(self):
        result = bzrlib.tests.TextTestResult(self._log_file, descriptions=0,
                                             verbosity=1)
        test = self.get_passing_test()
        feature = "Unsupported Feature"
        result.addNotSupported(test, feature)
        self.assertFalse(result.wasStrictlySuccessful())
        self.assertEqual(None, result._extractBenchmarkTime(test))

    def test_strict_with_known_failure(self):
        result = bzrlib.tests.TextTestResult(self._log_file, descriptions=0,
                                             verbosity=1)
        test = self.get_passing_test()
        err = (tests.KnownFailure, tests.KnownFailure('foo'), None)
        result._addKnownFailure(test, err)
        self.assertFalse(result.wasStrictlySuccessful())
        self.assertEqual(None, result._extractBenchmarkTime(test))

    def test_strict_with_success(self):
        result = bzrlib.tests.TextTestResult(self._log_file, descriptions=0,
                                             verbosity=1)
        test = self.get_passing_test()
        result.addSuccess(test)
        self.assertTrue(result.wasStrictlySuccessful())
        self.assertEqual(None, result._extractBenchmarkTime(test))

    def test_startTests(self):
        """Starting the first test should trigger startTests."""
        class InstrumentedTestResult(tests.ExtendedTestResult):
            calls = 0
            def startTests(self): self.calls += 1
            def report_test_start(self, test): pass
        result = InstrumentedTestResult(None, None, None, None)
        def test_function():
            pass
        test = unittest.FunctionTestCase(test_function)
        test.run(result)
        self.assertEquals(1, result.calls)


class TestUnicodeFilenameFeature(tests.TestCase):

    def test_probe_passes(self):
        """UnicodeFilenameFeature._probe passes."""
        # We can't test much more than that because the behaviour depends
        # on the platform.
        tests.UnicodeFilenameFeature._probe()


class TestRunner(tests.TestCase):

    def dummy_test(self):
        pass

    def run_test_runner(self, testrunner, test):
        """Run suite in testrunner, saving global state and restoring it.

        This current saves and restores:
        TestCaseInTempDir.TEST_ROOT

        There should be no tests in this file that use
        bzrlib.tests.TextTestRunner without using this convenience method,
        because of our use of global state.
        """
        old_root = tests.TestCaseInTempDir.TEST_ROOT
        try:
            tests.TestCaseInTempDir.TEST_ROOT = None
            return testrunner.run(test)
        finally:
            tests.TestCaseInTempDir.TEST_ROOT = old_root

    def test_known_failure_failed_run(self):
        # run a test that generates a known failure which should be printed in
        # the final output when real failures occur.
        def known_failure_test():
            raise tests.KnownFailure('failed')
        test = unittest.TestSuite()
        test.addTest(unittest.FunctionTestCase(known_failure_test))
        def failing_test():
            raise AssertionError('foo')
        test.addTest(unittest.FunctionTestCase(failing_test))
        stream = StringIO()
        runner = tests.TextTestRunner(stream=stream)
        result = self.run_test_runner(runner, test)
        lines = stream.getvalue().splitlines()
        self.assertContainsRe(stream.getvalue(),
            '(?sm)^testing.*$'
            '.*'
            '^======================================================================\n'
            '^FAIL: unittest.FunctionTestCase \\(failing_test\\)\n'
            '^----------------------------------------------------------------------\n'
            'Traceback \\(most recent call last\\):\n'
            '  .*' # File .*, line .*, in failing_test' - but maybe not from .pyc
            '    raise AssertionError\\(\'foo\'\\)\n'
            '.*'
            '^----------------------------------------------------------------------\n'
            '.*'
            'FAILED \\(failures=1, known_failure_count=1\\)'
            )

    def test_known_failure_ok_run(self):
        # run a test that generates a known failure which should be printed in the final output.
        def known_failure_test():
            raise tests.KnownFailure('failed')
        test = unittest.FunctionTestCase(known_failure_test)
        stream = StringIO()
        runner = tests.TextTestRunner(stream=stream)
        result = self.run_test_runner(runner, test)
        self.assertContainsRe(stream.getvalue(),
            '\n'
            '-*\n'
            'Ran 1 test in .*\n'
            '\n'
            'OK \\(known_failures=1\\)\n')

    def test_skipped_test(self):
        # run a test that is skipped, and check the suite as a whole still
        # succeeds.
        # skipping_test must be hidden in here so it's not run as a real test
        class SkippingTest(tests.TestCase):
            def skipping_test(self):
                raise tests.TestSkipped('test intentionally skipped')
        runner = tests.TextTestRunner(stream=self._log_file)
        test = SkippingTest("skipping_test")
        result = self.run_test_runner(runner, test)
        self.assertTrue(result.wasSuccessful())

    def test_skipped_from_setup(self):
        calls = []
        class SkippedSetupTest(tests.TestCase):

            def setUp(self):
                calls.append('setUp')
                self.addCleanup(self.cleanup)
                raise tests.TestSkipped('skipped setup')

            def test_skip(self):
                self.fail('test reached')

            def cleanup(self):
                calls.append('cleanup')

        runner = tests.TextTestRunner(stream=self._log_file)
        test = SkippedSetupTest('test_skip')
        result = self.run_test_runner(runner, test)
        self.assertTrue(result.wasSuccessful())
        # Check if cleanup was called the right number of times.
        self.assertEqual(['setUp', 'cleanup'], calls)

    def test_skipped_from_test(self):
        calls = []
        class SkippedTest(tests.TestCase):

            def setUp(self):
                tests.TestCase.setUp(self)
                calls.append('setUp')
                self.addCleanup(self.cleanup)

            def test_skip(self):
                raise tests.TestSkipped('skipped test')

            def cleanup(self):
                calls.append('cleanup')

        runner = tests.TextTestRunner(stream=self._log_file)
        test = SkippedTest('test_skip')
        result = self.run_test_runner(runner, test)
        self.assertTrue(result.wasSuccessful())
        # Check if cleanup was called the right number of times.
        self.assertEqual(['setUp', 'cleanup'], calls)

    def test_not_applicable(self):
        # run a test that is skipped because it's not applicable
        def not_applicable_test():
            raise tests.TestNotApplicable('this test never runs')
        out = StringIO()
        runner = tests.TextTestRunner(stream=out, verbosity=2)
        test = unittest.FunctionTestCase(not_applicable_test)
        result = self.run_test_runner(runner, test)
        self._log_file.write(out.getvalue())
        self.assertTrue(result.wasSuccessful())
        self.assertTrue(result.wasStrictlySuccessful())
        self.assertContainsRe(out.getvalue(),
                r'(?m)not_applicable_test   * N/A')
        self.assertContainsRe(out.getvalue(),
                r'(?m)^    this test never runs')

    def test_not_applicable_demo(self):
        # just so you can see it in the test output
        raise tests.TestNotApplicable('this test is just a demonstation')

    def test_unsupported_features_listed(self):
        """When unsupported features are encountered they are detailed."""
        class Feature1(tests.Feature):
            def _probe(self): return False
        class Feature2(tests.Feature):
            def _probe(self): return False
        # create sample tests
        test1 = SampleTestCase('_test_pass')
        test1._test_needs_features = [Feature1()]
        test2 = SampleTestCase('_test_pass')
        test2._test_needs_features = [Feature2()]
        test = unittest.TestSuite()
        test.addTest(test1)
        test.addTest(test2)
        stream = StringIO()
        runner = tests.TextTestRunner(stream=stream)
        result = self.run_test_runner(runner, test)
        lines = stream.getvalue().splitlines()
        self.assertEqual([
            'OK',
            "Missing feature 'Feature1' skipped 1 tests.",
            "Missing feature 'Feature2' skipped 1 tests.",
            ],
            lines[-3:])

    def test_bench_history(self):
        # tests that the running the benchmark produces a history file
        # containing a timestamp and the revision id of the bzrlib source which
        # was tested.
        workingtree = _get_bzr_source_tree()
        test = TestRunner('dummy_test')
        output = StringIO()
        runner = tests.TextTestRunner(stream=self._log_file,
                                      bench_history=output)
        result = self.run_test_runner(runner, test)
        output_string = output.getvalue()
        self.assertContainsRe(output_string, "--date [0-9.]+")
        if workingtree is not None:
            revision_id = workingtree.get_parent_ids()[0]
            self.assertEndsWith(output_string.rstrip(), revision_id)

    def assertLogDeleted(self, test):
        log = test._get_log()
        self.assertEqual("DELETED log file to reduce memory footprint", log)
        self.assertEqual('', test._log_contents)
        self.assertIs(None, test._log_file_name)

    def test_success_log_deleted(self):
        """Successful tests have their log deleted"""

        class LogTester(tests.TestCase):

            def test_success(self):
                self.log('this will be removed\n')

        sio = StringIO()
        runner = tests.TextTestRunner(stream=sio)
        test = LogTester('test_success')
        result = self.run_test_runner(runner, test)

        self.assertLogDeleted(test)

    def test_skipped_log_deleted(self):
        """Skipped tests have their log deleted"""

        class LogTester(tests.TestCase):

            def test_skipped(self):
                self.log('this will be removed\n')
                raise tests.TestSkipped()

        sio = StringIO()
        runner = tests.TextTestRunner(stream=sio)
        test = LogTester('test_skipped')
        result = self.run_test_runner(runner, test)

        self.assertLogDeleted(test)

    def test_not_aplicable_log_deleted(self):
        """Not applicable tests have their log deleted"""

        class LogTester(tests.TestCase):

            def test_not_applicable(self):
                self.log('this will be removed\n')
                raise tests.TestNotApplicable()

        sio = StringIO()
        runner = tests.TextTestRunner(stream=sio)
        test = LogTester('test_not_applicable')
        result = self.run_test_runner(runner, test)

        self.assertLogDeleted(test)

    def test_known_failure_log_deleted(self):
        """Know failure tests have their log deleted"""

        class LogTester(tests.TestCase):

            def test_known_failure(self):
                self.log('this will be removed\n')
                raise tests.KnownFailure()

        sio = StringIO()
        runner = tests.TextTestRunner(stream=sio)
        test = LogTester('test_known_failure')
        result = self.run_test_runner(runner, test)

        self.assertLogDeleted(test)

    def test_fail_log_kept(self):
        """Failed tests have their log kept"""

        class LogTester(tests.TestCase):

            def test_fail(self):
                self.log('this will be kept\n')
                self.fail('this test fails')

        sio = StringIO()
        runner = tests.TextTestRunner(stream=sio)
        test = LogTester('test_fail')
        result = self.run_test_runner(runner, test)

        text = sio.getvalue()
        self.assertContainsRe(text, 'this will be kept')
        self.assertContainsRe(text, 'this test fails')

        log = test._get_log()
        self.assertContainsRe(log, 'this will be kept')
        self.assertEqual(log, test._log_contents)

    def test_error_log_kept(self):
        """Tests with errors have their log kept"""

        class LogTester(tests.TestCase):

            def test_error(self):
                self.log('this will be kept\n')
                raise ValueError('random exception raised')

        sio = StringIO()
        runner = tests.TextTestRunner(stream=sio)
        test = LogTester('test_error')
        result = self.run_test_runner(runner, test)

        text = sio.getvalue()
        self.assertContainsRe(text, 'this will be kept')
        self.assertContainsRe(text, 'random exception raised')

        log = test._get_log()
        self.assertContainsRe(log, 'this will be kept')
        self.assertEqual(log, test._log_contents)


class SampleTestCase(tests.TestCase):

    def _test_pass(self):
        pass

class _TestException(Exception):
    pass


class TestTestCase(tests.TestCase):
    """Tests that test the core bzrlib TestCase."""

    def test_assertLength_matches_empty(self):
        a_list = []
        self.assertLength(0, a_list)

    def test_assertLength_matches_nonempty(self):
        a_list = [1, 2, 3]
        self.assertLength(3, a_list)

    def test_assertLength_fails_different(self):
        a_list = []
        self.assertRaises(AssertionError, self.assertLength, 1, a_list)

    def test_assertLength_shows_sequence_in_failure(self):
        a_list = [1, 2, 3]
        exception = self.assertRaises(AssertionError, self.assertLength, 2,
            a_list)
        self.assertEqual('Incorrect length: wanted 2, got 3 for [1, 2, 3]',
            exception.args[0])

    def test_base_setUp_not_called_causes_failure(self):
        class TestCaseWithBrokenSetUp(tests.TestCase):
            def setUp(self):
                pass # does not call TestCase.setUp
            def test_foo(self):
                pass
        test = TestCaseWithBrokenSetUp('test_foo')
        result = unittest.TestResult()
        test.run(result)
        self.assertFalse(result.wasSuccessful())
        self.assertEqual(1, result.testsRun)

    def test_base_tearDown_not_called_causes_failure(self):
        class TestCaseWithBrokenTearDown(tests.TestCase):
            def tearDown(self):
                pass # does not call TestCase.tearDown
            def test_foo(self):
                pass
        test = TestCaseWithBrokenTearDown('test_foo')
        result = unittest.TestResult()
        test.run(result)
        self.assertFalse(result.wasSuccessful())
        self.assertEqual(1, result.testsRun)

    def test_debug_flags_sanitised(self):
        """The bzrlib debug flags should be sanitised by setUp."""
        if 'allow_debug' in tests.selftest_debug_flags:
            raise tests.TestNotApplicable(
                '-Eallow_debug option prevents debug flag sanitisation')
        # we could set something and run a test that will check
        # it gets santised, but this is probably sufficient for now:
        # if someone runs the test with -Dsomething it will error.
        flags = set()
        if self._lock_check_thorough:
            flags.add('strict_locks')
        self.assertEqual(flags, bzrlib.debug.debug_flags)

    def change_selftest_debug_flags(self, new_flags):
        orig_selftest_flags = tests.selftest_debug_flags
        self.addCleanup(self._restore_selftest_debug_flags, orig_selftest_flags)
        tests.selftest_debug_flags = set(new_flags)

    def _restore_selftest_debug_flags(self, flags):
        tests.selftest_debug_flags = flags

    def test_allow_debug_flag(self):
        """The -Eallow_debug flag prevents bzrlib.debug.debug_flags from being
        sanitised (i.e. cleared) before running a test.
        """
        self.change_selftest_debug_flags(set(['allow_debug']))
        bzrlib.debug.debug_flags = set(['a-flag'])
        class TestThatRecordsFlags(tests.TestCase):
            def test_foo(nested_self):
                self.flags = set(bzrlib.debug.debug_flags)
        test = TestThatRecordsFlags('test_foo')
        test.run(self.make_test_result())
        flags = set(['a-flag'])
        if 'disable_lock_checks' not in tests.selftest_debug_flags:
            flags.add('strict_locks')
        self.assertEqual(flags, self.flags)

    def test_disable_lock_checks(self):
        """The -Edisable_lock_checks flag disables thorough checks."""
        class TestThatRecordsFlags(tests.TestCase):
            def test_foo(nested_self):
                self.flags = set(bzrlib.debug.debug_flags)
                self.test_lock_check_thorough = nested_self._lock_check_thorough
        self.change_selftest_debug_flags(set())
        test = TestThatRecordsFlags('test_foo')
        test.run(self.make_test_result())
        # By default we do strict lock checking and thorough lock/unlock
        # tracking.
        self.assertTrue(self.test_lock_check_thorough)
        self.assertEqual(set(['strict_locks']), self.flags)
        # Now set the disable_lock_checks flag, and show that this changed.
        self.change_selftest_debug_flags(set(['disable_lock_checks']))
        test = TestThatRecordsFlags('test_foo')
        test.run(self.make_test_result())
        self.assertFalse(self.test_lock_check_thorough)
        self.assertEqual(set(), self.flags)

    def test_this_fails_strict_lock_check(self):
        class TestThatRecordsFlags(tests.TestCase):
            def test_foo(nested_self):
                self.flags1 = set(bzrlib.debug.debug_flags)
                self.thisFailsStrictLockCheck()
                self.flags2 = set(bzrlib.debug.debug_flags)
        # Make sure lock checking is active
        self.change_selftest_debug_flags(set())
        test = TestThatRecordsFlags('test_foo')
        test.run(self.make_test_result())
        self.assertEqual(set(['strict_locks']), self.flags1)
        self.assertEqual(set(), self.flags2)

    def test_debug_flags_restored(self):
        """The bzrlib debug flags should be restored to their original state
        after the test was run, even if allow_debug is set.
        """
        self.change_selftest_debug_flags(set(['allow_debug']))
        # Now run a test that modifies debug.debug_flags.
        bzrlib.debug.debug_flags = set(['original-state'])
        class TestThatModifiesFlags(tests.TestCase):
            def test_foo(self):
                bzrlib.debug.debug_flags = set(['modified'])
        test = TestThatModifiesFlags('test_foo')
        test.run(self.make_test_result())
        self.assertEqual(set(['original-state']), bzrlib.debug.debug_flags)

    def make_test_result(self):
        return tests.TextTestResult(self._log_file, descriptions=0, verbosity=1)

    def inner_test(self):
        # the inner child test
        note("inner_test")

    def outer_child(self):
        # the outer child test
        note("outer_start")
        self.inner_test = TestTestCase("inner_child")
        result = self.make_test_result()
        self.inner_test.run(result)
        note("outer finish")

    def test_trace_nesting(self):
        # this tests that each test case nests its trace facility correctly.
        # we do this by running a test case manually. That test case (A)
        # should setup a new log, log content to it, setup a child case (B),
        # which should log independently, then case (A) should log a trailer
        # and return.
        # we do two nested children so that we can verify the state of the
        # logs after the outer child finishes is correct, which a bad clean
        # up routine in tearDown might trigger a fault in our test with only
        # one child, we should instead see the bad result inside our test with
        # the two children.
        # the outer child test
        original_trace = bzrlib.trace._trace_file
        outer_test = TestTestCase("outer_child")
        result = self.make_test_result()
        outer_test.run(result)
        self.assertEqual(original_trace, bzrlib.trace._trace_file)

    def method_that_times_a_bit_twice(self):
        # call self.time twice to ensure it aggregates
        self.time(time.sleep, 0.007)
        self.time(time.sleep, 0.007)

    def test_time_creates_benchmark_in_result(self):
        """Test that the TestCase.time() method accumulates a benchmark time."""
        sample_test = TestTestCase("method_that_times_a_bit_twice")
        output_stream = StringIO()
        result = bzrlib.tests.VerboseTestResult(
            unittest._WritelnDecorator(output_stream),
            descriptions=0,
            verbosity=2)
        sample_test.run(result)
        self.assertContainsRe(
            output_stream.getvalue(),
            r"\d+ms\*\n$")

    def test_hooks_sanitised(self):
        """The bzrlib hooks should be sanitised by setUp."""
        # Note this test won't fail with hooks that the core library doesn't
        # use - but it trigger with a plugin that adds hooks, so its still a
        # useful warning in that case.
        self.assertEqual(bzrlib.branch.BranchHooks(),
            bzrlib.branch.Branch.hooks)
        self.assertEqual(bzrlib.smart.server.SmartServerHooks(),
            bzrlib.smart.server.SmartTCPServer.hooks)
        self.assertEqual(bzrlib.commands.CommandHooks(),
            bzrlib.commands.Command.hooks)

    def test__gather_lsprof_in_benchmarks(self):
        """When _gather_lsprof_in_benchmarks is on, accumulate profile data.

        Each self.time() call is individually and separately profiled.
        """
        self.requireFeature(test_lsprof.LSProfFeature)
        # overrides the class member with an instance member so no cleanup
        # needed.
        self._gather_lsprof_in_benchmarks = True
        self.time(time.sleep, 0.000)
        self.time(time.sleep, 0.003)
        self.assertEqual(2, len(self._benchcalls))
        self.assertEqual((time.sleep, (0.000,), {}), self._benchcalls[0][0])
        self.assertEqual((time.sleep, (0.003,), {}), self._benchcalls[1][0])
        self.assertIsInstance(self._benchcalls[0][1], bzrlib.lsprof.Stats)
        self.assertIsInstance(self._benchcalls[1][1], bzrlib.lsprof.Stats)

    def test_knownFailure(self):
        """Self.knownFailure() should raise a KnownFailure exception."""
        self.assertRaises(tests.KnownFailure, self.knownFailure, "A Failure")

    def test_requireFeature_available(self):
        """self.requireFeature(available) is a no-op."""
        class Available(tests.Feature):
            def _probe(self):return True
        feature = Available()
        self.requireFeature(feature)

    def test_requireFeature_unavailable(self):
        """self.requireFeature(unavailable) raises UnavailableFeature."""
        class Unavailable(tests.Feature):
            def _probe(self):return False
        feature = Unavailable()
        self.assertRaises(tests.UnavailableFeature,
                          self.requireFeature, feature)

    def test_run_no_parameters(self):
        test = SampleTestCase('_test_pass')
        test.run()

    def test_run_enabled_unittest_result(self):
        """Test we revert to regular behaviour when the test is enabled."""
        test = SampleTestCase('_test_pass')
        class EnabledFeature(object):
            def available(self):
                return True
        test._test_needs_features = [EnabledFeature()]
        result = unittest.TestResult()
        test.run(result)
        self.assertEqual(1, result.testsRun)
        self.assertEqual([], result.errors)
        self.assertEqual([], result.failures)

    def test_run_disabled_unittest_result(self):
        """Test our compatability for disabled tests with unittest results."""
        test = SampleTestCase('_test_pass')
        class DisabledFeature(object):
            def available(self):
                return False
        test._test_needs_features = [DisabledFeature()]
        result = unittest.TestResult()
        test.run(result)
        self.assertEqual(1, result.testsRun)
        self.assertEqual([], result.errors)
        self.assertEqual([], result.failures)

    def test_run_disabled_supporting_result(self):
        """Test disabled tests behaviour with support aware results."""
        test = SampleTestCase('_test_pass')
        class DisabledFeature(object):
            def available(self):
                return False
        the_feature = DisabledFeature()
        test._test_needs_features = [the_feature]
        class InstrumentedTestResult(unittest.TestResult):
            def __init__(self):
                unittest.TestResult.__init__(self)
                self.calls = []
            def startTest(self, test):
                self.calls.append(('startTest', test))
            def stopTest(self, test):
                self.calls.append(('stopTest', test))
            def addNotSupported(self, test, feature):
                self.calls.append(('addNotSupported', test, feature))
        result = InstrumentedTestResult()
        test.run(result)
        self.assertEqual([
            ('startTest', test),
            ('addNotSupported', test, the_feature),
            ('stopTest', test),
            ],
            result.calls)

    def test_assert_list_raises_on_generator(self):
        def generator_which_will_raise():
            # This will not raise until after the first yield
            yield 1
            raise _TestException()

        e = self.assertListRaises(_TestException, generator_which_will_raise)
        self.assertIsInstance(e, _TestException)

        e = self.assertListRaises(Exception, generator_which_will_raise)
        self.assertIsInstance(e, _TestException)

    def test_assert_list_raises_on_plain(self):
        def plain_exception():
            raise _TestException()
            return []

        e = self.assertListRaises(_TestException, plain_exception)
        self.assertIsInstance(e, _TestException)

        e = self.assertListRaises(Exception, plain_exception)
        self.assertIsInstance(e, _TestException)

    def test_assert_list_raises_assert_wrong_exception(self):
        class _NotTestException(Exception):
            pass

        def wrong_exception():
            raise _NotTestException()

        def wrong_exception_generator():
            yield 1
            yield 2
            raise _NotTestException()

        # Wrong exceptions are not intercepted
        self.assertRaises(_NotTestException,
            self.assertListRaises, _TestException, wrong_exception)
        self.assertRaises(_NotTestException,
            self.assertListRaises, _TestException, wrong_exception_generator)

    def test_assert_list_raises_no_exception(self):
        def success():
            return []

        def success_generator():
            yield 1
            yield 2

        self.assertRaises(AssertionError,
            self.assertListRaises, _TestException, success)

        self.assertRaises(AssertionError,
            self.assertListRaises, _TestException, success_generator)


# NB: Don't delete this; it's not actually from 0.11!
@deprecated_function(deprecated_in((0, 11, 0)))
def sample_deprecated_function():
    """A deprecated function to test applyDeprecated with."""
    return 2


def sample_undeprecated_function(a_param):
    """A undeprecated function to test applyDeprecated with."""


class ApplyDeprecatedHelper(object):
    """A helper class for ApplyDeprecated tests."""

    @deprecated_method(deprecated_in((0, 11, 0)))
    def sample_deprecated_method(self, param_one):
        """A deprecated method for testing with."""
        return param_one

    def sample_normal_method(self):
        """A undeprecated method."""

    @deprecated_method(deprecated_in((0, 10, 0)))
    def sample_nested_deprecation(self):
        return sample_deprecated_function()


class TestExtraAssertions(tests.TestCase):
    """Tests for new test assertions in bzrlib test suite"""

    def test_assert_isinstance(self):
        self.assertIsInstance(2, int)
        self.assertIsInstance(u'', basestring)
        e = self.assertRaises(AssertionError, self.assertIsInstance, None, int)
        self.assertEquals(str(e),
            "None is an instance of <type 'NoneType'> rather than <type 'int'>")
        self.assertRaises(AssertionError, self.assertIsInstance, 23.3, int)
        e = self.assertRaises(AssertionError,
            self.assertIsInstance, None, int, "it's just not")
        self.assertEquals(str(e),
            "None is an instance of <type 'NoneType'> rather than <type 'int'>"
            ": it's just not")

    def test_assertEndsWith(self):
        self.assertEndsWith('foo', 'oo')
        self.assertRaises(AssertionError, self.assertEndsWith, 'o', 'oo')

    def test_applyDeprecated_not_deprecated(self):
        sample_object = ApplyDeprecatedHelper()
        # calling an undeprecated callable raises an assertion
        self.assertRaises(AssertionError, self.applyDeprecated,
            deprecated_in((0, 11, 0)),
            sample_object.sample_normal_method)
        self.assertRaises(AssertionError, self.applyDeprecated,
            deprecated_in((0, 11, 0)),
            sample_undeprecated_function, "a param value")
        # calling a deprecated callable (function or method) with the wrong
        # expected deprecation fails.
        self.assertRaises(AssertionError, self.applyDeprecated,
            deprecated_in((0, 10, 0)),
            sample_object.sample_deprecated_method, "a param value")
        self.assertRaises(AssertionError, self.applyDeprecated,
            deprecated_in((0, 10, 0)),
            sample_deprecated_function)
        # calling a deprecated callable (function or method) with the right
        # expected deprecation returns the functions result.
        self.assertEqual("a param value",
            self.applyDeprecated(deprecated_in((0, 11, 0)),
            sample_object.sample_deprecated_method, "a param value"))
        self.assertEqual(2, self.applyDeprecated(deprecated_in((0, 11, 0)),
            sample_deprecated_function))
        # calling a nested deprecation with the wrong deprecation version
        # fails even if a deeper nested function was deprecated with the
        # supplied version.
        self.assertRaises(AssertionError, self.applyDeprecated,
            deprecated_in((0, 11, 0)), sample_object.sample_nested_deprecation)
        # calling a nested deprecation with the right deprecation value
        # returns the calls result.
        self.assertEqual(2, self.applyDeprecated(deprecated_in((0, 10, 0)),
            sample_object.sample_nested_deprecation))

    def test_callDeprecated(self):
        def testfunc(be_deprecated, result=None):
            if be_deprecated is True:
                symbol_versioning.warn('i am deprecated', DeprecationWarning,
                                       stacklevel=1)
            return result
        result = self.callDeprecated(['i am deprecated'], testfunc, True)
        self.assertIs(None, result)
        result = self.callDeprecated([], testfunc, False, 'result')
        self.assertEqual('result', result)
        self.callDeprecated(['i am deprecated'], testfunc, be_deprecated=True)
        self.callDeprecated([], testfunc, be_deprecated=False)


class TestWarningTests(tests.TestCase):
    """Tests for calling methods that raise warnings."""

    def test_callCatchWarnings(self):
        def meth(a, b):
            warnings.warn("this is your last warning")
            return a + b
        wlist, result = self.callCatchWarnings(meth, 1, 2)
        self.assertEquals(3, result)
        # would like just to compare them, but UserWarning doesn't implement
        # eq well
        w0, = wlist
        self.assertIsInstance(w0, UserWarning)
        self.assertEquals("this is your last warning", str(w0))


class TestConvenienceMakers(tests.TestCaseWithTransport):
    """Test for the make_* convenience functions."""

    def test_make_branch_and_tree_with_format(self):
        # we should be able to supply a format to make_branch_and_tree
        self.make_branch_and_tree('a', format=bzrlib.bzrdir.BzrDirMetaFormat1())
        self.make_branch_and_tree('b', format=bzrlib.bzrdir.BzrDirFormat6())
        self.assertIsInstance(bzrlib.bzrdir.BzrDir.open('a')._format,
                              bzrlib.bzrdir.BzrDirMetaFormat1)
        self.assertIsInstance(bzrlib.bzrdir.BzrDir.open('b')._format,
                              bzrlib.bzrdir.BzrDirFormat6)

    def test_make_branch_and_memory_tree(self):
        # we should be able to get a new branch and a mutable tree from
        # TestCaseWithTransport
        tree = self.make_branch_and_memory_tree('a')
        self.assertIsInstance(tree, bzrlib.memorytree.MemoryTree)


class TestSFTPMakeBranchAndTree(test_sftp_transport.TestCaseWithSFTPServer):

    def test_make_tree_for_sftp_branch(self):
        """Transports backed by local directories create local trees."""

        tree = self.make_branch_and_tree('t1')
        base = tree.bzrdir.root_transport.base
        self.failIf(base.startswith('sftp'),
                'base %r is on sftp but should be local' % base)
        self.assertEquals(tree.bzrdir.root_transport,
                tree.branch.bzrdir.root_transport)
        self.assertEquals(tree.bzrdir.root_transport,
                tree.branch.repository.bzrdir.root_transport)


class TestSelftest(tests.TestCase):
    """Tests of bzrlib.tests.selftest."""

    def test_selftest_benchmark_parameter_invokes_test_suite__benchmark__(self):
        factory_called = []
        def factory():
            factory_called.append(True)
            return TestUtil.TestSuite()
        out = StringIO()
        err = StringIO()
        self.apply_redirected(out, err, None, bzrlib.tests.selftest,
            test_suite_factory=factory)
        self.assertEqual([True], factory_called)


class TestKnownFailure(tests.TestCase):

    def test_known_failure(self):
        """Check that KnownFailure is defined appropriately."""
        # a KnownFailure is an assertion error for compatability with unaware
        # runners.
        self.assertIsInstance(tests.KnownFailure(""), AssertionError)

    def test_expect_failure(self):
        try:
            self.expectFailure("Doomed to failure", self.assertTrue, False)
        except tests.KnownFailure, e:
            self.assertEqual('Doomed to failure', e.args[0])
        try:
            self.expectFailure("Doomed to failure", self.assertTrue, True)
        except AssertionError, e:
            self.assertEqual('Unexpected success.  Should have failed:'
                             ' Doomed to failure', e.args[0])
        else:
            self.fail('Assertion not raised')


class TestFeature(tests.TestCase):

    def test_caching(self):
        """Feature._probe is called by the feature at most once."""
        class InstrumentedFeature(tests.Feature):
            def __init__(self):
                super(InstrumentedFeature, self).__init__()
                self.calls = []
            def _probe(self):
                self.calls.append('_probe')
                return False
        feature = InstrumentedFeature()
        feature.available()
        self.assertEqual(['_probe'], feature.calls)
        feature.available()
        self.assertEqual(['_probe'], feature.calls)

    def test_named_str(self):
        """Feature.__str__ should thunk to feature_name()."""
        class NamedFeature(tests.Feature):
            def feature_name(self):
                return 'symlinks'
        feature = NamedFeature()
        self.assertEqual('symlinks', str(feature))

    def test_default_str(self):
        """Feature.__str__ should default to __class__.__name__."""
        class NamedFeature(tests.Feature):
            pass
        feature = NamedFeature()
        self.assertEqual('NamedFeature', str(feature))


class TestUnavailableFeature(tests.TestCase):

    def test_access_feature(self):
        feature = tests.Feature()
        exception = tests.UnavailableFeature(feature)
        self.assertIs(feature, exception.args[0])


class TestSelftestFiltering(tests.TestCase):

    def setUp(self):
        tests.TestCase.setUp(self)
        self.suite = TestUtil.TestSuite()
        self.loader = TestUtil.TestLoader()
        self.suite.addTest(self.loader.loadTestsFromModuleNames([
            'bzrlib.tests.test_selftest']))
        self.all_names = _test_ids(self.suite)

    def test_condition_id_re(self):
        test_name = ('bzrlib.tests.test_selftest.TestSelftestFiltering.'
            'test_condition_id_re')
        filtered_suite = tests.filter_suite_by_condition(
            self.suite, tests.condition_id_re('test_condition_id_re'))
        self.assertEqual([test_name], _test_ids(filtered_suite))

    def test_condition_id_in_list(self):
        test_names = ['bzrlib.tests.test_selftest.TestSelftestFiltering.'
                      'test_condition_id_in_list']
        id_list = tests.TestIdList(test_names)
        filtered_suite = tests.filter_suite_by_condition(
            self.suite, tests.condition_id_in_list(id_list))
        my_pattern = 'TestSelftestFiltering.*test_condition_id_in_list'
        re_filtered = tests.filter_suite_by_re(self.suite, my_pattern)
        self.assertEqual(_test_ids(re_filtered), _test_ids(filtered_suite))

    def test_condition_id_startswith(self):
        klass = 'bzrlib.tests.test_selftest.TestSelftestFiltering.'
        start1 = klass + 'test_condition_id_starts'
        start2 = klass + 'test_condition_id_in'
        test_names = [ klass + 'test_condition_id_in_list',
                      klass + 'test_condition_id_startswith',
                     ]
        filtered_suite = tests.filter_suite_by_condition(
            self.suite, tests.condition_id_startswith([start1, start2]))
        self.assertEqual(test_names, _test_ids(filtered_suite))

    def test_condition_isinstance(self):
        filtered_suite = tests.filter_suite_by_condition(
            self.suite, tests.condition_isinstance(self.__class__))
        class_pattern = 'bzrlib.tests.test_selftest.TestSelftestFiltering.'
        re_filtered = tests.filter_suite_by_re(self.suite, class_pattern)
        self.assertEqual(_test_ids(re_filtered), _test_ids(filtered_suite))

    def test_exclude_tests_by_condition(self):
        excluded_name = ('bzrlib.tests.test_selftest.TestSelftestFiltering.'
            'test_exclude_tests_by_condition')
        filtered_suite = tests.exclude_tests_by_condition(self.suite,
            lambda x:x.id() == excluded_name)
        self.assertEqual(len(self.all_names) - 1,
            filtered_suite.countTestCases())
        self.assertFalse(excluded_name in _test_ids(filtered_suite))
        remaining_names = list(self.all_names)
        remaining_names.remove(excluded_name)
        self.assertEqual(remaining_names, _test_ids(filtered_suite))

    def test_exclude_tests_by_re(self):
        self.all_names = _test_ids(self.suite)
        filtered_suite = tests.exclude_tests_by_re(self.suite,
                                                   'exclude_tests_by_re')
        excluded_name = ('bzrlib.tests.test_selftest.TestSelftestFiltering.'
            'test_exclude_tests_by_re')
        self.assertEqual(len(self.all_names) - 1,
            filtered_suite.countTestCases())
        self.assertFalse(excluded_name in _test_ids(filtered_suite))
        remaining_names = list(self.all_names)
        remaining_names.remove(excluded_name)
        self.assertEqual(remaining_names, _test_ids(filtered_suite))

    def test_filter_suite_by_condition(self):
        test_name = ('bzrlib.tests.test_selftest.TestSelftestFiltering.'
            'test_filter_suite_by_condition')
        filtered_suite = tests.filter_suite_by_condition(self.suite,
            lambda x:x.id() == test_name)
        self.assertEqual([test_name], _test_ids(filtered_suite))

    def test_filter_suite_by_re(self):
        filtered_suite = tests.filter_suite_by_re(self.suite,
                                                  'test_filter_suite_by_r')
        filtered_names = _test_ids(filtered_suite)
        self.assertEqual(filtered_names, ['bzrlib.tests.test_selftest.'
            'TestSelftestFiltering.test_filter_suite_by_re'])

    def test_filter_suite_by_id_list(self):
        test_list = ['bzrlib.tests.test_selftest.'
                     'TestSelftestFiltering.test_filter_suite_by_id_list']
        filtered_suite = tests.filter_suite_by_id_list(
            self.suite, tests.TestIdList(test_list))
        filtered_names = _test_ids(filtered_suite)
        self.assertEqual(
            filtered_names,
            ['bzrlib.tests.test_selftest.'
             'TestSelftestFiltering.test_filter_suite_by_id_list'])

    def test_filter_suite_by_id_startswith(self):
        # By design this test may fail if another test is added whose name also
        # begins with one of the start value used.
        klass = 'bzrlib.tests.test_selftest.TestSelftestFiltering.'
        start1 = klass + 'test_filter_suite_by_id_starts'
        start2 = klass + 'test_filter_suite_by_id_li'
        test_list = [klass + 'test_filter_suite_by_id_list',
                     klass + 'test_filter_suite_by_id_startswith',
                     ]
        filtered_suite = tests.filter_suite_by_id_startswith(
            self.suite, [start1, start2])
        self.assertEqual(
            test_list,
            _test_ids(filtered_suite),
            )

    def test_preserve_input(self):
        # NB: Surely this is something in the stdlib to do this?
        self.assertTrue(self.suite is tests.preserve_input(self.suite))
        self.assertTrue("@#$" is tests.preserve_input("@#$"))

    def test_randomize_suite(self):
        randomized_suite = tests.randomize_suite(self.suite)
        # randomizing should not add or remove test names.
        self.assertEqual(set(_test_ids(self.suite)),
                         set(_test_ids(randomized_suite)))
        # Technically, this *can* fail, because random.shuffle(list) can be
        # equal to list. Trying multiple times just pushes the frequency back.
        # As its len(self.all_names)!:1, the failure frequency should be low
        # enough to ignore. RBC 20071021.
        # It should change the order.
        self.assertNotEqual(self.all_names, _test_ids(randomized_suite))
        # But not the length. (Possibly redundant with the set test, but not
        # necessarily.)
        self.assertEqual(len(self.all_names), len(_test_ids(randomized_suite)))

    def test_split_suit_by_condition(self):
        self.all_names = _test_ids(self.suite)
        condition = tests.condition_id_re('test_filter_suite_by_r')
        split_suite = tests.split_suite_by_condition(self.suite, condition)
        filtered_name = ('bzrlib.tests.test_selftest.TestSelftestFiltering.'
            'test_filter_suite_by_re')
        self.assertEqual([filtered_name], _test_ids(split_suite[0]))
        self.assertFalse(filtered_name in _test_ids(split_suite[1]))
        remaining_names = list(self.all_names)
        remaining_names.remove(filtered_name)
        self.assertEqual(remaining_names, _test_ids(split_suite[1]))

    def test_split_suit_by_re(self):
        self.all_names = _test_ids(self.suite)
        split_suite = tests.split_suite_by_re(self.suite,
                                              'test_filter_suite_by_r')
        filtered_name = ('bzrlib.tests.test_selftest.TestSelftestFiltering.'
            'test_filter_suite_by_re')
        self.assertEqual([filtered_name], _test_ids(split_suite[0]))
        self.assertFalse(filtered_name in _test_ids(split_suite[1]))
        remaining_names = list(self.all_names)
        remaining_names.remove(filtered_name)
        self.assertEqual(remaining_names, _test_ids(split_suite[1]))


class TestCheckInventoryShape(tests.TestCaseWithTransport):

    def test_check_inventory_shape(self):
        files = ['a', 'b/', 'b/c']
        tree = self.make_branch_and_tree('.')
        self.build_tree(files)
        tree.add(files)
        tree.lock_read()
        try:
            self.check_inventory_shape(tree.inventory, files)
        finally:
            tree.unlock()


class TestBlackboxSupport(tests.TestCase):
    """Tests for testsuite blackbox features."""

    def test_run_bzr_failure_not_caught(self):
        # When we run bzr in blackbox mode, we want any unexpected errors to
        # propagate up to the test suite so that it can show the error in the
        # usual way, and we won't get a double traceback.
        e = self.assertRaises(
            AssertionError,
            self.run_bzr, ['assert-fail'])
        # make sure we got the real thing, not an error from somewhere else in
        # the test framework
        self.assertEquals('always fails', str(e))
        # check that there's no traceback in the test log
        self.assertNotContainsRe(self._get_log(keep_log_file=True),
            r'Traceback')

    def test_run_bzr_user_error_caught(self):
        # Running bzr in blackbox mode, normal/expected/user errors should be
        # caught in the regular way and turned into an error message plus exit
        # code.
        out, err = self.run_bzr(["log", "/nonexistantpath"], retcode=3)
        self.assertEqual(out, '')
        self.assertContainsRe(err,
            'bzr: ERROR: Not a branch: ".*nonexistantpath/".\n')


class TestTestLoader(tests.TestCase):
    """Tests for the test loader."""

    def _get_loader_and_module(self):
        """Gets a TestLoader and a module with one test in it."""
        loader = TestUtil.TestLoader()
        module = {}
        class Stub(tests.TestCase):
            def test_foo(self):
                pass
        class MyModule(object):
            pass
        MyModule.a_class = Stub
        module = MyModule()
        return loader, module

    def test_module_no_load_tests_attribute_loads_classes(self):
        loader, module = self._get_loader_and_module()
        self.assertEqual(1, loader.loadTestsFromModule(module).countTestCases())

    def test_module_load_tests_attribute_gets_called(self):
        loader, module = self._get_loader_and_module()
        # 'self' is here because we're faking the module with a class. Regular
        # load_tests do not need that :)
        def load_tests(self, standard_tests, module, loader):
            result = loader.suiteClass()
            for test in tests.iter_suite_tests(standard_tests):
                result.addTests([test, test])
            return result
        # add a load_tests() method which multiplies the tests from the module.
        module.__class__.load_tests = load_tests
        self.assertEqual(2, loader.loadTestsFromModule(module).countTestCases())

    def test_load_tests_from_module_name_smoke_test(self):
        loader = TestUtil.TestLoader()
        suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
        self.assertEquals(['bzrlib.tests.test_sampler.DemoTest.test_nothing'],
                          _test_ids(suite))

    def test_load_tests_from_module_name_with_bogus_module_name(self):
        loader = TestUtil.TestLoader()
        self.assertRaises(ImportError, loader.loadTestsFromModuleName, 'bogus')


class TestTestIdList(tests.TestCase):

    def _create_id_list(self, test_list):
        return tests.TestIdList(test_list)

    def _create_suite(self, test_id_list):

        class Stub(tests.TestCase):
            def test_foo(self):
                pass

        def _create_test_id(id):
            return lambda: id

        suite = TestUtil.TestSuite()
        for id in test_id_list:
            t  = Stub('test_foo')
            t.id = _create_test_id(id)
            suite.addTest(t)
        return suite

    def _test_ids(self, test_suite):
        """Get the ids for the tests in a test suite."""
        return [t.id() for t in tests.iter_suite_tests(test_suite)]

    def test_empty_list(self):
        id_list = self._create_id_list([])
        self.assertEquals({}, id_list.tests)
        self.assertEquals({}, id_list.modules)

    def test_valid_list(self):
        id_list = self._create_id_list(
            ['mod1.cl1.meth1', 'mod1.cl1.meth2',
             'mod1.func1', 'mod1.cl2.meth2',
             'mod1.submod1',
             'mod1.submod2.cl1.meth1', 'mod1.submod2.cl2.meth2',
             ])
        self.assertTrue(id_list.refers_to('mod1'))
        self.assertTrue(id_list.refers_to('mod1.submod1'))
        self.assertTrue(id_list.refers_to('mod1.submod2'))
        self.assertTrue(id_list.includes('mod1.cl1.meth1'))
        self.assertTrue(id_list.includes('mod1.submod1'))
        self.assertTrue(id_list.includes('mod1.func1'))

    def test_bad_chars_in_params(self):
        id_list = self._create_id_list(['mod1.cl1.meth1(xx.yy)'])
        self.assertTrue(id_list.refers_to('mod1'))
        self.assertTrue(id_list.includes('mod1.cl1.meth1(xx.yy)'))

    def test_module_used(self):
        id_list = self._create_id_list(['mod.class.meth'])
        self.assertTrue(id_list.refers_to('mod'))
        self.assertTrue(id_list.refers_to('mod.class'))
        self.assertTrue(id_list.refers_to('mod.class.meth'))

    def test_test_suite_matches_id_list_with_unknown(self):
        loader = TestUtil.TestLoader()
        suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
        test_list = ['bzrlib.tests.test_sampler.DemoTest.test_nothing',
                     'bogus']
        not_found, duplicates = tests.suite_matches_id_list(suite, test_list)
        self.assertEquals(['bogus'], not_found)
        self.assertEquals([], duplicates)

    def test_suite_matches_id_list_with_duplicates(self):
        loader = TestUtil.TestLoader()
        suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
        dupes = loader.suiteClass()
        for test in tests.iter_suite_tests(suite):
            dupes.addTest(test)
            dupes.addTest(test) # Add it again

        test_list = ['bzrlib.tests.test_sampler.DemoTest.test_nothing',]
        not_found, duplicates = tests.suite_matches_id_list(
            dupes, test_list)
        self.assertEquals([], not_found)
        self.assertEquals(['bzrlib.tests.test_sampler.DemoTest.test_nothing'],
                          duplicates)


class TestTestSuite(tests.TestCase):

    def test_test_suite(self):
        # This test is slow, so we do a single test with one test in each
        # category
        test_list = [
            # testmod_names
            'bzrlib.tests.blackbox.test_branch.TestBranch.test_branch',
            ('bzrlib.tests.per_transport.TransportTests'
             '.test_abspath(LocalURLServer)'),
            'bzrlib.tests.test_selftest.TestTestSuite.test_test_suite',
            # modules_to_doctest
            'bzrlib.timestamp.format_highres_date',
            # plugins can't be tested that way since selftest may be run with
            # --no-plugins
            ]
        suite = tests.test_suite(test_list)
        self.assertEquals(test_list, _test_ids(suite))

    def test_test_suite_list_and_start(self):
        test_list = ['bzrlib.tests.test_selftest.TestTestSuite.test_test_suite']
        suite = tests.test_suite(test_list,
                                 ['bzrlib.tests.test_selftest.TestTestSuite'])
        # test_test_suite_list_and_start is not included 
        self.assertEquals(test_list, _test_ids(suite))


class TestLoadTestIdList(tests.TestCaseInTempDir):

    def _create_test_list_file(self, file_name, content):
        fl = open(file_name, 'wt')
        fl.write(content)
        fl.close()

    def test_load_unknown(self):
        self.assertRaises(errors.NoSuchFile,
                          tests.load_test_id_list, 'i_do_not_exist')

    def test_load_test_list(self):
        test_list_fname = 'test.list'
        self._create_test_list_file(test_list_fname,
                                    'mod1.cl1.meth1\nmod2.cl2.meth2\n')
        tlist = tests.load_test_id_list(test_list_fname)
        self.assertEquals(2, len(tlist))
        self.assertEquals('mod1.cl1.meth1', tlist[0])
        self.assertEquals('mod2.cl2.meth2', tlist[1])

    def test_load_dirty_file(self):
        test_list_fname = 'test.list'
        self._create_test_list_file(test_list_fname,
                                    '  mod1.cl1.meth1\n\nmod2.cl2.meth2  \n'
                                    'bar baz\n')
        tlist = tests.load_test_id_list(test_list_fname)
        self.assertEquals(4, len(tlist))
        self.assertEquals('mod1.cl1.meth1', tlist[0])
        self.assertEquals('', tlist[1])
        self.assertEquals('mod2.cl2.meth2', tlist[2])
        self.assertEquals('bar baz', tlist[3])


class TestFilteredByModuleTestLoader(tests.TestCase):

    def _create_loader(self, test_list):
        id_filter = tests.TestIdList(test_list)
        loader = TestUtil.FilteredByModuleTestLoader(id_filter.refers_to)
        return loader

    def test_load_tests(self):
        test_list = ['bzrlib.tests.test_sampler.DemoTest.test_nothing']
        loader = self._create_loader(test_list)

        suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
        self.assertEquals(test_list, _test_ids(suite))

    def test_exclude_tests(self):
        test_list = ['bogus']
        loader = self._create_loader(test_list)

        suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
        self.assertEquals([], _test_ids(suite))


class TestFilteredByNameStartTestLoader(tests.TestCase):

    def _create_loader(self, name_start):
        def needs_module(name):
            return name.startswith(name_start) or name_start.startswith(name)
        loader = TestUtil.FilteredByModuleTestLoader(needs_module)
        return loader

    def test_load_tests(self):
        test_list = ['bzrlib.tests.test_sampler.DemoTest.test_nothing']
        loader = self._create_loader('bzrlib.tests.test_samp')

        suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
        self.assertEquals(test_list, _test_ids(suite))

    def test_load_tests_inside_module(self):
        test_list = ['bzrlib.tests.test_sampler.DemoTest.test_nothing']
        loader = self._create_loader('bzrlib.tests.test_sampler.Demo')

        suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
        self.assertEquals(test_list, _test_ids(suite))

    def test_exclude_tests(self):
        test_list = ['bogus']
        loader = self._create_loader('bogus')

        suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
        self.assertEquals([], _test_ids(suite))


class TestTestPrefixRegistry(tests.TestCase):

    def _get_registry(self):
        tp_registry = tests.TestPrefixAliasRegistry()
        return tp_registry

    def test_register_new_prefix(self):
        tpr = self._get_registry()
        tpr.register('foo', 'fff.ooo.ooo')
        self.assertEquals('fff.ooo.ooo', tpr.get('foo'))

    def test_register_existing_prefix(self):
        tpr = self._get_registry()
        tpr.register('bar', 'bbb.aaa.rrr')
        tpr.register('bar', 'bBB.aAA.rRR')
        self.assertEquals('bbb.aaa.rrr', tpr.get('bar'))
        self.assertContainsRe(self._get_log(keep_log_file=True),
                              r'.*bar.*bbb.aaa.rrr.*bBB.aAA.rRR')

    def test_get_unknown_prefix(self):
        tpr = self._get_registry()
        self.assertRaises(KeyError, tpr.get, 'I am not a prefix')

    def test_resolve_prefix(self):
        tpr = self._get_registry()
        tpr.register('bar', 'bb.aa.rr')
        self.assertEquals('bb.aa.rr', tpr.resolve_alias('bar'))

    def test_resolve_unknown_alias(self):
        tpr = self._get_registry()
        self.assertRaises(errors.BzrCommandError,
                          tpr.resolve_alias, 'I am not a prefix')

    def test_predefined_prefixes(self):
        tpr = tests.test_prefix_alias_registry
        self.assertEquals('bzrlib', tpr.resolve_alias('bzrlib'))
        self.assertEquals('bzrlib.doc', tpr.resolve_alias('bd'))
        self.assertEquals('bzrlib.utils', tpr.resolve_alias('bu'))
        self.assertEquals('bzrlib.tests', tpr.resolve_alias('bt'))
        self.assertEquals('bzrlib.tests.blackbox', tpr.resolve_alias('bb'))
        self.assertEquals('bzrlib.plugins', tpr.resolve_alias('bp'))


class TestRunSuite(tests.TestCase):

    def test_runner_class(self):
        """run_suite accepts and uses a runner_class keyword argument."""
        class Stub(tests.TestCase):
            def test_foo(self):
                pass
        suite = Stub("test_foo")
        calls = []
        class MyRunner(tests.TextTestRunner):
            def run(self, test):
                calls.append(test)
                return tests.ExtendedTestResult(self.stream, self.descriptions,
                                                self.verbosity)
        tests.run_suite(suite, runner_class=MyRunner, stream=StringIO())
        self.assertLength(1, calls)

    def test_done(self):
        """run_suite should call result.done()"""
        self.calls = 0
        def one_more_call(): self.calls += 1
        def test_function():
            pass
        test = unittest.FunctionTestCase(test_function)
        class InstrumentedTestResult(tests.ExtendedTestResult):
            def done(self): one_more_call()
        class MyRunner(tests.TextTestRunner):
            def run(self, test):
                return InstrumentedTestResult(self.stream, self.descriptions,
                                              self.verbosity)
        tests.run_suite(test, runner_class=MyRunner, stream=StringIO())
        self.assertEquals(1, self.calls)