author | Matt Mackall <mpm@selenic.com> |
Mon, 25 Jul 2011 12:58:47 -0500 | |
changeset 14937 | 0b3e57c1b8c0 |
parent 14928 | dca59d5be12d |
child 14982 | 4c01478991a3 |
permissions | -rw-r--r-- |
14928 | 1 |
import sys, os, subprocess |
2 |
||
3 |
try: |
|
4 |
subprocess.check_call(['%s/hghave' % os.environ['TESTDIR'], 'cacheable']) |
|
5 |
except subprocess.CalledProcessError: |
|
6 |
sys.exit(80) |
|
7 |
||
8 |
from mercurial import util, scmutil, extensions |
|
9 |
||
10 |
filecache = scmutil.filecache |
|
11 |
||
12 |
class fakerepo(object): |
|
13 |
def __init__(self): |
|
14 |
self._filecache = {} |
|
15 |
||
16 |
def join(self, p): |
|
17 |
return p |
|
18 |
||
19 |
def sjoin(self, p): |
|
20 |
return p |
|
21 |
||
22 |
@filecache('x') |
|
23 |
def cached(self): |
|
24 |
print 'creating' |
|
25 |
||
26 |
def invalidate(self): |
|
27 |
for k in self._filecache: |
|
28 |
try: |
|
29 |
delattr(self, k) |
|
30 |
except AttributeError: |
|
31 |
pass |
|
32 |
||
33 |
def basic(repo): |
|
34 |
# file doesn't exist, calls function |
|
35 |
repo.cached |
|
36 |
||
37 |
repo.invalidate() |
|
38 |
# file still doesn't exist, uses cache |
|
39 |
repo.cached |
|
40 |
||
41 |
# create empty file |
|
42 |
f = open('x', 'w') |
|
43 |
f.close() |
|
44 |
repo.invalidate() |
|
45 |
# should recreate the object |
|
46 |
repo.cached |
|
47 |
||
48 |
f = open('x', 'w') |
|
49 |
f.write('a') |
|
50 |
f.close() |
|
51 |
repo.invalidate() |
|
52 |
# should recreate the object |
|
53 |
repo.cached |
|
54 |
||
55 |
repo.invalidate() |
|
56 |
# stats file again, nothing changed, reuses object |
|
57 |
repo.cached |
|
58 |
||
59 |
# atomic replace file, size doesn't change |
|
60 |
# hopefully st_mtime doesn't change as well so this doesn't use the cache |
|
61 |
# because of inode change |
|
62 |
f = scmutil.opener('.')('x', 'w', atomictemp=True) |
|
63 |
f.write('b') |
|
64 |
f.rename() |
|
65 |
||
66 |
repo.invalidate() |
|
67 |
repo.cached |
|
68 |
||
69 |
def fakeuncacheable(): |
|
70 |
def wrapcacheable(orig, *args, **kwargs): |
|
71 |
return False |
|
72 |
||
73 |
def wrapinit(orig, *args, **kwargs): |
|
74 |
pass |
|
75 |
||
76 |
originit = extensions.wrapfunction(util.cachestat, '__init__', wrapinit) |
|
14937
0b3e57c1b8c0
filecache: fix check-code complaint
Matt Mackall <mpm@selenic.com>
parents:
14928
diff
changeset
|
77 |
origcacheable = extensions.wrapfunction(util.cachestat, 'cacheable', |
0b3e57c1b8c0
filecache: fix check-code complaint
Matt Mackall <mpm@selenic.com>
parents:
14928
diff
changeset
|
78 |
wrapcacheable) |
14928 | 79 |
|
80 |
try: |
|
81 |
os.remove('x') |
|
82 |
except: |
|
83 |
pass |
|
84 |
||
85 |
basic(fakerepo()) |
|
86 |
||
87 |
util.cachestat.cacheable = origcacheable |
|
88 |
util.cachestat.__init__ = originit |
|
89 |
||
90 |
print 'basic:' |
|
91 |
||
92 |
basic(fakerepo()) |
|
93 |
||
94 |
print 'fakeuncacheable:' |
|
95 |
||
96 |
fakeuncacheable() |