##// END OF EJS Templates
test-filecache: sort import lines
Yuya Nishihara -
r28802:b16eacf5 default
parent child Browse files
Show More
@@ -1,187 +1,193 b''
1 from __future__ import absolute_import, print_function
1 from __future__ import absolute_import, print_function
2 import os
2 import os
3 import subprocess
3 import subprocess
4 import sys
4 import sys
5
5
6 if subprocess.call(['python', '%s/hghave' % os.environ['TESTDIR'],
6 if subprocess.call(['python', '%s/hghave' % os.environ['TESTDIR'],
7 'cacheable']):
7 'cacheable']):
8 sys.exit(80)
8 sys.exit(80)
9
9
10 from mercurial import util, scmutil, extensions, hg, ui
10 from mercurial import (
11 extensions,
12 hg,
13 scmutil,
14 ui,
15 util,
16 )
11
17
12 filecache = scmutil.filecache
18 filecache = scmutil.filecache
13
19
14 class fakerepo(object):
20 class fakerepo(object):
15 def __init__(self):
21 def __init__(self):
16 self._filecache = {}
22 self._filecache = {}
17
23
18 def join(self, p):
24 def join(self, p):
19 return p
25 return p
20
26
21 def sjoin(self, p):
27 def sjoin(self, p):
22 return p
28 return p
23
29
24 @filecache('x', 'y')
30 @filecache('x', 'y')
25 def cached(self):
31 def cached(self):
26 print('creating')
32 print('creating')
27 return 'string from function'
33 return 'string from function'
28
34
29 def invalidate(self):
35 def invalidate(self):
30 for k in self._filecache:
36 for k in self._filecache:
31 try:
37 try:
32 delattr(self, k)
38 delattr(self, k)
33 except AttributeError:
39 except AttributeError:
34 pass
40 pass
35
41
36 def basic(repo):
42 def basic(repo):
37 print("* neither file exists")
43 print("* neither file exists")
38 # calls function
44 # calls function
39 repo.cached
45 repo.cached
40
46
41 repo.invalidate()
47 repo.invalidate()
42 print("* neither file still exists")
48 print("* neither file still exists")
43 # uses cache
49 # uses cache
44 repo.cached
50 repo.cached
45
51
46 # create empty file
52 # create empty file
47 f = open('x', 'w')
53 f = open('x', 'w')
48 f.close()
54 f.close()
49 repo.invalidate()
55 repo.invalidate()
50 print("* empty file x created")
56 print("* empty file x created")
51 # should recreate the object
57 # should recreate the object
52 repo.cached
58 repo.cached
53
59
54 f = open('x', 'w')
60 f = open('x', 'w')
55 f.write('a')
61 f.write('a')
56 f.close()
62 f.close()
57 repo.invalidate()
63 repo.invalidate()
58 print("* file x changed size")
64 print("* file x changed size")
59 # should recreate the object
65 # should recreate the object
60 repo.cached
66 repo.cached
61
67
62 repo.invalidate()
68 repo.invalidate()
63 print("* nothing changed with either file")
69 print("* nothing changed with either file")
64 # stats file again, reuses object
70 # stats file again, reuses object
65 repo.cached
71 repo.cached
66
72
67 # atomic replace file, size doesn't change
73 # atomic replace file, size doesn't change
68 # hopefully st_mtime doesn't change as well so this doesn't use the cache
74 # hopefully st_mtime doesn't change as well so this doesn't use the cache
69 # because of inode change
75 # because of inode change
70 f = scmutil.opener('.')('x', 'w', atomictemp=True)
76 f = scmutil.opener('.')('x', 'w', atomictemp=True)
71 f.write('b')
77 f.write('b')
72 f.close()
78 f.close()
73
79
74 repo.invalidate()
80 repo.invalidate()
75 print("* file x changed inode")
81 print("* file x changed inode")
76 repo.cached
82 repo.cached
77
83
78 # create empty file y
84 # create empty file y
79 f = open('y', 'w')
85 f = open('y', 'w')
80 f.close()
86 f.close()
81 repo.invalidate()
87 repo.invalidate()
82 print("* empty file y created")
88 print("* empty file y created")
83 # should recreate the object
89 # should recreate the object
84 repo.cached
90 repo.cached
85
91
86 f = open('y', 'w')
92 f = open('y', 'w')
87 f.write('A')
93 f.write('A')
88 f.close()
94 f.close()
89 repo.invalidate()
95 repo.invalidate()
90 print("* file y changed size")
96 print("* file y changed size")
91 # should recreate the object
97 # should recreate the object
92 repo.cached
98 repo.cached
93
99
94 f = scmutil.opener('.')('y', 'w', atomictemp=True)
100 f = scmutil.opener('.')('y', 'w', atomictemp=True)
95 f.write('B')
101 f.write('B')
96 f.close()
102 f.close()
97
103
98 repo.invalidate()
104 repo.invalidate()
99 print("* file y changed inode")
105 print("* file y changed inode")
100 repo.cached
106 repo.cached
101
107
102 f = scmutil.opener('.')('x', 'w', atomictemp=True)
108 f = scmutil.opener('.')('x', 'w', atomictemp=True)
103 f.write('c')
109 f.write('c')
104 f.close()
110 f.close()
105 f = scmutil.opener('.')('y', 'w', atomictemp=True)
111 f = scmutil.opener('.')('y', 'w', atomictemp=True)
106 f.write('C')
112 f.write('C')
107 f.close()
113 f.close()
108
114
109 repo.invalidate()
115 repo.invalidate()
110 print("* both files changed inode")
116 print("* both files changed inode")
111 repo.cached
117 repo.cached
112
118
113 def fakeuncacheable():
119 def fakeuncacheable():
114 def wrapcacheable(orig, *args, **kwargs):
120 def wrapcacheable(orig, *args, **kwargs):
115 return False
121 return False
116
122
117 def wrapinit(orig, *args, **kwargs):
123 def wrapinit(orig, *args, **kwargs):
118 pass
124 pass
119
125
120 originit = extensions.wrapfunction(util.cachestat, '__init__', wrapinit)
126 originit = extensions.wrapfunction(util.cachestat, '__init__', wrapinit)
121 origcacheable = extensions.wrapfunction(util.cachestat, 'cacheable',
127 origcacheable = extensions.wrapfunction(util.cachestat, 'cacheable',
122 wrapcacheable)
128 wrapcacheable)
123
129
124 for fn in ['x', 'y']:
130 for fn in ['x', 'y']:
125 try:
131 try:
126 os.remove(fn)
132 os.remove(fn)
127 except OSError:
133 except OSError:
128 pass
134 pass
129
135
130 basic(fakerepo())
136 basic(fakerepo())
131
137
132 util.cachestat.cacheable = origcacheable
138 util.cachestat.cacheable = origcacheable
133 util.cachestat.__init__ = originit
139 util.cachestat.__init__ = originit
134
140
135 def test_filecache_synced():
141 def test_filecache_synced():
136 # test old behavior that caused filecached properties to go out of sync
142 # test old behavior that caused filecached properties to go out of sync
137 os.system('hg init && echo a >> a && hg ci -qAm.')
143 os.system('hg init && echo a >> a && hg ci -qAm.')
138 repo = hg.repository(ui.ui())
144 repo = hg.repository(ui.ui())
139 # first rollback clears the filecache, but changelog to stays in __dict__
145 # first rollback clears the filecache, but changelog to stays in __dict__
140 repo.rollback()
146 repo.rollback()
141 repo.commit('.')
147 repo.commit('.')
142 # second rollback comes along and touches the changelog externally
148 # second rollback comes along and touches the changelog externally
143 # (file is moved)
149 # (file is moved)
144 repo.rollback()
150 repo.rollback()
145 # but since changelog isn't under the filecache control anymore, we don't
151 # but since changelog isn't under the filecache control anymore, we don't
146 # see that it changed, and return the old changelog without reconstructing
152 # see that it changed, and return the old changelog without reconstructing
147 # it
153 # it
148 repo.commit('.')
154 repo.commit('.')
149
155
150 def setbeforeget(repo):
156 def setbeforeget(repo):
151 os.remove('x')
157 os.remove('x')
152 os.remove('y')
158 os.remove('y')
153 repo.cached = 'string set externally'
159 repo.cached = 'string set externally'
154 repo.invalidate()
160 repo.invalidate()
155 print("* neither file exists")
161 print("* neither file exists")
156 print(repo.cached)
162 print(repo.cached)
157 repo.invalidate()
163 repo.invalidate()
158 f = open('x', 'w')
164 f = open('x', 'w')
159 f.write('a')
165 f.write('a')
160 f.close()
166 f.close()
161 print("* file x created")
167 print("* file x created")
162 print(repo.cached)
168 print(repo.cached)
163
169
164 repo.cached = 'string 2 set externally'
170 repo.cached = 'string 2 set externally'
165 repo.invalidate()
171 repo.invalidate()
166 print("* string set externally again")
172 print("* string set externally again")
167 print(repo.cached)
173 print(repo.cached)
168
174
169 repo.invalidate()
175 repo.invalidate()
170 f = open('y', 'w')
176 f = open('y', 'w')
171 f.write('b')
177 f.write('b')
172 f.close()
178 f.close()
173 print("* file y created")
179 print("* file y created")
174 print(repo.cached)
180 print(repo.cached)
175
181
176 print('basic:')
182 print('basic:')
177 print()
183 print()
178 basic(fakerepo())
184 basic(fakerepo())
179 print()
185 print()
180 print('fakeuncacheable:')
186 print('fakeuncacheable:')
181 print()
187 print()
182 fakeuncacheable()
188 fakeuncacheable()
183 test_filecache_synced()
189 test_filecache_synced()
184 print()
190 print()
185 print('setbeforeget:')
191 print('setbeforeget:')
186 print()
192 print()
187 setbeforeget(fakerepo())
193 setbeforeget(fakerepo())
General Comments 0
You need to be logged in to leave comments. Login now