32
32
from pyfakefs import fake_filesystem
33
33
34
34
35
- def _GetDummyTime (start_time , increment ):
36
- def _DummyTime ():
37
- _DummyTime ._curr_time += increment
38
- return _DummyTime ._curr_time
35
+ class _DummyTime (object ):
36
+ """Mock replacement for time.time. Increases returned time on access."""
39
37
40
- _DummyTime ._curr_time = start_time - increment # pylint: disable-msg=W0612
41
- return _DummyTime
38
+ def __init__ (self , curr_time , increment ):
39
+ self .curr_time = curr_time
40
+ self .increment = increment
41
+ self .started = False
42
+
43
+ def start (self ):
44
+ self .started = True
45
+
46
+ def __call__ (self , * args , ** kwargs ):
47
+ if self .started :
48
+ self .curr_time += self .increment
49
+ return self .curr_time
42
50
43
51
44
52
class TestCase (unittest .TestCase ):
@@ -67,7 +75,7 @@ def assertRaisesOSError(self, subtype, expression, *args, **kwargs):
67
75
class FakeDirectoryUnitTest (TestCase ):
68
76
def setUp (self ):
69
77
self .orig_time = time .time
70
- time .time = _GetDummyTime (10 , 1 )
78
+ time .time = _DummyTime (10 , 1 )
71
79
self .fake_file = fake_filesystem .FakeFile ('foobar' , contents = 'dummy_file' )
72
80
self .fake_dir = fake_filesystem .FakeDirectory ('somedir' )
73
81
@@ -676,7 +684,7 @@ def setUp(self):
676
684
self .rwx = self .os .R_OK | self .os .W_OK | self .os .X_OK
677
685
self .rw = self .os .R_OK | self .os .W_OK
678
686
self .orig_time = time .time
679
- time .time = _GetDummyTime (200 , 20 )
687
+ time .time = _DummyTime (200 , 20 )
680
688
681
689
def tearDown (self ):
682
690
time .time = self .orig_time
@@ -1717,6 +1725,8 @@ def testChmodStCtime(self):
1717
1725
file_path = 'some_file'
1718
1726
self .filesystem .CreateFile (file_path )
1719
1727
self .assertTrue (self .filesystem .Exists (file_path ))
1728
+ time .time .start ()
1729
+
1720
1730
st = self .os .stat (file_path )
1721
1731
self .assertEqual (200 , st .st_ctime )
1722
1732
# tests
@@ -1728,6 +1738,8 @@ def testUtimeSetsCurrentTimeIfArgsIsNone(self):
1728
1738
# set up
1729
1739
path = '/some_file'
1730
1740
self ._CreateTestFile (path )
1741
+ time .time .start ()
1742
+
1731
1743
st = self .os .stat (path )
1732
1744
# 200 is the current time established in setUp().
1733
1745
self .assertEqual (200 , st .st_atime )
@@ -1736,39 +1748,51 @@ def testUtimeSetsCurrentTimeIfArgsIsNone(self):
1736
1748
self .os .utime (path , None )
1737
1749
st = self .os .stat (path )
1738
1750
self .assertEqual (220 , st .st_atime )
1739
- self .assertEqual (240 , st .st_mtime )
1751
+ self .assertEqual (220 , st .st_mtime )
1740
1752
1741
1753
def testUtimeSetsCurrentTimeIfArgsIsNoneWithFloats (self ):
1742
1754
# set up
1743
1755
# we set os.stat_float_times() to False, so atime/ctime/mtime
1744
1756
# are converted as ints (seconds since epoch)
1745
- time .time = _GetDummyTime (200.9123 , 20 )
1757
+ time .time = _DummyTime (200.9123 , 20 )
1746
1758
path = '/some_file'
1747
1759
fake_filesystem .FakeOsModule .stat_float_times (False )
1748
1760
self ._CreateTestFile (path )
1761
+ time .time .start ()
1762
+
1749
1763
st = self .os .stat (path )
1750
1764
# 200 is the current time established above (if converted to int).
1751
1765
self .assertEqual (200 , st .st_atime )
1752
1766
self .assertTrue (isinstance (st .st_atime , int ))
1753
1767
self .assertEqual (200 , st .st_mtime )
1754
1768
self .assertTrue (isinstance (st .st_mtime , int ))
1769
+
1770
+ if sys .version_info >= (3 , 3 ):
1771
+ self .assertEqual (200912300000 , st .st_atime_ns )
1772
+ self .assertEqual (200912300000 , st .st_mtime_ns )
1773
+
1774
+ self .assertEqual (200 , st .st_mtime )
1755
1775
# actual tests
1756
1776
self .os .utime (path , None )
1757
1777
st = self .os .stat (path )
1758
1778
self .assertEqual (220 , st .st_atime )
1759
1779
self .assertTrue (isinstance (st .st_atime , int ))
1760
- self .assertEqual (240 , st .st_mtime )
1780
+ self .assertEqual (220 , st .st_mtime )
1761
1781
self .assertTrue (isinstance (st .st_mtime , int ))
1782
+ if sys .version_info >= (3 , 3 ):
1783
+ self .assertEqual (220912300000 , st .st_atime_ns )
1784
+ self .assertEqual (220912300000 , st .st_mtime_ns )
1762
1785
1763
1786
def testUtimeSetsCurrentTimeIfArgsIsNoneWithFloatsNSec (self ):
1764
1787
self .filesystem = fake_filesystem .FakeFilesystem (path_separator = '/' )
1765
1788
self .os = fake_filesystem .FakeOsModule (self .filesystem )
1766
- self . assertTrue ( not self . os . stat_float_times () )
1789
+ fake_filesystem . FakeOsModule . stat_float_times (False )
1767
1790
1768
- time .time = _GetDummyTime (200.9123 , 20 )
1791
+ time .time = _DummyTime (200.9123 , 20 )
1769
1792
path = '/some_file'
1770
1793
test_file = self ._CreateTestFile (path )
1771
1794
1795
+ time .time .start ()
1772
1796
st = self .os .stat (path )
1773
1797
self .assertEqual (200 , st .st_ctime )
1774
1798
self .assertEqual (200 , test_file .st_ctime )
@@ -1796,7 +1820,7 @@ def testUtimeSetsCurrentTimeIfArgsIsNoneWithFloatsNSec(self):
1796
1820
self .os .utime (path , None )
1797
1821
st = self .os .stat (path )
1798
1822
self .assertEqual (220.9123 , st .st_atime )
1799
- self .assertEqual (240 .9123 , st .st_mtime )
1823
+ self .assertEqual (220 .9123 , st .st_mtime )
1800
1824
1801
1825
def testUtimeSetsSpecifiedTime (self ):
1802
1826
# set up
@@ -1865,6 +1889,8 @@ def testUtimeSetsSpecifiedTimeInNs(self):
1865
1889
# set up
1866
1890
path = '/some_file'
1867
1891
self ._CreateTestFile (path )
1892
+ time .time .start ()
1893
+
1868
1894
st = self .os .stat (path )
1869
1895
# actual tests
1870
1896
self .os .utime (path , ns = (200000000 , 400000000 ))
@@ -2358,6 +2384,12 @@ def testStat(self):
2358
2384
self .assertEqual (self .filesystem .ResolveObject ('/linked/plugh/dir' ).st_mtime ,
2359
2385
self .dir_entries [2 ].stat ().st_mtime )
2360
2386
2387
+ def testIndexAccessToStatTimesReturnsInt (self ):
2388
+ self .assertEqual (self .os .stat ('/xyzzy/plugh/dir' )[stat .ST_CTIME ],
2389
+ int (self .dir_entries [0 ].stat ().st_ctime ))
2390
+ self .assertEqual (self .os .stat ('/linked/plugh/dir' )[stat .ST_MTIME ],
2391
+ int (self .dir_entries [2 ].stat ().st_mtime ))
2392
+
2361
2393
def testStatInoDevPosix (self ):
2362
2394
self .filesystem .is_windows_fs = False
2363
2395
file_obj = self .filesystem .ResolveObject ('/linked/plugh/file' )
@@ -2522,7 +2554,7 @@ def testCreateTopLevelDirectory(self):
2522
2554
class FakePathModuleTest (TestCase ):
2523
2555
def setUp (self ):
2524
2556
self .orig_time = time .time
2525
- time .time = _GetDummyTime (10 , 1 )
2557
+ time .time = _DummyTime (10 , 1 )
2526
2558
self .filesystem = fake_filesystem .FakeFilesystem (path_separator = '!' )
2527
2559
self .os = fake_filesystem .FakeOsModule (self .filesystem )
2528
2560
self .path = self .os .path
@@ -2726,9 +2758,8 @@ def testIsfile(self):
2726
2758
2727
2759
def testGetMtime (self ):
2728
2760
test_file = self .filesystem .CreateFile ('foo!bar1.txt' )
2729
- # The root directory ('', effectively '!') is created at time 10,
2730
- # the parent directory ('foo') at time 11, and the file at time 12.
2731
- self .assertEqual (12 , test_file .st_mtime )
2761
+ time .time .start ()
2762
+ self .assertEqual (10 , test_file .st_mtime )
2732
2763
test_file .SetMTime (24 )
2733
2764
self .assertEqual (24 , self .path .getmtime ('foo!bar1.txt' ))
2734
2765
@@ -2897,7 +2928,7 @@ def setUp(self):
2897
2928
self .open = self .file
2898
2929
self .os = fake_filesystem .FakeOsModule (self .filesystem )
2899
2930
self .orig_time = time .time
2900
- time .time = _GetDummyTime (100 , 10 )
2931
+ time .time = _DummyTime (100 , 10 )
2901
2932
2902
2933
def tearDown (self ):
2903
2934
time .time = self .orig_time
@@ -3276,41 +3307,52 @@ def testOpenStCtime(self):
3276
3307
self .assertFalse (self .filesystem .Exists (file_path ))
3277
3308
# tests
3278
3309
fake_file = self .file (file_path , 'w' )
3310
+ time .time .start ()
3279
3311
st = self .os .stat (file_path )
3280
- self .assertEqual (100 , st .st_ctime , st .st_mtime )
3312
+ self .assertEqual (100 , st .st_ctime )
3313
+ self .assertEqual (100 , st .st_mtime )
3281
3314
fake_file .close ()
3282
3315
st = self .os .stat (file_path )
3283
- self .assertEqual (110 , st .st_ctime , st .st_mtime )
3316
+ self .assertEqual (110 , st .st_ctime )
3317
+ self .assertEqual (110 , st .st_mtime )
3284
3318
3285
3319
fake_file = self .file (file_path , 'w' )
3286
3320
st = self .os .stat (file_path )
3287
3321
# truncating the file cause an additional stat update
3288
- self .assertEqual (120 , st .st_ctime , st .st_mtime )
3322
+ self .assertEqual (120 , st .st_ctime )
3323
+ self .assertEqual (120 , st .st_mtime )
3289
3324
fake_file .close ()
3290
3325
st = self .os .stat (file_path )
3291
- self .assertEqual (130 , st .st_ctime , st .st_mtime )
3326
+ self .assertEqual (130 , st .st_ctime )
3327
+ self .assertEqual (130 , st .st_mtime )
3292
3328
3293
3329
fake_file = self .file (file_path , 'w+' )
3294
3330
st = self .os .stat (file_path )
3295
- self .assertEqual (140 , st .st_ctime , st .st_mtime )
3331
+ self .assertEqual (140 , st .st_ctime )
3332
+ self .assertEqual (140 , st .st_mtime )
3296
3333
fake_file .close ()
3297
3334
st = self .os .stat (file_path )
3298
- self .assertEqual (150 , st .st_ctime , st .st_mtime )
3335
+ self .assertEqual (150 , st .st_ctime )
3336
+ self .assertEqual (150 , st .st_mtime )
3299
3337
3300
3338
fake_file = self .file (file_path , 'a' )
3301
3339
st = self .os .stat (file_path )
3302
3340
# not updating m_time or c_time here, since no truncating.
3303
- self .assertEqual (150 , st .st_ctime , st .st_mtime )
3341
+ self .assertEqual (150 , st .st_ctime )
3342
+ self .assertEqual (150 , st .st_mtime )
3304
3343
fake_file .close ()
3305
3344
st = self .os .stat (file_path )
3306
- self .assertEqual (160 , st .st_ctime , st .st_mtime )
3345
+ self .assertEqual (160 , st .st_ctime )
3346
+ self .assertEqual (160 , st .st_mtime )
3307
3347
3308
3348
fake_file = self .file (file_path , 'r' )
3309
3349
st = self .os .stat (file_path )
3310
- self .assertEqual (160 , st .st_ctime , st .st_mtime )
3350
+ self .assertEqual (160 , st .st_ctime )
3351
+ self .assertEqual (160 , st .st_mtime )
3311
3352
fake_file .close ()
3312
3353
st = self .os .stat (file_path )
3313
- self .assertEqual (160 , st .st_ctime , st .st_mtime )
3354
+ self .assertEqual (160 , st .st_ctime )
3355
+ self .assertEqual (160 , st .st_mtime )
3314
3356
3315
3357
def _CreateWithPermission (self , file_path , perm_bits ):
3316
3358
self .filesystem .CreateFile (file_path )
@@ -4589,9 +4631,9 @@ def checkFakeFileStat(self, fake_file, real_file_path):
4589
4631
real_stat = os .stat (real_file_path )
4590
4632
self .assertIsNone (fake_file ._byte_contents )
4591
4633
self .assertEqual (fake_file .st_size , real_stat .st_size )
4592
- self .assertEqual (fake_file .st_ctime , real_stat .st_ctime )
4593
- self .assertEqual (fake_file .st_atime , real_stat .st_atime )
4594
- self .assertEqual (fake_file .st_mtime , real_stat .st_mtime )
4634
+ self .assertAlmostEqual (fake_file .st_ctime , real_stat .st_ctime , places = 5 )
4635
+ self .assertAlmostEqual (fake_file .st_atime , real_stat .st_atime , places = 5 )
4636
+ self .assertAlmostEqual (fake_file .st_mtime , real_stat .st_mtime , places = 5 )
4595
4637
self .assertEqual (fake_file .st_uid , real_stat .st_uid )
4596
4638
self .assertEqual (fake_file .st_gid , real_stat .st_gid )
4597
4639
0 commit comments