Mercurial > public > mercurial-scm > hg-stable
annotate tests/test-filecache.py @ 20040:ed80cecdfc57
test-filecache.py: make setbeforeget test clearer
'0' and 'None' as outputs tripped me up. Make the distinction between values
set externally and values computed by calling the decorated function clearer.
author | Siddharth Agarwal <sid0@fb.com> |
---|---|
date | Sat, 16 Nov 2013 14:10:28 -0800 |
parents | f36375576ed5 |
children | 42deff43460a |
rev | line source |
---|---|
14928 | 1 import sys, os, subprocess |
2 | |
16683 | 3 if subprocess.call(['python', '%s/hghave' % os.environ['TESTDIR'], |
4 'cacheable']): | |
14928 | 5 sys.exit(80) |
6 | |
18313
3e4a944c0d04
destroyed: keep the filecache in sync with __dict__ (issue3335) (issue3693) (issue3743)
Idan Kamara <idankk86@gmail.com>
parents:
16688
diff
changeset
|
7 from mercurial import util, scmutil, extensions, hg, ui |
14928 | 8 |
9 filecache = scmutil.filecache | |
10 | |
11 class fakerepo(object): | |
12 def __init__(self): | |
13 self._filecache = {} | |
14 | |
15 def join(self, p): | |
16 return p | |
17 | |
18 def sjoin(self, p): | |
19 return p | |
20 | |
21 @filecache('x') | |
22 def cached(self): | |
23 print 'creating' | |
20040
ed80cecdfc57
test-filecache.py: make setbeforeget test clearer
Siddharth Agarwal <sid0@fb.com>
parents:
18316
diff
changeset
|
24 return 'string from function' |
14928 | 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') | |
15057
774da7121fc9
atomictempfile: make close() consistent with other file-like objects.
Greg Ward <greg@gerg.ca>
parents:
14982
diff
changeset
|
64 f.close() |
14928 | 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') | |
16688
cfb6682961b8
cleanup: replace naked excepts with more specific ones
Brodie Rao <brodie@sf.io>
parents:
16683
diff
changeset
|
82 except OSError: |
14928 | 83 pass |
84 | |
85 basic(fakerepo()) | |
86 | |
87 util.cachestat.cacheable = origcacheable | |
88 util.cachestat.__init__ = originit | |
89 | |
18313
3e4a944c0d04
destroyed: keep the filecache in sync with __dict__ (issue3335) (issue3693) (issue3743)
Idan Kamara <idankk86@gmail.com>
parents:
16688
diff
changeset
|
90 def test_filecache_synced(): |
3e4a944c0d04
destroyed: keep the filecache in sync with __dict__ (issue3335) (issue3693) (issue3743)
Idan Kamara <idankk86@gmail.com>
parents:
16688
diff
changeset
|
91 # test old behaviour that caused filecached properties to go out of sync |
3e4a944c0d04
destroyed: keep the filecache in sync with __dict__ (issue3335) (issue3693) (issue3743)
Idan Kamara <idankk86@gmail.com>
parents:
16688
diff
changeset
|
92 os.system('hg init && echo a >> a && hg ci -qAm.') |
3e4a944c0d04
destroyed: keep the filecache in sync with __dict__ (issue3335) (issue3693) (issue3743)
Idan Kamara <idankk86@gmail.com>
parents:
16688
diff
changeset
|
93 repo = hg.repository(ui.ui()) |
3e4a944c0d04
destroyed: keep the filecache in sync with __dict__ (issue3335) (issue3693) (issue3743)
Idan Kamara <idankk86@gmail.com>
parents:
16688
diff
changeset
|
94 # first rollback clears the filecache, but changelog to stays in __dict__ |
3e4a944c0d04
destroyed: keep the filecache in sync with __dict__ (issue3335) (issue3693) (issue3743)
Idan Kamara <idankk86@gmail.com>
parents:
16688
diff
changeset
|
95 repo.rollback() |
3e4a944c0d04
destroyed: keep the filecache in sync with __dict__ (issue3335) (issue3693) (issue3743)
Idan Kamara <idankk86@gmail.com>
parents:
16688
diff
changeset
|
96 repo.commit('.') |
3e4a944c0d04
destroyed: keep the filecache in sync with __dict__ (issue3335) (issue3693) (issue3743)
Idan Kamara <idankk86@gmail.com>
parents:
16688
diff
changeset
|
97 # second rollback comes along and touches the changelog externally |
3e4a944c0d04
destroyed: keep the filecache in sync with __dict__ (issue3335) (issue3693) (issue3743)
Idan Kamara <idankk86@gmail.com>
parents:
16688
diff
changeset
|
98 # (file is moved) |
3e4a944c0d04
destroyed: keep the filecache in sync with __dict__ (issue3335) (issue3693) (issue3743)
Idan Kamara <idankk86@gmail.com>
parents:
16688
diff
changeset
|
99 repo.rollback() |
3e4a944c0d04
destroyed: keep the filecache in sync with __dict__ (issue3335) (issue3693) (issue3743)
Idan Kamara <idankk86@gmail.com>
parents:
16688
diff
changeset
|
100 # but since changelog isn't under the filecache control anymore, we don't |
3e4a944c0d04
destroyed: keep the filecache in sync with __dict__ (issue3335) (issue3693) (issue3743)
Idan Kamara <idankk86@gmail.com>
parents:
16688
diff
changeset
|
101 # see that it changed, and return the old changelog without reconstructing |
3e4a944c0d04
destroyed: keep the filecache in sync with __dict__ (issue3335) (issue3693) (issue3743)
Idan Kamara <idankk86@gmail.com>
parents:
16688
diff
changeset
|
102 # it |
3e4a944c0d04
destroyed: keep the filecache in sync with __dict__ (issue3335) (issue3693) (issue3743)
Idan Kamara <idankk86@gmail.com>
parents:
16688
diff
changeset
|
103 repo.commit('.') |
3e4a944c0d04
destroyed: keep the filecache in sync with __dict__ (issue3335) (issue3693) (issue3743)
Idan Kamara <idankk86@gmail.com>
parents:
16688
diff
changeset
|
104 |
18316
f36375576ed5
filecache: create an entry in _filecache when __set__ is called for a missing one
Idan Kamara <idankk86@gmail.com>
parents:
18313
diff
changeset
|
105 def setbeforeget(repo): |
f36375576ed5
filecache: create an entry in _filecache when __set__ is called for a missing one
Idan Kamara <idankk86@gmail.com>
parents:
18313
diff
changeset
|
106 os.remove('x') |
20040
ed80cecdfc57
test-filecache.py: make setbeforeget test clearer
Siddharth Agarwal <sid0@fb.com>
parents:
18316
diff
changeset
|
107 repo.cached = 'string set externally' |
18316
f36375576ed5
filecache: create an entry in _filecache when __set__ is called for a missing one
Idan Kamara <idankk86@gmail.com>
parents:
18313
diff
changeset
|
108 repo.invalidate() |
f36375576ed5
filecache: create an entry in _filecache when __set__ is called for a missing one
Idan Kamara <idankk86@gmail.com>
parents:
18313
diff
changeset
|
109 print repo.cached |
f36375576ed5
filecache: create an entry in _filecache when __set__ is called for a missing one
Idan Kamara <idankk86@gmail.com>
parents:
18313
diff
changeset
|
110 repo.invalidate() |
f36375576ed5
filecache: create an entry in _filecache when __set__ is called for a missing one
Idan Kamara <idankk86@gmail.com>
parents:
18313
diff
changeset
|
111 f = open('x', 'w') |
f36375576ed5
filecache: create an entry in _filecache when __set__ is called for a missing one
Idan Kamara <idankk86@gmail.com>
parents:
18313
diff
changeset
|
112 f.write('a') |
f36375576ed5
filecache: create an entry in _filecache when __set__ is called for a missing one
Idan Kamara <idankk86@gmail.com>
parents:
18313
diff
changeset
|
113 f.close() |
f36375576ed5
filecache: create an entry in _filecache when __set__ is called for a missing one
Idan Kamara <idankk86@gmail.com>
parents:
18313
diff
changeset
|
114 print repo.cached |
f36375576ed5
filecache: create an entry in _filecache when __set__ is called for a missing one
Idan Kamara <idankk86@gmail.com>
parents:
18313
diff
changeset
|
115 |
14928 | 116 print 'basic:' |
117 print | |
118 basic(fakerepo()) | |
119 print | |
120 print 'fakeuncacheable:' | |
121 print | |
122 fakeuncacheable() | |
18313
3e4a944c0d04
destroyed: keep the filecache in sync with __dict__ (issue3335) (issue3693) (issue3743)
Idan Kamara <idankk86@gmail.com>
parents:
16688
diff
changeset
|
123 test_filecache_synced() |
18316
f36375576ed5
filecache: create an entry in _filecache when __set__ is called for a missing one
Idan Kamara <idankk86@gmail.com>
parents:
18313
diff
changeset
|
124 print |
f36375576ed5
filecache: create an entry in _filecache when __set__ is called for a missing one
Idan Kamara <idankk86@gmail.com>
parents:
18313
diff
changeset
|
125 print 'setbeforeget:' |
f36375576ed5
filecache: create an entry in _filecache when __set__ is called for a missing one
Idan Kamara <idankk86@gmail.com>
parents:
18313
diff
changeset
|
126 print |
f36375576ed5
filecache: create an entry in _filecache when __set__ is called for a missing one
Idan Kamara <idankk86@gmail.com>
parents:
18313
diff
changeset
|
127 setbeforeget(fakerepo()) |