1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
|
# Copyright (C) 2005-2011 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 finding and reading the bzr config file[s]."""
# import system imports here
from cStringIO import StringIO
import os
import sys
import threading
from testtools import matchers
#import bzrlib specific imports here
from bzrlib import (
branch,
bzrdir,
config,
diff,
errors,
osutils,
mail_client,
mergetools,
ui,
urlutils,
registry,
remote,
tests,
trace,
transport,
)
from bzrlib.symbol_versioning import (
deprecated_in,
deprecated_method,
)
from bzrlib.transport import remote as transport_remote
from bzrlib.tests import (
features,
scenarios,
test_server,
)
from bzrlib.util.configobj import configobj
def lockable_config_scenarios():
return [
('global',
{'config_class': config.GlobalConfig,
'config_args': [],
'config_section': 'DEFAULT'}),
('locations',
{'config_class': config.LocationConfig,
'config_args': ['.'],
'config_section': '.'}),]
load_tests = scenarios.load_tests_apply_scenarios
# Register helpers to build stores
config.test_store_builder_registry.register(
'configobj', lambda test: config.IniFileStore(test.get_transport(),
'configobj.conf'))
config.test_store_builder_registry.register(
'bazaar', lambda test: config.GlobalStore())
config.test_store_builder_registry.register(
'location', lambda test: config.LocationStore())
def build_backing_branch(test, relpath,
transport_class=None, server_class=None):
"""Test helper to create a backing branch only once.
Some tests needs multiple stores/stacks to check concurrent update
behaviours. As such, they need to build different branch *objects* even if
they share the branch on disk.
:param relpath: The relative path to the branch. (Note that the helper
should always specify the same relpath).
:param transport_class: The Transport class the test needs to use.
:param server_class: The server associated with the ``transport_class``
above.
Either both or neither of ``transport_class`` and ``server_class`` should
be specified.
"""
if transport_class is not None and server_class is not None:
test.transport_class = transport_class
test.transport_server = server_class
elif not (transport_class is None and server_class is None):
raise AssertionError('Specify both ``transport_class`` and '
'``server_class`` or neither of them')
if getattr(test, 'backing_branch', None) is None:
# First call, let's build the branch on disk
test.backing_branch = test.make_branch(relpath)
def build_branch_store(test):
build_backing_branch(test, 'branch')
b = branch.Branch.open('branch')
return config.BranchStore(b)
config.test_store_builder_registry.register('branch', build_branch_store)
def build_remote_branch_store(test):
# There is only one permutation (but we won't be able to handle more with
# this design anyway)
(transport_class,
server_class) = transport_remote.get_test_permutations()[0]
build_backing_branch(test, 'branch', transport_class, server_class)
b = branch.Branch.open(test.get_url('branch'))
return config.BranchStore(b)
config.test_store_builder_registry.register('remote_branch',
build_remote_branch_store)
config.test_stack_builder_registry.register(
'bazaar', lambda test: config.GlobalStack())
config.test_stack_builder_registry.register(
'location', lambda test: config.LocationStack('.'))
def build_branch_stack(test):
build_backing_branch(test, 'branch')
b = branch.Branch.open('branch')
return config.BranchStack(b)
config.test_stack_builder_registry.register('branch', build_branch_stack)
def build_remote_branch_stack(test):
# There is only one permutation (but we won't be able to handle more with
# this design anyway)
(transport_class,
server_class) = transport_remote.get_test_permutations()[0]
build_backing_branch(test, 'branch', transport_class, server_class)
b = branch.Branch.open(test.get_url('branch'))
return config.BranchStack(b)
config.test_stack_builder_registry.register('remote_branch',
build_remote_branch_stack)
sample_long_alias="log -r-15..-1 --line"
sample_config_text = u"""
[DEFAULT]
email=Erik B\u00e5gfors <erik@bagfors.nu>
editor=vim
change_editor=vimdiff -of @new_path @old_path
gpg_signing_command=gnome-gpg
log_format=short
validate_signatures_in_log=true
acceptable_keys=amy
user_global_option=something
bzr.mergetool.sometool=sometool {base} {this} {other} -o {result}
bzr.mergetool.funkytool=funkytool "arg with spaces" {this_temp}
bzr.default_mergetool=sometool
[ALIASES]
h=help
ll=""" + sample_long_alias + "\n"
sample_always_signatures = """
[DEFAULT]
check_signatures=ignore
create_signatures=always
"""
sample_ignore_signatures = """
[DEFAULT]
check_signatures=require
create_signatures=never
"""
sample_maybe_signatures = """
[DEFAULT]
check_signatures=ignore
create_signatures=when-required
"""
sample_branches_text = """
[http://www.example.com]
# Top level policy
email=Robert Collins <robertc@example.org>
normal_option = normal
appendpath_option = append
appendpath_option:policy = appendpath
norecurse_option = norecurse
norecurse_option:policy = norecurse
[http://www.example.com/ignoreparent]
# different project: ignore parent dir config
ignore_parents=true
[http://www.example.com/norecurse]
# configuration items that only apply to this dir
recurse=false
normal_option = norecurse
[http://www.example.com/dir]
appendpath_option = normal
[/b/]
check_signatures=require
# test trailing / matching with no children
[/a/]
check_signatures=check-available
gpg_signing_command=false
user_local_option=local
# test trailing / matching
[/a/*]
#subdirs will match but not the parent
[/a/c]
check_signatures=ignore
post_commit=bzrlib.tests.test_config.post_commit
#testing explicit beats globs
"""
def create_configs(test):
"""Create configuration files for a given test.
This requires creating a tree (and populate the ``test.tree`` attribute)
and its associated branch and will populate the following attributes:
- branch_config: A BranchConfig for the associated branch.
- locations_config : A LocationConfig for the associated branch
- bazaar_config: A GlobalConfig.
The tree and branch are created in a 'tree' subdirectory so the tests can
still use the test directory to stay outside of the branch.
"""
tree = test.make_branch_and_tree('tree')
test.tree = tree
test.branch_config = config.BranchConfig(tree.branch)
test.locations_config = config.LocationConfig(tree.basedir)
test.bazaar_config = config.GlobalConfig()
def create_configs_with_file_option(test):
"""Create configuration files with a ``file`` option set in each.
This builds on ``create_configs`` and add one ``file`` option in each
configuration with a value which allows identifying the configuration file.
"""
create_configs(test)
test.bazaar_config.set_user_option('file', 'bazaar')
test.locations_config.set_user_option('file', 'locations')
test.branch_config.set_user_option('file', 'branch')
class TestOptionsMixin:
def assertOptions(self, expected, conf):
# We don't care about the parser (as it will make tests hard to write
# and error-prone anyway)
self.assertThat([opt[:4] for opt in conf._get_options()],
matchers.Equals(expected))
class InstrumentedConfigObj(object):
"""A config obj look-enough-alike to record calls made to it."""
def __contains__(self, thing):
self._calls.append(('__contains__', thing))
return False
def __getitem__(self, key):
self._calls.append(('__getitem__', key))
return self
def __init__(self, input, encoding=None):
self._calls = [('__init__', input, encoding)]
def __setitem__(self, key, value):
self._calls.append(('__setitem__', key, value))
def __delitem__(self, key):
self._calls.append(('__delitem__', key))
def keys(self):
self._calls.append(('keys',))
return []
def reload(self):
self._calls.append(('reload',))
def write(self, arg):
self._calls.append(('write',))
def as_bool(self, value):
self._calls.append(('as_bool', value))
return False
def get_value(self, section, name):
self._calls.append(('get_value', section, name))
return None
class FakeBranch(object):
def __init__(self, base=None, user_id=None):
if base is None:
self.base = "http://example.com/branches/demo"
else:
self.base = base
self._transport = self.control_files = \
FakeControlFilesAndTransport(user_id=user_id)
def _get_config(self):
return config.TransportConfig(self._transport, 'branch.conf')
def lock_write(self):
pass
def unlock(self):
pass
class FakeControlFilesAndTransport(object):
def __init__(self, user_id=None):
self.files = {}
if user_id:
self.files['email'] = user_id
self._transport = self
def get_utf8(self, filename):
# from LockableFiles
raise AssertionError("get_utf8 should no longer be used")
def get(self, filename):
# from Transport
try:
return StringIO(self.files[filename])
except KeyError:
raise errors.NoSuchFile(filename)
def get_bytes(self, filename):
# from Transport
try:
return self.files[filename]
except KeyError:
raise errors.NoSuchFile(filename)
def put(self, filename, fileobj):
self.files[filename] = fileobj.read()
def put_file(self, filename, fileobj):
return self.put(filename, fileobj)
class InstrumentedConfig(config.Config):
"""An instrumented config that supplies stubs for template methods."""
def __init__(self):
super(InstrumentedConfig, self).__init__()
self._calls = []
self._signatures = config.CHECK_NEVER
def _get_user_id(self):
self._calls.append('_get_user_id')
return "Robert Collins <robert.collins@example.org>"
def _get_signature_checking(self):
self._calls.append('_get_signature_checking')
return self._signatures
def _get_change_editor(self):
self._calls.append('_get_change_editor')
return 'vimdiff -fo @new_path @old_path'
bool_config = """[DEFAULT]
active = true
inactive = false
[UPPERCASE]
active = True
nonactive = False
"""
class TestConfigObj(tests.TestCase):
def test_get_bool(self):
co = config.ConfigObj(StringIO(bool_config))
self.assertIs(co.get_bool('DEFAULT', 'active'), True)
self.assertIs(co.get_bool('DEFAULT', 'inactive'), False)
self.assertIs(co.get_bool('UPPERCASE', 'active'), True)
self.assertIs(co.get_bool('UPPERCASE', 'nonactive'), False)
def test_hash_sign_in_value(self):
"""
Before 4.5.0, ConfigObj did not quote # signs in values, so they'd be
treated as comments when read in again. (#86838)
"""
co = config.ConfigObj()
co['test'] = 'foo#bar'
outfile = StringIO()
co.write(outfile=outfile)
lines = outfile.getvalue().splitlines()
self.assertEqual(lines, ['test = "foo#bar"'])
co2 = config.ConfigObj(lines)
self.assertEqual(co2['test'], 'foo#bar')
def test_triple_quotes(self):
# Bug #710410: if the value string has triple quotes
# then ConfigObj versions up to 4.7.2 will quote them wrong
# and won't able to read them back
triple_quotes_value = '''spam
""" that's my spam """
eggs'''
co = config.ConfigObj()
co['test'] = triple_quotes_value
# While writing this test another bug in ConfigObj has been found:
# method co.write() without arguments produces list of lines
# one option per line, and multiline values are not split
# across multiple lines,
# and that breaks the parsing these lines back by ConfigObj.
# This issue only affects test, but it's better to avoid
# `co.write()` construct at all.
# [bialix 20110222] bug report sent to ConfigObj's author
outfile = StringIO()
co.write(outfile=outfile)
output = outfile.getvalue()
# now we're trying to read it back
co2 = config.ConfigObj(StringIO(output))
self.assertEquals(triple_quotes_value, co2['test'])
erroneous_config = """[section] # line 1
good=good # line 2
[section] # line 3
whocares=notme # line 4
"""
class TestConfigObjErrors(tests.TestCase):
def test_duplicate_section_name_error_line(self):
try:
co = configobj.ConfigObj(StringIO(erroneous_config),
raise_errors=True)
except config.configobj.DuplicateError, e:
self.assertEqual(3, e.line_number)
else:
self.fail('Error in config file not detected')
class TestConfig(tests.TestCase):
def test_constructs(self):
config.Config()
def test_no_default_editor(self):
self.assertRaises(
NotImplementedError,
self.applyDeprecated, deprecated_in((2, 4, 0)),
config.Config().get_editor)
def test_user_email(self):
my_config = InstrumentedConfig()
self.assertEqual('robert.collins@example.org', my_config.user_email())
self.assertEqual(['_get_user_id'], my_config._calls)
def test_username(self):
my_config = InstrumentedConfig()
self.assertEqual('Robert Collins <robert.collins@example.org>',
my_config.username())
self.assertEqual(['_get_user_id'], my_config._calls)
def test_signatures_default(self):
my_config = config.Config()
self.assertFalse(my_config.signature_needed())
self.assertEqual(config.CHECK_IF_POSSIBLE,
my_config.signature_checking())
self.assertEqual(config.SIGN_WHEN_REQUIRED,
my_config.signing_policy())
def test_signatures_template_method(self):
my_config = InstrumentedConfig()
self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
self.assertEqual(['_get_signature_checking'], my_config._calls)
def test_signatures_template_method_none(self):
my_config = InstrumentedConfig()
my_config._signatures = None
self.assertEqual(config.CHECK_IF_POSSIBLE,
my_config.signature_checking())
self.assertEqual(['_get_signature_checking'], my_config._calls)
def test_gpg_signing_command_default(self):
my_config = config.Config()
self.assertEqual('gpg', my_config.gpg_signing_command())
def test_get_user_option_default(self):
my_config = config.Config()
self.assertEqual(None, my_config.get_user_option('no_option'))
def test_post_commit_default(self):
my_config = config.Config()
self.assertEqual(None, my_config.post_commit())
def test_log_format_default(self):
my_config = config.Config()
self.assertEqual('long', my_config.log_format())
def test_acceptable_keys_default(self):
my_config = config.Config()
self.assertEqual(None, my_config.acceptable_keys())
def test_validate_signatures_in_log_default(self):
my_config = config.Config()
self.assertEqual(False, my_config.validate_signatures_in_log())
def test_get_change_editor(self):
my_config = InstrumentedConfig()
change_editor = my_config.get_change_editor('old_tree', 'new_tree')
self.assertEqual(['_get_change_editor'], my_config._calls)
self.assertIs(diff.DiffFromTool, change_editor.__class__)
self.assertEqual(['vimdiff', '-fo', '@new_path', '@old_path'],
change_editor.command_template)
class TestConfigPath(tests.TestCase):
def setUp(self):
super(TestConfigPath, self).setUp()
self.overrideEnv('HOME', '/home/bogus')
self.overrideEnv('XDG_CACHE_DIR', '')
if sys.platform == 'win32':
self.overrideEnv(
'BZR_HOME', r'C:\Documents and Settings\bogus\Application Data')
self.bzr_home = \
'C:/Documents and Settings/bogus/Application Data/bazaar/2.0'
else:
self.bzr_home = '/home/bogus/.bazaar'
def test_config_dir(self):
self.assertEqual(config.config_dir(), self.bzr_home)
def test_config_filename(self):
self.assertEqual(config.config_filename(),
self.bzr_home + '/bazaar.conf')
def test_locations_config_filename(self):
self.assertEqual(config.locations_config_filename(),
self.bzr_home + '/locations.conf')
def test_authentication_config_filename(self):
self.assertEqual(config.authentication_config_filename(),
self.bzr_home + '/authentication.conf')
def test_xdg_cache_dir(self):
self.assertEqual(config.xdg_cache_dir(),
'/home/bogus/.cache')
class TestXDGConfigDir(tests.TestCaseInTempDir):
# must be in temp dir because config tests for the existence of the bazaar
# subdirectory of $XDG_CONFIG_HOME
def setUp(self):
if sys.platform in ('darwin', 'win32'):
raise tests.TestNotApplicable(
'XDG config dir not used on this platform')
super(TestXDGConfigDir, self).setUp()
self.overrideEnv('HOME', self.test_home_dir)
# BZR_HOME overrides everything we want to test so unset it.
self.overrideEnv('BZR_HOME', None)
def test_xdg_config_dir_exists(self):
"""When ~/.config/bazaar exists, use it as the config dir."""
newdir = osutils.pathjoin(self.test_home_dir, '.config', 'bazaar')
os.makedirs(newdir)
self.assertEqual(config.config_dir(), newdir)
def test_xdg_config_home(self):
"""When XDG_CONFIG_HOME is set, use it."""
xdgconfigdir = osutils.pathjoin(self.test_home_dir, 'xdgconfig')
self.overrideEnv('XDG_CONFIG_HOME', xdgconfigdir)
newdir = osutils.pathjoin(xdgconfigdir, 'bazaar')
os.makedirs(newdir)
self.assertEqual(config.config_dir(), newdir)
class TestIniConfig(tests.TestCaseInTempDir):
def make_config_parser(self, s):
conf = config.IniBasedConfig.from_string(s)
return conf, conf._get_parser()
class TestIniConfigBuilding(TestIniConfig):
def test_contructs(self):
my_config = config.IniBasedConfig()
def test_from_fp(self):
my_config = config.IniBasedConfig.from_string(sample_config_text)
self.assertIsInstance(my_config._get_parser(), configobj.ConfigObj)
def test_cached(self):
my_config = config.IniBasedConfig.from_string(sample_config_text)
parser = my_config._get_parser()
self.assertTrue(my_config._get_parser() is parser)
def _dummy_chown(self, path, uid, gid):
self.path, self.uid, self.gid = path, uid, gid
def test_ini_config_ownership(self):
"""Ensure that chown is happening during _write_config_file"""
self.requireFeature(features.chown_feature)
self.overrideAttr(os, 'chown', self._dummy_chown)
self.path = self.uid = self.gid = None
conf = config.IniBasedConfig(file_name='./foo.conf')
conf._write_config_file()
self.assertEquals(self.path, './foo.conf')
self.assertTrue(isinstance(self.uid, int))
self.assertTrue(isinstance(self.gid, int))
def test_get_filename_parameter_is_deprecated_(self):
conf = self.callDeprecated([
'IniBasedConfig.__init__(get_filename) was deprecated in 2.3.'
' Use file_name instead.'],
config.IniBasedConfig, lambda: 'ini.conf')
self.assertEqual('ini.conf', conf.file_name)
def test_get_parser_file_parameter_is_deprecated_(self):
config_file = StringIO(sample_config_text.encode('utf-8'))
conf = config.IniBasedConfig.from_string(sample_config_text)
conf = self.callDeprecated([
'IniBasedConfig._get_parser(file=xxx) was deprecated in 2.3.'
' Use IniBasedConfig(_content=xxx) instead.'],
conf._get_parser, file=config_file)
class TestIniConfigSaving(tests.TestCaseInTempDir):
def test_cant_save_without_a_file_name(self):
conf = config.IniBasedConfig()
self.assertRaises(AssertionError, conf._write_config_file)
def test_saved_with_content(self):
content = 'foo = bar\n'
conf = config.IniBasedConfig.from_string(
content, file_name='./test.conf', save=True)
self.assertFileEqual(content, 'test.conf')
class TestIniConfigOptionExpansionDefaultValue(tests.TestCaseInTempDir):
"""What is the default value of expand for config options.
This is an opt-in beta feature used to evaluate whether or not option
references can appear in dangerous place raising exceptions, disapearing
(and as such corrupting data) or if it's safe to activate the option by
default.
Note that these tests relies on config._expand_default_value being already
overwritten in the parent class setUp.
"""
def setUp(self):
super(TestIniConfigOptionExpansionDefaultValue, self).setUp()
self.config = None
self.warnings = []
def warning(*args):
self.warnings.append(args[0] % args[1:])
self.overrideAttr(trace, 'warning', warning)
def get_config(self, expand):
c = config.GlobalConfig.from_string('bzr.config.expand=%s' % (expand,),
save=True)
return c
def assertExpandIs(self, expected):
actual = config._get_expand_default_value()
#self.config.get_user_option_as_bool('bzr.config.expand')
self.assertEquals(expected, actual)
def test_default_is_None(self):
self.assertEquals(None, config._expand_default_value)
def test_default_is_False_even_if_None(self):
self.config = self.get_config(None)
self.assertExpandIs(False)
def test_default_is_False_even_if_invalid(self):
self.config = self.get_config('<your choice>')
self.assertExpandIs(False)
# ...
# Huh ? My choice is False ? Thanks, always happy to hear that :D
# Wait, you've been warned !
self.assertLength(1, self.warnings)
self.assertEquals(
'Value "<your choice>" is not a boolean for "bzr.config.expand"',
self.warnings[0])
def test_default_is_True(self):
self.config = self.get_config(True)
self.assertExpandIs(True)
def test_default_is_False(self):
self.config = self.get_config(False)
self.assertExpandIs(False)
class TestIniConfigOptionExpansion(tests.TestCase):
"""Test option expansion from the IniConfig level.
What we really want here is to test the Config level, but the class being
abstract as far as storing values is concerned, this can't be done
properly (yet).
"""
# FIXME: This should be rewritten when all configs share a storage
# implementation -- vila 2011-02-18
def get_config(self, string=None):
if string is None:
string = ''
c = config.IniBasedConfig.from_string(string)
return c
def assertExpansion(self, expected, conf, string, env=None):
self.assertEquals(expected, conf.expand_options(string, env))
def test_no_expansion(self):
c = self.get_config('')
self.assertExpansion('foo', c, 'foo')
def test_env_adding_options(self):
c = self.get_config('')
self.assertExpansion('bar', c, '{foo}', {'foo': 'bar'})
def test_env_overriding_options(self):
c = self.get_config('foo=baz')
self.assertExpansion('bar', c, '{foo}', {'foo': 'bar'})
def test_simple_ref(self):
c = self.get_config('foo=xxx')
self.assertExpansion('xxx', c, '{foo}')
def test_unknown_ref(self):
c = self.get_config('')
self.assertRaises(errors.ExpandingUnknownOption,
c.expand_options, '{foo}')
def test_indirect_ref(self):
c = self.get_config('''
foo=xxx
bar={foo}
''')
self.assertExpansion('xxx', c, '{bar}')
def test_embedded_ref(self):
c = self.get_config('''
foo=xxx
bar=foo
''')
self.assertExpansion('xxx', c, '{{bar}}')
def test_simple_loop(self):
c = self.get_config('foo={foo}')
self.assertRaises(errors.OptionExpansionLoop, c.expand_options, '{foo}')
def test_indirect_loop(self):
c = self.get_config('''
foo={bar}
bar={baz}
baz={foo}''')
e = self.assertRaises(errors.OptionExpansionLoop,
c.expand_options, '{foo}')
self.assertEquals('foo->bar->baz', e.refs)
self.assertEquals('{foo}', e.string)
def test_list(self):
conf = self.get_config('''
foo=start
bar=middle
baz=end
list={foo},{bar},{baz}
''')
self.assertEquals(['start', 'middle', 'end'],
conf.get_user_option('list', expand=True))
def test_cascading_list(self):
conf = self.get_config('''
foo=start,{bar}
bar=middle,{baz}
baz=end
list={foo}
''')
self.assertEquals(['start', 'middle', 'end'],
conf.get_user_option('list', expand=True))
def test_pathological_hidden_list(self):
conf = self.get_config('''
foo=bin
bar=go
start={foo
middle=},{
end=bar}
hidden={start}{middle}{end}
''')
# Nope, it's either a string or a list, and the list wins as soon as a
# ',' appears, so the string concatenation never occur.
self.assertEquals(['{foo', '}', '{', 'bar}'],
conf.get_user_option('hidden', expand=True))
class TestLocationConfigOptionExpansion(tests.TestCaseInTempDir):
def get_config(self, location, string=None):
if string is None:
string = ''
# Since we don't save the config we won't strictly require to inherit
# from TestCaseInTempDir, but an error occurs so quickly...
c = config.LocationConfig.from_string(string, location)
return c
def test_dont_cross_unrelated_section(self):
c = self.get_config('/another/branch/path','''
[/one/branch/path]
foo = hello
bar = {foo}/2
[/another/branch/path]
bar = {foo}/2
''')
self.assertRaises(errors.ExpandingUnknownOption,
c.get_user_option, 'bar', expand=True)
def test_cross_related_sections(self):
c = self.get_config('/project/branch/path','''
[/project]
foo = qu
[/project/branch/path]
bar = {foo}ux
''')
self.assertEquals('quux', c.get_user_option('bar', expand=True))
class TestIniBaseConfigOnDisk(tests.TestCaseInTempDir):
def test_cannot_reload_without_name(self):
conf = config.IniBasedConfig.from_string(sample_config_text)
self.assertRaises(AssertionError, conf.reload)
def test_reload_see_new_value(self):
c1 = config.IniBasedConfig.from_string('editor=vim\n',
file_name='./test/conf')
c1._write_config_file()
c2 = config.IniBasedConfig.from_string('editor=emacs\n',
file_name='./test/conf')
c2._write_config_file()
self.assertEqual('vim', c1.get_user_option('editor'))
self.assertEqual('emacs', c2.get_user_option('editor'))
# Make sure we get the Right value
c1.reload()
self.assertEqual('emacs', c1.get_user_option('editor'))
class TestLockableConfig(tests.TestCaseInTempDir):
scenarios = lockable_config_scenarios()
# Set by load_tests
config_class = None
config_args = None
config_section = None
def setUp(self):
super(TestLockableConfig, self).setUp()
self._content = '[%s]\none=1\ntwo=2\n' % (self.config_section,)
self.config = self.create_config(self._content)
def get_existing_config(self):
return self.config_class(*self.config_args)
def create_config(self, content):
kwargs = dict(save=True)
c = self.config_class.from_string(content, *self.config_args, **kwargs)
return c
def test_simple_read_access(self):
self.assertEquals('1', self.config.get_user_option('one'))
def test_simple_write_access(self):
self.config.set_user_option('one', 'one')
self.assertEquals('one', self.config.get_user_option('one'))
def test_listen_to_the_last_speaker(self):
c1 = self.config
c2 = self.get_existing_config()
c1.set_user_option('one', 'ONE')
c2.set_user_option('two', 'TWO')
self.assertEquals('ONE', c1.get_user_option('one'))
self.assertEquals('TWO', c2.get_user_option('two'))
# The second update respect the first one
self.assertEquals('ONE', c2.get_user_option('one'))
def test_last_speaker_wins(self):
# If the same config is not shared, the same variable modified twice
# can only see a single result.
c1 = self.config
c2 = self.get_existing_config()
c1.set_user_option('one', 'c1')
c2.set_user_option('one', 'c2')
self.assertEquals('c2', c2._get_user_option('one'))
# The first modification is still available until another refresh
# occur
self.assertEquals('c1', c1._get_user_option('one'))
c1.set_user_option('two', 'done')
self.assertEquals('c2', c1._get_user_option('one'))
def test_writes_are_serialized(self):
c1 = self.config
c2 = self.get_existing_config()
# We spawn a thread that will pause *during* the write
before_writing = threading.Event()
after_writing = threading.Event()
writing_done = threading.Event()
c1_orig = c1._write_config_file
def c1_write_config_file():
before_writing.set()
c1_orig()
# The lock is held. We wait for the main thread to decide when to
# continue
after_writing.wait()
c1._write_config_file = c1_write_config_file
def c1_set_option():
c1.set_user_option('one', 'c1')
writing_done.set()
t1 = threading.Thread(target=c1_set_option)
# Collect the thread after the test
self.addCleanup(t1.join)
# Be ready to unblock the thread if the test goes wrong
self.addCleanup(after_writing.set)
t1.start()
before_writing.wait()
self.assertTrue(c1._lock.is_held)
self.assertRaises(errors.LockContention,
c2.set_user_option, 'one', 'c2')
self.assertEquals('c1', c1.get_user_option('one'))
# Let the lock be released
after_writing.set()
writing_done.wait()
c2.set_user_option('one', 'c2')
self.assertEquals('c2', c2.get_user_option('one'))
def test_read_while_writing(self):
c1 = self.config
# We spawn a thread that will pause *during* the write
ready_to_write = threading.Event()
do_writing = threading.Event()
writing_done = threading.Event()
c1_orig = c1._write_config_file
def c1_write_config_file():
ready_to_write.set()
# The lock is held. We wait for the main thread to decide when to
# continue
do_writing.wait()
c1_orig()
writing_done.set()
c1._write_config_file = c1_write_config_file
def c1_set_option():
c1.set_user_option('one', 'c1')
t1 = threading.Thread(target=c1_set_option)
# Collect the thread after the test
self.addCleanup(t1.join)
# Be ready to unblock the thread if the test goes wrong
self.addCleanup(do_writing.set)
t1.start()
# Ensure the thread is ready to write
ready_to_write.wait()
self.assertTrue(c1._lock.is_held)
self.assertEquals('c1', c1.get_user_option('one'))
# If we read during the write, we get the old value
c2 = self.get_existing_config()
self.assertEquals('1', c2.get_user_option('one'))
# Let the writing occur and ensure it occurred
do_writing.set()
writing_done.wait()
# Now we get the updated value
c3 = self.get_existing_config()
self.assertEquals('c1', c3.get_user_option('one'))
class TestGetUserOptionAs(TestIniConfig):
def test_get_user_option_as_bool(self):
conf, parser = self.make_config_parser("""
a_true_bool = true
a_false_bool = 0
an_invalid_bool = maybe
a_list = hmm, who knows ? # This is interpreted as a list !
""")
get_bool = conf.get_user_option_as_bool
self.assertEqual(True, get_bool('a_true_bool'))
self.assertEqual(False, get_bool('a_false_bool'))
warnings = []
def warning(*args):
warnings.append(args[0] % args[1:])
self.overrideAttr(trace, 'warning', warning)
msg = 'Value "%s" is not a boolean for "%s"'
self.assertIs(None, get_bool('an_invalid_bool'))
self.assertEquals(msg % ('maybe', 'an_invalid_bool'), warnings[0])
warnings = []
self.assertIs(None, get_bool('not_defined_in_this_config'))
self.assertEquals([], warnings)
def test_get_user_option_as_list(self):
conf, parser = self.make_config_parser("""
a_list = a,b,c
length_1 = 1,
one_item = x
""")
get_list = conf.get_user_option_as_list
self.assertEqual(['a', 'b', 'c'], get_list('a_list'))
self.assertEqual(['1'], get_list('length_1'))
self.assertEqual('x', conf.get_user_option('one_item'))
# automatically cast to list
self.assertEqual(['x'], get_list('one_item'))
class TestSupressWarning(TestIniConfig):
def make_warnings_config(self, s):
conf, parser = self.make_config_parser(s)
return conf.suppress_warning
def test_suppress_warning_unknown(self):
suppress_warning = self.make_warnings_config('')
self.assertEqual(False, suppress_warning('unknown_warning'))
def test_suppress_warning_known(self):
suppress_warning = self.make_warnings_config('suppress_warnings=a,b')
self.assertEqual(False, suppress_warning('c'))
self.assertEqual(True, suppress_warning('a'))
self.assertEqual(True, suppress_warning('b'))
class TestGetConfig(tests.TestCase):
def test_constructs(self):
my_config = config.GlobalConfig()
def test_calls_read_filenames(self):
# replace the class that is constructed, to check its parameters
oldparserclass = config.ConfigObj
config.ConfigObj = InstrumentedConfigObj
my_config = config.GlobalConfig()
try:
parser = my_config._get_parser()
finally:
config.ConfigObj = oldparserclass
self.assertIsInstance(parser, InstrumentedConfigObj)
self.assertEqual(parser._calls, [('__init__', config.config_filename(),
'utf-8')])
class TestBranchConfig(tests.TestCaseWithTransport):
def test_constructs(self):
branch = FakeBranch()
my_config = config.BranchConfig(branch)
self.assertRaises(TypeError, config.BranchConfig)
def test_get_location_config(self):
branch = FakeBranch()
my_config = config.BranchConfig(branch)
location_config = my_config._get_location_config()
self.assertEqual(branch.base, location_config.location)
self.assertIs(location_config, my_config._get_location_config())
def test_get_config(self):
"""The Branch.get_config method works properly"""
b = bzrdir.BzrDir.create_standalone_workingtree('.').branch
my_config = b.get_config()
self.assertIs(my_config.get_user_option('wacky'), None)
my_config.set_user_option('wacky', 'unlikely')
self.assertEqual(my_config.get_user_option('wacky'), 'unlikely')
# Ensure we get the same thing if we start again
b2 = branch.Branch.open('.')
my_config2 = b2.get_config()
self.assertEqual(my_config2.get_user_option('wacky'), 'unlikely')
def test_has_explicit_nickname(self):
b = self.make_branch('.')
self.assertFalse(b.get_config().has_explicit_nickname())
b.nick = 'foo'
self.assertTrue(b.get_config().has_explicit_nickname())
def test_config_url(self):
"""The Branch.get_config will use section that uses a local url"""
branch = self.make_branch('branch')
self.assertEqual('branch', branch.nick)
local_url = urlutils.local_path_to_url('branch')
conf = config.LocationConfig.from_string(
'[%s]\nnickname = foobar' % (local_url,),
local_url, save=True)
self.assertEqual('foobar', branch.nick)
def test_config_local_path(self):
"""The Branch.get_config will use a local system path"""
branch = self.make_branch('branch')
self.assertEqual('branch', branch.nick)
local_path = osutils.getcwd().encode('utf8')
conf = config.LocationConfig.from_string(
'[%s/branch]\nnickname = barry' % (local_path,),
'branch', save=True)
self.assertEqual('barry', branch.nick)
def test_config_creates_local(self):
"""Creating a new entry in config uses a local path."""
branch = self.make_branch('branch', format='knit')
branch.set_push_location('http://foobar')
local_path = osutils.getcwd().encode('utf8')
# Surprisingly ConfigObj doesn't create a trailing newline
self.check_file_contents(config.locations_config_filename(),
'[%s/branch]\n'
'push_location = http://foobar\n'
'push_location:policy = norecurse\n'
% (local_path,))
def test_autonick_urlencoded(self):
b = self.make_branch('!repo')
self.assertEqual('!repo', b.get_config().get_nickname())
def test_warn_if_masked(self):
warnings = []
def warning(*args):
warnings.append(args[0] % args[1:])
self.overrideAttr(trace, 'warning', warning)
def set_option(store, warn_masked=True):
warnings[:] = []
conf.set_user_option('example_option', repr(store), store=store,
warn_masked=warn_masked)
def assertWarning(warning):
if warning is None:
self.assertEqual(0, len(warnings))
else:
self.assertEqual(1, len(warnings))
self.assertEqual(warning, warnings[0])
branch = self.make_branch('.')
conf = branch.get_config()
set_option(config.STORE_GLOBAL)
assertWarning(None)
set_option(config.STORE_BRANCH)
assertWarning(None)
set_option(config.STORE_GLOBAL)
assertWarning('Value "4" is masked by "3" from branch.conf')
set_option(config.STORE_GLOBAL, warn_masked=False)
assertWarning(None)
set_option(config.STORE_LOCATION)
assertWarning(None)
set_option(config.STORE_BRANCH)
assertWarning('Value "3" is masked by "0" from locations.conf')
set_option(config.STORE_BRANCH, warn_masked=False)
assertWarning(None)
class TestGlobalConfigItems(tests.TestCaseInTempDir):
def test_user_id(self):
my_config = config.GlobalConfig.from_string(sample_config_text)
self.assertEqual(u"Erik B\u00e5gfors <erik@bagfors.nu>",
my_config._get_user_id())
def test_absent_user_id(self):
my_config = config.GlobalConfig()
self.assertEqual(None, my_config._get_user_id())
def test_configured_editor(self):
my_config = config.GlobalConfig.from_string(sample_config_text)
editor = self.applyDeprecated(
deprecated_in((2, 4, 0)), my_config.get_editor)
self.assertEqual('vim', editor)
def test_signatures_always(self):
my_config = config.GlobalConfig.from_string(sample_always_signatures)
self.assertEqual(config.CHECK_NEVER,
my_config.signature_checking())
self.assertEqual(config.SIGN_ALWAYS,
my_config.signing_policy())
self.assertEqual(True, my_config.signature_needed())
def test_signatures_if_possible(self):
my_config = config.GlobalConfig.from_string(sample_maybe_signatures)
self.assertEqual(config.CHECK_NEVER,
my_config.signature_checking())
self.assertEqual(config.SIGN_WHEN_REQUIRED,
my_config.signing_policy())
self.assertEqual(False, my_config.signature_needed())
def test_signatures_ignore(self):
my_config = config.GlobalConfig.from_string(sample_ignore_signatures)
self.assertEqual(config.CHECK_ALWAYS,
my_config.signature_checking())
self.assertEqual(config.SIGN_NEVER,
my_config.signing_policy())
self.assertEqual(False, my_config.signature_needed())
def _get_sample_config(self):
my_config = config.GlobalConfig.from_string(sample_config_text)
return my_config
def test_gpg_signing_command(self):
my_config = self._get_sample_config()
self.assertEqual("gnome-gpg", my_config.gpg_signing_command())
self.assertEqual(False, my_config.signature_needed())
def _get_empty_config(self):
my_config = config.GlobalConfig()
return my_config
def test_gpg_signing_command_unset(self):
my_config = self._get_empty_config()
self.assertEqual("gpg", my_config.gpg_signing_command())
def test_get_user_option_default(self):
my_config = self._get_empty_config()
self.assertEqual(None, my_config.get_user_option('no_option'))
def test_get_user_option_global(self):
my_config = self._get_sample_config()
self.assertEqual("something",
my_config.get_user_option('user_global_option'))
def test_post_commit_default(self):
my_config = self._get_sample_config()
self.assertEqual(None, my_config.post_commit())
def test_configured_logformat(self):
my_config = self._get_sample_config()
self.assertEqual("short", my_config.log_format())
def test_configured_acceptable_keys(self):
my_config = self._get_sample_config()
self.assertEqual("amy", my_config.acceptable_keys())
def test_configured_validate_signatures_in_log(self):
my_config = self._get_sample_config()
self.assertEqual(True, my_config.validate_signatures_in_log())
def test_get_alias(self):
my_config = self._get_sample_config()
self.assertEqual('help', my_config.get_alias('h'))
def test_get_aliases(self):
my_config = self._get_sample_config()
aliases = my_config.get_aliases()
self.assertEqual(2, len(aliases))
sorted_keys = sorted(aliases)
self.assertEqual('help', aliases[sorted_keys[0]])
self.assertEqual(sample_long_alias, aliases[sorted_keys[1]])
def test_get_no_alias(self):
my_config = self._get_sample_config()
self.assertEqual(None, my_config.get_alias('foo'))
def test_get_long_alias(self):
my_config = self._get_sample_config()
self.assertEqual(sample_long_alias, my_config.get_alias('ll'))
def test_get_change_editor(self):
my_config = self._get_sample_config()
change_editor = my_config.get_change_editor('old', 'new')
self.assertIs(diff.DiffFromTool, change_editor.__class__)
self.assertEqual('vimdiff -of @new_path @old_path',
' '.join(change_editor.command_template))
def test_get_no_change_editor(self):
my_config = self._get_empty_config()
change_editor = my_config.get_change_editor('old', 'new')
self.assertIs(None, change_editor)
def test_get_merge_tools(self):
conf = self._get_sample_config()
tools = conf.get_merge_tools()
self.log(repr(tools))
self.assertEqual(
{u'funkytool' : u'funkytool "arg with spaces" {this_temp}',
u'sometool' : u'sometool {base} {this} {other} -o {result}'},
tools)
def test_get_merge_tools_empty(self):
conf = self._get_empty_config()
tools = conf.get_merge_tools()
self.assertEqual({}, tools)
def test_find_merge_tool(self):
conf = self._get_sample_config()
cmdline = conf.find_merge_tool('sometool')
self.assertEqual('sometool {base} {this} {other} -o {result}', cmdline)
def test_find_merge_tool_not_found(self):
conf = self._get_sample_config()
cmdline = conf.find_merge_tool('DOES NOT EXIST')
self.assertIs(cmdline, None)
def test_find_merge_tool_known(self):
conf = self._get_empty_config()
cmdline = conf.find_merge_tool('kdiff3')
self.assertEquals('kdiff3 {base} {this} {other} -o {result}', cmdline)
def test_find_merge_tool_override_known(self):
conf = self._get_empty_config()
conf.set_user_option('bzr.mergetool.kdiff3', 'kdiff3 blah')
cmdline = conf.find_merge_tool('kdiff3')
self.assertEqual('kdiff3 blah', cmdline)
class TestGlobalConfigSavingOptions(tests.TestCaseInTempDir):
def test_empty(self):
my_config = config.GlobalConfig()
self.assertEqual(0, len(my_config.get_aliases()))
def test_set_alias(self):
my_config = config.GlobalConfig()
alias_value = 'commit --strict'
my_config.set_alias('commit', alias_value)
new_config = config.GlobalConfig()
self.assertEqual(alias_value, new_config.get_alias('commit'))
def test_remove_alias(self):
my_config = config.GlobalConfig()
my_config.set_alias('commit', 'commit --strict')
# Now remove the alias again.
my_config.unset_alias('commit')
new_config = config.GlobalConfig()
self.assertIs(None, new_config.get_alias('commit'))
class TestLocationConfig(tests.TestCaseInTempDir, TestOptionsMixin):
def test_constructs(self):
my_config = config.LocationConfig('http://example.com')
self.assertRaises(TypeError, config.LocationConfig)
def test_branch_calls_read_filenames(self):
# This is testing the correct file names are provided.
# TODO: consolidate with the test for GlobalConfigs filename checks.
#
# replace the class that is constructed, to check its parameters
oldparserclass = config.ConfigObj
config.ConfigObj = InstrumentedConfigObj
try:
my_config = config.LocationConfig('http://www.example.com')
parser = my_config._get_parser()
finally:
config.ConfigObj = oldparserclass
self.assertIsInstance(parser, InstrumentedConfigObj)
self.assertEqual(parser._calls,
[('__init__', config.locations_config_filename(),
'utf-8')])
def test_get_global_config(self):
my_config = config.BranchConfig(FakeBranch('http://example.com'))
global_config = my_config._get_global_config()
self.assertIsInstance(global_config, config.GlobalConfig)
self.assertIs(global_config, my_config._get_global_config())
def assertLocationMatching(self, expected):
self.assertEqual(expected,
list(self.my_location_config._get_matching_sections()))
def test__get_matching_sections_no_match(self):
self.get_branch_config('/')
self.assertLocationMatching([])
def test__get_matching_sections_exact(self):
self.get_branch_config('http://www.example.com')
self.assertLocationMatching([('http://www.example.com', '')])
def test__get_matching_sections_suffix_does_not(self):
self.get_branch_config('http://www.example.com-com')
self.assertLocationMatching([])
def test__get_matching_sections_subdir_recursive(self):
self.get_branch_config('http://www.example.com/com')
self.assertLocationMatching([('http://www.example.com', 'com')])
def test__get_matching_sections_ignoreparent(self):
self.get_branch_config('http://www.example.com/ignoreparent')
self.assertLocationMatching([('http://www.example.com/ignoreparent',
'')])
def test__get_matching_sections_ignoreparent_subdir(self):
self.get_branch_config(
'http://www.example.com/ignoreparent/childbranch')
self.assertLocationMatching([('http://www.example.com/ignoreparent',
'childbranch')])
def test__get_matching_sections_subdir_trailing_slash(self):
self.get_branch_config('/b')
self.assertLocationMatching([('/b/', '')])
def test__get_matching_sections_subdir_child(self):
self.get_branch_config('/a/foo')
self.assertLocationMatching([('/a/*', ''), ('/a/', 'foo')])
def test__get_matching_sections_subdir_child_child(self):
self.get_branch_config('/a/foo/bar')
self.assertLocationMatching([('/a/*', 'bar'), ('/a/', 'foo/bar')])
def test__get_matching_sections_trailing_slash_with_children(self):
self.get_branch_config('/a/')
self.assertLocationMatching([('/a/', '')])
def test__get_matching_sections_explicit_over_glob(self):
# XXX: 2006-09-08 jamesh
# This test only passes because ord('c') > ord('*'). If there
# was a config section for '/a/?', it would get precedence
# over '/a/c'.
self.get_branch_config('/a/c')
self.assertLocationMatching([('/a/c', ''), ('/a/*', ''), ('/a/', 'c')])
def test__get_option_policy_normal(self):
self.get_branch_config('http://www.example.com')
self.assertEqual(
self.my_location_config._get_config_policy(
'http://www.example.com', 'normal_option'),
config.POLICY_NONE)
def test__get_option_policy_norecurse(self):
self.get_branch_config('http://www.example.com')
self.assertEqual(
self.my_location_config._get_option_policy(
'http://www.example.com', 'norecurse_option'),
config.POLICY_NORECURSE)
# Test old recurse=False setting:
self.assertEqual(
self.my_location_config._get_option_policy(
'http://www.example.com/norecurse', 'normal_option'),
config.POLICY_NORECURSE)
def test__get_option_policy_normal(self):
self.get_branch_config('http://www.example.com')
self.assertEqual(
self.my_location_config._get_option_policy(
'http://www.example.com', 'appendpath_option'),
config.POLICY_APPENDPATH)
def test__get_options_with_policy(self):
self.get_branch_config('/dir/subdir',
location_config="""\
[/dir]
other_url = /other-dir
other_url:policy = appendpath
[/dir/subdir]
other_url = /other-subdir
""")
self.assertOptions(
[(u'other_url', u'/other-subdir', u'/dir/subdir', 'locations'),
(u'other_url', u'/other-dir', u'/dir', 'locations'),
(u'other_url:policy', u'appendpath', u'/dir', 'locations')],
self.my_location_config)
def test_location_without_username(self):
self.get_branch_config('http://www.example.com/ignoreparent')
self.assertEqual(u'Erik B\u00e5gfors <erik@bagfors.nu>',
self.my_config.username())
def test_location_not_listed(self):
"""Test that the global username is used when no location matches"""
self.get_branch_config('/home/robertc/sources')
self.assertEqual(u'Erik B\u00e5gfors <erik@bagfors.nu>',
self.my_config.username())
def test_overriding_location(self):
self.get_branch_config('http://www.example.com/foo')
self.assertEqual('Robert Collins <robertc@example.org>',
self.my_config.username())
def test_signatures_not_set(self):
self.get_branch_config('http://www.example.com',
global_config=sample_ignore_signatures)
self.assertEqual(config.CHECK_ALWAYS,
self.my_config.signature_checking())
self.assertEqual(config.SIGN_NEVER,
self.my_config.signing_policy())
def test_signatures_never(self):
self.get_branch_config('/a/c')
self.assertEqual(config.CHECK_NEVER,
self.my_config.signature_checking())
def test_signatures_when_available(self):
self.get_branch_config('/a/', global_config=sample_ignore_signatures)
self.assertEqual(config.CHECK_IF_POSSIBLE,
self.my_config.signature_checking())
def test_signatures_always(self):
self.get_branch_config('/b')
self.assertEqual(config.CHECK_ALWAYS,
self.my_config.signature_checking())
def test_gpg_signing_command(self):
self.get_branch_config('/b')
self.assertEqual("gnome-gpg", self.my_config.gpg_signing_command())
def test_gpg_signing_command_missing(self):
self.get_branch_config('/a')
self.assertEqual("false", self.my_config.gpg_signing_command())
def test_get_user_option_global(self):
self.get_branch_config('/a')
self.assertEqual('something',
self.my_config.get_user_option('user_global_option'))
def test_get_user_option_local(self):
self.get_branch_config('/a')
self.assertEqual('local',
self.my_config.get_user_option('user_local_option'))
def test_get_user_option_appendpath(self):
# returned as is for the base path:
self.get_branch_config('http://www.example.com')
self.assertEqual('append',
self.my_config.get_user_option('appendpath_option'))
# Extra path components get appended:
self.get_branch_config('http://www.example.com/a/b/c')
self.assertEqual('append/a/b/c',
self.my_config.get_user_option('appendpath_option'))
# Overriden for http://www.example.com/dir, where it is a
# normal option:
self.get_branch_config('http://www.example.com/dir/a/b/c')
self.assertEqual('normal',
self.my_config.get_user_option('appendpath_option'))
def test_get_user_option_norecurse(self):
self.get_branch_config('http://www.example.com')
self.assertEqual('norecurse',
self.my_config.get_user_option('norecurse_option'))
self.get_branch_config('http://www.example.com/dir')
self.assertEqual(None,
self.my_config.get_user_option('norecurse_option'))
# http://www.example.com/norecurse is a recurse=False section
# that redefines normal_option. Subdirectories do not pick up
# this redefinition.
self.get_branch_config('http://www.example.com/norecurse')
self.assertEqual('norecurse',
self.my_config.get_user_option('normal_option'))
self.get_branch_config('http://www.example.com/norecurse/subdir')
self.assertEqual('normal',
self.my_config.get_user_option('normal_option'))
def test_set_user_option_norecurse(self):
self.get_branch_config('http://www.example.com')
self.my_config.set_user_option('foo', 'bar',
store=config.STORE_LOCATION_NORECURSE)
self.assertEqual(
self.my_location_config._get_option_policy(
'http://www.example.com', 'foo'),
config.POLICY_NORECURSE)
def test_set_user_option_appendpath(self):
self.get_branch_config('http://www.example.com')
self.my_config.set_user_option('foo', 'bar',
store=config.STORE_LOCATION_APPENDPATH)
self.assertEqual(
self.my_location_config._get_option_policy(
'http://www.example.com', 'foo'),
config.POLICY_APPENDPATH)
def test_set_user_option_change_policy(self):
self.get_branch_config('http://www.example.com')
self.my_config.set_user_option('norecurse_option', 'normal',
store=config.STORE_LOCATION)
self.assertEqual(
self.my_location_config._get_option_policy(
'http://www.example.com', 'norecurse_option'),
config.POLICY_NONE)
def test_set_user_option_recurse_false_section(self):
# The following section has recurse=False set. The test is to
# make sure that a normal option can be added to the section,
# converting recurse=False to the norecurse policy.
self.get_branch_config('http://www.example.com/norecurse')
self.callDeprecated(['The recurse option is deprecated as of 0.14. '
'The section "http://www.example.com/norecurse" '
'has been converted to use policies.'],
self.my_config.set_user_option,
'foo', 'bar', store=config.STORE_LOCATION)
self.assertEqual(
self.my_location_config._get_option_policy(
'http://www.example.com/norecurse', 'foo'),
config.POLICY_NONE)
# The previously existing option is still norecurse:
self.assertEqual(
self.my_location_config._get_option_policy(
'http://www.example.com/norecurse', 'normal_option'),
config.POLICY_NORECURSE)
def test_post_commit_default(self):
self.get_branch_config('/a/c')
self.assertEqual('bzrlib.tests.test_config.post_commit',
self.my_config.post_commit())
def get_branch_config(self, location, global_config=None,
location_config=None):
my_branch = FakeBranch(location)
if global_config is None:
global_config = sample_config_text
if location_config is None:
location_config = sample_branches_text
my_global_config = config.GlobalConfig.from_string(global_config,
save=True)
my_location_config = config.LocationConfig.from_string(
location_config, my_branch.base, save=True)
my_config = config.BranchConfig(my_branch)
self.my_config = my_config
self.my_location_config = my_config._get_location_config()
def test_set_user_setting_sets_and_saves(self):
self.get_branch_config('/a/c')
record = InstrumentedConfigObj("foo")
self.my_location_config._parser = record
self.callDeprecated(['The recurse option is deprecated as of '
'0.14. The section "/a/c" has been '
'converted to use policies.'],
self.my_config.set_user_option,
'foo', 'bar', store=config.STORE_LOCATION)
self.assertEqual([('reload',),
('__contains__', '/a/c'),
('__contains__', '/a/c/'),
('__setitem__', '/a/c', {}),
('__getitem__', '/a/c'),
('__setitem__', 'foo', 'bar'),
('__getitem__', '/a/c'),
('as_bool', 'recurse'),
('__getitem__', '/a/c'),
('__delitem__', 'recurse'),
('__getitem__', '/a/c'),
('keys',),
('__getitem__', '/a/c'),
('__contains__', 'foo:policy'),
('write',)],
record._calls[1:])
def test_set_user_setting_sets_and_saves2(self):
self.get_branch_config('/a/c')
self.assertIs(self.my_config.get_user_option('foo'), None)
self.my_config.set_user_option('foo', 'bar')
self.assertEqual(
self.my_config.branch.control_files.files['branch.conf'].strip(),
'foo = bar')
self.assertEqual(self.my_config.get_user_option('foo'), 'bar')
self.my_config.set_user_option('foo', 'baz',
store=config.STORE_LOCATION)
self.assertEqual(self.my_config.get_user_option('foo'), 'baz')
self.my_config.set_user_option('foo', 'qux')
self.assertEqual(self.my_config.get_user_option('foo'), 'baz')
def test_get_bzr_remote_path(self):
my_config = config.LocationConfig('/a/c')
self.assertEqual('bzr', my_config.get_bzr_remote_path())
my_config.set_user_option('bzr_remote_path', '/path-bzr')
self.assertEqual('/path-bzr', my_config.get_bzr_remote_path())
self.overrideEnv('BZR_REMOTE_PATH', '/environ-bzr')
self.assertEqual('/environ-bzr', my_config.get_bzr_remote_path())
precedence_global = 'option = global'
precedence_branch = 'option = branch'
precedence_location = """
[http://]
recurse = true
option = recurse
[http://example.com/specific]
option = exact
"""
class TestBranchConfigItems(tests.TestCaseInTempDir):
def get_branch_config(self, global_config=None, location=None,
location_config=None, branch_data_config=None):
my_branch = FakeBranch(location)
if global_config is not None:
my_global_config = config.GlobalConfig.from_string(global_config,
save=True)
if location_config is not None:
my_location_config = config.LocationConfig.from_string(
location_config, my_branch.base, save=True)
my_config = config.BranchConfig(my_branch)
if branch_data_config is not None:
my_config.branch.control_files.files['branch.conf'] = \
branch_data_config
return my_config
def test_user_id(self):
branch = FakeBranch(user_id='Robert Collins <robertc@example.net>')
my_config = config.BranchConfig(branch)
self.assertEqual("Robert Collins <robertc@example.net>",
my_config.username())
my_config.branch.control_files.files['email'] = "John"
my_config.set_user_option('email',
"Robert Collins <robertc@example.org>")
self.assertEqual("John", my_config.username())
del my_config.branch.control_files.files['email']
self.assertEqual("Robert Collins <robertc@example.org>",
my_config.username())
def test_not_set_in_branch(self):
my_config = self.get_branch_config(global_config=sample_config_text)
self.assertEqual(u"Erik B\u00e5gfors <erik@bagfors.nu>",
my_config._get_user_id())
my_config.branch.control_files.files['email'] = "John"
self.assertEqual("John", my_config._get_user_id())
def test_BZR_EMAIL_OVERRIDES(self):
self.overrideEnv('BZR_EMAIL', "Robert Collins <robertc@example.org>")
branch = FakeBranch()
my_config = config.BranchConfig(branch)
self.assertEqual("Robert Collins <robertc@example.org>",
my_config.username())
def test_signatures_forced(self):
my_config = self.get_branch_config(
global_config=sample_always_signatures)
self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
self.assertEqual(config.SIGN_ALWAYS, my_config.signing_policy())
self.assertTrue(my_config.signature_needed())
def test_signatures_forced_branch(self):
my_config = self.get_branch_config(
global_config=sample_ignore_signatures,
branch_data_config=sample_always_signatures)
self.assertEqual(config.CHECK_NEVER, my_config.signature_checking())
self.assertEqual(config.SIGN_ALWAYS, my_config.signing_policy())
self.assertTrue(my_config.signature_needed())
def test_gpg_signing_command(self):
my_config = self.get_branch_config(
global_config=sample_config_text,
# branch data cannot set gpg_signing_command
branch_data_config="gpg_signing_command=pgp")
self.assertEqual('gnome-gpg', my_config.gpg_signing_command())
def test_get_user_option_global(self):
my_config = self.get_branch_config(global_config=sample_config_text)
self.assertEqual('something',
my_config.get_user_option('user_global_option'))
def test_post_commit_default(self):
my_config = self.get_branch_config(global_config=sample_config_text,
location='/a/c',
location_config=sample_branches_text)
self.assertEqual(my_config.branch.base, '/a/c')
self.assertEqual('bzrlib.tests.test_config.post_commit',
my_config.post_commit())
my_config.set_user_option('post_commit', 'rmtree_root')
# post-commit is ignored when present in branch data
self.assertEqual('bzrlib.tests.test_config.post_commit',
my_config.post_commit())
my_config.set_user_option('post_commit', 'rmtree_root',
store=config.STORE_LOCATION)
self.assertEqual('rmtree_root', my_config.post_commit())
def test_config_precedence(self):
# FIXME: eager test, luckily no persitent config file makes it fail
# -- vila 20100716
my_config = self.get_branch_config(global_config=precedence_global)
self.assertEqual(my_config.get_user_option('option'), 'global')
my_config = self.get_branch_config(global_config=precedence_global,
branch_data_config=precedence_branch)
self.assertEqual(my_config.get_user_option('option'), 'branch')
my_config = self.get_branch_config(
global_config=precedence_global,
branch_data_config=precedence_branch,
location_config=precedence_location)
self.assertEqual(my_config.get_user_option('option'), 'recurse')
my_config = self.get_branch_config(
global_config=precedence_global,
branch_data_config=precedence_branch,
location_config=precedence_location,
location='http://example.com/specific')
self.assertEqual(my_config.get_user_option('option'), 'exact')
def test_get_mail_client(self):
config = self.get_branch_config()
client = config.get_mail_client()
self.assertIsInstance(client, mail_client.DefaultMail)
# Specific clients
config.set_user_option('mail_client', 'evolution')
client = config.get_mail_client()
self.assertIsInstance(client, mail_client.Evolution)
config.set_user_option('mail_client', 'kmail')
client = config.get_mail_client()
self.assertIsInstance(client, mail_client.KMail)
config.set_user_option('mail_client', 'mutt')
client = config.get_mail_client()
self.assertIsInstance(client, mail_client.Mutt)
config.set_user_option('mail_client', 'thunderbird')
client = config.get_mail_client()
self.assertIsInstance(client, mail_client.Thunderbird)
# Generic options
config.set_user_option('mail_client', 'default')
client = config.get_mail_client()
self.assertIsInstance(client, mail_client.DefaultMail)
config.set_user_option('mail_client', 'editor')
client = config.get_mail_client()
self.assertIsInstance(client, mail_client.Editor)
config.set_user_option('mail_client', 'mapi')
client = config.get_mail_client()
self.assertIsInstance(client, mail_client.MAPIClient)
config.set_user_option('mail_client', 'xdg-email')
client = config.get_mail_client()
self.assertIsInstance(client, mail_client.XDGEmail)
config.set_user_option('mail_client', 'firebird')
self.assertRaises(errors.UnknownMailClient, config.get_mail_client)
class TestMailAddressExtraction(tests.TestCase):
def test_extract_email_address(self):
self.assertEqual('jane@test.com',
config.extract_email_address('Jane <jane@test.com>'))
self.assertRaises(errors.NoEmailInUsername,
config.extract_email_address, 'Jane Tester')
def test_parse_username(self):
self.assertEqual(('', 'jdoe@example.com'),
config.parse_username('jdoe@example.com'))
self.assertEqual(('', 'jdoe@example.com'),
config.parse_username('<jdoe@example.com>'))
self.assertEqual(('John Doe', 'jdoe@example.com'),
config.parse_username('John Doe <jdoe@example.com>'))
self.assertEqual(('John Doe', ''),
config.parse_username('John Doe'))
self.assertEqual(('John Doe', 'jdoe@example.com'),
config.parse_username('John Doe jdoe@example.com'))
class TestTreeConfig(tests.TestCaseWithTransport):
def test_get_value(self):
"""Test that retreiving a value from a section is possible"""
branch = self.make_branch('.')
tree_config = config.TreeConfig(branch)
tree_config.set_option('value', 'key', 'SECTION')
tree_config.set_option('value2', 'key2')
tree_config.set_option('value3-top', 'key3')
tree_config.set_option('value3-section', 'key3', 'SECTION')
value = tree_config.get_option('key', 'SECTION')
self.assertEqual(value, 'value')
value = tree_config.get_option('key2')
self.assertEqual(value, 'value2')
self.assertEqual(tree_config.get_option('non-existant'), None)
value = tree_config.get_option('non-existant', 'SECTION')
self.assertEqual(value, None)
value = tree_config.get_option('non-existant', default='default')
self.assertEqual(value, 'default')
self.assertEqual(tree_config.get_option('key2', 'NOSECTION'), None)
value = tree_config.get_option('key2', 'NOSECTION', default='default')
self.assertEqual(value, 'default')
value = tree_config.get_option('key3')
self.assertEqual(value, 'value3-top')
value = tree_config.get_option('key3', 'SECTION')
self.assertEqual(value, 'value3-section')
class TestTransportConfig(tests.TestCaseWithTransport):
def test_load_utf8(self):
"""Ensure we can load an utf8-encoded file."""
t = self.get_transport()
unicode_user = u'b\N{Euro Sign}ar'
unicode_content = u'user=%s' % (unicode_user,)
utf8_content = unicode_content.encode('utf8')
# Store the raw content in the config file
t.put_bytes('foo.conf', utf8_content)
conf = config.TransportConfig(t, 'foo.conf')
self.assertEquals(unicode_user, conf.get_option('user'))
def test_load_non_ascii(self):
"""Ensure we display a proper error on non-ascii, non utf-8 content."""
t = self.get_transport()
t.put_bytes('foo.conf', 'user=foo\n#\xff\n')
conf = config.TransportConfig(t, 'foo.conf')
self.assertRaises(errors.ConfigContentError, conf._get_configobj)
def test_load_erroneous_content(self):
"""Ensure we display a proper error on content that can't be parsed."""
t = self.get_transport()
t.put_bytes('foo.conf', '[open_section\n')
conf = config.TransportConfig(t, 'foo.conf')
self.assertRaises(errors.ParseConfigError, conf._get_configobj)
def test_get_value(self):
"""Test that retreiving a value from a section is possible"""
bzrdir_config = config.TransportConfig(self.get_transport('.'),
'control.conf')
bzrdir_config.set_option('value', 'key', 'SECTION')
bzrdir_config.set_option('value2', 'key2')
bzrdir_config.set_option('value3-top', 'key3')
bzrdir_config.set_option('value3-section', 'key3', 'SECTION')
value = bzrdir_config.get_option('key', 'SECTION')
self.assertEqual(value, 'value')
value = bzrdir_config.get_option('key2')
self.assertEqual(value, 'value2')
self.assertEqual(bzrdir_config.get_option('non-existant'), None)
value = bzrdir_config.get_option('non-existant', 'SECTION')
self.assertEqual(value, None)
value = bzrdir_config.get_option('non-existant', default='default')
self.assertEqual(value, 'default')
self.assertEqual(bzrdir_config.get_option('key2', 'NOSECTION'), None)
value = bzrdir_config.get_option('key2', 'NOSECTION',
default='default')
self.assertEqual(value, 'default')
value = bzrdir_config.get_option('key3')
self.assertEqual(value, 'value3-top')
value = bzrdir_config.get_option('key3', 'SECTION')
self.assertEqual(value, 'value3-section')
def test_set_unset_default_stack_on(self):
my_dir = self.make_bzrdir('.')
bzrdir_config = config.BzrDirConfig(my_dir)
self.assertIs(None, bzrdir_config.get_default_stack_on())
bzrdir_config.set_default_stack_on('Foo')
self.assertEqual('Foo', bzrdir_config._config.get_option(
'default_stack_on'))
self.assertEqual('Foo', bzrdir_config.get_default_stack_on())
bzrdir_config.set_default_stack_on(None)
self.assertIs(None, bzrdir_config.get_default_stack_on())
class TestOldConfigHooks(tests.TestCaseWithTransport):
def setUp(self):
super(TestOldConfigHooks, self).setUp()
create_configs_with_file_option(self)
def assertGetHook(self, conf, name, value):
calls = []
def hook(*args):
calls.append(args)
config.OldConfigHooks.install_named_hook('get', hook, None)
self.addCleanup(
config.OldConfigHooks.uninstall_named_hook, 'get', None)
self.assertLength(0, calls)
actual_value = conf.get_user_option(name)
self.assertEquals(value, actual_value)
self.assertLength(1, calls)
self.assertEquals((conf, name, value), calls[0])
def test_get_hook_bazaar(self):
self.assertGetHook(self.bazaar_config, 'file', 'bazaar')
def test_get_hook_locations(self):
self.assertGetHook(self.locations_config, 'file', 'locations')
def test_get_hook_branch(self):
# Since locations masks branch, we define a different option
self.branch_config.set_user_option('file2', 'branch')
self.assertGetHook(self.branch_config, 'file2', 'branch')
def assertSetHook(self, conf, name, value):
calls = []
def hook(*args):
calls.append(args)
config.OldConfigHooks.install_named_hook('set', hook, None)
self.addCleanup(
config.OldConfigHooks.uninstall_named_hook, 'set', None)
self.assertLength(0, calls)
conf.set_user_option(name, value)
self.assertLength(1, calls)
# We can't assert the conf object below as different configs use
# different means to implement set_user_option and we care only about
# coverage here.
self.assertEquals((name, value), calls[0][1:])
def test_set_hook_bazaar(self):
self.assertSetHook(self.bazaar_config, 'foo', 'bazaar')
def test_set_hook_locations(self):
self.assertSetHook(self.locations_config, 'foo', 'locations')
def test_set_hook_branch(self):
self.assertSetHook(self.branch_config, 'foo', 'branch')
def assertRemoveHook(self, conf, name, section_name=None):
calls = []
def hook(*args):
calls.append(args)
config.OldConfigHooks.install_named_hook('remove', hook, None)
self.addCleanup(
config.OldConfigHooks.uninstall_named_hook, 'remove', None)
self.assertLength(0, calls)
conf.remove_user_option(name, section_name)
self.assertLength(1, calls)
# We can't assert the conf object below as different configs use
# different means to implement remove_user_option and we care only about
# coverage here.
self.assertEquals((name,), calls[0][1:])
def test_remove_hook_bazaar(self):
self.assertRemoveHook(self.bazaar_config, 'file')
def test_remove_hook_locations(self):
self.assertRemoveHook(self.locations_config, 'file',
self.locations_config.location)
def test_remove_hook_branch(self):
self.assertRemoveHook(self.branch_config, 'file')
def assertLoadHook(self, name, conf_class, *conf_args):
calls = []
def hook(*args):
calls.append(args)
config.OldConfigHooks.install_named_hook('load', hook, None)
self.addCleanup(
config.OldConfigHooks.uninstall_named_hook, 'load', None)
self.assertLength(0, calls)
# Build a config
conf = conf_class(*conf_args)
# Access an option to trigger a load
conf.get_user_option(name)
self.assertLength(1, calls)
# Since we can't assert about conf, we just use the number of calls ;-/
def test_load_hook_bazaar(self):
self.assertLoadHook('file', config.GlobalConfig)
def test_load_hook_locations(self):
self.assertLoadHook('file', config.LocationConfig, self.tree.basedir)
def test_load_hook_branch(self):
self.assertLoadHook('file', config.BranchConfig, self.tree.branch)
def assertSaveHook(self, conf):
calls = []
def hook(*args):
calls.append(args)
config.OldConfigHooks.install_named_hook('save', hook, None)
self.addCleanup(
config.OldConfigHooks.uninstall_named_hook, 'save', None)
self.assertLength(0, calls)
# Setting an option triggers a save
conf.set_user_option('foo', 'bar')
self.assertLength(1, calls)
# Since we can't assert about conf, we just use the number of calls ;-/
def test_save_hook_bazaar(self):
self.assertSaveHook(self.bazaar_config)
def test_save_hook_locations(self):
self.assertSaveHook(self.locations_config)
def test_save_hook_branch(self):
self.assertSaveHook(self.branch_config)
class TestOldConfigHooksForRemote(tests.TestCaseWithTransport):
"""Tests config hooks for remote configs.
No tests for the remove hook as this is not implemented there.
"""
def setUp(self):
super(TestOldConfigHooksForRemote, self).setUp()
self.transport_server = test_server.SmartTCPServer_for_testing
create_configs_with_file_option(self)
def assertGetHook(self, conf, name, value):
calls = []
def hook(*args):
calls.append(args)
config.OldConfigHooks.install_named_hook('get', hook, None)
self.addCleanup(
config.OldConfigHooks.uninstall_named_hook, 'get', None)
self.assertLength(0, calls)
actual_value = conf.get_option(name)
self.assertEquals(value, actual_value)
self.assertLength(1, calls)
self.assertEquals((conf, name, value), calls[0])
def test_get_hook_remote_branch(self):
remote_branch = branch.Branch.open(self.get_url('tree'))
self.assertGetHook(remote_branch._get_config(), 'file', 'branch')
def test_get_hook_remote_bzrdir(self):
remote_bzrdir = bzrdir.BzrDir.open(self.get_url('tree'))
conf = remote_bzrdir._get_config()
conf.set_option('remotedir', 'file')
self.assertGetHook(conf, 'file', 'remotedir')
def assertSetHook(self, conf, name, value):
calls = []
def hook(*args):
calls.append(args)
config.OldConfigHooks.install_named_hook('set', hook, None)
self.addCleanup(
config.OldConfigHooks.uninstall_named_hook, 'set', None)
self.assertLength(0, calls)
conf.set_option(value, name)
self.assertLength(1, calls)
# We can't assert the conf object below as different configs use
# different means to implement set_user_option and we care only about
# coverage here.
self.assertEquals((name, value), calls[0][1:])
def test_set_hook_remote_branch(self):
remote_branch = branch.Branch.open(self.get_url('tree'))
self.addCleanup(remote_branch.lock_write().unlock)
self.assertSetHook(remote_branch._get_config(), 'file', 'remote')
def test_set_hook_remote_bzrdir(self):
remote_branch = branch.Branch.open(self.get_url('tree'))
self.addCleanup(remote_branch.lock_write().unlock)
remote_bzrdir = bzrdir.BzrDir.open(self.get_url('tree'))
self.assertSetHook(remote_bzrdir._get_config(), 'file', 'remotedir')
def assertLoadHook(self, expected_nb_calls, name, conf_class, *conf_args):
calls = []
def hook(*args):
calls.append(args)
config.OldConfigHooks.install_named_hook('load', hook, None)
self.addCleanup(
config.OldConfigHooks.uninstall_named_hook, 'load', None)
self.assertLength(0, calls)
# Build a config
conf = conf_class(*conf_args)
# Access an option to trigger a load
conf.get_option(name)
self.assertLength(expected_nb_calls, calls)
# Since we can't assert about conf, we just use the number of calls ;-/
def test_load_hook_remote_branch(self):
remote_branch = branch.Branch.open(self.get_url('tree'))
self.assertLoadHook(1, 'file', remote.RemoteBranchConfig, remote_branch)
def test_load_hook_remote_bzrdir(self):
remote_bzrdir = bzrdir.BzrDir.open(self.get_url('tree'))
# The config file doesn't exist, set an option to force its creation
conf = remote_bzrdir._get_config()
conf.set_option('remotedir', 'file')
# We get one call for the server and one call for the client, this is
# caused by the differences in implementations betwen
# SmartServerBzrDirRequestConfigFile (in smart/bzrdir.py) and
# SmartServerBranchGetConfigFile (in smart/branch.py)
self.assertLoadHook(2 ,'file', remote.RemoteBzrDirConfig, remote_bzrdir)
def assertSaveHook(self, conf):
calls = []
def hook(*args):
calls.append(args)
config.OldConfigHooks.install_named_hook('save', hook, None)
self.addCleanup(
config.OldConfigHooks.uninstall_named_hook, 'save', None)
self.assertLength(0, calls)
# Setting an option triggers a save
conf.set_option('foo', 'bar')
self.assertLength(1, calls)
# Since we can't assert about conf, we just use the number of calls ;-/
def test_save_hook_remote_branch(self):
remote_branch = branch.Branch.open(self.get_url('tree'))
self.addCleanup(remote_branch.lock_write().unlock)
self.assertSaveHook(remote_branch._get_config())
def test_save_hook_remote_bzrdir(self):
remote_branch = branch.Branch.open(self.get_url('tree'))
self.addCleanup(remote_branch.lock_write().unlock)
remote_bzrdir = bzrdir.BzrDir.open(self.get_url('tree'))
self.assertSaveHook(remote_bzrdir._get_config())
class TestOption(tests.TestCase):
def test_default_value(self):
opt = config.Option('foo', default='bar')
self.assertEquals('bar', opt.get_default())
class TestOptionRegistry(tests.TestCase):
def setUp(self):
super(TestOptionRegistry, self).setUp()
# Always start with an empty registry
self.overrideAttr(config, 'option_registry', registry.Registry())
self.registry = config.option_registry
def test_register(self):
opt = config.Option('foo')
self.registry.register('foo', opt)
self.assertIs(opt, self.registry.get('foo'))
lazy_option = config.Option('lazy_foo')
def test_register_lazy(self):
self.registry.register_lazy('foo', self.__module__,
'TestOptionRegistry.lazy_option')
self.assertIs(self.lazy_option, self.registry.get('foo'))
def test_registered_help(self):
opt = config.Option('foo')
self.registry.register('foo', opt, help='A simple option')
self.assertEquals('A simple option', self.registry.get_help('foo'))
class TestRegisteredOptions(tests.TestCase):
"""All registered options should verify some constraints."""
scenarios = [(key, {'option_name': key, 'option': option}) for key, option
in config.option_registry.iteritems()]
def setUp(self):
super(TestRegisteredOptions, self).setUp()
self.registry = config.option_registry
def test_proper_name(self):
# An option should be registered under its own name, this can't be
# checked at registration time for the lazy ones.
self.assertEquals(self.option_name, self.option.name)
def test_help_is_set(self):
option_help = self.registry.get_help(self.option_name)
self.assertNotEquals(None, option_help)
# Come on, think about the user, he really wants to know whst the
# option is about
self.assertNotEquals('', option_help)
class TestSection(tests.TestCase):
# FIXME: Parametrize so that all sections produced by Stores run these
# tests -- vila 2011-04-01
def test_get_a_value(self):
a_dict = dict(foo='bar')
section = config.Section('myID', a_dict)
self.assertEquals('bar', section.get('foo'))
def test_get_unknown_option(self):
a_dict = dict()
section = config.Section(None, a_dict)
self.assertEquals('out of thin air',
section.get('foo', 'out of thin air'))
def test_options_is_shared(self):
a_dict = dict()
section = config.Section(None, a_dict)
self.assertIs(a_dict, section.options)
class TestMutableSection(tests.TestCase):
# FIXME: Parametrize so that all sections (including os.environ and the
# ones produced by Stores) run these tests -- vila 2011-04-01
def test_set(self):
a_dict = dict(foo='bar')
section = config.MutableSection('myID', a_dict)
section.set('foo', 'new_value')
self.assertEquals('new_value', section.get('foo'))
# The change appears in the shared section
self.assertEquals('new_value', a_dict.get('foo'))
# We keep track of the change
self.assertTrue('foo' in section.orig)
self.assertEquals('bar', section.orig.get('foo'))
def test_set_preserve_original_once(self):
a_dict = dict(foo='bar')
section = config.MutableSection('myID', a_dict)
section.set('foo', 'first_value')
section.set('foo', 'second_value')
# We keep track of the original value
self.assertTrue('foo' in section.orig)
self.assertEquals('bar', section.orig.get('foo'))
def test_remove(self):
a_dict = dict(foo='bar')
section = config.MutableSection('myID', a_dict)
section.remove('foo')
# We get None for unknown options via the default value
self.assertEquals(None, section.get('foo'))
# Or we just get the default value
self.assertEquals('unknown', section.get('foo', 'unknown'))
self.assertFalse('foo' in section.options)
# We keep track of the deletion
self.assertTrue('foo' in section.orig)
self.assertEquals('bar', section.orig.get('foo'))
def test_remove_new_option(self):
a_dict = dict()
section = config.MutableSection('myID', a_dict)
section.set('foo', 'bar')
section.remove('foo')
self.assertFalse('foo' in section.options)
# The option didn't exist initially so it we need to keep track of it
# with a special value
self.assertTrue('foo' in section.orig)
self.assertEquals(config._NewlyCreatedOption, section.orig['foo'])
class TestStore(tests.TestCaseWithTransport):
def assertSectionContent(self, expected, section):
"""Assert that some options have the proper values in a section."""
expected_name, expected_options = expected
self.assertEquals(expected_name, section.id)
self.assertEquals(
expected_options,
dict([(k, section.get(k)) for k in expected_options.keys()]))
class TestReadonlyStore(TestStore):
scenarios = [(key, {'get_store': builder}) for key, builder
in config.test_store_builder_registry.iteritems()]
def setUp(self):
super(TestReadonlyStore, self).setUp()
def test_building_delays_load(self):
store = self.get_store(self)
self.assertEquals(False, store.is_loaded())
store._load_from_string('')
self.assertEquals(True, store.is_loaded())
def test_get_no_sections_for_empty(self):
store = self.get_store(self)
store._load_from_string('')
self.assertEquals([], list(store.get_sections()))
def test_get_default_section(self):
store = self.get_store(self)
store._load_from_string('foo=bar')
sections = list(store.get_sections())
self.assertLength(1, sections)
self.assertSectionContent((None, {'foo': 'bar'}), sections[0])
def test_get_named_section(self):
store = self.get_store(self)
store._load_from_string('[baz]\nfoo=bar')
sections = list(store.get_sections())
self.assertLength(1, sections)
self.assertSectionContent(('baz', {'foo': 'bar'}), sections[0])
def test_load_from_string_fails_for_non_empty_store(self):
store = self.get_store(self)
store._load_from_string('foo=bar')
self.assertRaises(AssertionError, store._load_from_string, 'bar=baz')
class TestIniFileStoreContent(tests.TestCaseWithTransport):
"""Simulate loading a config store without content of various encodings.
All files produced by bzr are in utf8 content.
Users may modify them manually and end up with a file that can't be
loaded. We need to issue proper error messages in this case.
"""
invalid_utf8_char = '\xff'
def test_load_utf8(self):
"""Ensure we can load an utf8-encoded file."""
t = self.get_transport()
# From http://pad.lv/799212
unicode_user = u'b\N{Euro Sign}ar'
unicode_content = u'user=%s' % (unicode_user,)
utf8_content = unicode_content.encode('utf8')
# Store the raw content in the config file
t.put_bytes('foo.conf', utf8_content)
store = config.IniFileStore(t, 'foo.conf')
store.load()
stack = config.Stack([store.get_sections], store)
self.assertEquals(unicode_user, stack.get('user'))
def test_load_non_ascii(self):
"""Ensure we display a proper error on non-ascii, non utf-8 content."""
t = self.get_transport()
t.put_bytes('foo.conf', 'user=foo\n#%s\n' % (self.invalid_utf8_char,))
store = config.IniFileStore(t, 'foo.conf')
self.assertRaises(errors.ConfigContentError, store.load)
def test_load_erroneous_content(self):
"""Ensure we display a proper error on content that can't be parsed."""
t = self.get_transport()
t.put_bytes('foo.conf', '[open_section\n')
store = config.IniFileStore(t, 'foo.conf')
self.assertRaises(errors.ParseConfigError, store.load)
class TestIniConfigContent(tests.TestCaseWithTransport):
"""Simulate loading a IniBasedConfig without content of various encodings.
All files produced by bzr are in utf8 content.
Users may modify them manually and end up with a file that can't be
loaded. We need to issue proper error messages in this case.
"""
invalid_utf8_char = '\xff'
def test_load_utf8(self):
"""Ensure we can load an utf8-encoded file."""
# From http://pad.lv/799212
unicode_user = u'b\N{Euro Sign}ar'
unicode_content = u'user=%s' % (unicode_user,)
utf8_content = unicode_content.encode('utf8')
# Store the raw content in the config file
with open('foo.conf', 'wb') as f:
f.write(utf8_content)
conf = config.IniBasedConfig(file_name='foo.conf')
self.assertEquals(unicode_user, conf.get_user_option('user'))
def test_load_badly_encoded_content(self):
"""Ensure we display a proper error on non-ascii, non utf-8 content."""
with open('foo.conf', 'wb') as f:
f.write('user=foo\n#%s\n' % (self.invalid_utf8_char,))
conf = config.IniBasedConfig(file_name='foo.conf')
self.assertRaises(errors.ConfigContentError, conf._get_parser)
def test_load_erroneous_content(self):
"""Ensure we display a proper error on content that can't be parsed."""
with open('foo.conf', 'wb') as f:
f.write('[open_section\n')
conf = config.IniBasedConfig(file_name='foo.conf')
self.assertRaises(errors.ParseConfigError, conf._get_parser)
class TestMutableStore(TestStore):
scenarios = [(key, {'store_id': key, 'get_store': builder}) for key, builder
in config.test_store_builder_registry.iteritems()]
def setUp(self):
super(TestMutableStore, self).setUp()
self.transport = self.get_transport()
def has_store(self, store):
store_basename = urlutils.relative_url(self.transport.external_url(),
store.external_url())
return self.transport.has(store_basename)
def test_save_empty_creates_no_file(self):
# FIXME: There should be a better way than relying on the test
# parametrization to identify branch.conf -- vila 2011-0526
if self.store_id in ('branch', 'remote_branch'):
raise tests.TestNotApplicable(
'branch.conf is *always* created when a branch is initialized')
store = self.get_store(self)
store.save()
self.assertEquals(False, self.has_store(store))
def test_save_emptied_succeeds(self):
store = self.get_store(self)
store._load_from_string('foo=bar\n')
section = store.get_mutable_section(None)
section.remove('foo')
store.save()
self.assertEquals(True, self.has_store(store))
modified_store = self.get_store(self)
sections = list(modified_store.get_sections())
self.assertLength(0, sections)
def test_save_with_content_succeeds(self):
# FIXME: There should be a better way than relying on the test
# parametrization to identify branch.conf -- vila 2011-0526
if self.store_id in ('branch', 'remote_branch'):
raise tests.TestNotApplicable(
'branch.conf is *always* created when a branch is initialized')
store = self.get_store(self)
store._load_from_string('foo=bar\n')
self.assertEquals(False, self.has_store(store))
store.save()
self.assertEquals(True, self.has_store(store))
modified_store = self.get_store(self)
sections = list(modified_store.get_sections())
self.assertLength(1, sections)
self.assertSectionContent((None, {'foo': 'bar'}), sections[0])
def test_set_option_in_empty_store(self):
store = self.get_store(self)
section = store.get_mutable_section(None)
section.set('foo', 'bar')
store.save()
modified_store = self.get_store(self)
sections = list(modified_store.get_sections())
self.assertLength(1, sections)
self.assertSectionContent((None, {'foo': 'bar'}), sections[0])
def test_set_option_in_default_section(self):
store = self.get_store(self)
store._load_from_string('')
section = store.get_mutable_section(None)
section.set('foo', 'bar')
store.save()
modified_store = self.get_store(self)
sections = list(modified_store.get_sections())
self.assertLength(1, sections)
self.assertSectionContent((None, {'foo': 'bar'}), sections[0])
def test_set_option_in_named_section(self):
store = self.get_store(self)
store._load_from_string('')
section = store.get_mutable_section('baz')
section.set('foo', 'bar')
store.save()
modified_store = self.get_store(self)
sections = list(modified_store.get_sections())
self.assertLength(1, sections)
self.assertSectionContent(('baz', {'foo': 'bar'}), sections[0])
def test_load_hook(self):
# We first needs to ensure that the store exists
store = self.get_store(self)
section = store.get_mutable_section('baz')
section.set('foo', 'bar')
store.save()
# Now we can try to load it
store = self.get_store(self)
calls = []
def hook(*args):
calls.append(args)
config.ConfigHooks.install_named_hook('load', hook, None)
self.assertLength(0, calls)
store.load()
self.assertLength(1, calls)
self.assertEquals((store,), calls[0])
def test_save_hook(self):
calls = []
def hook(*args):
calls.append(args)
config.ConfigHooks.install_named_hook('save', hook, None)
self.assertLength(0, calls)
store = self.get_store(self)
section = store.get_mutable_section('baz')
section.set('foo', 'bar')
store.save()
self.assertLength(1, calls)
self.assertEquals((store,), calls[0])
class TestIniFileStore(TestStore):
def test_loading_unknown_file_fails(self):
store = config.IniFileStore(self.get_transport(), 'I-do-not-exist')
self.assertRaises(errors.NoSuchFile, store.load)
def test_invalid_content(self):
store = config.IniFileStore(self.get_transport(), 'foo.conf', )
self.assertEquals(False, store.is_loaded())
exc = self.assertRaises(
errors.ParseConfigError, store._load_from_string,
'this is invalid !')
self.assertEndsWith(exc.filename, 'foo.conf')
# And the load failed
self.assertEquals(False, store.is_loaded())
def test_get_embedded_sections(self):
# A more complicated example (which also shows that section names and
# option names share the same name space...)
# FIXME: This should be fixed by forbidding dicts as values ?
# -- vila 2011-04-05
store = config.IniFileStore(self.get_transport(), 'foo.conf', )
store._load_from_string('''
foo=bar
l=1,2
[DEFAULT]
foo_in_DEFAULT=foo_DEFAULT
[bar]
foo_in_bar=barbar
[baz]
foo_in_baz=barbaz
[[qux]]
foo_in_qux=quux
''')
sections = list(store.get_sections())
self.assertLength(4, sections)
# The default section has no name.
# List values are provided as lists
self.assertSectionContent((None, {'foo': 'bar', 'l': ['1', '2']}),
sections[0])
self.assertSectionContent(
('DEFAULT', {'foo_in_DEFAULT': 'foo_DEFAULT'}), sections[1])
self.assertSectionContent(
('bar', {'foo_in_bar': 'barbar'}), sections[2])
# sub sections are provided as embedded dicts.
self.assertSectionContent(
('baz', {'foo_in_baz': 'barbaz', 'qux': {'foo_in_qux': 'quux'}}),
sections[3])
class TestLockableIniFileStore(TestStore):
def test_create_store_in_created_dir(self):
self.assertPathDoesNotExist('dir')
t = self.get_transport('dir/subdir')
store = config.LockableIniFileStore(t, 'foo.conf')
store.get_mutable_section(None).set('foo', 'bar')
store.save()
self.assertPathExists('dir/subdir')
class TestConcurrentStoreUpdates(TestStore):
"""Test that Stores properly handle conccurent updates.
New Store implementation may fail some of these tests but until such
implementations exist it's hard to properly filter them from the scenarios
applied here. If you encounter such a case, contact the bzr devs.
"""
scenarios = [(key, {'get_stack': builder}) for key, builder
in config.test_stack_builder_registry.iteritems()]
def setUp(self):
super(TestConcurrentStoreUpdates, self).setUp()
self._content = 'one=1\ntwo=2\n'
self.stack = self.get_stack(self)
if not isinstance(self.stack, config._CompatibleStack):
raise tests.TestNotApplicable(
'%s is not meant to be compatible with the old config design'
% (self.stack,))
self.stack.store._load_from_string(self._content)
# Flush the store
self.stack.store.save()
def test_simple_read_access(self):
self.assertEquals('1', self.stack.get('one'))
def test_simple_write_access(self):
self.stack.set('one', 'one')
self.assertEquals('one', self.stack.get('one'))
def test_listen_to_the_last_speaker(self):
c1 = self.stack
c2 = self.get_stack(self)
c1.set('one', 'ONE')
c2.set('two', 'TWO')
self.assertEquals('ONE', c1.get('one'))
self.assertEquals('TWO', c2.get('two'))
# The second update respect the first one
self.assertEquals('ONE', c2.get('one'))
def test_last_speaker_wins(self):
# If the same config is not shared, the same variable modified twice
# can only see a single result.
c1 = self.stack
c2 = self.get_stack(self)
c1.set('one', 'c1')
c2.set('one', 'c2')
self.assertEquals('c2', c2.get('one'))
# The first modification is still available until another refresh
# occur
self.assertEquals('c1', c1.get('one'))
c1.set('two', 'done')
self.assertEquals('c2', c1.get('one'))
def test_writes_are_serialized(self):
c1 = self.stack
c2 = self.get_stack(self)
# We spawn a thread that will pause *during* the config saving.
before_writing = threading.Event()
after_writing = threading.Event()
writing_done = threading.Event()
c1_save_without_locking_orig = c1.store.save_without_locking
def c1_save_without_locking():
before_writing.set()
c1_save_without_locking_orig()
# The lock is held. We wait for the main thread to decide when to
# continue
after_writing.wait()
c1.store.save_without_locking = c1_save_without_locking
def c1_set():
c1.set('one', 'c1')
writing_done.set()
t1 = threading.Thread(target=c1_set)
# Collect the thread after the test
self.addCleanup(t1.join)
# Be ready to unblock the thread if the test goes wrong
self.addCleanup(after_writing.set)
t1.start()
before_writing.wait()
self.assertRaises(errors.LockContention,
c2.set, 'one', 'c2')
self.assertEquals('c1', c1.get('one'))
# Let the lock be released
after_writing.set()
writing_done.wait()
c2.set('one', 'c2')
self.assertEquals('c2', c2.get('one'))
def test_read_while_writing(self):
c1 = self.stack
# We spawn a thread that will pause *during* the write
ready_to_write = threading.Event()
do_writing = threading.Event()
writing_done = threading.Event()
# We override the _save implementation so we know the store is locked
c1_save_without_locking_orig = c1.store.save_without_locking
def c1_save_without_locking():
ready_to_write.set()
# The lock is held. We wait for the main thread to decide when to
# continue
do_writing.wait()
c1_save_without_locking_orig()
writing_done.set()
c1.store.save_without_locking = c1_save_without_locking
def c1_set():
c1.set('one', 'c1')
t1 = threading.Thread(target=c1_set)
# Collect the thread after the test
self.addCleanup(t1.join)
# Be ready to unblock the thread if the test goes wrong
self.addCleanup(do_writing.set)
t1.start()
# Ensure the thread is ready to write
ready_to_write.wait()
self.assertEquals('c1', c1.get('one'))
# If we read during the write, we get the old value
c2 = self.get_stack(self)
self.assertEquals('1', c2.get('one'))
# Let the writing occur and ensure it occurred
do_writing.set()
writing_done.wait()
# Now we get the updated value
c3 = self.get_stack(self)
self.assertEquals('c1', c3.get('one'))
# FIXME: It may be worth looking into removing the lock dir when it's not
# needed anymore and look at possible fallouts for concurrent lockers. This
# will matter if/when we use config files outside of bazaar directories
# (.bazaar or .bzr) -- vila 20110-04-11
class TestSectionMatcher(TestStore):
scenarios = [('location', {'matcher': config.LocationMatcher})]
def get_store(self, file_name):
return config.IniFileStore(self.get_readonly_transport(), file_name)
def test_no_matches_for_empty_stores(self):
store = self.get_store('foo.conf')
store._load_from_string('')
matcher = self.matcher(store, '/bar')
self.assertEquals([], list(matcher.get_sections()))
def test_build_doesnt_load_store(self):
store = self.get_store('foo.conf')
matcher = self.matcher(store, '/bar')
self.assertFalse(store.is_loaded())
class TestLocationSection(tests.TestCase):
def get_section(self, options, extra_path):
section = config.Section('foo', options)
# We don't care about the length so we use '0'
return config.LocationSection(section, 0, extra_path)
def test_simple_option(self):
section = self.get_section({'foo': 'bar'}, '')
self.assertEquals('bar', section.get('foo'))
def test_option_with_extra_path(self):
section = self.get_section({'foo': 'bar', 'foo:policy': 'appendpath'},
'baz')
self.assertEquals('bar/baz', section.get('foo'))
def test_invalid_policy(self):
section = self.get_section({'foo': 'bar', 'foo:policy': 'die'},
'baz')
# invalid policies are ignored
self.assertEquals('bar', section.get('foo'))
class TestLocationMatcher(TestStore):
def get_store(self, file_name):
return config.IniFileStore(self.get_readonly_transport(), file_name)
def test_more_specific_sections_first(self):
store = self.get_store('foo.conf')
store._load_from_string('''
[/foo]
section=/foo
[/foo/bar]
section=/foo/bar
''')
self.assertEquals(['/foo', '/foo/bar'],
[section.id for section in store.get_sections()])
matcher = config.LocationMatcher(store, '/foo/bar/baz')
sections = list(matcher.get_sections())
self.assertEquals([3, 2],
[section.length for section in sections])
self.assertEquals(['/foo/bar', '/foo'],
[section.id for section in sections])
self.assertEquals(['baz', 'bar/baz'],
[section.extra_path for section in sections])
def test_appendpath_in_no_name_section(self):
# It's a bit weird to allow appendpath in a no-name section, but
# someone may found a use for it
store = self.get_store('foo.conf')
store._load_from_string('''
foo=bar
foo:policy = appendpath
''')
matcher = config.LocationMatcher(store, 'dir/subdir')
sections = list(matcher.get_sections())
self.assertLength(1, sections)
self.assertEquals('bar/dir/subdir', sections[0].get('foo'))
def test_file_urls_are_normalized(self):
store = self.get_store('foo.conf')
if sys.platform == 'win32':
expected_url = 'file:///C:/dir/subdir'
expected_location = 'C:/dir/subdir'
else:
expected_url = 'file:///dir/subdir'
expected_location = '/dir/subdir'
matcher = config.LocationMatcher(store, expected_url)
self.assertEquals(expected_location, matcher.location)
class TestStackGet(tests.TestCase):
# FIXME: This should be parametrized for all known Stack or dedicated
# paramerized tests created to avoid bloating -- vila 2011-03-31
def test_single_config_get(self):
conf = dict(foo='bar')
conf_stack = config.Stack([conf])
self.assertEquals('bar', conf_stack.get('foo'))
def test_get_with_registered_default_value(self):
conf_stack = config.Stack([dict()])
opt = config.Option('foo', default='bar')
self.overrideAttr(config, 'option_registry', registry.Registry())
config.option_registry.register('foo', opt)
self.assertEquals('bar', conf_stack.get('foo'))
def test_get_without_registered_default_value(self):
conf_stack = config.Stack([dict()])
opt = config.Option('foo')
self.overrideAttr(config, 'option_registry', registry.Registry())
config.option_registry.register('foo', opt)
self.assertEquals(None, conf_stack.get('foo'))
def test_get_without_default_value_for_not_registered(self):
conf_stack = config.Stack([dict()])
opt = config.Option('foo')
self.overrideAttr(config, 'option_registry', registry.Registry())
self.assertEquals(None, conf_stack.get('foo'))
def test_get_first_definition(self):
conf1 = dict(foo='bar')
conf2 = dict(foo='baz')
conf_stack = config.Stack([conf1, conf2])
self.assertEquals('bar', conf_stack.get('foo'))
def test_get_embedded_definition(self):
conf1 = dict(yy='12')
conf2 = config.Stack([dict(xx='42'), dict(foo='baz')])
conf_stack = config.Stack([conf1, conf2])
self.assertEquals('baz', conf_stack.get('foo'))
def test_get_for_empty_section_callable(self):
conf_stack = config.Stack([lambda : []])
self.assertEquals(None, conf_stack.get('foo'))
def test_get_for_broken_callable(self):
# Trying to use and invalid callable raises an exception on first use
conf_stack = config.Stack([lambda : object()])
self.assertRaises(TypeError, conf_stack.get, 'foo')
class TestStackWithTransport(tests.TestCaseWithTransport):
scenarios = [(key, {'get_stack': builder}) for key, builder
in config.test_stack_builder_registry.iteritems()]
class TestConcreteStacks(TestStackWithTransport):
def test_build_stack(self):
# Just a smoke test to help debug builders
stack = self.get_stack(self)
class TestStackGet(TestStackWithTransport):
def test_get_for_empty_stack(self):
conf = self.get_stack(self)
self.assertEquals(None, conf.get('foo'))
def test_get_hook(self):
conf = self.get_stack(self)
conf.store._load_from_string('foo=bar')
calls = []
def hook(*args):
calls.append(args)
config.ConfigHooks.install_named_hook('get', hook, None)
self.assertLength(0, calls)
value = conf.get('foo')
self.assertEquals('bar', value)
self.assertLength(1, calls)
self.assertEquals((conf, 'foo', 'bar'), calls[0])
class TestStackSet(TestStackWithTransport):
def test_simple_set(self):
conf = self.get_stack(self)
conf.store._load_from_string('foo=bar')
self.assertEquals('bar', conf.get('foo'))
conf.set('foo', 'baz')
# Did we get it back ?
self.assertEquals('baz', conf.get('foo'))
def test_set_creates_a_new_section(self):
conf = self.get_stack(self)
conf.set('foo', 'baz')
self.assertEquals, 'baz', conf.get('foo')
def test_set_hook(self):
calls = []
def hook(*args):
calls.append(args)
config.ConfigHooks.install_named_hook('set', hook, None)
self.assertLength(0, calls)
conf = self.get_stack(self)
conf.set('foo', 'bar')
self.assertLength(1, calls)
self.assertEquals((conf, 'foo', 'bar'), calls[0])
class TestStackRemove(TestStackWithTransport):
def test_remove_existing(self):
conf = self.get_stack(self)
conf.store._load_from_string('foo=bar')
self.assertEquals('bar', conf.get('foo'))
conf.remove('foo')
# Did we get it back ?
self.assertEquals(None, conf.get('foo'))
def test_remove_unknown(self):
conf = self.get_stack(self)
self.assertRaises(KeyError, conf.remove, 'I_do_not_exist')
def test_remove_hook(self):
calls = []
def hook(*args):
calls.append(args)
config.ConfigHooks.install_named_hook('remove', hook, None)
self.assertLength(0, calls)
conf = self.get_stack(self)
conf.store._load_from_string('foo=bar')
conf.remove('foo')
self.assertLength(1, calls)
self.assertEquals((conf, 'foo'), calls[0])
class TestConfigGetOptions(tests.TestCaseWithTransport, TestOptionsMixin):
def setUp(self):
super(TestConfigGetOptions, self).setUp()
create_configs(self)
def test_no_variable(self):
# Using branch should query branch, locations and bazaar
self.assertOptions([], self.branch_config)
def test_option_in_bazaar(self):
self.bazaar_config.set_user_option('file', 'bazaar')
self.assertOptions([('file', 'bazaar', 'DEFAULT', 'bazaar')],
self.bazaar_config)
def test_option_in_locations(self):
self.locations_config.set_user_option('file', 'locations')
self.assertOptions(
[('file', 'locations', self.tree.basedir, 'locations')],
self.locations_config)
def test_option_in_branch(self):
self.branch_config.set_user_option('file', 'branch')
self.assertOptions([('file', 'branch', 'DEFAULT', 'branch')],
self.branch_config)
def test_option_in_bazaar_and_branch(self):
self.bazaar_config.set_user_option('file', 'bazaar')
self.branch_config.set_user_option('file', 'branch')
self.assertOptions([('file', 'branch', 'DEFAULT', 'branch'),
('file', 'bazaar', 'DEFAULT', 'bazaar'),],
self.branch_config)
def test_option_in_branch_and_locations(self):
# Hmm, locations override branch :-/
self.locations_config.set_user_option('file', 'locations')
self.branch_config.set_user_option('file', 'branch')
self.assertOptions(
[('file', 'locations', self.tree.basedir, 'locations'),
('file', 'branch', 'DEFAULT', 'branch'),],
self.branch_config)
def test_option_in_bazaar_locations_and_branch(self):
self.bazaar_config.set_user_option('file', 'bazaar')
self.locations_config.set_user_option('file', 'locations')
self.branch_config.set_user_option('file', 'branch')
self.assertOptions(
[('file', 'locations', self.tree.basedir, 'locations'),
('file', 'branch', 'DEFAULT', 'branch'),
('file', 'bazaar', 'DEFAULT', 'bazaar'),],
self.branch_config)
class TestConfigRemoveOption(tests.TestCaseWithTransport, TestOptionsMixin):
def setUp(self):
super(TestConfigRemoveOption, self).setUp()
create_configs_with_file_option(self)
def test_remove_in_locations(self):
self.locations_config.remove_user_option('file', self.tree.basedir)
self.assertOptions(
[('file', 'branch', 'DEFAULT', 'branch'),
('file', 'bazaar', 'DEFAULT', 'bazaar'),],
self.branch_config)
def test_remove_in_branch(self):
self.branch_config.remove_user_option('file')
self.assertOptions(
[('file', 'locations', self.tree.basedir, 'locations'),
('file', 'bazaar', 'DEFAULT', 'bazaar'),],
self.branch_config)
def test_remove_in_bazaar(self):
self.bazaar_config.remove_user_option('file')
self.assertOptions(
[('file', 'locations', self.tree.basedir, 'locations'),
('file', 'branch', 'DEFAULT', 'branch'),],
self.branch_config)
class TestConfigGetSections(tests.TestCaseWithTransport):
def setUp(self):
super(TestConfigGetSections, self).setUp()
create_configs(self)
def assertSectionNames(self, expected, conf, name=None):
"""Check which sections are returned for a given config.
If fallback configurations exist their sections can be included.
:param expected: A list of section names.
:param conf: The configuration that will be queried.
:param name: An optional section name that will be passed to
get_sections().
"""
sections = list(conf._get_sections(name))
self.assertLength(len(expected), sections)
self.assertEqual(expected, [name for name, _, _ in sections])
def test_bazaar_default_section(self):
self.assertSectionNames(['DEFAULT'], self.bazaar_config)
def test_locations_default_section(self):
# No sections are defined in an empty file
self.assertSectionNames([], self.locations_config)
def test_locations_named_section(self):
self.locations_config.set_user_option('file', 'locations')
self.assertSectionNames([self.tree.basedir], self.locations_config)
def test_locations_matching_sections(self):
loc_config = self.locations_config
loc_config.set_user_option('file', 'locations')
# We need to cheat a bit here to create an option in sections above and
# below the 'location' one.
parser = loc_config._get_parser()
# locations.cong deals with '/' ignoring native os.sep
location_names = self.tree.basedir.split('/')
parent = '/'.join(location_names[:-1])
child = '/'.join(location_names + ['child'])
parser[parent] = {}
parser[parent]['file'] = 'parent'
parser[child] = {}
parser[child]['file'] = 'child'
self.assertSectionNames([self.tree.basedir, parent], loc_config)
def test_branch_data_default_section(self):
self.assertSectionNames([None],
self.branch_config._get_branch_data_config())
def test_branch_default_sections(self):
# No sections are defined in an empty locations file
self.assertSectionNames([None, 'DEFAULT'],
self.branch_config)
# Unless we define an option
self.branch_config._get_location_config().set_user_option(
'file', 'locations')
self.assertSectionNames([self.tree.basedir, None, 'DEFAULT'],
self.branch_config)
def test_bazaar_named_section(self):
# We need to cheat as the API doesn't give direct access to sections
# other than DEFAULT.
self.bazaar_config.set_alias('bazaar', 'bzr')
self.assertSectionNames(['ALIASES'], self.bazaar_config, 'ALIASES')
class TestAuthenticationConfigFile(tests.TestCase):
"""Test the authentication.conf file matching"""
def _got_user_passwd(self, expected_user, expected_password,
config, *args, **kwargs):
credentials = config.get_credentials(*args, **kwargs)
if credentials is None:
user = None
password = None
else:
user = credentials['user']
password = credentials['password']
self.assertEquals(expected_user, user)
self.assertEquals(expected_password, password)
def test_empty_config(self):
conf = config.AuthenticationConfig(_file=StringIO())
self.assertEquals({}, conf._get_config())
self._got_user_passwd(None, None, conf, 'http', 'foo.net')
def test_non_utf8_config(self):
conf = config.AuthenticationConfig(_file=StringIO(
'foo = bar\xff'))
self.assertRaises(errors.ConfigContentError, conf._get_config)
def test_missing_auth_section_header(self):
conf = config.AuthenticationConfig(_file=StringIO('foo = bar'))
self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
def test_auth_section_header_not_closed(self):
conf = config.AuthenticationConfig(_file=StringIO('[DEF'))
self.assertRaises(errors.ParseConfigError, conf._get_config)
def test_auth_value_not_boolean(self):
conf = config.AuthenticationConfig(_file=StringIO(
"""[broken]
scheme=ftp
user=joe
verify_certificates=askme # Error: Not a boolean
"""))
self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
def test_auth_value_not_int(self):
conf = config.AuthenticationConfig(_file=StringIO(
"""[broken]
scheme=ftp
user=joe
port=port # Error: Not an int
"""))
self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
def test_unknown_password_encoding(self):
conf = config.AuthenticationConfig(_file=StringIO(
"""[broken]
scheme=ftp
user=joe
password_encoding=unknown
"""))
self.assertRaises(ValueError, conf.get_password,
'ftp', 'foo.net', 'joe')
def test_credentials_for_scheme_host(self):
conf = config.AuthenticationConfig(_file=StringIO(
"""# Identity on foo.net
[ftp definition]
scheme=ftp
host=foo.net
user=joe
password=secret-pass
"""))
# Basic matching
self._got_user_passwd('joe', 'secret-pass', conf, 'ftp', 'foo.net')
# different scheme
self._got_user_passwd(None, None, conf, 'http', 'foo.net')
# different host
self._got_user_passwd(None, None, conf, 'ftp', 'bar.net')
def test_credentials_for_host_port(self):
conf = config.AuthenticationConfig(_file=StringIO(
"""# Identity on foo.net
[ftp definition]
scheme=ftp
port=10021
host=foo.net
user=joe
password=secret-pass
"""))
# No port
self._got_user_passwd('joe', 'secret-pass',
conf, 'ftp', 'foo.net', port=10021)
# different port
self._got_user_passwd(None, None, conf, 'ftp', 'foo.net')
def test_for_matching_host(self):
conf = config.AuthenticationConfig(_file=StringIO(
"""# Identity on foo.net
[sourceforge]
scheme=bzr
host=bzr.sf.net
user=joe
password=joepass
[sourceforge domain]
scheme=bzr
host=.bzr.sf.net
user=georges
password=bendover
"""))
# matching domain
self._got_user_passwd('georges', 'bendover',
conf, 'bzr', 'foo.bzr.sf.net')
# phishing attempt
self._got_user_passwd(None, None,
conf, 'bzr', 'bbzr.sf.net')
def test_for_matching_host_None(self):
conf = config.AuthenticationConfig(_file=StringIO(
"""# Identity on foo.net
[catchup bzr]
scheme=bzr
user=joe
password=joepass
[DEFAULT]
user=georges
password=bendover
"""))
# match no host
self._got_user_passwd('joe', 'joepass',
conf, 'bzr', 'quux.net')
# no host but different scheme
self._got_user_passwd('georges', 'bendover',
conf, 'ftp', 'quux.net')
def test_credentials_for_path(self):
conf = config.AuthenticationConfig(_file=StringIO(
"""
[http dir1]
scheme=http
host=bar.org
path=/dir1
user=jim
password=jimpass
[http dir2]
scheme=http
host=bar.org
path=/dir2
user=georges
password=bendover
"""))
# no path no dice
self._got_user_passwd(None, None,
conf, 'http', host='bar.org', path='/dir3')
# matching path
self._got_user_passwd('georges', 'bendover',
conf, 'http', host='bar.org', path='/dir2')
# matching subdir
self._got_user_passwd('jim', 'jimpass',
conf, 'http', host='bar.org',path='/dir1/subdir')
def test_credentials_for_user(self):
conf = config.AuthenticationConfig(_file=StringIO(
"""
[with user]
scheme=http
host=bar.org
user=jim
password=jimpass
"""))
# Get user
self._got_user_passwd('jim', 'jimpass',
conf, 'http', 'bar.org')
# Get same user
self._got_user_passwd('jim', 'jimpass',
conf, 'http', 'bar.org', user='jim')
# Don't get a different user if one is specified
self._got_user_passwd(None, None,
conf, 'http', 'bar.org', user='georges')
def test_credentials_for_user_without_password(self):
conf = config.AuthenticationConfig(_file=StringIO(
"""
[without password]
scheme=http
host=bar.org
user=jim
"""))
# Get user but no password
self._got_user_passwd('jim', None,
conf, 'http', 'bar.org')
def test_verify_certificates(self):
conf = config.AuthenticationConfig(_file=StringIO(
"""
[self-signed]
scheme=https
host=bar.org
user=jim
password=jimpass
verify_certificates=False
[normal]
scheme=https
host=foo.net
user=georges
password=bendover
"""))
credentials = conf.get_credentials('https', 'bar.org')
self.assertEquals(False, credentials.get('verify_certificates'))
credentials = conf.get_credentials('https', 'foo.net')
self.assertEquals(True, credentials.get('verify_certificates'))
class TestAuthenticationStorage(tests.TestCaseInTempDir):
def test_set_credentials(self):
conf = config.AuthenticationConfig()
conf.set_credentials('name', 'host', 'user', 'scheme', 'password',
99, path='/foo', verify_certificates=False, realm='realm')
credentials = conf.get_credentials(host='host', scheme='scheme',
port=99, path='/foo',
realm='realm')
CREDENTIALS = {'name': 'name', 'user': 'user', 'password': 'password',
'verify_certificates': False, 'scheme': 'scheme',
'host': 'host', 'port': 99, 'path': '/foo',
'realm': 'realm'}
self.assertEqual(CREDENTIALS, credentials)
credentials_from_disk = config.AuthenticationConfig().get_credentials(
host='host', scheme='scheme', port=99, path='/foo', realm='realm')
self.assertEqual(CREDENTIALS, credentials_from_disk)
def test_reset_credentials_different_name(self):
conf = config.AuthenticationConfig()
conf.set_credentials('name', 'host', 'user', 'scheme', 'password'),
conf.set_credentials('name2', 'host', 'user2', 'scheme', 'password'),
self.assertIs(None, conf._get_config().get('name'))
credentials = conf.get_credentials(host='host', scheme='scheme')
CREDENTIALS = {'name': 'name2', 'user': 'user2', 'password':
'password', 'verify_certificates': True,
'scheme': 'scheme', 'host': 'host', 'port': None,
'path': None, 'realm': None}
self.assertEqual(CREDENTIALS, credentials)
class TestAuthenticationConfig(tests.TestCase):
"""Test AuthenticationConfig behaviour"""
def _check_default_password_prompt(self, expected_prompt_format, scheme,
host=None, port=None, realm=None,
path=None):
if host is None:
host = 'bar.org'
user, password = 'jim', 'precious'
expected_prompt = expected_prompt_format % {
'scheme': scheme, 'host': host, 'port': port,
'user': user, 'realm': realm}
stdout = tests.StringIOWrapper()
stderr = tests.StringIOWrapper()
ui.ui_factory = tests.TestUIFactory(stdin=password + '\n',
stdout=stdout, stderr=stderr)
# We use an empty conf so that the user is always prompted
conf = config.AuthenticationConfig()
self.assertEquals(password,
conf.get_password(scheme, host, user, port=port,
realm=realm, path=path))
self.assertEquals(expected_prompt, stderr.getvalue())
self.assertEquals('', stdout.getvalue())
def _check_default_username_prompt(self, expected_prompt_format, scheme,
host=None, port=None, realm=None,
path=None):
if host is None:
host = 'bar.org'
username = 'jim'
expected_prompt = expected_prompt_format % {
'scheme': scheme, 'host': host, 'port': port,
'realm': realm}
stdout = tests.StringIOWrapper()
stderr = tests.StringIOWrapper()
ui.ui_factory = tests.TestUIFactory(stdin=username+ '\n',
stdout=stdout, stderr=stderr)
# We use an empty conf so that the user is always prompted
conf = config.AuthenticationConfig()
self.assertEquals(username, conf.get_user(scheme, host, port=port,
realm=realm, path=path, ask=True))
self.assertEquals(expected_prompt, stderr.getvalue())
self.assertEquals('', stdout.getvalue())
def test_username_defaults_prompts(self):
# HTTP prompts can't be tested here, see test_http.py
self._check_default_username_prompt(u'FTP %(host)s username: ', 'ftp')
self._check_default_username_prompt(
u'FTP %(host)s:%(port)d username: ', 'ftp', port=10020)
self._check_default_username_prompt(
u'SSH %(host)s:%(port)d username: ', 'ssh', port=12345)
def test_username_default_no_prompt(self):
conf = config.AuthenticationConfig()
self.assertEquals(None,
conf.get_user('ftp', 'example.com'))
self.assertEquals("explicitdefault",
conf.get_user('ftp', 'example.com', default="explicitdefault"))
def test_password_default_prompts(self):
# HTTP prompts can't be tested here, see test_http.py
self._check_default_password_prompt(
u'FTP %(user)s@%(host)s password: ', 'ftp')
self._check_default_password_prompt(
u'FTP %(user)s@%(host)s:%(port)d password: ', 'ftp', port=10020)
self._check_default_password_prompt(
u'SSH %(user)s@%(host)s:%(port)d password: ', 'ssh', port=12345)
# SMTP port handling is a bit special (it's handled if embedded in the
# host too)
# FIXME: should we: forbid that, extend it to other schemes, leave
# things as they are that's fine thank you ?
self._check_default_password_prompt(
u'SMTP %(user)s@%(host)s password: ', 'smtp')
self._check_default_password_prompt(
u'SMTP %(user)s@%(host)s password: ', 'smtp', host='bar.org:10025')
self._check_default_password_prompt(
u'SMTP %(user)s@%(host)s:%(port)d password: ', 'smtp', port=10025)
def test_ssh_password_emits_warning(self):
conf = config.AuthenticationConfig(_file=StringIO(
"""
[ssh with password]
scheme=ssh
host=bar.org
user=jim
password=jimpass
"""))
entered_password = 'typed-by-hand'
stdout = tests.StringIOWrapper()
stderr = tests.StringIOWrapper()
ui.ui_factory = tests.TestUIFactory(stdin=entered_password + '\n',
stdout=stdout, stderr=stderr)
# Since the password defined in the authentication config is ignored,
# the user is prompted
self.assertEquals(entered_password,
conf.get_password('ssh', 'bar.org', user='jim'))
self.assertContainsRe(
self.get_log(),
'password ignored in section \[ssh with password\]')
def test_ssh_without_password_doesnt_emit_warning(self):
conf = config.AuthenticationConfig(_file=StringIO(
"""
[ssh with password]
scheme=ssh
host=bar.org
user=jim
"""))
entered_password = 'typed-by-hand'
stdout = tests.StringIOWrapper()
stderr = tests.StringIOWrapper()
ui.ui_factory = tests.TestUIFactory(stdin=entered_password + '\n',
stdout=stdout,
stderr=stderr)
# Since the password defined in the authentication config is ignored,
# the user is prompted
self.assertEquals(entered_password,
conf.get_password('ssh', 'bar.org', user='jim'))
# No warning shoud be emitted since there is no password. We are only
# providing "user".
self.assertNotContainsRe(
self.get_log(),
'password ignored in section \[ssh with password\]')
def test_uses_fallback_stores(self):
self.overrideAttr(config, 'credential_store_registry',
config.CredentialStoreRegistry())
store = StubCredentialStore()
store.add_credentials("http", "example.com", "joe", "secret")
config.credential_store_registry.register("stub", store, fallback=True)
conf = config.AuthenticationConfig(_file=StringIO())
creds = conf.get_credentials("http", "example.com")
self.assertEquals("joe", creds["user"])
self.assertEquals("secret", creds["password"])
class StubCredentialStore(config.CredentialStore):
def __init__(self):
self._username = {}
self._password = {}
def add_credentials(self, scheme, host, user, password=None):
self._username[(scheme, host)] = user
self._password[(scheme, host)] = password
def get_credentials(self, scheme, host, port=None, user=None,
path=None, realm=None):
key = (scheme, host)
if not key in self._username:
return None
return { "scheme": scheme, "host": host, "port": port,
"user": self._username[key], "password": self._password[key]}
class CountingCredentialStore(config.CredentialStore):
def __init__(self):
self._calls = 0
def get_credentials(self, scheme, host, port=None, user=None,
path=None, realm=None):
self._calls += 1
return None
class TestCredentialStoreRegistry(tests.TestCase):
def _get_cs_registry(self):
return config.credential_store_registry
def test_default_credential_store(self):
r = self._get_cs_registry()
default = r.get_credential_store(None)
self.assertIsInstance(default, config.PlainTextCredentialStore)
def test_unknown_credential_store(self):
r = self._get_cs_registry()
# It's hard to imagine someone creating a credential store named
# 'unknown' so we use that as an never registered key.
self.assertRaises(KeyError, r.get_credential_store, 'unknown')
def test_fallback_none_registered(self):
r = config.CredentialStoreRegistry()
self.assertEquals(None,
r.get_fallback_credentials("http", "example.com"))
def test_register(self):
r = config.CredentialStoreRegistry()
r.register("stub", StubCredentialStore(), fallback=False)
r.register("another", StubCredentialStore(), fallback=True)
self.assertEquals(["another", "stub"], r.keys())
def test_register_lazy(self):
r = config.CredentialStoreRegistry()
r.register_lazy("stub", "bzrlib.tests.test_config",
"StubCredentialStore", fallback=False)
self.assertEquals(["stub"], r.keys())
self.assertIsInstance(r.get_credential_store("stub"),
StubCredentialStore)
def test_is_fallback(self):
r = config.CredentialStoreRegistry()
r.register("stub1", None, fallback=False)
r.register("stub2", None, fallback=True)
self.assertEquals(False, r.is_fallback("stub1"))
self.assertEquals(True, r.is_fallback("stub2"))
def test_no_fallback(self):
r = config.CredentialStoreRegistry()
store = CountingCredentialStore()
r.register("count", store, fallback=False)
self.assertEquals(None,
r.get_fallback_credentials("http", "example.com"))
self.assertEquals(0, store._calls)
def test_fallback_credentials(self):
r = config.CredentialStoreRegistry()
store = StubCredentialStore()
store.add_credentials("http", "example.com",
"somebody", "geheim")
r.register("stub", store, fallback=True)
creds = r.get_fallback_credentials("http", "example.com")
self.assertEquals("somebody", creds["user"])
self.assertEquals("geheim", creds["password"])
def test_fallback_first_wins(self):
r = config.CredentialStoreRegistry()
stub1 = StubCredentialStore()
stub1.add_credentials("http", "example.com",
"somebody", "stub1")
r.register("stub1", stub1, fallback=True)
stub2 = StubCredentialStore()
stub2.add_credentials("http", "example.com",
"somebody", "stub2")
r.register("stub2", stub1, fallback=True)
creds = r.get_fallback_credentials("http", "example.com")
self.assertEquals("somebody", creds["user"])
self.assertEquals("stub1", creds["password"])
class TestPlainTextCredentialStore(tests.TestCase):
def test_decode_password(self):
r = config.credential_store_registry
plain_text = r.get_credential_store()
decoded = plain_text.decode_password(dict(password='secret'))
self.assertEquals('secret', decoded)
# FIXME: Once we have a way to declare authentication to all test servers, we
# can implement generic tests.
# test_user_password_in_url
# test_user_in_url_password_from_config
# test_user_in_url_password_prompted
# test_user_in_config
# test_user_getpass.getuser
# test_user_prompted ?
class TestAuthenticationRing(tests.TestCaseWithTransport):
pass
class TestAutoUserId(tests.TestCase):
"""Test inferring an automatic user name."""
def test_auto_user_id(self):
"""Automatic inference of user name.
This is a bit hard to test in an isolated way, because it depends on
system functions that go direct to /etc or perhaps somewhere else.
But it's reasonable to say that on Unix, with an /etc/mailname, we ought
to be able to choose a user name with no configuration.
"""
if sys.platform == 'win32':
raise tests.TestSkipped(
"User name inference not implemented on win32")
realname, address = config._auto_user_id()
if os.path.exists('/etc/mailname'):
self.assertIsNot(None, realname)
self.assertIsNot(None, address)
else:
self.assertEquals((None, None), (realname, address))
|