aboutsummaryrefslogtreecommitdiff
path: root/pyfakefs/tests/fake_filesystem_test.py
blob: 696f8a82f3811af07b96dea8e8aa504c6e657ff3 (plain)
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
# Copyright 2009 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Unittest for fake_filesystem module."""

import contextlib
import errno
import os
import stat
import sys
import unittest
from unittest.mock import patch

from pyfakefs import fake_filesystem, fake_os, fake_open
from pyfakefs.fake_filesystem import (
    set_uid,
    set_gid,
    is_root,
    reset_ids,
    OSType,
)
from pyfakefs.helpers import IS_WIN
from pyfakefs.tests.test_utils import TestCase, RealFsTestCase, time_mock


class FakeDirectoryUnitTest(TestCase):
    def setUp(self):
        self.filesystem = fake_filesystem.FakeFilesystem(path_separator="/")
        self.os = fake_os.FakeOsModule(self.filesystem)
        self.time = time_mock(10, 1)
        self.time.start()
        self.fake_file = fake_filesystem.FakeFile(
            "foobar", contents="dummy_file", filesystem=self.filesystem
        )
        self.fake_dir = fake_filesystem.FakeDirectory(
            "somedir", filesystem=self.filesystem
        )

    def tearDown(self):
        self.time.stop()

    def test_new_file_and_directory(self):
        self.assertTrue(stat.S_IFREG & self.fake_file.st_mode)
        self.assertTrue(stat.S_IFDIR & self.fake_dir.st_mode)
        self.assertEqual({}, self.fake_dir.entries)

    def test_add_entry(self):
        self.fake_dir.add_entry(self.fake_file)
        self.assertEqual({"foobar": self.fake_file}, self.fake_dir.entries)

    def test_get_entry(self):
        self.fake_dir.add_entry(self.fake_file)
        self.assertEqual(self.fake_file, self.fake_dir.get_entry("foobar"))

    def test_path(self):
        root_dir = self.filesystem.root_dir_name
        self.filesystem.root.add_entry(self.fake_dir)
        self.fake_dir.add_entry(self.fake_file)
        self.assertEqual(f"{root_dir}somedir/foobar", self.fake_file.path)
        self.assertEqual(f"{root_dir}somedir", self.fake_dir.path)

    def test_path_with_drive(self):
        self.filesystem.is_windows_fs = True
        dir_path = "C:/foo/bar/baz"
        self.filesystem.create_dir(dir_path)
        dir_object = self.filesystem.get_object(dir_path)
        self.assertEqual(dir_path, dir_object.path)

    def test_path_after_chdir(self):
        root_dir = self.filesystem.root_dir_name
        dir_path = "/foo/bar/baz"
        self.filesystem.create_dir(dir_path)
        self.os.chdir(dir_path)
        dir_object = self.filesystem.get_object(dir_path)
        self.assertEqual(f"{root_dir}foo/bar/baz", dir_object.path)

    def test_path_after_chdir_with_drive(self):
        self.filesystem.is_windows_fs = True
        dir_path = "C:/foo/bar/baz"
        self.filesystem.create_dir(dir_path)
        self.os.chdir(dir_path)
        dir_object = self.filesystem.get_object(dir_path)
        self.assertEqual(dir_path, dir_object.path)

    def test_remove_entry(self):
        self.fake_dir.add_entry(self.fake_file)
        self.assertEqual(self.fake_file, self.fake_dir.get_entry("foobar"))
        self.fake_dir.remove_entry("foobar")
        with self.assertRaises(KeyError):
            self.fake_dir.get_entry("foobar")

    def test_should_throw_if_set_size_is_not_integer(self):
        with self.raises_os_error(errno.ENOSPC):
            self.fake_file.size = 0.1

    def test_should_throw_if_set_size_is_negative(self):
        with self.raises_os_error(errno.ENOSPC):
            self.fake_file.size = -1

    def test_produce_empty_file_if_set_size_is_zero(self):
        self.fake_file.size = 0
        self.assertEqual("", self.fake_file.contents)

    def test_sets_content_empty_if_set_size_is_zero(self):
        self.fake_file.size = 0
        self.assertEqual("", self.fake_file.contents)

    def test_truncate_file_if_size_is_smaller_than_current_size(self):
        self.fake_file.size = 6
        self.assertEqual("dummy_", self.fake_file.contents)

    def test_leave_file_unchanged_if_size_is_equal_to_current_size(self):
        self.fake_file.size = 10
        self.assertEqual("dummy_file", self.fake_file.contents)

    def test_set_contents_to_dir_raises(self):
        # Regression test for #276
        self.filesystem.is_windows_fs = True
        with self.raises_os_error(errno.EISDIR):
            self.fake_dir.set_contents("a")
        self.filesystem.is_windows_fs = False
        with self.raises_os_error(errno.EISDIR):
            self.fake_dir.set_contents("a")

    def test_pads_with_nullbytes_if_size_is_greater_than_current_size(self):
        self.fake_file.size = 13
        self.assertEqual("dummy_file\0\0\0", self.fake_file.contents)

    def test_set_m_time(self):
        self.assertEqual(10, self.fake_file.st_mtime)
        self.fake_file.st_mtime = 14
        self.assertEqual(14, self.fake_file.st_mtime)
        self.fake_file.st_mtime = 131
        self.assertEqual(131, self.fake_file.st_mtime)

    def test_file_inode(self):
        filesystem = fake_filesystem.FakeFilesystem(path_separator="/")
        fake_os_module = fake_os.FakeOsModule(filesystem)
        file_path = "some_file1"
        filesystem.create_file(file_path, contents="contents here1")
        self.assertLess(0, fake_os_module.stat(file_path)[stat.ST_INO])

        file_obj = filesystem.get_object(file_path)
        file_obj.st_ino = 43
        self.assertEqual(43, fake_os_module.stat(file_path)[stat.ST_INO])

    def test_directory_inode(self):
        filesystem = fake_filesystem.FakeFilesystem(path_separator="/")
        fake_os_module = fake_os.FakeOsModule(filesystem)
        dirpath = "testdir"
        filesystem.create_dir(dirpath)
        self.assertLess(0, fake_os_module.stat(dirpath)[stat.ST_INO])

        dir_obj = filesystem.get_object(dirpath)
        dir_obj.st_ino = 43
        self.assertEqual(43, fake_os_module.stat(dirpath)[stat.ST_INO])

    def test_directory_size(self):
        fs = fake_filesystem.FakeFilesystem(path_separator="/")
        foo_dir = fs.create_dir("/foo")
        fs.create_file("/foo/bar.txt", st_size=20)
        bar_dir = fs.create_dir("/foo/bar/")
        fs.create_file("/foo/bar/baz1.txt", st_size=30)
        fs.create_file("/foo/bar/baz2.txt", st_size=40)
        foo1_dir = fs.create_dir("/foo1")
        fs.create_file("/foo1/bar.txt", st_size=50)
        fs.create_file("/foo1/bar/baz/file", st_size=60)
        self.assertEqual(90, foo_dir.size)
        self.assertEqual(70, bar_dir.size)
        self.assertEqual(110, foo1_dir.size)
        self.assertEqual(200, fs.root_dir.size)
        with self.raises_os_error(errno.EISDIR):
            foo1_dir.size = 100

    def test_ordered_dirs(self):
        filesystem = fake_filesystem.FakeFilesystem(path_separator="/")
        filesystem.create_dir("/foo")
        filesystem.create_file("/foo/2")
        filesystem.create_file("/foo/4")
        filesystem.create_file("/foo/1")
        filesystem.create_file("/foo/3")
        fake_dir = filesystem.get_object("/foo")
        self.assertEqual(["2", "4", "1", "3"], fake_dir.ordered_dirs)


class SetLargeFileSizeTest(TestCase):
    def setUp(self):
        filesystem = fake_filesystem.FakeFilesystem()
        self.fake_file = fake_filesystem.FakeFile("foobar", filesystem=filesystem)

    def test_should_throw_if_size_is_not_integer(self):
        with self.raises_os_error(errno.ENOSPC):
            self.fake_file.set_large_file_size(0.1)

    def test_should_throw_if_size_is_negative(self):
        with self.raises_os_error(errno.ENOSPC):
            self.fake_file.set_large_file_size(-1)

    def test_sets_content_none_if_size_is_non_negative_integer(self):
        self.fake_file.set_large_file_size(1000000000)
        self.assertEqual(None, self.fake_file.contents)
        self.assertEqual(1000000000, self.fake_file.st_size)


class NormalizePathTest(TestCase):
    def setUp(self):
        self.filesystem = fake_filesystem.FakeFilesystem(path_separator="/")
        self.root_name = self.filesystem.root_dir_name

    def test_empty_path_should_get_normalized_to_root_path(self):
        self.assertEqual(self.root_name, self.filesystem.absnormpath(""))

    def test_root_path_remains_unchanged(self):
        self.assertEqual(self.root_name, self.filesystem.absnormpath(self.root_name))

    def test_relative_path_forced_to_cwd(self):
        path = "bar"
        self.filesystem.cwd = "/foo"
        self.assertEqual("/foo/bar", self.filesystem.absnormpath(path))

    def test_absolute_path_remains_unchanged(self):
        path = "foo/bar"
        self.assertEqual(self.root_name + path, self.filesystem.absnormpath(path))

    def test_dotted_path_is_normalized(self):
        path = "/foo/.."
        self.assertEqual(
            self.filesystem.root_dir_name, self.filesystem.absnormpath(path)
        )
        path = "foo/../bar"
        self.assertEqual(
            f"{self.filesystem.root_dir_name}bar",
            self.filesystem.absnormpath(path),
        )

    def test_dot_path_is_normalized(self):
        path = "."
        self.assertEqual(self.root_name, self.filesystem.absnormpath(path))


class GetPathComponentsTest(TestCase):
    def setUp(self):
        self.filesystem = fake_filesystem.FakeFilesystem(path_separator="/")
        self.root_name = "/"

    def test_root_path_should_return_empty_list(self):
        self.assertEqual([], self.filesystem._path_components(self.root_name))

    def test_empty_path_should_return_empty_list(self):
        self.assertEqual([], self.filesystem._path_components(""))

    def test_relative_path_with_one_component_should_return_component(self):
        self.assertEqual(["foo"], self.filesystem._path_components("foo"))

    def test_absolute_path_with_one_component_should_return_component(self):
        self.assertEqual(["foo"], self.filesystem._path_components("/foo"))

    def test_two_level_relative_path_should_return_components(self):
        self.assertEqual(["foo", "bar"], self.filesystem._path_components("foo/bar"))

    def test_two_level_absolute_path_should_return_components(self):
        self.assertEqual(["foo", "bar"], self.filesystem._path_components("/foo/bar"))


class FakeFilesystemUnitTest(TestCase):
    def setUp(self):
        self.filesystem = fake_filesystem.FakeFilesystem(path_separator="/")
        self.root_name = self.filesystem.root_dir_name
        self.fake_file = fake_filesystem.FakeFile("foobar", filesystem=self.filesystem)
        self.fake_child = fake_filesystem.FakeDirectory(
            "foobaz", filesystem=self.filesystem
        )
        self.fake_grandchild = fake_filesystem.FakeDirectory(
            "quux", filesystem=self.filesystem
        )

    def test_new_filesystem(self):
        self.assertEqual("/", self.filesystem.path_separator)
        self.assertTrue(stat.S_IFDIR & self.filesystem.root.st_mode)
        self.assertEqual({}, self.filesystem.root_dir.entries)

    def test_none_raises_type_error(self):
        with self.assertRaises(TypeError):
            self.filesystem.exists(None)

    def test_empty_string_does_not_exist(self):
        self.assertFalse(self.filesystem.exists(""))

    def test_exists_root(self):
        self.assertTrue(self.filesystem.exists(self.root_name))

    def test_exists_unadded_file(self):
        self.assertFalse(self.filesystem.exists(self.fake_file.name))

    def test_not_exists_subpath_named_like_file_contents(self):
        # Regression test for #219
        file_path = "/foo/bar"
        self.filesystem.create_file(file_path, contents="baz")
        self.assertFalse(self.filesystem.exists(file_path + "/baz"))

    def test_get_root_object(self):
        self.assertEqual(
            self.filesystem.root_dir,
            self.filesystem.get_object(self.root_name),
        )

    def test_add_object_to_root(self):
        self.filesystem.add_object(self.root_name, self.fake_file)
        self.assertEqual({"foobar": self.fake_file}, self.filesystem.root_dir.entries)

    def test_windows_root_dir_name(self):
        self.filesystem.is_windows_fs = True
        self.assertEqual("C:/", self.filesystem.root_dir_name)
        self.filesystem.cwd = "E:/foo"
        self.assertEqual("E:/", self.filesystem.root_dir_name)
        self.filesystem.cwd = "//foo/bar"
        self.assertEqual("//foo/bar/", self.filesystem.root_dir_name)

    def test_exists_added_file(self):
        self.filesystem.add_object(self.root_name, self.fake_file)
        self.assertTrue(self.filesystem.exists(self.fake_file.name))

    def test_exists_relative_path_posix(self):
        self.filesystem.is_windows_fs = False
        self.filesystem.create_file("/a/b/file_one")
        self.filesystem.create_file("/a/c/file_two")
        self.assertTrue(self.filesystem.exists("a/b/../c/file_two"))
        self.assertTrue(self.filesystem.exists("/a/c/../b/file_one"))
        self.assertTrue(self.filesystem.exists("/a/c/../../a/b/file_one"))
        self.assertFalse(self.filesystem.exists("a/b/../z/d"))
        self.assertFalse(self.filesystem.exists("a/b/../z/../c/file_two"))
        self.filesystem.cwd = "/a/c"
        self.assertTrue(self.filesystem.exists("../b/file_one"))
        self.assertTrue(self.filesystem.exists("../../a/b/file_one"))
        self.assertTrue(self.filesystem.exists("../../a/b/../../a/c/file_two"))
        self.assertFalse(self.filesystem.exists("../z/file_one"))
        self.assertFalse(self.filesystem.exists("../z/../c/file_two"))

    def test_exists_relative_path_windows(self):
        self.filesystem.is_windows_fs = True
        self.filesystem.is_macos = False
        self.filesystem.create_file("/a/b/file_one")
        self.filesystem.create_file("/a/c/file_two")
        self.assertTrue(self.filesystem.exists("a/b/../c/file_two"))
        self.assertTrue(self.filesystem.exists("/a/c/../b/file_one"))
        self.assertTrue(self.filesystem.exists("/a/c/../../a/b/file_one"))
        self.assertFalse(self.filesystem.exists("a/b/../z/d"))
        self.assertTrue(self.filesystem.exists("a/b/../z/../c/file_two"))
        self.filesystem.cwd = "C:/a/c"
        self.assertTrue(self.filesystem.exists("../b/file_one"))
        self.assertTrue(self.filesystem.exists("../../a/b/file_one"))
        self.assertTrue(self.filesystem.exists("../../a/b/../../a/c/file_two"))
        self.assertFalse(self.filesystem.exists("../z/file_one"))
        self.assertTrue(self.filesystem.exists("../z/../c/file_two"))

    def test_get_object_from_root(self):
        self.filesystem.add_object(self.root_name, self.fake_file)
        self.assertEqual(self.fake_file, self.filesystem.get_object("foobar"))

    def test_get_nonexistent_object_from_root_error(self):
        self.filesystem.add_object(self.root_name, self.fake_file)
        self.assertEqual(self.fake_file, self.filesystem.get_object("foobar"))
        with self.raises_os_error(errno.ENOENT):
            self.filesystem.get_object("some_bogus_filename")

    def test_remove_object_from_root(self):
        self.filesystem.add_object(self.root_name, self.fake_file)
        self.filesystem.remove_object(self.fake_file.name)
        with self.raises_os_error(errno.ENOENT):
            self.filesystem.get_object(self.fake_file.name)

    def test_remove_nonexisten_object_from_root_error(self):
        with self.raises_os_error(errno.ENOENT):
            self.filesystem.remove_object("some_bogus_filename")

    def test_exists_removed_file(self):
        self.filesystem.add_object(self.root_name, self.fake_file)
        self.filesystem.remove_object(self.fake_file.name)
        self.assertFalse(self.filesystem.exists(self.fake_file.name))

    def test_add_object_to_child(self):
        self.filesystem.add_object(self.root_name, self.fake_child)
        self.filesystem.add_object(self.fake_child.name, self.fake_file)
        self.assertEqual(
            {self.fake_file.name: self.fake_file},
            self.filesystem.root_dir.get_entry(self.fake_child.name).entries,
        )

    def test_add_object_to_regular_file_error_posix(self):
        self.filesystem.is_windows_fs = False
        self.filesystem.add_object(self.filesystem.root_dir_name, self.fake_file)
        with self.raises_os_error(errno.ENOTDIR):
            self.filesystem.add_object(self.fake_file.name, self.fake_file)

    def test_add_object_to_regular_file_error_windows(self):
        self.filesystem.is_windows_fs = True
        self.filesystem.add_object(self.root_name, self.fake_file)
        with self.raises_os_error(errno.ENOENT):
            self.filesystem.add_object(self.fake_file.name, self.fake_file)

    def test_exists_file_added_to_child(self):
        self.filesystem.add_object(self.root_name, self.fake_child)
        self.filesystem.add_object(self.fake_child.name, self.fake_file)
        path = self.filesystem.joinpaths(self.fake_child.name, self.fake_file.name)
        self.assertTrue(self.filesystem.exists(path))

    def test_get_object_from_child(self):
        self.filesystem.add_object(self.root_name, self.fake_child)
        self.filesystem.add_object(self.fake_child.name, self.fake_file)
        self.assertEqual(
            self.fake_file,
            self.filesystem.get_object(
                self.filesystem.joinpaths(self.fake_child.name, self.fake_file.name)
            ),
        )

    def test_get_nonexistent_object_from_child_error(self):
        self.filesystem.add_object(self.root_name, self.fake_child)
        self.filesystem.add_object(self.fake_child.name, self.fake_file)
        with self.raises_os_error(errno.ENOENT):
            self.filesystem.get_object(
                self.filesystem.joinpaths(self.fake_child.name, "some_bogus_filename")
            )

    def test_remove_object_from_child(self):
        self.filesystem.add_object(self.root_name, self.fake_child)
        self.filesystem.add_object(self.fake_child.name, self.fake_file)
        target_path = self.filesystem.joinpaths(
            self.fake_child.name, self.fake_file.name
        )
        self.filesystem.remove_object(target_path)
        with self.raises_os_error(errno.ENOENT):
            self.filesystem.get_object(target_path)

    def test_remove_object_from_child_error(self):
        self.filesystem.add_object(self.root_name, self.fake_child)
        with self.raises_os_error(errno.ENOENT):
            self.filesystem.remove_object(
                self.filesystem.joinpaths(self.fake_child.name, "some_bogus_filename")
            )

    def test_remove_object_from_non_directory_error(self):
        self.filesystem.add_object(self.root_name, self.fake_file)
        with self.raises_os_error(errno.ENOTDIR):
            self.filesystem.remove_object(
                self.filesystem.joinpaths(
                    "%s" % self.fake_file.name,
                    "file_does_not_matter_since_parent_not_a_directory",
                )
            )

    def test_exists_file_removed_from_child(self):
        self.filesystem.add_object(self.root_name, self.fake_child)
        self.filesystem.add_object(self.fake_child.name, self.fake_file)
        path = self.filesystem.joinpaths(self.fake_child.name, self.fake_file.name)
        self.filesystem.remove_object(path)
        self.assertFalse(self.filesystem.exists(path))

    def test_operate_on_grandchild_directory(self):
        self.filesystem.add_object(self.root_name, self.fake_child)
        self.filesystem.add_object(self.fake_child.name, self.fake_grandchild)
        grandchild_directory = self.filesystem.joinpaths(
            self.fake_child.name, self.fake_grandchild.name
        )
        grandchild_file = self.filesystem.joinpaths(
            grandchild_directory, self.fake_file.name
        )
        with self.assertRaises(OSError):
            self.filesystem.get_object(grandchild_file)
        self.filesystem.add_object(grandchild_directory, self.fake_file)
        self.assertEqual(self.fake_file, self.filesystem.get_object(grandchild_file))
        self.assertTrue(self.filesystem.exists(grandchild_file))
        self.filesystem.remove_object(grandchild_file)
        with self.assertRaises(OSError):
            self.filesystem.get_object(grandchild_file)
        self.assertFalse(self.filesystem.exists(grandchild_file))

    def test_create_directory_in_root_directory(self):
        path = "foo"
        self.filesystem.create_dir(path)
        new_dir = self.filesystem.get_object(path)
        self.assertEqual(os.path.basename(path), new_dir.name)
        self.assertTrue(stat.S_IFDIR & new_dir.st_mode)

    def test_create_directory_in_root_directory_already_exists_error(self):
        path = "foo"
        self.filesystem.create_dir(path)
        with self.raises_os_error(errno.EEXIST):
            self.filesystem.create_dir(path)

    def test_create_directory(self):
        path = "foo/bar/baz"
        self.filesystem.create_dir(path)
        new_dir = self.filesystem.get_object(path)
        self.assertEqual(os.path.basename(path), new_dir.name)
        self.assertTrue(stat.S_IFDIR & new_dir.st_mode)

        # Create second directory to make sure first is OK.
        path = "%s/quux" % path
        self.filesystem.create_dir(path)
        new_dir = self.filesystem.get_object(path)
        self.assertEqual(os.path.basename(path), new_dir.name)
        self.assertTrue(stat.S_IFDIR & new_dir.st_mode)

    def test_create_directory_already_exists_error(self):
        path = "foo/bar/baz"
        self.filesystem.create_dir(path)
        with self.raises_os_error(errno.EEXIST):
            self.filesystem.create_dir(path)

    def test_create_file_in_read_only_directory_raises_in_posix(self):
        self.filesystem.is_windows_fs = False
        dir_path = "/foo/bar"
        self.filesystem.create_dir(dir_path, perm_bits=0o555)
        file_path = dir_path + "/baz"

        if not is_root():
            with self.raises_os_error(errno.EACCES):
                self.filesystem.create_file(file_path)
        else:
            self.filesystem.create_file(file_path)
            self.assertTrue(self.filesystem.exists(file_path))

    def test_create_file_in_read_only_directory_possible_in_windows(self):
        self.filesystem.is_windows_fs = True
        dir_path = "C:/foo/bar"
        self.filesystem.create_dir(dir_path, perm_bits=0o555)
        file_path = dir_path + "/baz"
        self.filesystem.create_file(file_path)
        self.assertTrue(self.filesystem.exists(file_path))

    def test_create_file_in_current_directory(self):
        path = "foo"
        contents = "dummy data"
        self.filesystem.create_file(path, contents=contents)
        self.assertTrue(self.filesystem.exists(path))
        self.assertFalse(self.filesystem.exists(os.path.dirname(path)))
        path = "./%s" % path
        self.assertTrue(self.filesystem.exists(os.path.dirname(path)))

    def test_create_file_in_root_directory(self):
        path = "/foo"
        contents = "dummy data"
        self.filesystem.create_file(path, contents=contents)
        new_file = self.filesystem.get_object(path)
        self.assertTrue(self.filesystem.exists(path))
        self.assertTrue(self.filesystem.exists(os.path.dirname(path)))
        self.assertEqual(os.path.basename(path), new_file.name)
        self.assertTrue(stat.S_IFREG & new_file.st_mode)
        self.assertEqual(contents, new_file.contents)

    def test_create_file_with_size_but_no_content_creates_large_file(self):
        path = "large_foo_bar"
        self.filesystem.create_file(path, st_size=100000000)
        new_file = self.filesystem.get_object(path)
        self.assertEqual(None, new_file.contents)
        self.assertEqual(100000000, new_file.st_size)

    def test_create_file_in_root_directory_already_exists_error(self):
        path = "foo"
        self.filesystem.create_file(path)
        with self.raises_os_error(errno.EEXIST):
            self.filesystem.create_file(path)

    def test_create_file(self):
        path = "foo/bar/baz"
        retval = self.filesystem.create_file(path, contents="dummy_data")
        self.assertTrue(self.filesystem.exists(path))
        self.assertTrue(self.filesystem.exists(os.path.dirname(path)))
        new_file = self.filesystem.get_object(path)
        self.assertEqual(os.path.basename(path), new_file.name)
        if IS_WIN:
            self.assertEqual(1, new_file.st_uid)
            self.assertEqual(1, new_file.st_gid)
        else:
            self.assertEqual(os.getuid(), new_file.st_uid)
            self.assertEqual(os.getgid(), new_file.st_gid)
        self.assertEqual(new_file, retval)

    def test_create_file_with_changed_ids(self):
        path = "foo/bar/baz"
        set_uid(42)
        set_gid(2)
        self.filesystem.create_file(path)
        self.assertTrue(self.filesystem.exists(path))
        new_file = self.filesystem.get_object(path)
        self.assertEqual(42, new_file.st_uid)
        self.assertEqual(2, new_file.st_gid)
        reset_ids()

    def test_empty_file_created_for_none_contents(self):
        fake_open = fake_filesystem.FakeFileOpen(self.filesystem)
        path = "foo/bar/baz"
        self.filesystem.create_file(path, contents=None)
        with fake_open(path) as f:
            self.assertEqual("", f.read())

    def test_create_file_with_incorrect_mode_type(self):
        with self.assertRaises(TypeError):
            self.filesystem.create_file("foo", "bar")

    def test_create_file_already_exists_error(self):
        path = "foo/bar/baz"
        self.filesystem.create_file(path, contents="dummy_data")
        with self.raises_os_error(errno.EEXIST):
            self.filesystem.create_file(path)

    def test_create_link(self):
        path = "foo/bar/baz"
        target_path = "foo/bar/quux"
        new_file = self.filesystem.create_symlink(path, "quux")
        # Neither the path nor the final target exists before we actually
        # write to one of them, even though the link appears in the file
        # system.
        self.assertFalse(self.filesystem.exists(path))
        self.assertFalse(self.filesystem.exists(target_path))
        self.assertTrue(stat.S_IFLNK & new_file.st_mode)

        # but once we write the linked to file, they both will exist.
        self.filesystem.create_file(target_path)
        self.assertTrue(self.filesystem.exists(path))
        self.assertTrue(self.filesystem.exists(target_path))

    def test_resolve_object(self):
        target_path = "dir/target"
        target_contents = "0123456789ABCDEF"
        link_name = "x"
        self.filesystem.create_dir("dir")
        self.filesystem.create_file("dir/target", contents=target_contents)
        self.filesystem.create_symlink(link_name, target_path)
        obj = self.filesystem.resolve(link_name)
        self.assertEqual("target", obj.name)
        self.assertEqual(target_contents, obj.contents)

    def check_lresolve_object(self):
        target_path = "dir/target"
        target_contents = "0123456789ABCDEF"
        link_name = "x"
        self.filesystem.create_dir("dir")
        self.filesystem.create_file("dir/target", contents=target_contents)
        self.filesystem.create_symlink(link_name, target_path)
        obj = self.filesystem.lresolve(link_name)
        self.assertEqual(link_name, obj.name)
        self.assertEqual(target_path, obj.contents)

    def test_lresolve_object_windows(self):
        self.filesystem.is_windows_fs = True
        self.check_lresolve_object()

    def test_lresolve_object_posix(self):
        self.filesystem.is_windows_fs = False
        self.check_lresolve_object()

    def check_directory_access_on_file(self, error_subtype):
        self.filesystem.create_file("not_a_dir")
        with self.raises_os_error(error_subtype):
            self.filesystem.resolve("not_a_dir/foo")
        with self.raises_os_error(error_subtype):
            self.filesystem.lresolve("not_a_dir/foo/bar")

    def test_directory_access_on_file_windows(self):
        self.filesystem.is_windows_fs = True
        self.check_directory_access_on_file(errno.ENOENT)

    def test_directory_access_on_file_posix(self):
        self.filesystem.is_windows_fs = False
        self.check_directory_access_on_file(errno.ENOTDIR)

    def test_pickle_fs(self):
        """Regression test for #445"""
        import pickle

        self.filesystem.open_files = []
        p = pickle.dumps(self.filesystem)
        fs = pickle.loads(p)
        self.assertEqual(str(fs.root), str(self.filesystem.root))
        self.assertEqual(fs.mount_points, self.filesystem.mount_points)


class CaseInsensitiveFakeFilesystemTest(TestCase):
    def setUp(self):
        self.filesystem = fake_filesystem.FakeFilesystem(path_separator="/")
        self.filesystem.is_case_sensitive = False
        self.os = fake_os.FakeOsModule(self.filesystem)
        self.path = self.os.path

    def test_get_object(self):
        self.filesystem.create_dir("/foo/bar")
        self.filesystem.create_file("/foo/bar/baz")
        self.assertTrue(self.filesystem.get_object("/Foo/Bar/Baz"))

    def test_remove_object(self):
        self.filesystem.create_dir("/foo/bar")
        self.filesystem.create_file("/foo/bar/baz")
        self.filesystem.remove_object("/Foo/Bar/Baz")
        self.assertFalse(self.filesystem.exists("/foo/bar/baz"))

    def test_exists(self):
        self.filesystem.create_dir("/Foo/Bar")
        self.assertTrue(self.filesystem.exists("/Foo/Bar"))
        self.assertTrue(self.filesystem.exists("/foo/bar"))

        self.filesystem.create_file("/foo/Bar/baz")
        self.assertTrue(self.filesystem.exists("/Foo/bar/BAZ"))
        self.assertTrue(self.filesystem.exists("/foo/bar/baz"))

    def test_create_directory_with_different_case_root(self):
        self.filesystem.create_dir("/Foo/Bar")
        self.filesystem.create_dir("/foo/bar/baz")
        dir1 = self.filesystem.get_object("/Foo/Bar")
        dir2 = self.filesystem.get_object("/foo/bar")
        self.assertEqual(dir1, dir2)

    def test_create_file_with_different_case_dir(self):
        self.filesystem.create_dir("/Foo/Bar")
        self.filesystem.create_file("/foo/bar/baz")
        dir1 = self.filesystem.get_object("/Foo/Bar")
        dir2 = self.filesystem.get_object("/foo/bar")
        self.assertEqual(dir1, dir2)

    def test_resolve_path(self):
        self.filesystem.create_dir("/foo/baz")
        self.filesystem.create_symlink("/Foo/Bar", "./baz/bip")
        self.assertEqual(
            f"{self.filesystem.root_dir_name}foo/baz/bip",
            self.filesystem.resolve_path("/foo/bar"),
        )

    def test_isdir_isfile(self):
        self.filesystem.create_file("foo/bar")
        self.assertTrue(self.path.isdir("Foo"))
        self.assertFalse(self.path.isfile("Foo"))
        self.assertTrue(self.path.isfile("Foo/Bar"))
        self.assertFalse(self.path.isdir("Foo/Bar"))

    def test_getsize(self):
        file_path = "foo/bar/baz"
        self.filesystem.create_file(file_path, contents="1234567")
        self.assertEqual(7, self.path.getsize("FOO/BAR/BAZ"))

    def test_getsize_with_looping_symlink(self):
        self.filesystem.is_windows_fs = False
        dir_path = "/foo/bar"
        self.filesystem.create_dir(dir_path)
        link_path = dir_path + "/link"
        link_target = link_path + "/link"
        self.os.symlink(link_target, link_path)
        with self.raises_os_error(errno.ELOOP):
            self.os.path.getsize(link_path)

    def test_get_mtime(self):
        test_file = self.filesystem.create_file("foo/bar1.txt")
        test_file.st_mtime = 24
        self.assertEqual(24, self.path.getmtime("Foo/Bar1.TXT"))

    def test_get_object_with_file_size(self):
        self.filesystem.create_file("/Foo/Bar", st_size=10)
        self.assertTrue(self.filesystem.get_object("/foo/bar"))


class CaseSensitiveFakeFilesystemTest(TestCase):
    def setUp(self):
        self.filesystem = fake_filesystem.FakeFilesystem(path_separator="/")
        self.filesystem.is_case_sensitive = True
        self.os = fake_os.FakeOsModule(self.filesystem)
        self.path = self.os.path

    def test_get_object(self):
        self.filesystem.create_dir("/foo/bar")
        self.filesystem.create_file("/foo/bar/baz")
        with self.assertRaises(OSError):
            self.filesystem.get_object("/Foo/Bar/Baz")

    def test_remove_object(self):
        self.filesystem.create_dir("/foo/bar")
        self.filesystem.create_file("/foo/bar/baz")
        with self.assertRaises(OSError):
            self.filesystem.remove_object("/Foo/Bar/Baz")
        self.assertTrue(self.filesystem.exists("/foo/bar/baz"))

    def test_exists(self):
        self.filesystem.create_dir("/Foo/Bar")
        self.assertTrue(self.filesystem.exists("/Foo/Bar"))
        self.assertFalse(self.filesystem.exists("/foo/bar"))

        self.filesystem.create_file("/foo/Bar/baz")
        self.assertFalse(self.filesystem.exists("/Foo/bar/BAZ"))
        self.assertFalse(self.filesystem.exists("/foo/bar/baz"))

    def test_create_directory_with_different_case_root(self):
        self.filesystem.create_dir("/Foo/Bar")
        self.filesystem.create_dir("/foo/bar/baz")
        dir1 = self.filesystem.get_object("/Foo/Bar")
        dir2 = self.filesystem.get_object("/foo/bar")
        self.assertNotEqual(dir1, dir2)

    def test_create_file_with_different_case_dir(self):
        self.filesystem.create_dir("/Foo/Bar")
        self.filesystem.create_file("/foo/bar/baz")
        dir1 = self.filesystem.get_object("/Foo/Bar")
        dir2 = self.filesystem.get_object("/foo/bar")
        self.assertNotEqual(dir1, dir2)

    def test_isdir_isfile(self):
        self.filesystem.create_file("foo/bar")
        self.assertFalse(self.path.isdir("Foo"))
        self.assertFalse(self.path.isfile("Foo"))
        self.assertFalse(self.path.isfile("Foo/Bar"))
        self.assertFalse(self.path.isdir("Foo/Bar"))

    def test_getsize(self):
        file_path = "foo/bar/baz"
        self.filesystem.create_file(file_path, contents="1234567")
        with self.assertRaises(os.error):
            self.path.getsize("FOO/BAR/BAZ")

    def test_get_mtime(self):
        test_file = self.filesystem.create_file("foo/bar1.txt")
        test_file.st_mtime = 24
        with self.raises_os_error(errno.ENOENT):
            self.path.getmtime("Foo/Bar1.TXT")


class OsPathInjectionRegressionTest(TestCase):
    """Test faking os.path before calling os.walk.

    Found when investigating a problem with
    gws/tools/labrat/rat_utils_unittest, which was faking out os.path
    before calling os.walk.
    """

    def setUp(self):
        self.filesystem = fake_filesystem.FakeFilesystem(path_separator="/")
        self.os_path = os.path
        # The bug was that when os.path gets faked, the FakePathModule doesn't
        # get called in self.os.walk(). FakePathModule now insists that it is
        # created as part of FakeOsModule.
        self.os = fake_os.FakeOsModule(self.filesystem)

    def tearDown(self):
        os.path = self.os_path

    def test_create_top_level_directory(self):
        top_level_dir = "/x"
        self.assertFalse(self.filesystem.exists(top_level_dir))
        self.filesystem.create_dir(top_level_dir)
        self.assertTrue(self.filesystem.exists("/"))
        self.assertTrue(self.filesystem.exists(top_level_dir))
        self.filesystem.create_dir("%s/po" % top_level_dir)
        self.filesystem.create_file("%s/po/control" % top_level_dir)
        self.filesystem.create_file("%s/po/experiment" % top_level_dir)
        self.filesystem.create_dir("%s/gv" % top_level_dir)
        self.filesystem.create_file("%s/gv/control" % top_level_dir)

        expected = [
            ("/", ["x"], []),
            ("/x", ["gv", "po"], []),
            ("/x/gv", [], ["control"]),
            ("/x/po", [], ["control", "experiment"]),
        ]
        # as the result is unsorted, we have to check against sorted results
        result = sorted([step for step in self.os.walk("/")], key=lambda v: v[0])
        self.assertEqual(len(expected), len(result))
        for entry, expected_entry in zip(result, expected):
            self.assertEqual(expected_entry[0], entry[0])
            self.assertEqual(expected_entry[1], sorted(entry[1]))
            self.assertEqual(expected_entry[2], sorted(entry[2]))


class FakePathModuleTest(TestCase):
    def setUp(self):
        self.filesystem = fake_filesystem.FakeFilesystem(path_separator="!")
        self.os = fake_os.FakeOsModule(self.filesystem)
        self.path = self.os.path

    def check_abspath(self, is_windows):
        # the implementation differs in Windows and Posix, so test both
        self.filesystem.is_windows_fs = is_windows
        filename = "foo"
        abspath = self.filesystem.root_dir_name + filename
        self.filesystem.create_file(abspath)
        self.assertEqual(abspath, self.path.abspath(abspath))
        self.assertEqual(abspath, self.path.abspath(filename))
        self.assertEqual(abspath, self.path.abspath("..!%s" % filename))

    def test_abspath_windows(self):
        self.check_abspath(is_windows=True)

    def test_abspath_posix(self):
        """abspath should return a consistent representation of a file."""
        self.check_abspath(is_windows=False)

    def check_abspath_bytes(self, is_windows):
        """abspath should return a consistent representation of a file."""
        self.filesystem.is_windows_fs = is_windows
        filename = b"foo"
        abspath = self.filesystem.root_dir_name.encode() + filename
        self.filesystem.create_file(abspath)
        self.assertEqual(abspath, self.path.abspath(abspath))
        self.assertEqual(abspath, self.path.abspath(filename))
        self.assertEqual(abspath, self.path.abspath(b"..!" + filename))

    def test_abspath_bytes_windows(self):
        self.check_abspath_bytes(is_windows=True)

    def test_abspath_bytes_posix(self):
        self.check_abspath_bytes(is_windows=False)

    def test_abspath_deals_with_relative_non_root_path(self):
        """abspath should correctly handle relative paths from a
        non-! directory.

        This test is distinct from the basic functionality test because
        fake_filesystem has historically been based in !.
        """
        filename = "!foo!bar!baz"
        file_components = filename.split(self.path.sep)
        root_name = self.filesystem.root_dir_name
        basedir = f"{root_name}{file_components[0]}"
        self.filesystem.create_file(filename)
        self.os.chdir(basedir)
        self.assertEqual(basedir, self.path.abspath(self.path.curdir))
        self.assertEqual(root_name, self.path.abspath(".."))
        self.assertEqual(
            self.path.join(basedir, file_components[1]),
            self.path.abspath(file_components[1]),
        )

    def test_abs_path_with_drive_component(self):
        self.filesystem.is_windows_fs = True
        self.filesystem.cwd = "C:!foo"
        self.assertEqual("C:!foo!bar", self.path.abspath("bar"))
        self.assertEqual("C:!foo!bar", self.path.abspath("C:bar"))
        self.assertEqual("C:!foo!bar", self.path.abspath("!foo!bar"))

    def test_isabs_with_drive_component(self):
        self.filesystem.is_windows_fs = False
        self.assertFalse(self.path.isabs("C:!foo"))
        self.assertFalse(self.path.isabs(b"C:!foo"))
        self.assertTrue(self.path.isabs("!"))
        self.assertTrue(self.path.isabs(b"!"))
        self.filesystem.is_windows_fs = True
        self.assertTrue(self.path.isabs("C:!foo"))
        self.assertTrue(self.path.isabs(b"C:!foo"))
        self.assertTrue(self.path.isabs("!"))
        self.assertTrue(self.path.isabs(b"!"))

    def test_relpath(self):
        path_foo = "!path!to!foo"
        path_bar = "!path!to!bar"
        path_other = "!some!where!else"
        with self.assertRaises(ValueError):
            self.path.relpath(None)
        with self.assertRaises(ValueError):
            self.path.relpath("")
        self.assertEqual("path!to!foo", self.path.relpath(path_foo))
        self.assertEqual("..!foo", self.path.relpath(path_foo, path_bar))
        self.assertEqual(
            "..!..!..%s" % path_other, self.path.relpath(path_other, path_bar)
        )
        self.assertEqual(".", self.path.relpath(path_bar, path_bar))

    def test_realpath_vs_abspath(self):
        self.filesystem.is_windows_fs = False
        self.filesystem.create_file("!george!washington!bridge")
        self.filesystem.create_symlink("!first!president", "!george!washington")
        self.assertEqual(
            "!first!president!bridge",
            self.os.path.abspath("!first!president!bridge"),
        )
        self.assertEqual(
            "!george!washington!bridge",
            self.os.path.realpath("!first!president!bridge"),
        )
        self.os.chdir("!first!president")
        self.assertEqual("!george!washington!bridge", self.os.path.realpath("bridge"))

    @unittest.skipIf(sys.version_info < (3, 10), "'strict' new in Python 3.10")
    def test_realpath_strict(self):
        self.filesystem.create_file("!foo!bar")
        root_dir = self.filesystem.root_dir_name
        self.filesystem.cwd = f"{root_dir}foo"
        self.assertEqual(
            f"{root_dir}foo!baz", self.os.path.realpath("baz", strict=False)
        )
        with self.raises_os_error(errno.ENOENT):
            self.os.path.realpath("baz", strict=True)
        self.assertEqual(
            f"{root_dir}foo!bar", self.os.path.realpath("bar", strict=True)
        )

    def test_samefile(self):
        file_path1 = "!foo!bar!baz"
        file_path2 = "!foo!bar!boo"
        self.filesystem.create_file(file_path1)
        self.filesystem.create_file(file_path2)
        self.assertTrue(self.path.samefile(file_path1, file_path1))
        self.assertFalse(self.path.samefile(file_path1, file_path2))
        self.assertTrue(self.path.samefile(file_path1, "!foo!..!foo!bar!..!bar!baz"))
        self.assertTrue(self.path.samefile(file_path1, b"!foo!..!foo!bar!..!bar!baz"))

    def test_exists(self):
        file_path = "foo!bar!baz"
        file_path_bytes = b"foo!bar!baz"
        self.filesystem.create_file(file_path)
        self.assertTrue(self.path.exists(file_path))
        self.assertTrue(self.path.exists(file_path_bytes))
        self.assertFalse(self.path.exists("!some!other!bogus!path"))

    def test_exists_with_drive(self):
        self.filesystem.os = OSType.WINDOWS
        self.filesystem.add_mount_point("F:")
        self.assertTrue(self.path.exists("C:"))
        self.assertTrue(self.path.exists("c:\\"))
        self.assertTrue(self.path.exists("f:"))
        self.assertTrue(self.path.exists("F:\\"))
        self.assertFalse(self.path.exists("Z:"))
        self.assertFalse(self.path.exists("z:\\"))

    def test_lexists(self):
        file_path = "foo!bar!baz"
        file_path_bytes = b"foo!bar!baz"
        self.filesystem.create_dir("foo!bar")
        self.filesystem.create_symlink(file_path, "bogus")
        self.assertTrue(self.path.lexists(file_path))
        self.assertTrue(self.path.lexists(file_path_bytes))
        self.assertFalse(self.path.exists(file_path))
        self.assertFalse(self.path.exists(file_path_bytes))
        self.filesystem.create_file("foo!bar!bogus")
        self.assertTrue(self.path.exists(file_path))

    def test_dirname_with_drive(self):
        self.filesystem.is_windows_fs = True
        self.assertEqual("c:!foo", self.path.dirname("c:!foo!bar"))
        self.assertEqual(b"c:!", self.path.dirname(b"c:!foo"))
        self.assertEqual("!foo", self.path.dirname("!foo!bar"))
        self.assertEqual(b"!", self.path.dirname(b"!foo"))
        self.assertEqual("c:foo", self.path.dirname("c:foo!bar"))
        self.assertEqual(b"c:", self.path.dirname(b"c:foo"))
        self.assertEqual("foo", self.path.dirname("foo!bar"))

    def test_dirname(self):
        dirname = "foo!bar"
        self.assertEqual(dirname, self.path.dirname("%s!baz" % dirname))

    def test_join_strings(self):
        components = ["foo", "bar", "baz"]
        self.assertEqual("foo!bar!baz", self.path.join(*components))

    def test_join_bytes(self):
        components = [b"foo", b"bar", b"baz"]
        self.assertEqual(b"foo!bar!baz", self.path.join(*components))

    @unittest.skipIf(sys.platform != "win32", "Windows specific test")
    @patch.dict(os.environ, {"USERPROFILE": r"C:\Users\John"})
    def test_expand_user_windows(self):
        self.assertEqual(self.path.expanduser("~"), "C:!Users!John")

    @unittest.skipIf(sys.platform == "win32", "Posix specific test")
    @patch.dict(os.environ, {"HOME": "/home/john"})
    def test_expand_user(self):
        self.assertEqual(self.path.expanduser("~"), "!home!john")

    @patch.dict(os.environ, {}, clear=True)
    def test_expand_user_no_home_environment(self):
        """Make sure this also works without HOME / USERPROFILE set"""
        # we just check that it does not crash and has some result,
        # as the result is system-dependent
        self.assertTrue(self.path.expanduser("~"))

    @unittest.skipIf(
        TestCase.is_windows or TestCase.is_cygwin,
        "only tested on unix systems",
    )
    def test_expand_root(self):
        if sys.platform == "darwin":
            roothome = "!var!root"
        else:
            roothome = "!root"
        self.assertEqual(self.path.expanduser("~root"), roothome)

    def test_getsize_path_nonexistent(self):
        file_path = "foo!bar!baz"
        with self.assertRaises(os.error):
            self.path.getsize(file_path)

    def test_getsize_file_empty(self):
        file_path = "foo!bar!baz"
        self.filesystem.create_file(file_path)
        self.assertEqual(0, self.path.getsize(file_path))

    def test_getsize_file_non_zero_size(self):
        file_path = "foo!bar!baz"
        file_path_bytes = b"foo!bar!baz"
        self.filesystem.create_file(file_path, contents="1234567")
        self.assertEqual(7, self.path.getsize(file_path))
        self.assertEqual(7, self.path.getsize(file_path_bytes))

    def test_getsize_dir_empty(self):
        # For directories, only require that the size is non-negative.
        dir_path = "foo!bar"
        self.filesystem.create_dir(dir_path)
        size = self.path.getsize(dir_path)
        self.assertFalse(int(size) < 0, "expected non-negative size; actual: %s" % size)

    def test_getsize_dir_non_zero_size(self):
        # For directories, only require that the size is non-negative.
        dir_path = "foo!bar"
        self.filesystem.create_file(self.filesystem.joinpaths(dir_path, "baz"))
        size = self.path.getsize(dir_path)
        self.assertFalse(int(size) < 0, "expected non-negative size; actual: %s" % size)

    def test_isdir(self):
        self.filesystem.create_file("foo!bar")
        self.assertTrue(self.path.isdir("foo"))
        self.assertTrue(self.path.isdir(b"foo"))
        self.assertFalse(self.path.isdir("foo!bar"))
        self.assertFalse(self.path.isdir("it_dont_exist"))

    def test_isdir_with_cwd_change(self):
        self.filesystem.create_file("!foo!bar!baz")
        self.assertTrue(self.path.isdir("!foo"))
        self.assertTrue(self.path.isdir("!foo!bar"))
        self.assertTrue(self.path.isdir("foo"))
        self.assertTrue(self.path.isdir("foo!bar"))
        self.filesystem.cwd = f"{self.filesystem.root_dir_name}foo"
        self.assertTrue(self.path.isdir("!foo"))
        self.assertTrue(self.path.isdir("!foo!bar"))
        self.assertTrue(self.path.isdir("bar"))

    def test_isfile(self):
        self.filesystem.create_file("foo!bar")
        self.assertFalse(self.path.isfile("foo"))
        self.assertTrue(self.path.isfile("foo!bar"))
        self.assertTrue(self.path.isfile(b"foo!bar"))
        self.assertFalse(self.path.isfile("it_dont_exist"))

    def test_get_mtime(self):
        test_file = self.filesystem.create_file("foo!bar1.txt")
        self.assertNotEqual(24, self.path.getmtime("foo!bar1.txt"))
        test_file.st_mtime = 24
        self.assertEqual(24, self.path.getmtime("foo!bar1.txt"))
        self.assertEqual(24, self.path.getmtime(b"foo!bar1.txt"))

    def test_get_mtime_raises_os_error(self):
        self.assertFalse(self.path.exists("does_not_exist"))
        with self.raises_os_error(errno.ENOENT):
            self.path.getmtime("does_not_exist")

    def test_islink(self):
        self.filesystem.create_dir("foo")
        self.filesystem.create_file("foo!regular_file")
        self.filesystem.create_symlink("foo!link_to_file", "regular_file")
        self.assertFalse(self.path.islink("foo"))

        # An object can be both a link and a file or file, according to the
        # comments in Python/Lib/posixpath.py.
        self.assertTrue(self.path.islink("foo!link_to_file"))
        self.assertTrue(self.path.isfile("foo!link_to_file"))
        self.assertTrue(self.path.islink(b"foo!link_to_file"))
        self.assertTrue(self.path.isfile(b"foo!link_to_file"))

        self.assertTrue(self.path.isfile("foo!regular_file"))
        self.assertFalse(self.path.islink("foo!regular_file"))

        self.assertFalse(self.path.islink("it_dont_exist"))

    def test_is_link_case_sensitive(self):
        # Regression test for #306
        self.filesystem.is_case_sensitive = False
        self.filesystem.create_dir("foo")
        self.filesystem.create_symlink("foo!bar", "foo")
        self.assertTrue(self.path.islink("foo!Bar"))

    def test_ismount(self):
        self.assertFalse(self.path.ismount(""))
        self.assertTrue(self.path.ismount("!"))
        self.assertTrue(self.path.ismount(b"!"))
        self.assertFalse(self.path.ismount("!mount!"))
        self.filesystem.add_mount_point("!mount")
        self.assertTrue(self.path.ismount("!mount"))
        self.assertTrue(self.path.ismount(b"!mount"))
        self.assertTrue(self.path.ismount("!mount!"))

    def test_ismount_with_drive_letters(self):
        self.filesystem.is_windows_fs = True
        self.assertTrue(self.path.ismount("!"))
        self.assertTrue(self.path.ismount("c:!"))
        self.assertFalse(self.path.ismount("c:"))
        self.assertTrue(self.path.ismount("z:!"))
        self.filesystem.add_mount_point("!mount")
        self.assertTrue(self.path.ismount("!mount"))
        self.assertTrue(self.path.ismount("!mount!"))

    def test_ismount_with_unc_paths(self):
        self.filesystem.is_windows_fs = True
        self.assertTrue(self.path.ismount("!!a!"))
        self.assertTrue(self.path.ismount("!!a!b"))
        self.assertTrue(self.path.ismount("!!a!b!"))
        self.assertFalse(self.path.ismount("!a!b!"))
        self.assertFalse(self.path.ismount("!!a!b!c"))

    def test_ismount_with_alternate_path_separator(self):
        self.filesystem.alternative_path_separator = "!"
        self.filesystem.add_mount_point("!mount")
        self.assertTrue(self.path.ismount("!mount"))
        self.assertTrue(self.path.ismount("!mount!"))
        self.assertTrue(self.path.ismount("!mount!!"))
        self.filesystem.is_windows_fs = True
        self.assertTrue(self.path.ismount("Z:!"))

    def test_getattr_forward_to_real_os_path(self):
        """Forwards any non-faked calls to os.path."""
        self.assertTrue(hasattr(self.path, "sep"), "Get a faked os.path function")
        private_path_function = None
        if sys.version_info < (3, 6):
            if self.is_windows:
                private_path_function = "_get_bothseps"
            else:
                private_path_function = "_join_real_path"
        if private_path_function:
            self.assertTrue(
                hasattr(self.path, private_path_function),
                "Get a real os.path function " "not implemented in fake os.path",
            )
        self.assertFalse(hasattr(self.path, "nonexistent"))

    def test_splitroot_posix(self):
        self.filesystem.is_windows_fs = False
        self.assertEqual(("", "", "foo!bar"), self.filesystem.splitroot("foo!bar"))
        self.assertEqual(("", "!", "foo!bar"), self.filesystem.splitroot("!foo!bar"))
        self.assertEqual(
            ("", "!!", "foo!!bar"), self.filesystem.splitroot("!!foo!!bar")
        )


class PathManipulationTestBase(TestCase):
    def setUp(self):
        self.filesystem = fake_filesystem.FakeFilesystem(path_separator="|")


class CollapsePathPipeSeparatorTest(PathManipulationTestBase):
    """Tests CollapsePath (mimics os.path.normpath) using |
    as path separator."""

    def test_empty_path_becomes_dot_path(self):
        self.assertEqual(".", self.filesystem.normpath(""))

    def test_dot_path_unchanged(self):
        self.assertEqual(".", self.filesystem.normpath("."))

    def test_slashes_are_not_collapsed(self):
        """Tests that '/' is not treated specially if the
            path separator is '|'.

        In particular, multiple slashes should not be collapsed.
        """
        self.assertEqual("/", self.filesystem.normpath("/"))
        self.assertEqual("/////", self.filesystem.normpath("/////"))

    def test_root_path(self):
        self.assertEqual("|", self.filesystem.normpath("|"))

    def test_multiple_separators_collapsed_into_root_path(self):
        self.assertEqual("|", self.filesystem.normpath("|||||"))

    def test_all_dot_paths_removed_but_one(self):
        self.assertEqual(".", self.filesystem.normpath(".|.|.|."))

    def test_all_dot_paths_removed_if_another_path_component_exists(self):
        self.assertEqual("|", self.filesystem.normpath("|.|.|.|"))
        self.assertEqual("foo|bar", self.filesystem.normpath("foo|.|.|.|bar"))

    def test_ignores_up_level_references_starting_from_root(self):
        self.assertEqual("|", self.filesystem.normpath("|..|..|..|"))
        self.assertEqual("|", self.filesystem.normpath("|..|..|foo|bar|..|..|"))
        self.filesystem.is_windows_fs = False  # not an UNC path
        self.assertEqual("|", self.filesystem.normpath("||..|.|..||"))

    def test_conserves_up_level_references_starting_from_current_dir(self):
        self.assertEqual("..|..", self.filesystem.normpath("..|foo|bar|..|..|.."))

    def test_combine_dot_and_up_level_references_in_absolute_path(self):
        self.assertEqual("|yes", self.filesystem.normpath("|||||.|..|||yes|no|..|.|||"))

    def test_dots_in_path_collapses_to_last_path(self):
        self.assertEqual("bar", self.filesystem.normpath("foo|..|bar"))
        self.assertEqual("bar", self.filesystem.normpath("foo|..|yes|..|no|..|bar"))


class SplitPathTest(PathManipulationTestBase):
    """Tests SplitPath (which mimics os.path.split)
    using | as path separator."""

    def test_empty_path(self):
        self.assertEqual(("", ""), self.filesystem.splitpath(""))

    def test_no_separators(self):
        self.assertEqual(("", "ab"), self.filesystem.splitpath("ab"))

    def test_slashes_do_not_split(self):
        """Tests that '/' is not treated specially if the
        path separator is '|'."""
        self.assertEqual(("", "a/b"), self.filesystem.splitpath("a/b"))

    def test_eliminate_trailing_separators_from_head(self):
        self.assertEqual(("a", "b"), self.filesystem.splitpath("a|b"))
        self.assertEqual(("a", "b"), self.filesystem.splitpath("a|||b"))
        self.assertEqual(("|a", "b"), self.filesystem.splitpath("|a||b"))
        self.assertEqual(("a|b", "c"), self.filesystem.splitpath("a|b|c"))
        self.assertEqual(("|a|b", "c"), self.filesystem.splitpath("|a|b|c"))

    def test_root_separator_is_not_stripped(self):
        self.assertEqual(("|||", ""), self.filesystem.splitpath("|||"))
        self.assertEqual(("|", "a"), self.filesystem.splitpath("|a"))
        self.assertEqual(("|||", "a"), self.filesystem.splitpath("|||a"))

    def test_empty_tail_if_path_ends_in_separator(self):
        self.assertEqual(("a|b", ""), self.filesystem.splitpath("a|b|"))

    def test_empty_path_components_are_preserved_in_head(self):
        self.assertEqual(("|a||b", "c"), self.filesystem.splitpath("|a||b||c"))


class JoinPathTest(PathManipulationTestBase):
    """Tests JoinPath (which mimics os.path.join) using | as path separator."""

    def test_one_empty_component(self):
        self.assertEqual("", self.filesystem.joinpaths(""))

    def test_multiple_empty_components(self):
        self.assertEqual("", self.filesystem.joinpaths("", "", ""))

    def test_separators_not_stripped_from_single_component(self):
        self.assertEqual("||a||", self.filesystem.joinpaths("||a||"))

    def test_one_separator_added_between_components(self):
        self.assertEqual("a|b|c|d", self.filesystem.joinpaths("a", "b", "c", "d"))

    def test_no_separator_added_for_components_ending_in_separator(self):
        self.assertEqual("a|b|c", self.filesystem.joinpaths("a|", "b|", "c"))
        self.assertEqual("a|||b|||c", self.filesystem.joinpaths("a|||", "b|||", "c"))

    def test_components_preceding_absolute_component_are_ignored(self):
        self.assertEqual("|c|d", self.filesystem.joinpaths("a", "|b", "|c", "d"))

    def test_one_separator_added_for_trailing_empty_components(self):
        self.assertEqual("a|", self.filesystem.joinpaths("a", ""))
        self.assertEqual("a|", self.filesystem.joinpaths("a", "", ""))

    def test_no_separator_added_for_leading_empty_components(self):
        self.assertEqual("a", self.filesystem.joinpaths("", "a"))

    def test_internal_empty_components_ignored(self):
        self.assertEqual("a|b", self.filesystem.joinpaths("a", "", "b"))
        self.assertEqual("a|b|", self.filesystem.joinpaths("a|", "", "b|"))


class PathSeparatorTest(TestCase):
    def test_os_path_sep_matches_fake_filesystem_separator(self):
        filesystem = fake_filesystem.FakeFilesystem(path_separator="!")
        fake_os_module = fake_os.FakeOsModule(filesystem)
        self.assertEqual("!", fake_os_module.sep)
        self.assertEqual("!", fake_os_module.path.sep)


class NormalizeCaseTest(TestCase):
    def setUp(self):
        self.filesystem = fake_filesystem.FakeFilesystem(path_separator="/")
        self.filesystem.is_case_sensitive = False

    def test_normalize_case(self):
        self.filesystem.create_file("/Foo/Bar")
        self.assertEqual(
            f"{self.filesystem.root_dir_name}Foo/Bar",
            self.filesystem._original_path("/foo/bar"),
        )
        self.assertEqual(
            f"{self.filesystem.root_dir_name}Foo/Bar",
            self.filesystem._original_path("/FOO/BAR"),
        )

    def test_normalize_case_for_drive(self):
        self.filesystem.is_windows_fs = True
        self.filesystem.create_file("C:/Foo/Bar")
        self.assertEqual("C:/Foo/Bar", self.filesystem._original_path("c:/foo/bar"))
        self.assertEqual("C:/Foo/Bar", self.filesystem._original_path("C:/FOO/BAR"))

    def test_normalize_case_for_non_existing_file(self):
        self.filesystem.create_dir("/Foo/Bar")
        self.assertEqual(
            f"{self.filesystem.root_dir_name}Foo/Bar/baz",
            self.filesystem._original_path("/foo/bar/baz"),
        )
        self.assertEqual(
            f"{self.filesystem.root_dir_name}Foo/Bar/BAZ",
            self.filesystem._original_path("/FOO/BAR/BAZ"),
        )

    @unittest.skipIf(
        not TestCase.is_windows, "Regression test for Windows problem only"
    )
    def test_normalize_case_for_lazily_added_empty_file(self):
        # regression test for specific issue with added empty real files
        filesystem = fake_filesystem.FakeFilesystem()
        real_dir_path = os.path.split(os.path.dirname(os.path.abspath(__file__)))[0]
        filesystem.add_real_directory(real_dir_path)
        initPyPath = os.path.join(real_dir_path, "__init__.py")
        self.assertEqual(initPyPath, filesystem._original_path(initPyPath.upper()))


class AlternativePathSeparatorTest(TestCase):
    def setUp(self):
        self.filesystem = fake_filesystem.FakeFilesystem(path_separator="!")
        self.filesystem.alternative_path_separator = "?"

    def test_initial_value(self):
        filesystem = fake_filesystem.FakeFilesystem()
        if self.is_windows:
            self.assertEqual("/", filesystem.alternative_path_separator)
        else:
            self.assertIsNone(filesystem.alternative_path_separator)

        filesystem = fake_filesystem.FakeFilesystem(path_separator="/")
        self.assertIsNone(filesystem.alternative_path_separator)

    def test_alt_sep(self):
        fake_os_module = fake_os.FakeOsModule(self.filesystem)
        self.assertEqual("?", fake_os_module.altsep)
        self.assertEqual("?", fake_os_module.path.altsep)

    def test_collapse_path_with_mixed_separators(self):
        self.assertEqual("!foo!bar", self.filesystem.normpath("!foo??bar"))

    def test_normalize_path_with_mixed_separators(self):
        path = "foo?..?bar"
        self.assertEqual(
            f"{self.filesystem.root_dir_name}bar",
            self.filesystem.absnormpath(path),
        )

    def test_exists_with_mixed_separators(self):
        self.filesystem.create_file("?foo?bar?baz")
        self.filesystem.create_file("!foo!bar!xyzzy!plugh")
        self.assertTrue(self.filesystem.exists("!foo!bar!baz"))
        self.assertTrue(self.filesystem.exists("?foo?bar?xyzzy?plugh"))


class DriveLetterSupportTest(TestCase):
    def setUp(self):
        self.filesystem = fake_filesystem.FakeFilesystem(path_separator="!")
        self.filesystem.alternative_path_separator = "^"
        self.filesystem.is_windows_fs = True

    def test_initial_value(self):
        filesystem = fake_filesystem.FakeFilesystem()
        if self.is_windows:
            self.assertTrue(filesystem.is_windows_fs)
        else:
            self.assertFalse(filesystem.is_windows_fs)

    def test_collapse_path(self):
        self.assertEqual("c:!foo!bar", self.filesystem.normpath("c:!!foo!!bar"))

    def test_collapse_unc_path(self):
        self.assertEqual("!!foo!bar!baz", self.filesystem.normpath("!!foo!bar!!baz!!"))

    def test_normalize_path_str(self):
        self.filesystem.cwd = ""
        self.assertEqual("c:!foo!bar", self.filesystem.absnormpath("c:!foo!!bar"))
        self.filesystem.cwd = "c:!foo"
        self.assertEqual("c:!foo!bar", self.filesystem.absnormpath("bar"))

    def test_normalize_path_bytes(self):
        self.filesystem.cwd = b""
        self.assertEqual(b"c:!foo!bar", self.filesystem.absnormpath(b"c:!foo!!bar"))
        self.filesystem.cwd = b"c:!foo"
        self.assertEqual(b"c:!foo!bar", self.filesystem.absnormpath(b"bar"))

    def test_split_path_str(self):
        self.assertEqual(("c:!foo", "bar"), self.filesystem.splitpath("c:!foo!bar"))
        self.assertEqual(("c:!", "foo"), self.filesystem.splitpath("c:!foo"))
        self.assertEqual(("!foo", "bar"), self.filesystem.splitpath("!foo!bar"))
        self.assertEqual(("!", "foo"), self.filesystem.splitpath("!foo"))
        self.assertEqual(("c:foo", "bar"), self.filesystem.splitpath("c:foo!bar"))
        self.assertEqual(("c:", "foo"), self.filesystem.splitpath("c:foo"))
        self.assertEqual(("foo", "bar"), self.filesystem.splitpath("foo!bar"))

    def test_split_with_alt_separator(self):
        self.assertEqual(("a^b", "c"), self.filesystem.splitpath("a^b^c"))
        self.assertEqual(("a^b!c", "d"), self.filesystem.splitpath("a^b!c^d"))
        self.assertEqual(("a^b!c", "d"), self.filesystem.splitpath("a^b!c!d"))
        self.assertEqual((b"a^b", b"c"), self.filesystem.splitpath(b"a^b^c"))
        self.assertEqual((b"a^b!c", b"d"), self.filesystem.splitpath(b"a^b!c^d"))
        self.assertEqual((b"a^b!c", b"d"), self.filesystem.splitpath(b"a^b!c!d"))

    def test_split_path_bytes(self):
        self.assertEqual((b"c:!foo", b"bar"), self.filesystem.splitpath(b"c:!foo!bar"))
        self.assertEqual((b"c:!", b"foo"), self.filesystem.splitpath(b"c:!foo"))
        self.assertEqual((b"!foo", b"bar"), self.filesystem.splitpath(b"!foo!bar"))
        self.assertEqual((b"!", b"foo"), self.filesystem.splitpath(b"!foo"))
        self.assertEqual((b"c:foo", b"bar"), self.filesystem.splitpath(b"c:foo!bar"))
        self.assertEqual((b"c:", b"foo"), self.filesystem.splitpath(b"c:foo"))
        self.assertEqual((b"foo", b"bar"), self.filesystem.splitpath(b"foo!bar"))

    def test_characters_before_root_ignored_in_join_paths(self):
        self.assertEqual("c:d", self.filesystem.joinpaths("b", "c:", "d"))

    def test_resolve_path(self):
        self.assertEqual("C:!foo!bar", self.filesystem.resolve_path("C:!foo!bar"))

    def test_get_path_components(self):
        self.assertEqual(
            ["c:", "foo", "bar"],
            self.filesystem._path_components("c:!foo!bar"),
        )
        self.assertEqual(["c:"], self.filesystem._path_components("c:"))

    def test_split_drive_str(self):
        self.assertEqual(("c:", "!foo!bar"), self.filesystem.splitdrive("c:!foo!bar"))
        self.assertEqual(("", "!foo!bar"), self.filesystem.splitdrive("!foo!bar"))
        self.assertEqual(("c:", "foo!bar"), self.filesystem.splitdrive("c:foo!bar"))
        self.assertEqual(("", "foo!bar"), self.filesystem.splitdrive("foo!bar"))

    def test_split_drive_bytes(self):
        self.assertEqual(
            (b"c:", b"!foo!bar"), self.filesystem.splitdrive(b"c:!foo!bar")
        )
        self.assertEqual((b"", b"!foo!bar"), self.filesystem.splitdrive(b"!foo!bar"))

    def test_split_drive_alt_sep(self):
        self.assertEqual(("c:", "^foo^bar"), self.filesystem.splitdrive("c:^foo^bar"))
        self.assertEqual(("", "foo^bar"), self.filesystem.splitdrive("foo^bar"))
        self.assertEqual(("", "foo^bar!baz"), self.filesystem.splitdrive("foo^bar!baz"))
        self.assertEqual(
            (b"c:", b"^foo^bar"), self.filesystem.splitdrive(b"c:^foo^bar")
        )
        self.assertEqual((b"", b"^foo^bar"), self.filesystem.splitdrive(b"^foo^bar"))
        self.assertEqual(
            (b"", b"^foo^bar!baz"), self.filesystem.splitdrive(b"^foo^bar!baz")
        )

    def test_split_drive_with_unc_path(self):
        self.assertEqual(
            ("!!foo!bar", "!baz"), self.filesystem.splitdrive("!!foo!bar!baz")
        )
        self.assertEqual(("", "!!foo"), self.filesystem.splitdrive("!!foo"))
        self.assertEqual(("", "!!foo!!bar"), self.filesystem.splitdrive("!!foo!!bar"))
        self.assertEqual(("!!foo!bar", "!!"), self.filesystem.splitdrive("!!foo!bar!!"))

    def test_split_drive_with_unc_path_alt_sep(self):
        self.assertEqual(
            ("^^foo^bar", "!baz"), self.filesystem.splitdrive("^^foo^bar!baz")
        )
        self.assertEqual(("", "^^foo"), self.filesystem.splitdrive("^^foo"))
        self.assertEqual(("", "^^foo^^bar"), self.filesystem.splitdrive("^^foo^^bar"))
        self.assertEqual(("^^foo^bar", "^^"), self.filesystem.splitdrive("^^foo^bar^^"))

    def test_split_path_with_drive(self):
        self.assertEqual(("d:!foo", "baz"), self.filesystem.splitpath("d:!foo!baz"))
        self.assertEqual(("d:!foo!baz", ""), self.filesystem.splitpath("d:!foo!baz!"))
        self.assertEqual(("c:", ""), self.filesystem.splitpath("c:"))
        self.assertEqual(("c:!", ""), self.filesystem.splitpath("c:!"))
        self.assertEqual(("c:!!", ""), self.filesystem.splitpath("c:!!"))

    def test_split_path_with_drive_alt_sep(self):
        self.assertEqual(("d:^foo", "baz"), self.filesystem.splitpath("d:^foo^baz"))
        self.assertEqual(("d:^foo^baz", ""), self.filesystem.splitpath("d:^foo^baz^"))
        self.assertEqual(("c:", ""), self.filesystem.splitpath("c:"))
        self.assertEqual(("c:^", ""), self.filesystem.splitpath("c:^"))
        self.assertEqual(("c:^^", ""), self.filesystem.splitpath("c:^^"))

    def test_split_path_with_unc_path(self):
        self.assertEqual(
            ("!!foo!bar!", "baz"), self.filesystem.splitpath("!!foo!bar!baz")
        )
        self.assertEqual(("!!foo!bar", ""), self.filesystem.splitpath("!!foo!bar"))
        self.assertEqual(("!!foo!bar!!", ""), self.filesystem.splitpath("!!foo!bar!!"))

    def test_split_path_with_unc_path_alt_sep(self):
        self.assertEqual(
            ("^^foo^bar^", "baz"), self.filesystem.splitpath("^^foo^bar^baz")
        )
        self.assertEqual(("^^foo^bar", ""), self.filesystem.splitpath("^^foo^bar"))
        self.assertEqual(("^^foo^bar^^", ""), self.filesystem.splitpath("^^foo^bar^^"))

    def test_splitroot_with_drive(self):
        self.assertEqual(
            ("E:", "!", "foo!bar"), self.filesystem.splitroot("E:!foo!bar")
        )
        self.assertEqual(
            ("E:", "!", "!foo!!!bar"), self.filesystem.splitroot("E:!!foo!!!bar")
        )
        self.assertEqual(
            (b"E:", b"!", b"!foo!!!bar"), self.filesystem.splitroot(b"E:!!foo!!!bar")
        )
        self.assertEqual(
            ("C:", "^", "foo^bar"), self.filesystem.splitroot("C:^foo^bar")
        )

    def test_splitroot_with_unc_path(self):
        self.assertEqual(
            ("!!foo!bar", "!", "baz"), self.filesystem.splitroot("!!foo!bar!baz")
        )
        self.assertEqual(
            ("!!?!UNC", "!", "foo!bar"), self.filesystem.splitroot("!!?!UNC!foo!bar")
        )
        self.assertEqual(
            ("^^foo^bar", "^", "baz"), self.filesystem.splitroot("^^foo^bar^baz")
        )
        self.assertEqual(
            (b"!!foo!bar", b"!", b"baz"), self.filesystem.splitroot(b"!!foo!bar!baz")
        )

    def test_splitroot_with_empty_parts(self):
        self.assertEqual(("", "", ""), self.filesystem.splitroot(""))
        self.assertEqual(("", "!", "foo"), self.filesystem.splitroot("!foo"))
        self.assertEqual(("!!foo!bar", "", ""), self.filesystem.splitroot("!!foo!bar"))
        self.assertEqual(("!!foo", "", ""), self.filesystem.splitroot("!!foo"))
        self.assertEqual(
            ("!!foo!bar", "!", ""), self.filesystem.splitroot("!!foo!bar!")
        )
        self.assertEqual(("C:", "", "foo!bar"), self.filesystem.splitroot("C:foo!bar"))


class DiskSpaceTest(TestCase):
    def setUp(self):
        self.fs = fake_filesystem.FakeFilesystem(path_separator="!", total_size=100)
        self.os = fake_os.FakeOsModule(self.fs)
        self.open = fake_open.FakeFileOpen(self.fs)

    def test_disk_usage_on_file_creation(self):
        total_size = 100
        self.fs.add_mount_point("!mount", total_size)

        def create_too_large_file():
            with self.open("!mount!file", "w") as dest:
                dest.write("a" * (total_size + 1))

        with self.assertRaises(OSError):
            create_too_large_file()

        self.assertEqual(0, self.fs.get_disk_usage("!mount").used)

        with self.open("!mount!file", "w") as dest:
            dest.write("a" * total_size)

        self.assertEqual(total_size, self.fs.get_disk_usage("!mount").used)

    def test_disk_usage_on_automounted_drive(self):
        self.fs.is_windows_fs = True
        self.fs.reset(total_size=100)
        self.fs.create_file("!foo!bar", st_size=50)
        self.assertEqual(0, self.fs.get_disk_usage("D:!").used)
        self.fs.cwd = "E:!foo"
        self.assertEqual(0, self.fs.get_disk_usage("!foo").used)

    def test_disk_usage_on_mounted_paths(self):
        self.fs.add_mount_point("!foo", total_size=200)
        self.fs.add_mount_point("!foo!bar", total_size=400)
        self.fs.create_file("!baz", st_size=50)
        self.fs.create_file("!foo!baz", st_size=60)
        self.fs.create_file("!foo!bar!baz", st_size=100)
        self.assertEqual(50, self.fs.get_disk_usage("!").used)
        self.assertEqual(60, self.fs.get_disk_usage("!foo").used)
        self.assertEqual(100, self.fs.get_disk_usage("!foo!bar").used)
        self.assertEqual(400, self.fs.get_disk_usage("!foo!bar").total)

    def test_file_system_size_after_large_file_creation(self):
        filesystem = fake_filesystem.FakeFilesystem(
            path_separator="!", total_size=1024 * 1024 * 1024 * 100
        )
        filesystem.create_file("!foo!baz", st_size=1024 * 1024 * 1024 * 10)
        self.assertEqual(
            (
                1024 * 1024 * 1024 * 100,
                1024 * 1024 * 1024 * 10,
                1024 * 1024 * 1024 * 90,
            ),
            filesystem.get_disk_usage(),
        )

    def test_file_system_size_after_binary_file_creation(self):
        self.fs.create_file("!foo!bar", contents=b"xyzzy")
        self.assertEqual((100, 5, 95), self.fs.get_disk_usage())

    def test_file_system_size_after_ascii_string_file_creation(self):
        self.fs.create_file("!foo!bar", contents="complicated")
        self.assertEqual((100, 11, 89), self.fs.get_disk_usage())

    def test_filesystem_size_after_2byte_unicode_file_creation(self):
        self.fs.create_file("!foo!bar", contents="сложно", encoding="utf-8")
        self.assertEqual((100, 12, 88), self.fs.get_disk_usage())

    def test_filesystem_size_after_3byte_unicode_file_creation(self):
        self.fs.create_file("!foo!bar", contents="複雑", encoding="utf-8")
        self.assertEqual((100, 6, 94), self.fs.get_disk_usage())

    def test_file_system_size_after_file_deletion(self):
        self.fs.create_file("!foo!bar", contents=b"xyzzy")
        self.fs.create_file("!foo!baz", st_size=20)
        self.fs.remove_object("!foo!bar")
        self.assertEqual((100, 20, 80), self.fs.get_disk_usage())

    def test_file_system_size_after_directory_removal(self):
        self.fs.create_file("!foo!bar", st_size=10)
        self.fs.create_file("!foo!baz", st_size=20)
        self.fs.create_file("!foo1!bar", st_size=40)
        self.fs.remove_object("!foo")
        self.assertEqual((100, 40, 60), self.fs.get_disk_usage())

    def test_creating_file_with_fitting_content(self):
        initial_usage = self.fs.get_disk_usage()

        try:
            self.fs.create_file("!foo!bar", contents=b"a" * 100)
        except OSError:
            self.fail(
                "File with contents fitting into disk space " "could not be written."
            )

        self.assertEqual(initial_usage.used + 100, self.fs.get_disk_usage().used)

    def test_creating_file_with_content_too_large(self):
        def create_large_file():
            self.fs.create_file("!foo!bar", contents=b"a" * 101)

        initial_usage = self.fs.get_disk_usage()

        with self.assertRaises(OSError):
            create_large_file()

        self.assertEqual(initial_usage, self.fs.get_disk_usage())

    def test_creating_file_with_fitting_size(self):
        initial_usage = self.fs.get_disk_usage()

        try:
            self.fs.create_file("!foo!bar", st_size=100)
        except OSError:
            self.fail("File with size fitting into disk space could not be written.")

        self.assertEqual(initial_usage.used + 100, self.fs.get_disk_usage().used)

    def test_creating_file_with_size_too_large(self):
        initial_usage = self.fs.get_disk_usage()

        def create_large_file():
            self.fs.create_file("!foo!bar", st_size=101)

        with self.assertRaises(OSError):
            create_large_file()

        self.assertEqual(initial_usage, self.fs.get_disk_usage())

    def test_resize_file_with_fitting_size(self):
        file_object = self.fs.create_file("!foo!bar", st_size=50)
        try:
            file_object.set_large_file_size(100)
            file_object.set_contents(b"a" * 100)
        except OSError:
            self.fail("Resizing file failed although disk space was sufficient.")

    def test_resize_file_with_size_too_large(self):
        file_object = self.fs.create_file("!foo!bar", st_size=50)
        with self.raises_os_error(errno.ENOSPC):
            file_object.set_large_file_size(200)
        with self.raises_os_error(errno.ENOSPC):
            file_object.set_contents("a" * 150)

    def test_file_system_size_after_directory_rename(self):
        self.fs.create_file("!foo!bar", st_size=20)
        self.os.rename("!foo", "!baz")
        self.assertEqual(20, self.fs.get_disk_usage().used)

    def test_file_system_size_after_file_rename(self):
        self.fs.create_file("!foo!bar", st_size=20)
        self.os.rename("!foo!bar", "!foo!baz")
        self.assertEqual(20, self.fs.get_disk_usage().used)

    def test_that_hard_link_does_not_change_used_size(self):
        file1_path = "test_file1"
        file2_path = "test_file2"
        self.fs.create_file(file1_path, st_size=20)
        self.assertEqual(20, self.fs.get_disk_usage().used)
        # creating a hard link shall not increase used space
        self.os.link(file1_path, file2_path)
        self.assertEqual(20, self.fs.get_disk_usage().used)
        # removing a file shall not decrease used space
        # if a hard link still exists
        self.os.unlink(file1_path)
        self.assertEqual(20, self.fs.get_disk_usage().used)
        self.os.unlink(file2_path)
        self.assertEqual(0, self.fs.get_disk_usage().used)

    def test_that_the_size_of_correct_mount_point_is_used(self):
        self.fs.add_mount_point("!mount_limited", total_size=50)
        self.fs.add_mount_point("!mount_unlimited")

        with self.raises_os_error(errno.ENOSPC):
            self.fs.create_file("!mount_limited!foo", st_size=60)
        with self.raises_os_error(errno.ENOSPC):
            self.fs.create_file("!bar", st_size=110)

        try:
            self.fs.create_file("!foo", st_size=60)
            self.fs.create_file("!mount_limited!foo", st_size=40)
            self.fs.create_file("!mount_unlimited!foo", st_size=1000000)
        except OSError:
            self.fail(
                "File with contents fitting into " "disk space could not be written."
            )

    def test_that_disk_usage_of_correct_mount_point_is_used(self):
        self.fs.add_mount_point("!mount1", total_size=20)
        self.fs.add_mount_point("!mount1!bar!mount2", total_size=50)

        self.fs.create_file("!foo!bar", st_size=10)
        self.fs.create_file("!mount1!foo!bar", st_size=10)
        self.fs.create_file("!mount1!bar!mount2!foo!bar", st_size=10)

        self.assertEqual(90, self.fs.get_disk_usage("!foo").free)
        self.assertEqual(10, self.fs.get_disk_usage("!mount1!foo").free)
        self.assertEqual(40, self.fs.get_disk_usage("!mount1!bar!mount2").free)

    def test_set_larger_disk_size(self):
        self.fs.add_mount_point("!mount1", total_size=20)
        with self.raises_os_error(errno.ENOSPC):
            self.fs.create_file("!mount1!foo", st_size=100)
        self.fs.set_disk_usage(total_size=200, path="!mount1")
        self.fs.create_file("!mount1!foo", st_size=100)
        self.assertEqual(100, self.fs.get_disk_usage("!mount1!foo").free)

    def test_set_smaller_disk_size(self):
        self.fs.add_mount_point("!mount1", total_size=200)
        self.fs.create_file("!mount1!foo", st_size=100)
        with self.raises_os_error(errno.ENOSPC):
            self.fs.set_disk_usage(total_size=50, path="!mount1")
        self.fs.set_disk_usage(total_size=150, path="!mount1")
        self.assertEqual(50, self.fs.get_disk_usage("!mount1!foo").free)

    def test_disk_size_on_unlimited_disk(self):
        self.fs.add_mount_point("!mount1")
        self.fs.create_file("!mount1!foo", st_size=100)
        self.fs.set_disk_usage(total_size=1000, path="!mount1")
        self.assertEqual(900, self.fs.get_disk_usage("!mount1!foo").free)

    def test_disk_size_on_auto_mounted_drive_on_file_creation(self):
        self.fs.is_windows_fs = True
        # drive d: shall be auto-mounted and the used size adapted
        self.fs.create_file("d:!foo!bar", st_size=100)
        self.fs.set_disk_usage(total_size=1000, path="d:")
        self.assertEqual(self.fs.get_disk_usage("d:!foo").free, 900)

    def test_disk_size_on_auto_mounted_drive_on_directory_creation(self):
        self.fs.is_windows_fs = True
        self.fs.create_dir("d:!foo!bar")
        self.fs.create_file("d:!foo!bar!baz", st_size=100)
        self.fs.create_file("d:!foo!baz", st_size=100)
        self.fs.set_disk_usage(total_size=1000, path="d:")
        self.assertEqual(800, self.fs.get_disk_usage("d:!foo").free)

    def test_copying_preserves_byte_contents(self):
        source_file = self.fs.create_file("foo", contents=b"somebytes")
        dest_file = self.fs.create_file("bar")
        dest_file.set_contents(source_file.contents)
        self.assertEqual(dest_file.contents, source_file.contents)

    def test_diskusage_after_open_write(self):
        with self.open("bar.txt", "w") as f:
            f.write("a" * 60)
            f.flush()
        self.assertEqual(60, self.fs.get_disk_usage()[1])

    def test_disk_full_after_reopened(self):
        with self.open("bar.txt", "w") as f:
            f.write("a" * 60)
        with self.open("bar.txt") as f:
            self.assertEqual("a" * 60, f.read())
        with self.raises_os_error(errno.ENOSPC):
            with self.open("bar.txt", "w") as f:
                f.write("b" * 110)
                with self.raises_os_error(errno.ENOSPC):
                    f.flush()
        with self.open("bar.txt") as f:
            self.assertEqual("", f.read())

    def test_disk_full_append(self):
        file_path = "bar.txt"
        with self.open(file_path, "w") as f:
            f.write("a" * 60)
        with self.open(file_path) as f:
            self.assertEqual("a" * 60, f.read())
        with self.raises_os_error(errno.ENOSPC):
            with self.open(file_path, "a") as f:
                f.write("b" * 41)
                with self.raises_os_error(errno.ENOSPC):
                    f.flush()
        with self.open("bar.txt") as f:
            self.assertEqual(f.read(), "a" * 60)

    def test_disk_full_after_reopened_rplus_seek(self):
        with self.open("bar.txt", "w") as f:
            f.write("a" * 60)
        with self.open("bar.txt") as f:
            self.assertEqual(f.read(), "a" * 60)
        with self.raises_os_error(errno.ENOSPC):
            with self.open("bar.txt", "r+") as f:
                f.seek(50)
                f.write("b" * 60)
                with self.raises_os_error(errno.ENOSPC):
                    f.flush()
        with self.open("bar.txt") as f:
            self.assertEqual(f.read(), "a" * 60)


class MountPointTest(TestCase):
    def setUp(self):
        self.filesystem = fake_filesystem.FakeFilesystem(
            path_separator="!", total_size=100
        )

    def add_mount_points(self):
        self.filesystem.add_mount_point("!foo")
        self.filesystem.add_mount_point("!bar")
        self.filesystem.add_mount_point("!foo!baz")

    def test_that_new_mount_points_get_new_device_number(self):
        self.add_mount_points()
        self.assertEqual(1, self.filesystem.get_object("!").st_dev)
        self.assertEqual(2, self.filesystem.get_object("!foo").st_dev)
        self.assertEqual(3, self.filesystem.get_object("!bar").st_dev)
        self.assertEqual(4, self.filesystem.get_object("!foo!baz").st_dev)

    def test_that_new_directories_get_correct_device_number(self):
        self.add_mount_points()
        self.assertEqual(1, self.filesystem.create_dir("!foo1!bar").st_dev)
        self.assertEqual(2, self.filesystem.create_dir("!foo!bar").st_dev)
        self.assertEqual(4, self.filesystem.create_dir("!foo!baz!foo!bar").st_dev)

    def test_that_new_files_get_correct_device_number(self):
        self.add_mount_points()
        self.assertEqual(1, self.filesystem.create_file("!foo1!bar").st_dev)
        self.assertEqual(2, self.filesystem.create_file("!foo!bar").st_dev)
        self.assertEqual(4, self.filesystem.create_file("!foo!baz!foo!bar").st_dev)

    def test_that_mount_point_cannot_be_added_twice(self):
        self.add_mount_points()
        with self.raises_os_error(errno.EEXIST):
            self.filesystem.add_mount_point("!foo")
        with self.raises_os_error(errno.EEXIST):
            self.filesystem.add_mount_point("!foo!")

    def test_that_drives_are_auto_mounted(self):
        self.filesystem.is_windows_fs = True
        self.add_mount_points()
        self.filesystem.create_dir("d:!foo!bar")
        self.filesystem.create_file("d:!foo!baz")
        self.filesystem.create_file("z:!foo!baz")
        self.assertEqual(5, self.filesystem.get_object("d:").st_dev)
        self.assertEqual(5, self.filesystem.get_object("d:!foo!bar").st_dev)
        self.assertEqual(5, self.filesystem.get_object("d:!foo!baz").st_dev)
        self.assertEqual(6, self.filesystem.get_object("z:!foo!baz").st_dev)

    def test_that_drives_are_auto_mounted_case_insensitive(self):
        self.filesystem.is_windows_fs = True
        self.add_mount_points()
        self.filesystem.is_case_sensitive = False
        self.filesystem.create_dir("D:!foo!bar")
        self.filesystem.create_file("e:!foo!baz")
        self.assertEqual(5, self.filesystem.get_object("D:").st_dev)
        self.assertEqual(5, self.filesystem.get_object("d:!foo!bar").st_dev)
        self.assertEqual(6, self.filesystem.get_object("e:!foo").st_dev)
        self.assertEqual(6, self.filesystem.get_object("E:!Foo!Baz").st_dev)

    def test_that_unc_paths_are_auto_mounted(self):
        self.filesystem.is_windows_fs = True
        self.add_mount_points()
        self.filesystem.create_dir("!!foo!bar!baz")
        self.filesystem.create_file("!!foo!bar!bip!bop")
        self.assertEqual(5, self.filesystem.get_object("!!foo!bar").st_dev)
        self.assertEqual(5, self.filesystem.get_object("!!foo!bar!bip!bop").st_dev)


class ConvenienceMethodTest(RealFsTestCase):
    def test_create_link_with_non_existent_parent(self):
        self.skip_if_symlink_not_supported()
        file1_path = self.make_path("test_file1")
        link_path = self.make_path("nonexistent", "test_file2")

        self.filesystem.create_file(file1_path, contents="link test")
        self.assertEqual(self.os.stat(file1_path).st_nlink, 1)
        self.filesystem.create_link(file1_path, link_path)
        self.assertEqual(self.os.stat(file1_path).st_nlink, 2)
        self.assertTrue(self.filesystem.exists(link_path))

    def test_create_symlink_with_non_existent_parent(self):
        self.skip_if_symlink_not_supported()
        file1_path = self.make_path("test_file1")
        link_path = self.make_path("nonexistent", "test_file2")

        self.filesystem.create_file(file1_path, contents="symlink test")
        self.filesystem.create_symlink(link_path, file1_path)
        self.assertTrue(self.filesystem.exists(link_path))
        self.assertTrue(self.filesystem.islink(link_path))


class RealFileSystemAccessTest(RealFsTestCase):
    def setUp(self):
        # use the real path separator to work with the real file system
        self.filesystem = fake_filesystem.FakeFilesystem()
        self.fake_open = fake_filesystem.FakeFileOpen(self.filesystem)
        self.pyfakefs_path = os.path.split(os.path.dirname(os.path.abspath(__file__)))[
            0
        ]
        self.root_path = os.path.split(self.pyfakefs_path)[0]

    def test_add_non_existing_real_file_raises(self):
        nonexisting_path = os.path.join("nonexisting", "test.txt")
        with self.assertRaises(OSError):
            self.filesystem.add_real_file(nonexisting_path)
        self.assertFalse(self.filesystem.exists(nonexisting_path))

    def test_add_non_existing_real_directory_raises(self):
        nonexisting_path = "/nonexisting"
        with self.raises_os_error(errno.ENOENT):
            self.filesystem.add_real_directory(nonexisting_path)
        self.assertFalse(self.filesystem.exists(nonexisting_path))

    def test_existing_fake_file_raises(self):
        real_file_path = __file__
        self.filesystem.create_file(real_file_path)
        with self.raises_os_error(errno.EEXIST):
            self.filesystem.add_real_file(real_file_path)

    def test_existing_fake_directory_raises(self):
        self.filesystem.create_dir(self.root_path)
        with self.raises_os_error(errno.EEXIST):
            self.filesystem.add_real_directory(self.root_path)

    def check_fake_file_stat(self, fake_file, real_file_path, target_path=None):
        if target_path is None or target_path == real_file_path:
            self.assertTrue(self.filesystem.exists(real_file_path))
        else:
            self.assertFalse(self.filesystem.exists(real_file_path))
            self.assertTrue(self.filesystem.exists(target_path))

        real_stat = os.stat(real_file_path)
        self.assertIsNone(fake_file._byte_contents)
        self.assertEqual(fake_file.st_size, real_stat.st_size)
        self.assertAlmostEqual(fake_file.st_ctime, real_stat.st_ctime, places=5)
        self.assertAlmostEqual(fake_file.st_atime, real_stat.st_atime, places=5)
        self.assertAlmostEqual(fake_file.st_mtime, real_stat.st_mtime, places=5)
        self.assertEqual(fake_file.st_uid, real_stat.st_uid)
        self.assertEqual(fake_file.st_gid, real_stat.st_gid)

    def check_read_only_file(self, fake_file, real_file_path):
        with open(real_file_path, "rb") as f:
            real_contents = f.read()
        self.assertEqual(fake_file.byte_contents, real_contents)
        if not is_root():
            with self.raises_os_error(errno.EACCES):
                self.fake_open(real_file_path, "w")
        else:
            with self.fake_open(real_file_path, "w"):
                pass

    def check_writable_file(self, fake_file, real_file_path):
        with open(real_file_path, "rb") as f:
            real_contents = f.read()
        self.assertEqual(fake_file.byte_contents, real_contents)
        with self.fake_open(real_file_path, "wb") as f:
            f.write(b"test")
        with open(real_file_path, "rb") as f:
            real_contents1 = f.read()
        self.assertEqual(real_contents1, real_contents)
        with self.fake_open(real_file_path, "rb") as f:
            fake_contents = f.read()
        self.assertEqual(fake_contents, b"test")

    def test_add_existing_real_file_read_only(self):
        real_file_path = os.path.abspath(__file__)
        fake_file = self.filesystem.add_real_file(real_file_path)
        self.check_fake_file_stat(fake_file, real_file_path)
        self.assertEqual(fake_file.st_mode & 0o333, 0)
        self.check_read_only_file(fake_file, real_file_path)

    def test_add_existing_real_file_read_write(self):
        real_file_path = os.path.realpath(__file__)
        fake_file = self.filesystem.add_real_file(real_file_path, read_only=False)

        self.check_fake_file_stat(fake_file, real_file_path)
        self.assertEqual(fake_file.st_mode, os.stat(real_file_path).st_mode)
        self.check_writable_file(fake_file, real_file_path)

    def test_add_real_file_to_existing_path(self):
        real_file_path = os.path.abspath(__file__)
        self.filesystem.create_file("/foo/bar")
        with self.raises_os_error(errno.EEXIST):
            self.filesystem.add_real_file(real_file_path, target_path="/foo/bar")

    def test_add_real_file_to_non_existing_path(self):
        real_file_path = os.path.abspath(__file__)
        fake_file = self.filesystem.add_real_file(
            real_file_path, target_path="/foo/bar"
        )
        self.check_fake_file_stat(fake_file, real_file_path, target_path="/foo/bar")

    def test_write_to_real_file(self):
        # regression test for #470
        real_file_path = os.path.abspath(__file__)
        self.filesystem.add_real_file(real_file_path, read_only=False)
        with self.fake_open(real_file_path, "w") as f:
            f.write("foo")

        with self.fake_open(real_file_path, "rb") as f:
            self.assertEqual(b"foo", f.read())

    def test_add_existing_real_directory_read_only(self):
        self.filesystem.add_real_directory(self.pyfakefs_path)
        self.assertTrue(self.filesystem.exists(self.pyfakefs_path))
        self.assertTrue(
            self.filesystem.exists(
                os.path.join(self.pyfakefs_path, "fake_filesystem.py")
            )
        )
        self.assertTrue(
            self.filesystem.exists(os.path.join(self.pyfakefs_path, "fake_pathlib.py"))
        )

        file_path = os.path.join(self.pyfakefs_path, "fake_filesystem_shutil.py")
        fake_file = self.filesystem.resolve(file_path)
        self.check_fake_file_stat(fake_file, file_path)
        self.check_read_only_file(fake_file, file_path)

    def test_add_existing_real_directory_tree(self):
        self.filesystem.add_real_directory(self.root_path)
        self.assertTrue(
            self.filesystem.exists(
                os.path.join(
                    self.root_path,
                    "pyfakefs",
                    "tests",
                    "fake_filesystem_test.py",
                )
            )
        )
        self.assertTrue(
            self.filesystem.exists(
                os.path.join(self.root_path, "pyfakefs", "fake_filesystem.py")
            )
        )
        self.assertTrue(
            self.filesystem.exists(
                os.path.join(self.root_path, "pyfakefs", "__init__.py")
            )
        )

    @contextlib.contextmanager
    def create_symlinks(self, symlinks):
        for link in symlinks:
            os.symlink(link[0], link[1])

        yield

        for link in symlinks:
            os.unlink(link[1])

    def test_add_existing_real_directory_symlink(self):
        fake_open = fake_filesystem.FakeFileOpen(self.filesystem)
        real_directory = os.path.join(self.root_path, "pyfakefs", "tests")
        symlinks = [
            (
                "..",
                os.path.join(real_directory, "fixtures", "symlink_dir_relative"),
            ),
            (
                "../all_tests.py",
                os.path.join(real_directory, "fixtures", "symlink_file_relative"),
            ),
            (
                real_directory,
                os.path.join(real_directory, "fixtures", "symlink_dir_absolute"),
            ),
            (
                os.path.join(real_directory, "all_tests.py"),
                os.path.join(real_directory, "fixtures", "symlink_file_absolute"),
            ),
            (
                "/etc/something",
                os.path.join(
                    real_directory, "fixtures", "symlink_file_absolute_outside"
                ),
            ),
        ]

        self.filesystem.create_file("/etc/something")

        with fake_open("/etc/something", "w") as f:
            f.write("good morning")

        try:
            with self.create_symlinks(symlinks):
                self.filesystem.add_real_directory(real_directory, lazy_read=False)
        except OSError:
            if self.is_windows:
                raise unittest.SkipTest("Symlinks under Windows need admin privileges")
            raise

        for link in symlinks:
            self.assertTrue(self.filesystem.islink(link[1]))

        # relative
        self.assertTrue(
            self.filesystem.exists(
                os.path.join(
                    self.root_path,
                    "pyfakefs",
                    "tests",
                    "fixtures/symlink_dir_relative",
                )
            )
        )
        self.assertTrue(
            self.filesystem.exists(
                os.path.join(
                    self.root_path,
                    "pyfakefs",
                    "tests",
                    "fixtures/symlink_dir_relative/all_tests.py",
                )
            )
        )
        self.assertTrue(
            self.filesystem.exists(
                os.path.join(
                    self.root_path,
                    "pyfakefs",
                    "tests",
                    "fixtures/symlink_file_relative",
                )
            )
        )

        # absolute
        self.assertTrue(
            self.filesystem.exists(
                os.path.join(
                    self.root_path,
                    "pyfakefs",
                    "tests",
                    "fixtures/symlink_dir_absolute",
                )
            )
        )
        self.assertTrue(
            self.filesystem.exists(
                os.path.join(
                    self.root_path,
                    "pyfakefs",
                    "tests",
                    "fixtures/symlink_dir_absolute/all_tests.py",
                )
            )
        )
        self.assertTrue(
            self.filesystem.exists(
                os.path.join(
                    self.root_path,
                    "pyfakefs",
                    "tests",
                    "fixtures/symlink_file_absolute",
                )
            )
        )

        # outside
        self.assertTrue(
            self.filesystem.exists(
                os.path.join(
                    self.root_path,
                    "pyfakefs",
                    "tests",
                    "fixtures/symlink_file_absolute_outside",
                )
            )
        )
        self.assertEqual(
            fake_open(
                os.path.join(
                    self.root_path,
                    "pyfakefs",
                    "tests",
                    "fixtures/symlink_file_absolute_outside",
                )
            ).read(),
            "good morning",
        )

    def test_add_existing_real_directory_symlink_target_path(self):
        self.skip_if_symlink_not_supported(force_real_fs=True)
        real_directory = os.path.join(self.root_path, "pyfakefs", "tests")
        symlinks = [
            (
                "..",
                os.path.join(real_directory, "fixtures", "symlink_dir_relative"),
            ),
            (
                "../all_tests.py",
                os.path.join(real_directory, "fixtures", "symlink_file_relative"),
            ),
        ]

        with self.create_symlinks(symlinks):
            self.filesystem.add_real_directory(
                real_directory, target_path="/path", lazy_read=False
            )

        self.assertTrue(self.filesystem.exists("/path/fixtures/symlink_dir_relative"))
        self.assertTrue(
            self.filesystem.exists("/path/fixtures/symlink_dir_relative/all_tests.py")
        )
        self.assertTrue(self.filesystem.exists("/path/fixtures/symlink_file_relative"))

    def test_add_existing_real_directory_symlink_lazy_read(self):
        self.skip_if_symlink_not_supported(force_real_fs=True)
        real_directory = os.path.join(self.root_path, "pyfakefs", "tests")
        symlinks = [
            (
                "..",
                os.path.join(real_directory, "fixtures", "symlink_dir_relative"),
            ),
            (
                "../all_tests.py",
                os.path.join(real_directory, "fixtures", "symlink_file_relative"),
            ),
        ]

        with self.create_symlinks(symlinks):
            self.filesystem.add_real_directory(
                real_directory, target_path="/path", lazy_read=True
            )

            self.assertTrue(
                self.filesystem.exists("/path/fixtures/symlink_dir_relative")
            )
            self.assertTrue(
                self.filesystem.exists(
                    "/path/fixtures/symlink_dir_relative/all_tests.py"
                )
            )
            self.assertTrue(
                self.filesystem.exists("/path/fixtures/symlink_file_relative")
            )

    def test_add_existing_real_directory_tree_to_existing_path(self):
        self.filesystem.create_dir("/foo/bar")
        with self.raises_os_error(errno.EEXIST):
            self.filesystem.add_real_directory(self.root_path, target_path="/foo/bar")

    def test_add_existing_real_directory_tree_to_other_path(self):
        self.filesystem.add_real_directory(self.root_path, target_path="/foo/bar")
        self.assertFalse(
            self.filesystem.exists(
                os.path.join(self.pyfakefs_path, "tests", "fake_filesystem_test.py")
            )
        )
        self.assertTrue(
            self.filesystem.exists(
                os.path.join(
                    "foo",
                    "bar",
                    "pyfakefs",
                    "tests",
                    "fake_filesystem_test.py",
                )
            )
        )
        self.assertFalse(
            self.filesystem.exists(
                os.path.join(self.root_path, "pyfakefs", "fake_filesystem.py")
            )
        )
        self.assertTrue(
            self.filesystem.exists(
                os.path.join("foo", "bar", "pyfakefs", "__init__.py")
            )
        )

    def test_get_object_from_lazily_added_real_directory(self):
        self.filesystem.is_case_sensitive = True
        self.filesystem.add_real_directory(self.root_path)
        self.assertTrue(
            self.filesystem.get_object(
                os.path.join(self.root_path, "pyfakefs", "fake_filesystem.py")
            )
        )
        self.assertTrue(
            self.filesystem.get_object(
                os.path.join(self.root_path, "pyfakefs", "__init__.py")
            )
        )

    def test_add_existing_real_directory_lazily(self):
        disk_size = 1024 * 1024 * 1024
        real_dir_path = os.path.join(self.root_path, "pyfakefs")
        self.filesystem.set_disk_usage(disk_size, real_dir_path)
        self.filesystem.add_real_directory(real_dir_path)

        # the directory contents have not been read, the the disk usage
        # has not changed
        self.assertEqual(disk_size, self.filesystem.get_disk_usage(real_dir_path).free)
        # checking for existence shall read the directory contents
        self.assertTrue(
            self.filesystem.get_object(
                os.path.join(real_dir_path, "fake_filesystem.py")
            )
        )
        # so now the free disk space shall have decreased
        self.assertGreater(
            disk_size, self.filesystem.get_disk_usage(real_dir_path).free
        )

    def test_add_existing_real_directory_not_lazily(self):
        disk_size = 1024 * 1024 * 1024
        self.filesystem.set_disk_usage(disk_size, self.pyfakefs_path)
        self.filesystem.add_real_directory(self.pyfakefs_path, lazy_read=False)

        # the directory has been read, so the file sizes have
        # been subtracted from the free space
        self.assertGreater(
            disk_size, self.filesystem.get_disk_usage(self.pyfakefs_path).free
        )

    def test_add_existing_real_directory_read_write(self):
        self.filesystem.add_real_directory(self.pyfakefs_path, read_only=False)
        self.assertTrue(self.filesystem.exists(self.pyfakefs_path))
        self.assertTrue(
            self.filesystem.exists(
                os.path.join(self.pyfakefs_path, "fake_filesystem.py")
            )
        )
        self.assertTrue(
            self.filesystem.exists(os.path.join(self.pyfakefs_path, "fake_pathlib.py"))
        )

        file_path = os.path.join(self.pyfakefs_path, "pytest_plugin.py")
        fake_file = self.filesystem.resolve(file_path)
        self.check_fake_file_stat(fake_file, file_path)
        self.check_writable_file(fake_file, file_path)

    def test_add_existing_real_paths_read_only(self):
        real_file_path = os.path.realpath(__file__)
        fixture_path = os.path.join(self.pyfakefs_path, "tests", "fixtures")
        self.filesystem.add_real_paths([real_file_path, fixture_path])

        fake_file = self.filesystem.resolve(real_file_path)
        self.check_fake_file_stat(fake_file, real_file_path)
        self.check_read_only_file(fake_file, real_file_path)

        real_file_path = os.path.join(fixture_path, "module_with_attributes.py")
        fake_file = self.filesystem.resolve(real_file_path)
        self.check_fake_file_stat(fake_file, real_file_path)
        self.check_read_only_file(fake_file, real_file_path)

    def test_add_existing_real_paths_read_write(self):
        real_file_path = os.path.realpath(__file__)
        fixture_path = os.path.join(self.pyfakefs_path, "tests", "fixtures")
        self.filesystem.add_real_paths([real_file_path, fixture_path], read_only=False)

        fake_file = self.filesystem.resolve(real_file_path)
        self.check_fake_file_stat(fake_file, real_file_path)
        self.check_writable_file(fake_file, real_file_path)

        real_file_path = os.path.join(fixture_path, "module_with_attributes.py")
        fake_file = self.filesystem.resolve(real_file_path)
        self.check_fake_file_stat(fake_file, real_file_path)
        self.check_writable_file(fake_file, real_file_path)


class FileSideEffectTests(TestCase):
    def side_effect(self):
        test_case = self
        test_case.side_effect_called = False

        def __side_effect(file_object):
            test_case.side_effect_called = True
            test_case.side_effect_file_object_content = file_object.contents

        return __side_effect

    def setUp(self):
        # use the real path separator to work with the real file system
        self.filesystem = fake_filesystem.FakeFilesystem()
        self.filesystem.create_file("/a/b/file_one", side_effect=self.side_effect())

    def test_side_effect_called(self):
        fake_open = fake_filesystem.FakeFileOpen(self.filesystem)
        self.side_effect_called = False
        with fake_open("/a/b/file_one", "w") as handle:
            handle.write("foo")
        self.assertTrue(self.side_effect_called)

    def test_side_effect_file_object(self):
        fake_open = fake_filesystem.FakeFileOpen(self.filesystem)
        self.side_effect_called = False
        with fake_open("/a/b/file_one", "w") as handle:
            handle.write("foo")
        self.assertEqual(self.side_effect_file_object_content, "foo")


if __name__ == "__main__":
    unittest.main()