Debian Codegraph @

tests/test-context.py

raw
1 import os
2 import stat
3 import sys
4 from mercurial.node import hex
5 from mercurial import (
6 context,
7 diffutil,
8 encoding,
9 scmutil,
10 ui as uimod,
11 )
12 from mercurial.repo import factory
13
14 print_ = print
15
16
17 def print(*args, **kwargs):
18 """print() wrapper that flushes stdout buffers to avoid py3 buffer issues
19
20 We could also just write directly to sys.stdout.buffer the way the
21 ui object will, but this was easier for porting the test.
22 """
23 print_(*args, **kwargs)
24 sys.stdout.flush()
25
26
27 def printb(data, end=b'\n'):
28 out = getattr(sys.stdout, 'buffer', sys.stdout)
29 out.write(data + end)
30 out.flush()
31
32
33 ui = uimod.ui.load()
34
35 repo = factory.repository(ui, b'test1', create=1)
36 os.chdir('test1')
37
38 # create 'foo' with fixed time stamp
39 f = open('foo', 'wb')
40 f.write(b'foo\n')
41 f.close()
42 os.utime('foo', (1000, 1000))
43
44 # add+commit 'foo'
45 with repo.wlock(), repo.lock(), repo.transaction(b'test-context'):
46 with repo.dirstate.changing_files(repo):
47 repo[None].add([b'foo'])
48 repo.commit(text=b'commit1', date=b"0 0")
49
50 d = repo[None][b'foo'].date()
51 if os.name == 'nt':
52 d = d[:2]
53 print("workingfilectx.date = (%d, %d)" % d)
54
55 # test memctx with non-ASCII commit message
56
57
58 def filectxfn(repo, memctx, path):
59 return context.memfilectx(repo, memctx, b"foo", b"")
60
61
62 ctx = context.memctx(
63 repo,
64 [b'tip', None],
65 encoding.tolocal(b"Gr\xc3\xbcezi!"),
66 [b"foo"],
67 filectxfn,
68 )
69 ctx.commit()
70 for enc in "ASCII", "Latin-1", "UTF-8":
71 encoding.encoding = enc
72 printb(b"%-8s: %s" % (enc.encode('ascii'), repo[b"tip"].description()))
73
74 # test performing a status
75
76
77 def getfilectx(repo, memctx, f):
78 fctx = memctx.p1()[f]
79 data, flags = fctx.data(), fctx.flags()
80 if f == b'foo':
81 data += b'bar\n'
82 return context.memfilectx(
83 repo, memctx, f, data, b'l' in flags, b'x' in flags
84 )
85
86
87 ctxa = repo[0]
88 ctxb = context.memctx(
89 repo,
90 [ctxa.node(), None],
91 b"test diff",
92 [b"foo"],
93 getfilectx,
94 ctxa.user(),
95 ctxa.date(),
96 )
97
98 print(ctxb.status(ctxa))
99
100 # test performing a diff on a memctx
101 diffopts = diffutil.diffallopts(repo.ui, {b'git': True})
102 for d in ctxb.diff(ctxa, opts=diffopts):
103 printb(d, end=b'')
104
105 # test safeness and correctness of "ctx.status()"
106 print('= checking context.status():')
107
108 # ancestor "wcctx ~ 2"
109 actx2 = repo[b'.']
110
111 repo.wwrite(b'bar-m', b'bar-m\n', b'')
112 repo.wwrite(b'bar-r', b'bar-r\n', b'')
113 with repo.wlock(), repo.lock(), repo.transaction(b'test-context'):
114 with repo.dirstate.changing_files(repo):
115 repo[None].add([b'bar-m', b'bar-r'])
116 repo.commit(text=b'add bar-m, bar-r', date=b"0 0")
117
118 # ancestor "wcctx ~ 1"
119 actx1 = repo[b'.']
120
121 repo.wwrite(b'bar-m', b'bar-m bar-m\n', b'')
122 repo.wwrite(b'bar-a', b'bar-a\n', b'')
123 with repo.wlock(), repo.lock(), repo.transaction(b'test-context'):
124 with repo.dirstate.changing_files(repo):
125 repo[None].add([b'bar-a'])
126 repo[None].forget([b'bar-r'])
127
128 # status at this point:
129 # M bar-m
130 # A bar-a
131 # R bar-r
132 # C foo
133
134 print('== checking workingctx.status:')
135
136 wctx = repo[None]
137 print('wctx._status=%s' % (str(wctx._status)))
138
139 print('=== with "pattern match":')
140 print(
141 actx1.status(other=wctx, match=scmutil.matchfiles(repo, [b'bar-m', b'foo']))
142 )
143 print('wctx._status=%s' % (str(wctx._status)))
144 print(
145 actx2.status(other=wctx, match=scmutil.matchfiles(repo, [b'bar-m', b'foo']))
146 )
147 print('wctx._status=%s' % (str(wctx._status)))
148
149 print('=== with "always match" and "listclean=True":')
150 print(actx1.status(other=wctx, listclean=True))
151 print('wctx._status=%s' % (str(wctx._status)))
152 print(actx2.status(other=wctx, listclean=True))
153 print('wctx._status=%s' % (str(wctx._status)))
154
155 print("== checking workingcommitctx.status:")
156
157 wcctx = context.workingcommitctx(
158 repo,
159 scmutil.status([b'bar-m'], [b'bar-a'], [], [], [], [], []),
160 text=b'',
161 date=b'0 0',
162 )
163 print('wcctx._status=%s' % (str(wcctx._status)))
164
165 print('=== with "always match":')
166 print(actx1.status(other=wcctx))
167 print('wcctx._status=%s' % (str(wcctx._status)))
168 print(actx2.status(other=wcctx))
169 print('wcctx._status=%s' % (str(wcctx._status)))
170
171 print('=== with "always match" and "listclean=True":')
172 print(actx1.status(other=wcctx, listclean=True))
173 print('wcctx._status=%s' % (str(wcctx._status)))
174 print(actx2.status(other=wcctx, listclean=True))
175 print('wcctx._status=%s' % (str(wcctx._status)))
176
177 print('=== with "pattern match":')
178 print(
179 actx1.status(
180 other=wcctx, match=scmutil.matchfiles(repo, [b'bar-m', b'foo'])
181 )
182 )
183 print('wcctx._status=%s' % (str(wcctx._status)))
184 print(
185 actx2.status(
186 other=wcctx, match=scmutil.matchfiles(repo, [b'bar-m', b'foo'])
187 )
188 )
189 print('wcctx._status=%s' % (str(wcctx._status)))
190
191 print('=== with "pattern match" and "listclean=True":')
192 print(
193 actx1.status(
194 other=wcctx,
195 match=scmutil.matchfiles(repo, [b'bar-r', b'foo']),
196 listclean=True,
197 )
198 )
199 print('wcctx._status=%s' % (str(wcctx._status)))
200 print(
201 actx2.status(
202 other=wcctx,
203 match=scmutil.matchfiles(repo, [b'bar-r', b'foo']),
204 listclean=True,
205 )
206 )
207 print('wcctx._status=%s' % (str(wcctx._status)))
208
209 os.chdir('..')
210
211 # test manifestlog being changed
212 print('== commit with manifestlog invalidated')
213
214 repo = factory.repository(ui, b'test2', create=1)
215 os.chdir('test2')
216
217 # make some commits
218 for i in [b'1', b'2', b'3']:
219 with open(i, 'wb') as f:
220 f.write(i)
221 status = scmutil.status([], [i], [], [], [], [], [])
222 ctx = context.workingcommitctx(
223 repo, status, text=i, user=b'[email protected]', date=(0, 0)
224 )
225 ctx.p1().manifest() # side effect: cache manifestctx
226 n = repo.commitctx(ctx)
227 printb(b'commit %s: %s' % (i, hex(n)))
228
229 # touch 00manifest.i mtime so storecache could expire.
230 # repo.__dict__['manifestlog'] is deleted by transaction releasefn.
231 st = repo.svfs.stat(b'00manifest.i')
232 repo.svfs.utime(
233 b'00manifest.i', (st[stat.ST_MTIME] + 1, st[stat.ST_MTIME] + 1)
234 )
235
236 # read the file just committed
237 try:
238 if repo[n][i].data() != i:
239 print('data mismatch')
240 except Exception as ex:
241 print('cannot read data: %r' % ex)
242
243 with repo.wlock(), repo.lock(), repo.transaction(b'test'):
244 with open(b'4', 'wb') as f:
245 f.write(b'4')
246 with repo.dirstate.changing_files(repo):
247 repo.dirstate.set_tracked(b'4')
248 repo.commit(b'4')
249 revsbefore = len(repo.changelog)
250 repo.invalidate(clearfilecache=True)
251 revsafter = len(repo.changelog)
252 if revsbefore != revsafter:
253 print('changeset lost by repo.invalidate()')