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