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