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
|
# Copyright (C) 2005-2015 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
"""Tests for the osutils wrapper."""
from cStringIO import StringIO
import errno
import os
import re
import select
import socket
import sys
import tempfile
import time
from bzrlib import (
errors,
lazy_regex,
osutils,
symbol_versioning,
tests,
trace,
win32utils,
)
from bzrlib.tests import (
features,
file_utils,
test__walkdirs_win32,
)
from bzrlib.tests.scenarios import load_tests_apply_scenarios
class _UTF8DirReaderFeature(features.Feature):
def _probe(self):
try:
from bzrlib import _readdir_pyx
self.reader = _readdir_pyx.UTF8DirReader
return True
except ImportError:
return False
def feature_name(self):
return 'bzrlib._readdir_pyx'
UTF8DirReaderFeature = features.ModuleAvailableFeature('bzrlib._readdir_pyx')
term_ios_feature = features.ModuleAvailableFeature('termios')
def _already_unicode(s):
return s
def _utf8_to_unicode(s):
return s.decode('UTF-8')
def dir_reader_scenarios():
# For each dir reader we define:
# - native_to_unicode: a function converting the native_abspath as returned
# by DirReader.read_dir to its unicode representation
# UnicodeDirReader is the fallback, it should be tested on all platforms.
scenarios = [('unicode',
dict(_dir_reader_class=osutils.UnicodeDirReader,
_native_to_unicode=_already_unicode))]
# Some DirReaders are platform specific and even there they may not be
# available.
if UTF8DirReaderFeature.available():
from bzrlib import _readdir_pyx
scenarios.append(('utf8',
dict(_dir_reader_class=_readdir_pyx.UTF8DirReader,
_native_to_unicode=_utf8_to_unicode)))
if test__walkdirs_win32.win32_readdir_feature.available():
try:
from bzrlib import _walkdirs_win32
scenarios.append(
('win32',
dict(_dir_reader_class=_walkdirs_win32.Win32ReadDir,
_native_to_unicode=_already_unicode)))
except ImportError:
pass
return scenarios
load_tests = load_tests_apply_scenarios
class TestContainsWhitespace(tests.TestCase):
def test_contains_whitespace(self):
self.assertTrue(osutils.contains_whitespace(u' '))
self.assertTrue(osutils.contains_whitespace(u'hello there'))
self.assertTrue(osutils.contains_whitespace(u'hellothere\n'))
self.assertTrue(osutils.contains_whitespace(u'hello\nthere'))
self.assertTrue(osutils.contains_whitespace(u'hello\rthere'))
self.assertTrue(osutils.contains_whitespace(u'hello\tthere'))
# \xa0 is "Non-breaking-space" which on some python locales thinks it
# is whitespace, but we do not.
self.assertFalse(osutils.contains_whitespace(u''))
self.assertFalse(osutils.contains_whitespace(u'hellothere'))
self.assertFalse(osutils.contains_whitespace(u'hello\xa0there'))
class TestRename(tests.TestCaseInTempDir):
def create_file(self, filename, content):
f = open(filename, 'wb')
try:
f.write(content)
finally:
f.close()
def _fancy_rename(self, a, b):
osutils.fancy_rename(a, b, rename_func=os.rename,
unlink_func=os.unlink)
def test_fancy_rename(self):
# This should work everywhere
self.create_file('a', 'something in a\n')
self._fancy_rename('a', 'b')
self.assertPathDoesNotExist('a')
self.assertPathExists('b')
self.check_file_contents('b', 'something in a\n')
self.create_file('a', 'new something in a\n')
self._fancy_rename('b', 'a')
self.check_file_contents('a', 'something in a\n')
def test_fancy_rename_fails_source_missing(self):
# An exception should be raised, and the target should be left in place
self.create_file('target', 'data in target\n')
self.assertRaises((IOError, OSError), self._fancy_rename,
'missingsource', 'target')
self.assertPathExists('target')
self.check_file_contents('target', 'data in target\n')
def test_fancy_rename_fails_if_source_and_target_missing(self):
self.assertRaises((IOError, OSError), self._fancy_rename,
'missingsource', 'missingtarget')
def test_rename(self):
# Rename should be semi-atomic on all platforms
self.create_file('a', 'something in a\n')
osutils.rename('a', 'b')
self.assertPathDoesNotExist('a')
self.assertPathExists('b')
self.check_file_contents('b', 'something in a\n')
self.create_file('a', 'new something in a\n')
osutils.rename('b', 'a')
self.check_file_contents('a', 'something in a\n')
# TODO: test fancy_rename using a MemoryTransport
def test_rename_change_case(self):
# on Windows we should be able to change filename case by rename
self.build_tree(['a', 'b/'])
osutils.rename('a', 'A')
osutils.rename('b', 'B')
# we can't use failUnlessExists on case-insensitive filesystem
# so try to check shape of the tree
shape = sorted(os.listdir('.'))
self.assertEquals(['A', 'B'], shape)
def test_rename_exception(self):
try:
osutils.rename('nonexistent_path', 'different_nonexistent_path')
except OSError, e:
self.assertEqual(e.old_filename, 'nonexistent_path')
self.assertEqual(e.new_filename, 'different_nonexistent_path')
self.assertTrue('nonexistent_path' in e.strerror)
self.assertTrue('different_nonexistent_path' in e.strerror)
class TestRandChars(tests.TestCase):
def test_01_rand_chars_empty(self):
result = osutils.rand_chars(0)
self.assertEqual(result, '')
def test_02_rand_chars_100(self):
result = osutils.rand_chars(100)
self.assertEqual(len(result), 100)
self.assertEqual(type(result), str)
self.assertContainsRe(result, r'^[a-z0-9]{100}$')
class TestIsInside(tests.TestCase):
def test_is_inside(self):
is_inside = osutils.is_inside
self.assertTrue(is_inside('src', 'src/foo.c'))
self.assertFalse(is_inside('src', 'srccontrol'))
self.assertTrue(is_inside('src', 'src/a/a/a/foo.c'))
self.assertTrue(is_inside('foo.c', 'foo.c'))
self.assertFalse(is_inside('foo.c', ''))
self.assertTrue(is_inside('', 'foo.c'))
def test_is_inside_any(self):
SRC_FOO_C = osutils.pathjoin('src', 'foo.c')
for dirs, fn in [(['src', 'doc'], SRC_FOO_C),
(['src'], SRC_FOO_C),
(['src'], 'src'),
]:
self.assert_(osutils.is_inside_any(dirs, fn))
for dirs, fn in [(['src'], 'srccontrol'),
(['src'], 'srccontrol/foo')]:
self.assertFalse(osutils.is_inside_any(dirs, fn))
def test_is_inside_or_parent_of_any(self):
for dirs, fn in [(['src', 'doc'], 'src/foo.c'),
(['src'], 'src/foo.c'),
(['src/bar.c'], 'src'),
(['src/bar.c', 'bla/foo.c'], 'src'),
(['src'], 'src'),
]:
self.assert_(osutils.is_inside_or_parent_of_any(dirs, fn))
for dirs, fn in [(['src'], 'srccontrol'),
(['srccontrol/foo.c'], 'src'),
(['src'], 'srccontrol/foo')]:
self.assertFalse(osutils.is_inside_or_parent_of_any(dirs, fn))
class TestLstat(tests.TestCaseInTempDir):
def test_lstat_matches_fstat(self):
# On Windows, lstat and fstat don't always agree, primarily in the
# 'st_ino' and 'st_dev' fields. So we force them to be '0' in our
# custom implementation.
if sys.platform == 'win32':
# We only have special lstat/fstat if we have the extension.
# Without it, we may end up re-reading content when we don't have
# to, but otherwise it doesn't effect correctness.
self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
f = open('test-file.txt', 'wb')
self.addCleanup(f.close)
f.write('some content\n')
f.flush()
self.assertEqualStat(osutils.fstat(f.fileno()),
osutils.lstat('test-file.txt'))
class TestRmTree(tests.TestCaseInTempDir):
def test_rmtree(self):
# Check to remove tree with read-only files/dirs
os.mkdir('dir')
f = file('dir/file', 'w')
f.write('spam')
f.close()
# would like to also try making the directory readonly, but at the
# moment python shutil.rmtree doesn't handle that properly - it would
# need to chmod the directory before removing things inside it - deferred
# for now -- mbp 20060505
# osutils.make_readonly('dir')
osutils.make_readonly('dir/file')
osutils.rmtree('dir')
self.assertPathDoesNotExist('dir/file')
self.assertPathDoesNotExist('dir')
class TestDeleteAny(tests.TestCaseInTempDir):
def test_delete_any_readonly(self):
# from <https://bugs.launchpad.net/bzr/+bug/218206>
self.build_tree(['d/', 'f'])
osutils.make_readonly('d')
osutils.make_readonly('f')
osutils.delete_any('f')
osutils.delete_any('d')
class TestKind(tests.TestCaseInTempDir):
def test_file_kind(self):
self.build_tree(['file', 'dir/'])
self.assertEquals('file', osutils.file_kind('file'))
self.assertEquals('directory', osutils.file_kind('dir/'))
if osutils.has_symlinks():
os.symlink('symlink', 'symlink')
self.assertEquals('symlink', osutils.file_kind('symlink'))
# TODO: jam 20060529 Test a block device
try:
os.lstat('/dev/null')
except OSError, e:
if e.errno not in (errno.ENOENT,):
raise
else:
self.assertEquals('chardev', osutils.file_kind('/dev/null'))
mkfifo = getattr(os, 'mkfifo', None)
if mkfifo:
mkfifo('fifo')
try:
self.assertEquals('fifo', osutils.file_kind('fifo'))
finally:
os.remove('fifo')
AF_UNIX = getattr(socket, 'AF_UNIX', None)
if AF_UNIX:
s = socket.socket(AF_UNIX)
s.bind('socket')
try:
self.assertEquals('socket', osutils.file_kind('socket'))
finally:
os.remove('socket')
def test_kind_marker(self):
self.assertEqual("", osutils.kind_marker("file"))
self.assertEqual("/", osutils.kind_marker('directory'))
self.assertEqual("/", osutils.kind_marker(osutils._directory_kind))
self.assertEqual("@", osutils.kind_marker("symlink"))
self.assertEqual("+", osutils.kind_marker("tree-reference"))
self.assertEqual("", osutils.kind_marker("fifo"))
self.assertEqual("", osutils.kind_marker("socket"))
self.assertEqual("", osutils.kind_marker("unknown"))
class TestUmask(tests.TestCaseInTempDir):
def test_get_umask(self):
if sys.platform == 'win32':
# umask always returns '0', no way to set it
self.assertEqual(0, osutils.get_umask())
return
orig_umask = osutils.get_umask()
self.addCleanup(os.umask, orig_umask)
os.umask(0222)
self.assertEqual(0222, osutils.get_umask())
os.umask(0022)
self.assertEqual(0022, osutils.get_umask())
os.umask(0002)
self.assertEqual(0002, osutils.get_umask())
os.umask(0027)
self.assertEqual(0027, osutils.get_umask())
class TestDateTime(tests.TestCase):
def assertFormatedDelta(self, expected, seconds):
"""Assert osutils.format_delta formats as expected"""
actual = osutils.format_delta(seconds)
self.assertEqual(expected, actual)
def test_format_delta(self):
self.assertFormatedDelta('0 seconds ago', 0)
self.assertFormatedDelta('1 second ago', 1)
self.assertFormatedDelta('10 seconds ago', 10)
self.assertFormatedDelta('59 seconds ago', 59)
self.assertFormatedDelta('89 seconds ago', 89)
self.assertFormatedDelta('1 minute, 30 seconds ago', 90)
self.assertFormatedDelta('3 minutes, 0 seconds ago', 180)
self.assertFormatedDelta('3 minutes, 1 second ago', 181)
self.assertFormatedDelta('10 minutes, 15 seconds ago', 615)
self.assertFormatedDelta('30 minutes, 59 seconds ago', 1859)
self.assertFormatedDelta('31 minutes, 0 seconds ago', 1860)
self.assertFormatedDelta('60 minutes, 0 seconds ago', 3600)
self.assertFormatedDelta('89 minutes, 59 seconds ago', 5399)
self.assertFormatedDelta('1 hour, 30 minutes ago', 5400)
self.assertFormatedDelta('2 hours, 30 minutes ago', 9017)
self.assertFormatedDelta('10 hours, 0 minutes ago', 36000)
self.assertFormatedDelta('24 hours, 0 minutes ago', 86400)
self.assertFormatedDelta('35 hours, 59 minutes ago', 129599)
self.assertFormatedDelta('36 hours, 0 minutes ago', 129600)
self.assertFormatedDelta('36 hours, 0 minutes ago', 129601)
self.assertFormatedDelta('36 hours, 1 minute ago', 129660)
self.assertFormatedDelta('36 hours, 1 minute ago', 129661)
self.assertFormatedDelta('84 hours, 10 minutes ago', 303002)
# We handle when time steps the wrong direction because computers
# don't have synchronized clocks.
self.assertFormatedDelta('84 hours, 10 minutes in the future', -303002)
self.assertFormatedDelta('1 second in the future', -1)
self.assertFormatedDelta('2 seconds in the future', -2)
def test_format_date(self):
self.assertRaises(errors.UnsupportedTimezoneFormat,
osutils.format_date, 0, timezone='foo')
self.assertIsInstance(osutils.format_date(0), str)
self.assertIsInstance(osutils.format_local_date(0), unicode)
# Testing for the actual value of the local weekday without
# duplicating the code from format_date is difficult.
# Instead blackbox.test_locale should check for localized
# dates once they do occur in output strings.
def test_format_date_with_offset_in_original_timezone(self):
self.assertEqual("Thu 1970-01-01 00:00:00 +0000",
osutils.format_date_with_offset_in_original_timezone(0))
self.assertEqual("Fri 1970-01-02 03:46:40 +0000",
osutils.format_date_with_offset_in_original_timezone(100000))
self.assertEqual("Fri 1970-01-02 05:46:40 +0200",
osutils.format_date_with_offset_in_original_timezone(100000, 7200))
def test_local_time_offset(self):
"""Test that local_time_offset() returns a sane value."""
offset = osutils.local_time_offset()
self.assertTrue(isinstance(offset, int))
# Test that the offset is no more than a eighteen hours in
# either direction.
# Time zone handling is system specific, so it is difficult to
# do more specific tests, but a value outside of this range is
# probably wrong.
eighteen_hours = 18 * 3600
self.assertTrue(-eighteen_hours < offset < eighteen_hours)
def test_local_time_offset_with_timestamp(self):
"""Test that local_time_offset() works with a timestamp."""
offset = osutils.local_time_offset(1000000000.1234567)
self.assertTrue(isinstance(offset, int))
eighteen_hours = 18 * 3600
self.assertTrue(-eighteen_hours < offset < eighteen_hours)
class TestFdatasync(tests.TestCaseInTempDir):
def do_fdatasync(self):
f = tempfile.NamedTemporaryFile()
osutils.fdatasync(f.fileno())
f.close()
@staticmethod
def raise_eopnotsupp(*args, **kwargs):
raise IOError(errno.EOPNOTSUPP, os.strerror(errno.EOPNOTSUPP))
@staticmethod
def raise_enotsup(*args, **kwargs):
raise IOError(errno.ENOTSUP, os.strerror(errno.ENOTSUP))
def test_fdatasync_handles_system_function(self):
self.overrideAttr(os, "fdatasync")
self.do_fdatasync()
def test_fdatasync_handles_no_fdatasync_no_fsync(self):
self.overrideAttr(os, "fdatasync")
self.overrideAttr(os, "fsync")
self.do_fdatasync()
def test_fdatasync_handles_no_EOPNOTSUPP(self):
self.overrideAttr(errno, "EOPNOTSUPP")
self.do_fdatasync()
def test_fdatasync_catches_ENOTSUP(self):
enotsup = getattr(errno, "ENOTSUP", None)
if enotsup is None:
raise tests.TestNotApplicable("No ENOTSUP on this platform")
self.overrideAttr(os, "fdatasync", self.raise_enotsup)
self.do_fdatasync()
def test_fdatasync_catches_EOPNOTSUPP(self):
enotsup = getattr(errno, "EOPNOTSUPP", None)
if enotsup is None:
raise tests.TestNotApplicable("No EOPNOTSUPP on this platform")
self.overrideAttr(os, "fdatasync", self.raise_eopnotsupp)
self.do_fdatasync()
class TestLinks(tests.TestCaseInTempDir):
def test_dereference_path(self):
self.requireFeature(features.SymlinkFeature)
cwd = osutils.realpath('.')
os.mkdir('bar')
bar_path = osutils.pathjoin(cwd, 'bar')
# Using './' to avoid bug #1213894 (first path component not
# dereferenced) in Python 2.4.1 and earlier
self.assertEqual(bar_path, osutils.realpath('./bar'))
os.symlink('bar', 'foo')
self.assertEqual(bar_path, osutils.realpath('./foo'))
# Does not dereference terminal symlinks
foo_path = osutils.pathjoin(cwd, 'foo')
self.assertEqual(foo_path, osutils.dereference_path('./foo'))
# Dereferences parent symlinks
os.mkdir('bar/baz')
baz_path = osutils.pathjoin(bar_path, 'baz')
self.assertEqual(baz_path, osutils.dereference_path('./foo/baz'))
# Dereferences parent symlinks that are the first path element
self.assertEqual(baz_path, osutils.dereference_path('foo/baz'))
# Dereferences parent symlinks in absolute paths
foo_baz_path = osutils.pathjoin(foo_path, 'baz')
self.assertEqual(baz_path, osutils.dereference_path(foo_baz_path))
def test_changing_access(self):
f = file('file', 'w')
f.write('monkey')
f.close()
# Make a file readonly
osutils.make_readonly('file')
mode = os.lstat('file').st_mode
self.assertEqual(mode, mode & 0777555)
# Make a file writable
osutils.make_writable('file')
mode = os.lstat('file').st_mode
self.assertEqual(mode, mode | 0200)
if osutils.has_symlinks():
# should not error when handed a symlink
os.symlink('nonexistent', 'dangling')
osutils.make_readonly('dangling')
osutils.make_writable('dangling')
def test_host_os_dereferences_symlinks(self):
osutils.host_os_dereferences_symlinks()
class TestCanonicalRelPath(tests.TestCaseInTempDir):
_test_needs_features = [features.CaseInsCasePresFilenameFeature]
def test_canonical_relpath_simple(self):
f = file('MixedCaseName', 'w')
f.close()
actual = osutils.canonical_relpath(self.test_base_dir, 'mixedcasename')
self.assertEqual('work/MixedCaseName', actual)
def test_canonical_relpath_missing_tail(self):
os.mkdir('MixedCaseParent')
actual = osutils.canonical_relpath(self.test_base_dir,
'mixedcaseparent/nochild')
self.assertEqual('work/MixedCaseParent/nochild', actual)
class Test_CICPCanonicalRelpath(tests.TestCaseWithTransport):
def assertRelpath(self, expected, base, path):
actual = osutils._cicp_canonical_relpath(base, path)
self.assertEqual(expected, actual)
def test_simple(self):
self.build_tree(['MixedCaseName'])
base = osutils.realpath(self.get_transport('.').local_abspath('.'))
self.assertRelpath('MixedCaseName', base, 'mixedcAsename')
def test_subdir_missing_tail(self):
self.build_tree(['MixedCaseParent/', 'MixedCaseParent/a_child'])
base = osutils.realpath(self.get_transport('.').local_abspath('.'))
self.assertRelpath('MixedCaseParent/a_child', base,
'MixedCaseParent/a_child')
self.assertRelpath('MixedCaseParent/a_child', base,
'MixedCaseParent/A_Child')
self.assertRelpath('MixedCaseParent/not_child', base,
'MixedCaseParent/not_child')
def test_at_root_slash(self):
# We can't test this on Windows, because it has a 'MIN_ABS_PATHLENGTH'
# check...
if osutils.MIN_ABS_PATHLENGTH > 1:
raise tests.TestSkipped('relpath requires %d chars'
% osutils.MIN_ABS_PATHLENGTH)
self.assertRelpath('foo', '/', '/foo')
def test_at_root_drive(self):
if sys.platform != 'win32':
raise tests.TestNotApplicable('we can only test drive-letter relative'
' paths on Windows where we have drive'
' letters.')
# see bug #322807
# The specific issue is that when at the root of a drive, 'abspath'
# returns "C:/" or just "/". However, the code assumes that abspath
# always returns something like "C:/foo" or "/foo" (no trailing slash).
self.assertRelpath('foo', 'C:/', 'C:/foo')
self.assertRelpath('foo', 'X:/', 'X:/foo')
self.assertRelpath('foo', 'X:/', 'X://foo')
class TestPumpFile(tests.TestCase):
"""Test pumpfile method."""
def setUp(self):
super(TestPumpFile, self).setUp()
# create a test datablock
self.block_size = 512
pattern = '0123456789ABCDEF'
self.test_data = pattern * (3 * self.block_size / len(pattern))
self.test_data_len = len(self.test_data)
def test_bracket_block_size(self):
"""Read data in blocks with the requested read size bracketing the
block size."""
# make sure test data is larger than max read size
self.assertTrue(self.test_data_len > self.block_size)
from_file = file_utils.FakeReadFile(self.test_data)
to_file = StringIO()
# read (max / 2) bytes and verify read size wasn't affected
num_bytes_to_read = self.block_size / 2
osutils.pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
self.assertEqual(from_file.get_max_read_size(), num_bytes_to_read)
self.assertEqual(from_file.get_read_count(), 1)
# read (max) bytes and verify read size wasn't affected
num_bytes_to_read = self.block_size
from_file.reset_read_count()
osutils.pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
self.assertEqual(from_file.get_max_read_size(), num_bytes_to_read)
self.assertEqual(from_file.get_read_count(), 1)
# read (max + 1) bytes and verify read size was limited
num_bytes_to_read = self.block_size + 1
from_file.reset_read_count()
osutils.pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
self.assertEqual(from_file.get_max_read_size(), self.block_size)
self.assertEqual(from_file.get_read_count(), 2)
# finish reading the rest of the data
num_bytes_to_read = self.test_data_len - to_file.tell()
osutils.pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
# report error if the data wasn't equal (we only report the size due
# to the length of the data)
response_data = to_file.getvalue()
if response_data != self.test_data:
message = "Data not equal. Expected %d bytes, received %d."
self.fail(message % (len(response_data), self.test_data_len))
def test_specified_size(self):
"""Request a transfer larger than the maximum block size and verify
that the maximum read doesn't exceed the block_size."""
# make sure test data is larger than max read size
self.assertTrue(self.test_data_len > self.block_size)
# retrieve data in blocks
from_file = file_utils.FakeReadFile(self.test_data)
to_file = StringIO()
osutils.pumpfile(from_file, to_file, self.test_data_len,
self.block_size)
# verify read size was equal to the maximum read size
self.assertTrue(from_file.get_max_read_size() > 0)
self.assertEqual(from_file.get_max_read_size(), self.block_size)
self.assertEqual(from_file.get_read_count(), 3)
# report error if the data wasn't equal (we only report the size due
# to the length of the data)
response_data = to_file.getvalue()
if response_data != self.test_data:
message = "Data not equal. Expected %d bytes, received %d."
self.fail(message % (len(response_data), self.test_data_len))
def test_to_eof(self):
"""Read to end-of-file and verify that the reads are not larger than
the maximum read size."""
# make sure test data is larger than max read size
self.assertTrue(self.test_data_len > self.block_size)
# retrieve data to EOF
from_file = file_utils.FakeReadFile(self.test_data)
to_file = StringIO()
osutils.pumpfile(from_file, to_file, -1, self.block_size)
# verify read size was equal to the maximum read size
self.assertEqual(from_file.get_max_read_size(), self.block_size)
self.assertEqual(from_file.get_read_count(), 4)
# report error if the data wasn't equal (we only report the size due
# to the length of the data)
response_data = to_file.getvalue()
if response_data != self.test_data:
message = "Data not equal. Expected %d bytes, received %d."
self.fail(message % (len(response_data), self.test_data_len))
def test_defaults(self):
"""Verifies that the default arguments will read to EOF -- this
test verifies that any existing usages of pumpfile will not be broken
with this new version."""
# retrieve data using default (old) pumpfile method
from_file = file_utils.FakeReadFile(self.test_data)
to_file = StringIO()
osutils.pumpfile(from_file, to_file)
# report error if the data wasn't equal (we only report the size due
# to the length of the data)
response_data = to_file.getvalue()
if response_data != self.test_data:
message = "Data not equal. Expected %d bytes, received %d."
self.fail(message % (len(response_data), self.test_data_len))
def test_report_activity(self):
activity = []
def log_activity(length, direction):
activity.append((length, direction))
from_file = StringIO(self.test_data)
to_file = StringIO()
osutils.pumpfile(from_file, to_file, buff_size=500,
report_activity=log_activity, direction='read')
self.assertEqual([(500, 'read'), (500, 'read'), (500, 'read'),
(36, 'read')], activity)
from_file = StringIO(self.test_data)
to_file = StringIO()
del activity[:]
osutils.pumpfile(from_file, to_file, buff_size=500,
report_activity=log_activity, direction='write')
self.assertEqual([(500, 'write'), (500, 'write'), (500, 'write'),
(36, 'write')], activity)
# And with a limited amount of data
from_file = StringIO(self.test_data)
to_file = StringIO()
del activity[:]
osutils.pumpfile(from_file, to_file, buff_size=500, read_length=1028,
report_activity=log_activity, direction='read')
self.assertEqual([(500, 'read'), (500, 'read'), (28, 'read')], activity)
class TestPumpStringFile(tests.TestCase):
def test_empty(self):
output = StringIO()
osutils.pump_string_file("", output)
self.assertEqual("", output.getvalue())
def test_more_than_segment_size(self):
output = StringIO()
osutils.pump_string_file("123456789", output, 2)
self.assertEqual("123456789", output.getvalue())
def test_segment_size(self):
output = StringIO()
osutils.pump_string_file("12", output, 2)
self.assertEqual("12", output.getvalue())
def test_segment_size_multiple(self):
output = StringIO()
osutils.pump_string_file("1234", output, 2)
self.assertEqual("1234", output.getvalue())
class TestRelpath(tests.TestCase):
def test_simple_relpath(self):
cwd = osutils.getcwd()
subdir = cwd + '/subdir'
self.assertEqual('subdir', osutils.relpath(cwd, subdir))
def test_deep_relpath(self):
cwd = osutils.getcwd()
subdir = cwd + '/sub/subsubdir'
self.assertEqual('sub/subsubdir', osutils.relpath(cwd, subdir))
def test_not_relative(self):
self.assertRaises(errors.PathNotChild,
osutils.relpath, 'C:/path', 'H:/path')
self.assertRaises(errors.PathNotChild,
osutils.relpath, 'C:/', 'H:/path')
class TestSafeUnicode(tests.TestCase):
def test_from_ascii_string(self):
self.assertEqual(u'foobar', osutils.safe_unicode('foobar'))
def test_from_unicode_string_ascii_contents(self):
self.assertEqual(u'bargam', osutils.safe_unicode(u'bargam'))
def test_from_unicode_string_unicode_contents(self):
self.assertEqual(u'bargam\xae', osutils.safe_unicode(u'bargam\xae'))
def test_from_utf8_string(self):
self.assertEqual(u'foo\xae', osutils.safe_unicode('foo\xc2\xae'))
def test_bad_utf8_string(self):
self.assertRaises(errors.BzrBadParameterNotUnicode,
osutils.safe_unicode,
'\xbb\xbb')
class TestSafeUtf8(tests.TestCase):
def test_from_ascii_string(self):
f = 'foobar'
self.assertEqual('foobar', osutils.safe_utf8(f))
def test_from_unicode_string_ascii_contents(self):
self.assertEqual('bargam', osutils.safe_utf8(u'bargam'))
def test_from_unicode_string_unicode_contents(self):
self.assertEqual('bargam\xc2\xae', osutils.safe_utf8(u'bargam\xae'))
def test_from_utf8_string(self):
self.assertEqual('foo\xc2\xae', osutils.safe_utf8('foo\xc2\xae'))
def test_bad_utf8_string(self):
self.assertRaises(errors.BzrBadParameterNotUnicode,
osutils.safe_utf8, '\xbb\xbb')
class TestSafeRevisionId(tests.TestCase):
def test_from_ascii_string(self):
# this shouldn't give a warning because it's getting an ascii string
self.assertEqual('foobar', osutils.safe_revision_id('foobar'))
def test_from_unicode_string_ascii_contents(self):
self.assertEqual('bargam',
osutils.safe_revision_id(u'bargam', warn=False))
def test_from_unicode_deprecated(self):
self.assertEqual('bargam',
self.callDeprecated([osutils._revision_id_warning],
osutils.safe_revision_id, u'bargam'))
def test_from_unicode_string_unicode_contents(self):
self.assertEqual('bargam\xc2\xae',
osutils.safe_revision_id(u'bargam\xae', warn=False))
def test_from_utf8_string(self):
self.assertEqual('foo\xc2\xae',
osutils.safe_revision_id('foo\xc2\xae'))
def test_none(self):
"""Currently, None is a valid revision_id"""
self.assertEqual(None, osutils.safe_revision_id(None))
class TestSafeFileId(tests.TestCase):
def test_from_ascii_string(self):
self.assertEqual('foobar', osutils.safe_file_id('foobar'))
def test_from_unicode_string_ascii_contents(self):
self.assertEqual('bargam', osutils.safe_file_id(u'bargam', warn=False))
def test_from_unicode_deprecated(self):
self.assertEqual('bargam',
self.callDeprecated([osutils._file_id_warning],
osutils.safe_file_id, u'bargam'))
def test_from_unicode_string_unicode_contents(self):
self.assertEqual('bargam\xc2\xae',
osutils.safe_file_id(u'bargam\xae', warn=False))
def test_from_utf8_string(self):
self.assertEqual('foo\xc2\xae',
osutils.safe_file_id('foo\xc2\xae'))
def test_none(self):
"""Currently, None is a valid revision_id"""
self.assertEqual(None, osutils.safe_file_id(None))
class TestSendAll(tests.TestCase):
def test_send_with_disconnected_socket(self):
class DisconnectedSocket(object):
def __init__(self, err):
self.err = err
def send(self, content):
raise self.err
def close(self):
pass
# All of these should be treated as ConnectionReset
errs = []
for err_cls in (IOError, socket.error):
for errnum in osutils._end_of_stream_errors:
errs.append(err_cls(errnum))
for err in errs:
sock = DisconnectedSocket(err)
self.assertRaises(errors.ConnectionReset,
osutils.send_all, sock, 'some more content')
def test_send_with_no_progress(self):
# See https://bugs.launchpad.net/bzr/+bug/1047309
# It seems that paramiko can get into a state where it doesn't error,
# but it returns 0 bytes sent for requests over and over again.
class NoSendingSocket(object):
def __init__(self):
self.call_count = 0
def send(self, bytes):
self.call_count += 1
if self.call_count > 100:
# Prevent the test suite from hanging
raise RuntimeError('too many calls')
return 0
sock = NoSendingSocket()
self.assertRaises(errors.ConnectionReset,
osutils.send_all, sock, 'content')
self.assertEqual(1, sock.call_count)
class TestPosixFuncs(tests.TestCase):
"""Test that the posix version of normpath returns an appropriate path
when used with 2 leading slashes."""
def test_normpath(self):
self.assertEqual('/etc/shadow', osutils._posix_normpath('/etc/shadow'))
self.assertEqual('/etc/shadow', osutils._posix_normpath('//etc/shadow'))
self.assertEqual('/etc/shadow', osutils._posix_normpath('///etc/shadow'))
class TestWin32Funcs(tests.TestCase):
"""Test that _win32 versions of os utilities return appropriate paths."""
def test_abspath(self):
self.requireFeature(features.win32_feature)
self.assertEqual('C:/foo', osutils._win32_abspath('C:\\foo'))
self.assertEqual('C:/foo', osutils._win32_abspath('C:/foo'))
self.assertEqual('//HOST/path', osutils._win32_abspath(r'\\HOST\path'))
self.assertEqual('//HOST/path', osutils._win32_abspath('//HOST/path'))
def test_realpath(self):
self.assertEqual('C:/foo', osutils._win32_realpath('C:\\foo'))
self.assertEqual('C:/foo', osutils._win32_realpath('C:/foo'))
def test_pathjoin(self):
self.assertEqual('path/to/foo',
osutils._win32_pathjoin('path', 'to', 'foo'))
self.assertEqual('C:/foo',
osutils._win32_pathjoin('path\\to', 'C:\\foo'))
self.assertEqual('C:/foo',
osutils._win32_pathjoin('path/to', 'C:/foo'))
self.assertEqual('path/to/foo',
osutils._win32_pathjoin('path/to/', 'foo'))
def test_pathjoin_late_bugfix(self):
if sys.version_info < (2, 7, 6):
expected = '/foo'
else:
expected = 'C:/foo'
self.assertEqual(expected,
osutils._win32_pathjoin('C:/path/to/', '/foo'))
self.assertEqual(expected,
osutils._win32_pathjoin('C:\\path\\to\\', '\\foo'))
def test_normpath(self):
self.assertEqual('path/to/foo',
osutils._win32_normpath(r'path\\from\..\to\.\foo'))
self.assertEqual('path/to/foo',
osutils._win32_normpath('path//from/../to/./foo'))
def test_getcwd(self):
cwd = osutils._win32_getcwd()
os_cwd = os.getcwdu()
self.assertEqual(os_cwd[1:].replace('\\', '/'), cwd[1:])
# win32 is inconsistent whether it returns lower or upper case
# and even if it was consistent the user might type the other
# so we force it to uppercase
# running python.exe under cmd.exe return capital C:\\
# running win32 python inside a cygwin shell returns lowercase
self.assertEqual(os_cwd[0].upper(), cwd[0])
def test_fixdrive(self):
self.assertEqual('H:/foo', osutils._win32_fixdrive('h:/foo'))
self.assertEqual('H:/foo', osutils._win32_fixdrive('H:/foo'))
self.assertEqual('C:\\foo', osutils._win32_fixdrive('c:\\foo'))
def test_win98_abspath(self):
self.requireFeature(features.win32_feature)
# absolute path
self.assertEqual('C:/foo', osutils._win98_abspath('C:\\foo'))
self.assertEqual('C:/foo', osutils._win98_abspath('C:/foo'))
# UNC path
self.assertEqual('//HOST/path', osutils._win98_abspath(r'\\HOST\path'))
self.assertEqual('//HOST/path', osutils._win98_abspath('//HOST/path'))
# relative path
cwd = osutils.getcwd().rstrip('/')
drive = osutils.ntpath.splitdrive(cwd)[0]
self.assertEqual(cwd+'/path', osutils._win98_abspath('path'))
self.assertEqual(drive+'/path', osutils._win98_abspath('/path'))
# unicode path
u = u'\u1234'
self.assertEqual(cwd+'/'+u, osutils._win98_abspath(u))
class TestWin32FuncsDirs(tests.TestCaseInTempDir):
"""Test win32 functions that create files."""
def test_getcwd(self):
self.requireFeature(features.UnicodeFilenameFeature)
os.mkdir(u'mu-\xb5')
os.chdir(u'mu-\xb5')
# TODO: jam 20060427 This will probably fail on Mac OSX because
# it will change the normalization of B\xe5gfors
# Consider using a different unicode character, or make
# osutils.getcwd() renormalize the path.
self.assertEndsWith(osutils._win32_getcwd(), u'mu-\xb5')
def test_minimum_path_selection(self):
self.assertEqual(set(),
osutils.minimum_path_selection([]))
self.assertEqual(set(['a']),
osutils.minimum_path_selection(['a']))
self.assertEqual(set(['a', 'b']),
osutils.minimum_path_selection(['a', 'b']))
self.assertEqual(set(['a/', 'b']),
osutils.minimum_path_selection(['a/', 'b']))
self.assertEqual(set(['a/', 'b']),
osutils.minimum_path_selection(['a/c', 'a/', 'b']))
self.assertEqual(set(['a-b', 'a', 'a0b']),
osutils.minimum_path_selection(['a-b', 'a/b', 'a0b', 'a']))
def test_mkdtemp(self):
tmpdir = osutils._win32_mkdtemp(dir='.')
self.assertFalse('\\' in tmpdir)
def test_rename(self):
a = open('a', 'wb')
a.write('foo\n')
a.close()
b = open('b', 'wb')
b.write('baz\n')
b.close()
osutils._win32_rename('b', 'a')
self.assertPathExists('a')
self.assertPathDoesNotExist('b')
self.assertFileEqual('baz\n', 'a')
def test_rename_missing_file(self):
a = open('a', 'wb')
a.write('foo\n')
a.close()
try:
osutils._win32_rename('b', 'a')
except (IOError, OSError), e:
self.assertEqual(errno.ENOENT, e.errno)
self.assertFileEqual('foo\n', 'a')
def test_rename_missing_dir(self):
os.mkdir('a')
try:
osutils._win32_rename('b', 'a')
except (IOError, OSError), e:
self.assertEqual(errno.ENOENT, e.errno)
def test_rename_current_dir(self):
os.mkdir('a')
os.chdir('a')
# You can't rename the working directory
# doing rename non-existant . usually
# just raises ENOENT, since non-existant
# doesn't exist.
try:
osutils._win32_rename('b', '.')
except (IOError, OSError), e:
self.assertEqual(errno.ENOENT, e.errno)
def test_splitpath(self):
def check(expected, path):
self.assertEqual(expected, osutils.splitpath(path))
check(['a'], 'a')
check(['a', 'b'], 'a/b')
check(['a', 'b'], 'a/./b')
check(['a', '.b'], 'a/.b')
check(['a', '.b'], 'a\\.b')
self.assertRaises(errors.BzrError, osutils.splitpath, 'a/../b')
class TestParentDirectories(tests.TestCaseInTempDir):
"""Test osutils.parent_directories()"""
def test_parent_directories(self):
self.assertEqual([], osutils.parent_directories('a'))
self.assertEqual(['a'], osutils.parent_directories('a/b'))
self.assertEqual(['a/b', 'a'], osutils.parent_directories('a/b/c'))
class TestMacFuncsDirs(tests.TestCaseInTempDir):
"""Test mac special functions that require directories."""
def test_getcwd(self):
self.requireFeature(features.UnicodeFilenameFeature)
os.mkdir(u'B\xe5gfors')
os.chdir(u'B\xe5gfors')
self.assertEndsWith(osutils._mac_getcwd(), u'B\xe5gfors')
def test_getcwd_nonnorm(self):
self.requireFeature(features.UnicodeFilenameFeature)
# Test that _mac_getcwd() will normalize this path
os.mkdir(u'Ba\u030agfors')
os.chdir(u'Ba\u030agfors')
self.assertEndsWith(osutils._mac_getcwd(), u'B\xe5gfors')
class TestChunksToLines(tests.TestCase):
def test_smoketest(self):
self.assertEqual(['foo\n', 'bar\n', 'baz\n'],
osutils.chunks_to_lines(['foo\nbar', '\nbaz\n']))
self.assertEqual(['foo\n', 'bar\n', 'baz\n'],
osutils.chunks_to_lines(['foo\n', 'bar\n', 'baz\n']))
def test_osutils_binding(self):
from bzrlib.tests import test__chunks_to_lines
if test__chunks_to_lines.compiled_chunkstolines_feature.available():
from bzrlib._chunks_to_lines_pyx import chunks_to_lines
else:
from bzrlib._chunks_to_lines_py import chunks_to_lines
self.assertIs(chunks_to_lines, osutils.chunks_to_lines)
class TestSplitLines(tests.TestCase):
def test_split_unicode(self):
self.assertEqual([u'foo\n', u'bar\xae'],
osutils.split_lines(u'foo\nbar\xae'))
self.assertEqual([u'foo\n', u'bar\xae\n'],
osutils.split_lines(u'foo\nbar\xae\n'))
def test_split_with_carriage_returns(self):
self.assertEqual(['foo\rbar\n'],
osutils.split_lines('foo\rbar\n'))
class TestWalkDirs(tests.TestCaseInTempDir):
def assertExpectedBlocks(self, expected, result):
self.assertEqual(expected,
[(dirinfo, [line[0:3] for line in block])
for dirinfo, block in result])
def test_walkdirs(self):
tree = [
'.bzr',
'0file',
'1dir/',
'1dir/0file',
'1dir/1dir/',
'2file'
]
self.build_tree(tree)
expected_dirblocks = [
(('', '.'),
[('0file', '0file', 'file'),
('1dir', '1dir', 'directory'),
('2file', '2file', 'file'),
]
),
(('1dir', './1dir'),
[('1dir/0file', '0file', 'file'),
('1dir/1dir', '1dir', 'directory'),
]
),
(('1dir/1dir', './1dir/1dir'),
[
]
),
]
result = []
found_bzrdir = False
for dirdetail, dirblock in osutils.walkdirs('.'):
if len(dirblock) and dirblock[0][1] == '.bzr':
# this tests the filtering of selected paths
found_bzrdir = True
del dirblock[0]
result.append((dirdetail, dirblock))
self.assertTrue(found_bzrdir)
self.assertExpectedBlocks(expected_dirblocks, result)
# you can search a subdir only, with a supplied prefix.
result = []
for dirblock in osutils.walkdirs('./1dir', '1dir'):
result.append(dirblock)
self.assertExpectedBlocks(expected_dirblocks[1:], result)
def test_walkdirs_os_error(self):
# <https://bugs.launchpad.net/bzr/+bug/338653>
# Pyrex readdir didn't raise useful messages if it had an error
# reading the directory
if sys.platform == 'win32':
raise tests.TestNotApplicable(
"readdir IOError not tested on win32")
self.requireFeature(features.not_running_as_root)
os.mkdir("test-unreadable")
os.chmod("test-unreadable", 0000)
# must chmod it back so that it can be removed
self.addCleanup(os.chmod, "test-unreadable", 0700)
# The error is not raised until the generator is actually evaluated.
# (It would be ok if it happened earlier but at the moment it
# doesn't.)
e = self.assertRaises(OSError, list, osutils._walkdirs_utf8("."))
self.assertEquals('./test-unreadable', e.filename)
self.assertEquals(errno.EACCES, e.errno)
# Ensure the message contains the file name
self.assertContainsRe(str(e), "\./test-unreadable")
def test_walkdirs_encoding_error(self):
# <https://bugs.launchpad.net/bzr/+bug/488519>
# walkdirs didn't raise a useful message when the filenames
# are not using the filesystem's encoding
# require a bytestring based filesystem
self.requireFeature(features.ByteStringNamedFilesystem)
tree = [
'.bzr',
'0file',
'1dir/',
'1dir/0file',
'1dir/1dir/',
'1file'
]
self.build_tree(tree)
# rename the 1file to a latin-1 filename
os.rename("./1file", "\xe8file")
if "\xe8file" not in os.listdir("."):
self.skip("Lack filesystem that preserves arbitrary bytes")
self._save_platform_info()
win32utils.winver = None # Avoid the win32 detection code
osutils._fs_enc = 'UTF-8'
# this should raise on error
def attempt():
for dirdetail, dirblock in osutils.walkdirs('.'):
pass
self.assertRaises(errors.BadFilenameEncoding, attempt)
def test__walkdirs_utf8(self):
tree = [
'.bzr',
'0file',
'1dir/',
'1dir/0file',
'1dir/1dir/',
'2file'
]
self.build_tree(tree)
expected_dirblocks = [
(('', '.'),
[('0file', '0file', 'file'),
('1dir', '1dir', 'directory'),
('2file', '2file', 'file'),
]
),
(('1dir', './1dir'),
[('1dir/0file', '0file', 'file'),
('1dir/1dir', '1dir', 'directory'),
]
),
(('1dir/1dir', './1dir/1dir'),
[
]
),
]
result = []
found_bzrdir = False
for dirdetail, dirblock in osutils._walkdirs_utf8('.'):
if len(dirblock) and dirblock[0][1] == '.bzr':
# this tests the filtering of selected paths
found_bzrdir = True
del dirblock[0]
result.append((dirdetail, dirblock))
self.assertTrue(found_bzrdir)
self.assertExpectedBlocks(expected_dirblocks, result)
# you can search a subdir only, with a supplied prefix.
result = []
for dirblock in osutils.walkdirs('./1dir', '1dir'):
result.append(dirblock)
self.assertExpectedBlocks(expected_dirblocks[1:], result)
def _filter_out_stat(self, result):
"""Filter out the stat value from the walkdirs result"""
for dirdetail, dirblock in result:
new_dirblock = []
for info in dirblock:
# Ignore info[3] which is the stat
new_dirblock.append((info[0], info[1], info[2], info[4]))
dirblock[:] = new_dirblock
def _save_platform_info(self):
self.overrideAttr(win32utils, 'winver')
self.overrideAttr(osutils, '_fs_enc')
self.overrideAttr(osutils, '_selected_dir_reader')
def assertDirReaderIs(self, expected):
"""Assert the right implementation for _walkdirs_utf8 is chosen."""
# Force it to redetect
osutils._selected_dir_reader = None
# Nothing to list, but should still trigger the selection logic
self.assertEqual([(('', '.'), [])], list(osutils._walkdirs_utf8('.')))
self.assertIsInstance(osutils._selected_dir_reader, expected)
def test_force_walkdirs_utf8_fs_utf8(self):
self.requireFeature(UTF8DirReaderFeature)
self._save_platform_info()
win32utils.winver = None # Avoid the win32 detection code
osutils._fs_enc = 'utf-8'
self.assertDirReaderIs(
UTF8DirReaderFeature.module.UTF8DirReader)
def test_force_walkdirs_utf8_fs_ascii(self):
self.requireFeature(UTF8DirReaderFeature)
self._save_platform_info()
win32utils.winver = None # Avoid the win32 detection code
osutils._fs_enc = 'ascii'
self.assertDirReaderIs(
UTF8DirReaderFeature.module.UTF8DirReader)
def test_force_walkdirs_utf8_fs_latin1(self):
self._save_platform_info()
win32utils.winver = None # Avoid the win32 detection code
osutils._fs_enc = 'iso-8859-1'
self.assertDirReaderIs(osutils.UnicodeDirReader)
def test_force_walkdirs_utf8_nt(self):
# Disabled because the thunk of the whole walkdirs api is disabled.
self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
self._save_platform_info()
win32utils.winver = 'Windows NT'
from bzrlib._walkdirs_win32 import Win32ReadDir
self.assertDirReaderIs(Win32ReadDir)
def test_force_walkdirs_utf8_98(self):
self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
self._save_platform_info()
win32utils.winver = 'Windows 98'
self.assertDirReaderIs(osutils.UnicodeDirReader)
def test_unicode_walkdirs(self):
"""Walkdirs should always return unicode paths."""
self.requireFeature(features.UnicodeFilenameFeature)
name0 = u'0file-\xb6'
name1 = u'1dir-\u062c\u0648'
name2 = u'2file-\u0633'
tree = [
name0,
name1 + '/',
name1 + '/' + name0,
name1 + '/' + name1 + '/',
name2,
]
self.build_tree(tree)
expected_dirblocks = [
((u'', u'.'),
[(name0, name0, 'file', './' + name0),
(name1, name1, 'directory', './' + name1),
(name2, name2, 'file', './' + name2),
]
),
((name1, './' + name1),
[(name1 + '/' + name0, name0, 'file', './' + name1
+ '/' + name0),
(name1 + '/' + name1, name1, 'directory', './' + name1
+ '/' + name1),
]
),
((name1 + '/' + name1, './' + name1 + '/' + name1),
[
]
),
]
result = list(osutils.walkdirs('.'))
self._filter_out_stat(result)
self.assertEqual(expected_dirblocks, result)
result = list(osutils.walkdirs(u'./'+name1, name1))
self._filter_out_stat(result)
self.assertEqual(expected_dirblocks[1:], result)
def test_unicode__walkdirs_utf8(self):
"""Walkdirs_utf8 should always return utf8 paths.
The abspath portion might be in unicode or utf-8
"""
self.requireFeature(features.UnicodeFilenameFeature)
name0 = u'0file-\xb6'
name1 = u'1dir-\u062c\u0648'
name2 = u'2file-\u0633'
tree = [
name0,
name1 + '/',
name1 + '/' + name0,
name1 + '/' + name1 + '/',
name2,
]
self.build_tree(tree)
name0 = name0.encode('utf8')
name1 = name1.encode('utf8')
name2 = name2.encode('utf8')
expected_dirblocks = [
(('', '.'),
[(name0, name0, 'file', './' + name0),
(name1, name1, 'directory', './' + name1),
(name2, name2, 'file', './' + name2),
]
),
((name1, './' + name1),
[(name1 + '/' + name0, name0, 'file', './' + name1
+ '/' + name0),
(name1 + '/' + name1, name1, 'directory', './' + name1
+ '/' + name1),
]
),
((name1 + '/' + name1, './' + name1 + '/' + name1),
[
]
),
]
result = []
# For ease in testing, if walkdirs_utf8 returns Unicode, assert that
# all abspaths are Unicode, and encode them back into utf8.
for dirdetail, dirblock in osutils._walkdirs_utf8('.'):
self.assertIsInstance(dirdetail[0], str)
if isinstance(dirdetail[1], unicode):
dirdetail = (dirdetail[0], dirdetail[1].encode('utf8'))
dirblock = [list(info) for info in dirblock]
for info in dirblock:
self.assertIsInstance(info[4], unicode)
info[4] = info[4].encode('utf8')
new_dirblock = []
for info in dirblock:
self.assertIsInstance(info[0], str)
self.assertIsInstance(info[1], str)
self.assertIsInstance(info[4], str)
# Remove the stat information
new_dirblock.append((info[0], info[1], info[2], info[4]))
result.append((dirdetail, new_dirblock))
self.assertEqual(expected_dirblocks, result)
def test__walkdirs_utf8_with_unicode_fs(self):
"""UnicodeDirReader should be a safe fallback everywhere
The abspath portion should be in unicode
"""
self.requireFeature(features.UnicodeFilenameFeature)
# Use the unicode reader. TODO: split into driver-and-driven unit
# tests.
self._save_platform_info()
osutils._selected_dir_reader = osutils.UnicodeDirReader()
name0u = u'0file-\xb6'
name1u = u'1dir-\u062c\u0648'
name2u = u'2file-\u0633'
tree = [
name0u,
name1u + '/',
name1u + '/' + name0u,
name1u + '/' + name1u + '/',
name2u,
]
self.build_tree(tree)
name0 = name0u.encode('utf8')
name1 = name1u.encode('utf8')
name2 = name2u.encode('utf8')
# All of the abspaths should be in unicode, all of the relative paths
# should be in utf8
expected_dirblocks = [
(('', '.'),
[(name0, name0, 'file', './' + name0u),
(name1, name1, 'directory', './' + name1u),
(name2, name2, 'file', './' + name2u),
]
),
((name1, './' + name1u),
[(name1 + '/' + name0, name0, 'file', './' + name1u
+ '/' + name0u),
(name1 + '/' + name1, name1, 'directory', './' + name1u
+ '/' + name1u),
]
),
((name1 + '/' + name1, './' + name1u + '/' + name1u),
[
]
),
]
result = list(osutils._walkdirs_utf8('.'))
self._filter_out_stat(result)
self.assertEqual(expected_dirblocks, result)
def test__walkdirs_utf8_win32readdir(self):
self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
self.requireFeature(features.UnicodeFilenameFeature)
from bzrlib._walkdirs_win32 import Win32ReadDir
self._save_platform_info()
osutils._selected_dir_reader = Win32ReadDir()
name0u = u'0file-\xb6'
name1u = u'1dir-\u062c\u0648'
name2u = u'2file-\u0633'
tree = [
name0u,
name1u + '/',
name1u + '/' + name0u,
name1u + '/' + name1u + '/',
name2u,
]
self.build_tree(tree)
name0 = name0u.encode('utf8')
name1 = name1u.encode('utf8')
name2 = name2u.encode('utf8')
# All of the abspaths should be in unicode, all of the relative paths
# should be in utf8
expected_dirblocks = [
(('', '.'),
[(name0, name0, 'file', './' + name0u),
(name1, name1, 'directory', './' + name1u),
(name2, name2, 'file', './' + name2u),
]
),
((name1, './' + name1u),
[(name1 + '/' + name0, name0, 'file', './' + name1u
+ '/' + name0u),
(name1 + '/' + name1, name1, 'directory', './' + name1u
+ '/' + name1u),
]
),
((name1 + '/' + name1, './' + name1u + '/' + name1u),
[
]
),
]
result = list(osutils._walkdirs_utf8(u'.'))
self._filter_out_stat(result)
self.assertEqual(expected_dirblocks, result)
def assertStatIsCorrect(self, path, win32stat):
os_stat = os.stat(path)
self.assertEqual(os_stat.st_size, win32stat.st_size)
self.assertAlmostEqual(os_stat.st_mtime, win32stat.st_mtime, places=4)
self.assertAlmostEqual(os_stat.st_ctime, win32stat.st_ctime, places=4)
self.assertAlmostEqual(os_stat.st_atime, win32stat.st_atime, places=4)
self.assertEqual(os_stat.st_dev, win32stat.st_dev)
self.assertEqual(os_stat.st_ino, win32stat.st_ino)
self.assertEqual(os_stat.st_mode, win32stat.st_mode)
def test__walkdirs_utf_win32_find_file_stat_file(self):
"""make sure our Stat values are valid"""
self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
self.requireFeature(features.UnicodeFilenameFeature)
from bzrlib._walkdirs_win32 import Win32ReadDir
name0u = u'0file-\xb6'
name0 = name0u.encode('utf8')
self.build_tree([name0u])
# I hate to sleep() here, but I'm trying to make the ctime different
# from the mtime
time.sleep(2)
f = open(name0u, 'ab')
try:
f.write('just a small update')
finally:
f.close()
result = Win32ReadDir().read_dir('', u'.')
entry = result[0]
self.assertEqual((name0, name0, 'file'), entry[:3])
self.assertEqual(u'./' + name0u, entry[4])
self.assertStatIsCorrect(entry[4], entry[3])
self.assertNotEqual(entry[3].st_mtime, entry[3].st_ctime)
def test__walkdirs_utf_win32_find_file_stat_directory(self):
"""make sure our Stat values are valid"""
self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
self.requireFeature(features.UnicodeFilenameFeature)
from bzrlib._walkdirs_win32 import Win32ReadDir
name0u = u'0dir-\u062c\u0648'
name0 = name0u.encode('utf8')
self.build_tree([name0u + '/'])
result = Win32ReadDir().read_dir('', u'.')
entry = result[0]
self.assertEqual((name0, name0, 'directory'), entry[:3])
self.assertEqual(u'./' + name0u, entry[4])
self.assertStatIsCorrect(entry[4], entry[3])
def assertPathCompare(self, path_less, path_greater):
"""check that path_less and path_greater compare correctly."""
self.assertEqual(0, osutils.compare_paths_prefix_order(
path_less, path_less))
self.assertEqual(0, osutils.compare_paths_prefix_order(
path_greater, path_greater))
self.assertEqual(-1, osutils.compare_paths_prefix_order(
path_less, path_greater))
self.assertEqual(1, osutils.compare_paths_prefix_order(
path_greater, path_less))
def test_compare_paths_prefix_order(self):
# root before all else
self.assertPathCompare("/", "/a")
# alpha within a dir
self.assertPathCompare("/a", "/b")
self.assertPathCompare("/b", "/z")
# high dirs before lower.
self.assertPathCompare("/z", "/a/a")
# except if the deeper dir should be output first
self.assertPathCompare("/a/b/c", "/d/g")
# lexical betwen dirs of the same height
self.assertPathCompare("/a/z", "/z/z")
self.assertPathCompare("/a/c/z", "/a/d/e")
# this should also be consistent for no leading / paths
# root before all else
self.assertPathCompare("", "a")
# alpha within a dir
self.assertPathCompare("a", "b")
self.assertPathCompare("b", "z")
# high dirs before lower.
self.assertPathCompare("z", "a/a")
# except if the deeper dir should be output first
self.assertPathCompare("a/b/c", "d/g")
# lexical betwen dirs of the same height
self.assertPathCompare("a/z", "z/z")
self.assertPathCompare("a/c/z", "a/d/e")
def test_path_prefix_sorting(self):
"""Doing a sort on path prefix should match our sample data."""
original_paths = [
'a',
'a/b',
'a/b/c',
'b',
'b/c',
'd',
'd/e',
'd/e/f',
'd/f',
'd/g',
'g',
]
dir_sorted_paths = [
'a',
'b',
'd',
'g',
'a/b',
'a/b/c',
'b/c',
'd/e',
'd/f',
'd/g',
'd/e/f',
]
self.assertEqual(
dir_sorted_paths,
sorted(original_paths, key=osutils.path_prefix_key))
# using the comparison routine shoudl work too:
self.assertEqual(
dir_sorted_paths,
sorted(original_paths, cmp=osutils.compare_paths_prefix_order))
class TestCopyTree(tests.TestCaseInTempDir):
def test_copy_basic_tree(self):
self.build_tree(['source/', 'source/a', 'source/b/', 'source/b/c'])
osutils.copy_tree('source', 'target')
self.assertEqual(['a', 'b'], sorted(os.listdir('target')))
self.assertEqual(['c'], os.listdir('target/b'))
def test_copy_tree_target_exists(self):
self.build_tree(['source/', 'source/a', 'source/b/', 'source/b/c',
'target/'])
osutils.copy_tree('source', 'target')
self.assertEqual(['a', 'b'], sorted(os.listdir('target')))
self.assertEqual(['c'], os.listdir('target/b'))
def test_copy_tree_symlinks(self):
self.requireFeature(features.SymlinkFeature)
self.build_tree(['source/'])
os.symlink('a/generic/path', 'source/lnk')
osutils.copy_tree('source', 'target')
self.assertEqual(['lnk'], os.listdir('target'))
self.assertEqual('a/generic/path', os.readlink('target/lnk'))
def test_copy_tree_handlers(self):
processed_files = []
processed_links = []
def file_handler(from_path, to_path):
processed_files.append(('f', from_path, to_path))
def dir_handler(from_path, to_path):
processed_files.append(('d', from_path, to_path))
def link_handler(from_path, to_path):
processed_links.append((from_path, to_path))
handlers = {'file':file_handler,
'directory':dir_handler,
'symlink':link_handler,
}
self.build_tree(['source/', 'source/a', 'source/b/', 'source/b/c'])
if osutils.has_symlinks():
os.symlink('a/generic/path', 'source/lnk')
osutils.copy_tree('source', 'target', handlers=handlers)
self.assertEqual([('d', 'source', 'target'),
('f', 'source/a', 'target/a'),
('d', 'source/b', 'target/b'),
('f', 'source/b/c', 'target/b/c'),
], processed_files)
self.assertPathDoesNotExist('target')
if osutils.has_symlinks():
self.assertEqual([('source/lnk', 'target/lnk')], processed_links)
class TestSetUnsetEnv(tests.TestCase):
"""Test updating the environment"""
def setUp(self):
super(TestSetUnsetEnv, self).setUp()
self.assertEqual(None, os.environ.get('BZR_TEST_ENV_VAR'),
'Environment was not cleaned up properly.'
' Variable BZR_TEST_ENV_VAR should not exist.')
def cleanup():
if 'BZR_TEST_ENV_VAR' in os.environ:
del os.environ['BZR_TEST_ENV_VAR']
self.addCleanup(cleanup)
def test_set(self):
"""Test that we can set an env variable"""
old = osutils.set_or_unset_env('BZR_TEST_ENV_VAR', 'foo')
self.assertEqual(None, old)
self.assertEqual('foo', os.environ.get('BZR_TEST_ENV_VAR'))
def test_double_set(self):
"""Test that we get the old value out"""
osutils.set_or_unset_env('BZR_TEST_ENV_VAR', 'foo')
old = osutils.set_or_unset_env('BZR_TEST_ENV_VAR', 'bar')
self.assertEqual('foo', old)
self.assertEqual('bar', os.environ.get('BZR_TEST_ENV_VAR'))
def test_unicode(self):
"""Environment can only contain plain strings
So Unicode strings must be encoded.
"""
uni_val, env_val = tests.probe_unicode_in_user_encoding()
if uni_val is None:
raise tests.TestSkipped(
'Cannot find a unicode character that works in encoding %s'
% (osutils.get_user_encoding(),))
old = osutils.set_or_unset_env('BZR_TEST_ENV_VAR', uni_val)
self.assertEqual(env_val, os.environ.get('BZR_TEST_ENV_VAR'))
def test_unset(self):
"""Test that passing None will remove the env var"""
osutils.set_or_unset_env('BZR_TEST_ENV_VAR', 'foo')
old = osutils.set_or_unset_env('BZR_TEST_ENV_VAR', None)
self.assertEqual('foo', old)
self.assertEqual(None, os.environ.get('BZR_TEST_ENV_VAR'))
self.assertFalse('BZR_TEST_ENV_VAR' in os.environ)
class TestSizeShaFile(tests.TestCaseInTempDir):
def test_sha_empty(self):
self.build_tree_contents([('foo', '')])
expected_sha = osutils.sha_string('')
f = open('foo')
self.addCleanup(f.close)
size, sha = osutils.size_sha_file(f)
self.assertEqual(0, size)
self.assertEqual(expected_sha, sha)
def test_sha_mixed_endings(self):
text = 'test\r\nwith\nall\rpossible line endings\r\n'
self.build_tree_contents([('foo', text)])
expected_sha = osutils.sha_string(text)
f = open('foo', 'rb')
self.addCleanup(f.close)
size, sha = osutils.size_sha_file(f)
self.assertEqual(38, size)
self.assertEqual(expected_sha, sha)
class TestShaFileByName(tests.TestCaseInTempDir):
def test_sha_empty(self):
self.build_tree_contents([('foo', '')])
expected_sha = osutils.sha_string('')
self.assertEqual(expected_sha, osutils.sha_file_by_name('foo'))
def test_sha_mixed_endings(self):
text = 'test\r\nwith\nall\rpossible line endings\r\n'
self.build_tree_contents([('foo', text)])
expected_sha = osutils.sha_string(text)
self.assertEqual(expected_sha, osutils.sha_file_by_name('foo'))
class TestResourceLoading(tests.TestCaseInTempDir):
def test_resource_string(self):
# test resource in bzrlib
text = osutils.resource_string('bzrlib', 'debug.py')
self.assertContainsRe(text, "debug_flags = set()")
# test resource under bzrlib
text = osutils.resource_string('bzrlib.ui', 'text.py')
self.assertContainsRe(text, "class TextUIFactory")
# test unsupported package
self.assertRaises(errors.BzrError, osutils.resource_string, 'zzzz',
'yyy.xx')
# test unknown resource
self.assertRaises(IOError, osutils.resource_string, 'bzrlib', 'yyy.xx')
class TestReCompile(tests.TestCase):
def _deprecated_re_compile_checked(self, *args, **kwargs):
return self.applyDeprecated(symbol_versioning.deprecated_in((2, 2, 0)),
osutils.re_compile_checked, *args, **kwargs)
def test_re_compile_checked(self):
r = self._deprecated_re_compile_checked(r'A*', re.IGNORECASE)
self.assertTrue(r.match('aaaa'))
self.assertTrue(r.match('aAaA'))
def test_re_compile_checked_error(self):
# like https://bugs.launchpad.net/bzr/+bug/251352
# Due to possible test isolation error, re.compile is not lazy at
# this point. We re-install lazy compile.
lazy_regex.install_lazy_compile()
err = self.assertRaises(
errors.BzrCommandError,
self._deprecated_re_compile_checked, '*', re.IGNORECASE, 'test case')
self.assertEqual(
'Invalid regular expression in test case: '
'"*" nothing to repeat',
str(err))
class TestDirReader(tests.TestCaseInTempDir):
scenarios = dir_reader_scenarios()
# Set by load_tests
_dir_reader_class = None
_native_to_unicode = None
def setUp(self):
super(TestDirReader, self).setUp()
self.overrideAttr(osutils,
'_selected_dir_reader', self._dir_reader_class())
def _get_ascii_tree(self):
tree = [
'0file',
'1dir/',
'1dir/0file',
'1dir/1dir/',
'2file'
]
expected_dirblocks = [
(('', '.'),
[('0file', '0file', 'file'),
('1dir', '1dir', 'directory'),
('2file', '2file', 'file'),
]
),
(('1dir', './1dir'),
[('1dir/0file', '0file', 'file'),
('1dir/1dir', '1dir', 'directory'),
]
),
(('1dir/1dir', './1dir/1dir'),
[
]
),
]
return tree, expected_dirblocks
def test_walk_cur_dir(self):
tree, expected_dirblocks = self._get_ascii_tree()
self.build_tree(tree)
result = list(osutils._walkdirs_utf8('.'))
# Filter out stat and abspath
self.assertEqual(expected_dirblocks,
[(dirinfo, [line[0:3] for line in block])
for dirinfo, block in result])
def test_walk_sub_dir(self):
tree, expected_dirblocks = self._get_ascii_tree()
self.build_tree(tree)
# you can search a subdir only, with a supplied prefix.
result = list(osutils._walkdirs_utf8('./1dir', '1dir'))
# Filter out stat and abspath
self.assertEqual(expected_dirblocks[1:],
[(dirinfo, [line[0:3] for line in block])
for dirinfo, block in result])
def _get_unicode_tree(self):
name0u = u'0file-\xb6'
name1u = u'1dir-\u062c\u0648'
name2u = u'2file-\u0633'
tree = [
name0u,
name1u + '/',
name1u + '/' + name0u,
name1u + '/' + name1u + '/',
name2u,
]
name0 = name0u.encode('UTF-8')
name1 = name1u.encode('UTF-8')
name2 = name2u.encode('UTF-8')
expected_dirblocks = [
(('', '.'),
[(name0, name0, 'file', './' + name0u),
(name1, name1, 'directory', './' + name1u),
(name2, name2, 'file', './' + name2u),
]
),
((name1, './' + name1u),
[(name1 + '/' + name0, name0, 'file', './' + name1u
+ '/' + name0u),
(name1 + '/' + name1, name1, 'directory', './' + name1u
+ '/' + name1u),
]
),
((name1 + '/' + name1, './' + name1u + '/' + name1u),
[
]
),
]
return tree, expected_dirblocks
def _filter_out(self, raw_dirblocks):
"""Filter out a walkdirs_utf8 result.
stat field is removed, all native paths are converted to unicode
"""
filtered_dirblocks = []
for dirinfo, block in raw_dirblocks:
dirinfo = (dirinfo[0], self._native_to_unicode(dirinfo[1]))
details = []
for line in block:
details.append(line[0:3] + (self._native_to_unicode(line[4]), ))
filtered_dirblocks.append((dirinfo, details))
return filtered_dirblocks
def test_walk_unicode_tree(self):
self.requireFeature(features.UnicodeFilenameFeature)
tree, expected_dirblocks = self._get_unicode_tree()
self.build_tree(tree)
result = list(osutils._walkdirs_utf8('.'))
self.assertEqual(expected_dirblocks, self._filter_out(result))
def test_symlink(self):
self.requireFeature(features.SymlinkFeature)
self.requireFeature(features.UnicodeFilenameFeature)
target = u'target\N{Euro Sign}'
link_name = u'l\N{Euro Sign}nk'
os.symlink(target, link_name)
target_utf8 = target.encode('UTF-8')
link_name_utf8 = link_name.encode('UTF-8')
expected_dirblocks = [
(('', '.'),
[(link_name_utf8, link_name_utf8,
'symlink', './' + link_name),],
)]
result = list(osutils._walkdirs_utf8('.'))
self.assertEqual(expected_dirblocks, self._filter_out(result))
class TestReadLink(tests.TestCaseInTempDir):
"""Exposes os.readlink() problems and the osutils solution.
The only guarantee offered by os.readlink(), starting with 2.6, is that a
unicode string will be returned if a unicode string is passed.
But prior python versions failed to properly encode the passed unicode
string.
"""
_test_needs_features = [features.SymlinkFeature, features.UnicodeFilenameFeature]
def setUp(self):
super(tests.TestCaseInTempDir, self).setUp()
self.link = u'l\N{Euro Sign}ink'
self.target = u'targe\N{Euro Sign}t'
os.symlink(self.target, self.link)
def test_os_readlink_link_encoding(self):
self.assertEquals(self.target, os.readlink(self.link))
def test_os_readlink_link_decoding(self):
self.assertEquals(self.target.encode(osutils._fs_enc),
os.readlink(self.link.encode(osutils._fs_enc)))
class TestConcurrency(tests.TestCase):
def setUp(self):
super(TestConcurrency, self).setUp()
self.overrideAttr(osutils, '_cached_local_concurrency')
def test_local_concurrency(self):
concurrency = osutils.local_concurrency()
self.assertIsInstance(concurrency, int)
def test_local_concurrency_environment_variable(self):
self.overrideEnv('BZR_CONCURRENCY', '2')
self.assertEqual(2, osutils.local_concurrency(use_cache=False))
self.overrideEnv('BZR_CONCURRENCY', '3')
self.assertEqual(3, osutils.local_concurrency(use_cache=False))
self.overrideEnv('BZR_CONCURRENCY', 'foo')
self.assertEqual(1, osutils.local_concurrency(use_cache=False))
def test_option_concurrency(self):
self.overrideEnv('BZR_CONCURRENCY', '1')
self.run_bzr('rocks --concurrency 42')
# Command line overrides environment variable
self.assertEquals('42', os.environ['BZR_CONCURRENCY'])
self.assertEquals(42, osutils.local_concurrency(use_cache=False))
class TestFailedToLoadExtension(tests.TestCase):
def _try_loading(self):
try:
import bzrlib._fictional_extension_py
except ImportError, e:
osutils.failed_to_load_extension(e)
return True
def setUp(self):
super(TestFailedToLoadExtension, self).setUp()
self.overrideAttr(osutils, '_extension_load_failures', [])
def test_failure_to_load(self):
self._try_loading()
self.assertLength(1, osutils._extension_load_failures)
self.assertEquals(osutils._extension_load_failures[0],
"No module named _fictional_extension_py")
def test_report_extension_load_failures_no_warning(self):
self.assertTrue(self._try_loading())
warnings, result = self.callCatchWarnings(osutils.report_extension_load_failures)
# it used to give a Python warning; it no longer does
self.assertLength(0, warnings)
def test_report_extension_load_failures_message(self):
log = StringIO()
trace.push_log_file(log)
self.assertTrue(self._try_loading())
osutils.report_extension_load_failures()
self.assertContainsRe(
log.getvalue(),
r"bzr: warning: some compiled extensions could not be loaded; "
"see <https://answers\.launchpad\.net/bzr/\+faq/703>\n"
)
class TestTerminalWidth(tests.TestCase):
def setUp(self):
super(TestTerminalWidth, self).setUp()
self._orig_terminal_size_state = osutils._terminal_size_state
self._orig_first_terminal_size = osutils._first_terminal_size
self.addCleanup(self.restore_osutils_globals)
osutils._terminal_size_state = 'no_data'
osutils._first_terminal_size = None
def restore_osutils_globals(self):
osutils._terminal_size_state = self._orig_terminal_size_state
osutils._first_terminal_size = self._orig_first_terminal_size
def replace_stdout(self, new):
self.overrideAttr(sys, 'stdout', new)
def replace__terminal_size(self, new):
self.overrideAttr(osutils, '_terminal_size', new)
def set_fake_tty(self):
class I_am_a_tty(object):
def isatty(self):
return True
self.replace_stdout(I_am_a_tty())
def test_default_values(self):
self.assertEqual(80, osutils.default_terminal_width)
def test_defaults_to_BZR_COLUMNS(self):
# BZR_COLUMNS is set by the test framework
self.assertNotEqual('12', os.environ['BZR_COLUMNS'])
self.overrideEnv('BZR_COLUMNS', '12')
self.assertEqual(12, osutils.terminal_width())
def test_BZR_COLUMNS_0_no_limit(self):
self.overrideEnv('BZR_COLUMNS', '0')
self.assertEqual(None, osutils.terminal_width())
def test_falls_back_to_COLUMNS(self):
self.overrideEnv('BZR_COLUMNS', None)
self.assertNotEqual('42', os.environ['COLUMNS'])
self.set_fake_tty()
self.overrideEnv('COLUMNS', '42')
self.assertEqual(42, osutils.terminal_width())
def test_tty_default_without_columns(self):
self.overrideEnv('BZR_COLUMNS', None)
self.overrideEnv('COLUMNS', None)
def terminal_size(w, h):
return 42, 42
self.set_fake_tty()
# We need to override the osutils definition as it depends on the
# running environment that we can't control (PQM running without a
# controlling terminal is one example).
self.replace__terminal_size(terminal_size)
self.assertEqual(42, osutils.terminal_width())
def test_non_tty_default_without_columns(self):
self.overrideEnv('BZR_COLUMNS', None)
self.overrideEnv('COLUMNS', None)
self.replace_stdout(None)
self.assertEqual(None, osutils.terminal_width())
def test_no_TIOCGWINSZ(self):
self.requireFeature(term_ios_feature)
termios = term_ios_feature.module
# bug 63539 is about a termios without TIOCGWINSZ attribute
try:
orig = termios.TIOCGWINSZ
except AttributeError:
# We won't remove TIOCGWINSZ, because it doesn't exist anyway :)
pass
else:
self.overrideAttr(termios, 'TIOCGWINSZ')
del termios.TIOCGWINSZ
self.overrideEnv('BZR_COLUMNS', None)
self.overrideEnv('COLUMNS', None)
# Whatever the result is, if we don't raise an exception, it's ok.
osutils.terminal_width()
class TestCreationOps(tests.TestCaseInTempDir):
_test_needs_features = [features.chown_feature]
def setUp(self):
super(TestCreationOps, self).setUp()
self.overrideAttr(os, 'chown', self._dummy_chown)
# params set by call to _dummy_chown
self.path = self.uid = self.gid = None
def _dummy_chown(self, path, uid, gid):
self.path, self.uid, self.gid = path, uid, gid
def test_copy_ownership_from_path(self):
"""copy_ownership_from_path test with specified src."""
ownsrc = '/'
f = open('test_file', 'wt')
osutils.copy_ownership_from_path('test_file', ownsrc)
s = os.stat(ownsrc)
self.assertEquals(self.path, 'test_file')
self.assertEquals(self.uid, s.st_uid)
self.assertEquals(self.gid, s.st_gid)
def test_copy_ownership_nonesrc(self):
"""copy_ownership_from_path test with src=None."""
f = open('test_file', 'wt')
# should use parent dir for permissions
osutils.copy_ownership_from_path('test_file')
s = os.stat('..')
self.assertEquals(self.path, 'test_file')
self.assertEquals(self.uid, s.st_uid)
self.assertEquals(self.gid, s.st_gid)
class TestPathFromEnviron(tests.TestCase):
def test_is_unicode(self):
self.overrideEnv('BZR_TEST_PATH', './anywhere at all/')
path = osutils.path_from_environ('BZR_TEST_PATH')
self.assertIsInstance(path, unicode)
self.assertEqual(u'./anywhere at all/', path)
def test_posix_path_env_ascii(self):
self.overrideEnv('BZR_TEST_PATH', '/tmp')
home = osutils._posix_path_from_environ('BZR_TEST_PATH')
self.assertIsInstance(home, unicode)
self.assertEqual(u'/tmp', home)
def test_posix_path_env_unicode(self):
self.requireFeature(features.ByteStringNamedFilesystem)
self.overrideEnv('BZR_TEST_PATH', '/home/\xa7test')
self.overrideAttr(osutils, "_fs_enc", "iso8859-1")
self.assertEqual(u'/home/\xa7test',
osutils._posix_path_from_environ('BZR_TEST_PATH'))
osutils._fs_enc = "iso8859-5"
self.assertEqual(u'/home/\u0407test',
osutils._posix_path_from_environ('BZR_TEST_PATH'))
osutils._fs_enc = "utf-8"
self.assertRaises(errors.BadFilenameEncoding,
osutils._posix_path_from_environ, 'BZR_TEST_PATH')
class TestGetHomeDir(tests.TestCase):
def test_is_unicode(self):
home = osutils._get_home_dir()
self.assertIsInstance(home, unicode)
def test_posix_homeless(self):
self.overrideEnv('HOME', None)
home = osutils._get_home_dir()
self.assertIsInstance(home, unicode)
def test_posix_home_ascii(self):
self.overrideEnv('HOME', '/home/test')
home = osutils._posix_get_home_dir()
self.assertIsInstance(home, unicode)
self.assertEqual(u'/home/test', home)
def test_posix_home_unicode(self):
self.requireFeature(features.ByteStringNamedFilesystem)
self.overrideEnv('HOME', '/home/\xa7test')
self.overrideAttr(osutils, "_fs_enc", "iso8859-1")
self.assertEqual(u'/home/\xa7test', osutils._posix_get_home_dir())
osutils._fs_enc = "iso8859-5"
self.assertEqual(u'/home/\u0407test', osutils._posix_get_home_dir())
osutils._fs_enc = "utf-8"
self.assertRaises(errors.BadFilenameEncoding,
osutils._posix_get_home_dir)
class TestGetuserUnicode(tests.TestCase):
def test_is_unicode(self):
user = osutils.getuser_unicode()
self.assertIsInstance(user, unicode)
def envvar_to_override(self):
if sys.platform == "win32":
# Disable use of platform calls on windows so envvar is used
self.overrideAttr(win32utils, 'has_ctypes', False)
return 'USERNAME' # only variable used on windows
return 'LOGNAME' # first variable checked by getpass.getuser()
def test_ascii_user(self):
self.overrideEnv(self.envvar_to_override(), 'jrandom')
self.assertEqual(u'jrandom', osutils.getuser_unicode())
def test_unicode_user(self):
ue = osutils.get_user_encoding()
uni_val, env_val = tests.probe_unicode_in_user_encoding()
if uni_val is None:
raise tests.TestSkipped(
'Cannot find a unicode character that works in encoding %s'
% (osutils.get_user_encoding(),))
uni_username = u'jrandom' + uni_val
encoded_username = uni_username.encode(ue)
self.overrideEnv(self.envvar_to_override(), encoded_username)
self.assertEqual(uni_username, osutils.getuser_unicode())
class TestBackupNames(tests.TestCase):
def setUp(self):
super(TestBackupNames, self).setUp()
self.backups = []
def backup_exists(self, name):
return name in self.backups
def available_backup_name(self, name):
backup_name = osutils.available_backup_name(name, self.backup_exists)
self.backups.append(backup_name)
return backup_name
def assertBackupName(self, expected, name):
self.assertEqual(expected, self.available_backup_name(name))
def test_empty(self):
self.assertBackupName('file.~1~', 'file')
def test_existing(self):
self.available_backup_name('file')
self.available_backup_name('file')
self.assertBackupName('file.~3~', 'file')
# Empty slots are found, this is not a strict requirement and may be
# revisited if we test against all implementations.
self.backups.remove('file.~2~')
self.assertBackupName('file.~2~', 'file')
class TestFindExecutableInPath(tests.TestCase):
def test_windows(self):
if sys.platform != 'win32':
raise tests.TestSkipped('test requires win32')
self.assertTrue(osutils.find_executable_on_path('explorer') is not None)
self.assertTrue(
osutils.find_executable_on_path('explorer.exe') is not None)
self.assertTrue(
osutils.find_executable_on_path('EXPLORER.EXE') is not None)
self.assertTrue(
osutils.find_executable_on_path('THIS SHOULD NOT EXIST') is None)
self.assertTrue(osutils.find_executable_on_path('file.txt') is None)
def test_windows_app_path(self):
if sys.platform != 'win32':
raise tests.TestSkipped('test requires win32')
# Override PATH env var so that exe can only be found on App Path
self.overrideEnv('PATH', '')
# Internt Explorer is always registered in the App Path
self.assertTrue(osutils.find_executable_on_path('iexplore') is not None)
def test_other(self):
if sys.platform == 'win32':
raise tests.TestSkipped('test requires non-win32')
self.assertTrue(osutils.find_executable_on_path('sh') is not None)
self.assertTrue(
osutils.find_executable_on_path('THIS SHOULD NOT EXIST') is None)
class TestEnvironmentErrors(tests.TestCase):
"""Test handling of environmental errors"""
def test_is_oserror(self):
self.assertTrue(osutils.is_environment_error(
OSError(errno.EINVAL, "Invalid parameter")))
def test_is_ioerror(self):
self.assertTrue(osutils.is_environment_error(
IOError(errno.EINVAL, "Invalid parameter")))
def test_is_socket_error(self):
self.assertTrue(osutils.is_environment_error(
socket.error(errno.EINVAL, "Invalid parameter")))
def test_is_select_error(self):
self.assertTrue(osutils.is_environment_error(
select.error(errno.EINVAL, "Invalid parameter")))
def test_is_pywintypes_error(self):
self.requireFeature(features.pywintypes)
import pywintypes
self.assertTrue(osutils.is_environment_error(
pywintypes.error(errno.EINVAL, "Invalid parameter", "Caller")))
|