test-context.py
249 lines
| 6.1 KiB
| text/x-python
|
PythonLexer
/ tests / test-context.py
|
r28738 | from __future__ import absolute_import, print_function | ||
|
r4110 | import os | ||
|
r36799 | import stat | ||
|
r37941 | import sys | ||
|
r32518 | from mercurial.node import hex | ||
|
r28735 | from mercurial import ( | ||
|
r28738 | context, | ||
|
r38607 | diffutil, | ||
|
r28738 | encoding, | ||
|
r28735 | hg, | ||
|
r32518 | scmutil, | ||
|
r28775 | ui as uimod, | ||
|
r28735 | ) | ||
|
r4110 | |||
|
r37941 | print_ = print | ||
|
r43346 | |||
|
r37941 | def print(*args, **kwargs): | ||
"""print() wrapper that flushes stdout buffers to avoid py3 buffer issues | ||||
We could also just write directly to sys.stdout.buffer the way the | ||||
ui object will, but this was easier for porting the test. | ||||
""" | ||||
print_(*args, **kwargs) | ||||
sys.stdout.flush() | ||||
|
r43346 | |||
|
r37941 | def printb(data, end=b'\n'): | ||
out = getattr(sys.stdout, 'buffer', sys.stdout) | ||||
out.write(data + end) | ||||
out.flush() | ||||
|
r43346 | |||
|
r42223 | ui = uimod.ui.load() | ||
|
r4110 | |||
|
r42223 | repo = hg.repository(ui, b'test1', create=1) | ||
|
r4110 | os.chdir('test1') | ||
# create 'foo' with fixed time stamp | ||||
|
r23060 | f = open('foo', 'wb') | ||
|
r29187 | f.write(b'foo\n') | ||
|
r4110 | f.close() | ||
os.utime('foo', (1000, 1000)) | ||||
# add+commit 'foo' | ||||
|
r37941 | repo[None].add([b'foo']) | ||
repo.commit(text=b'commit1', date=b"0 0") | ||||
|
r4110 | |||
|
r37941 | d = repo[None][b'foo'].date() | ||
|
r27056 | if os.name == 'nt': | ||
|
r33797 | d = d[:2] | ||
print("workingfilectx.date = (%d, %d)" % d) | ||||
|
r14379 | |||
# test memctx with non-ASCII commit message | ||||
|
r43346 | |||
|
r14379 | def filectxfn(repo, memctx, path): | ||
|
r37941 | return context.memfilectx(repo, memctx, b"foo", b"") | ||
|
r14379 | |||
|
r43346 | |||
ctx = context.memctx( | ||||
repo, | ||||
[b'tip', None], | ||||
encoding.tolocal(b"Gr\xc3\xbcezi!"), | ||||
[b"foo"], | ||||
filectxfn, | ||||
) | ||||
|
r14379 | ctx.commit() | ||
for enc in "ASCII", "Latin-1", "UTF-8": | ||||
encoding.encoding = enc | ||||
|
r37941 | printb(b"%-8s: %s" % (enc.encode('ascii'), repo[b"tip"].description())) | ||
|
r21836 | |||
# test performing a status | ||||
|
r43346 | |||
|
r21836 | def getfilectx(repo, memctx, f): | ||
|
r41442 | fctx = memctx.p1()[f] | ||
|
r21836 | data, flags = fctx.data(), fctx.flags() | ||
|
r37941 | if f == b'foo': | ||
data += b'bar\n' | ||||
return context.memfilectx( | ||||
|
r43346 | repo, memctx, f, data, b'l' in flags, b'x' in flags | ||
) | ||||
|
r21836 | |||
|
r37320 | ctxa = repo[0] | ||
|
r43346 | ctxb = context.memctx( | ||
repo, | ||||
[ctxa.node(), None], | ||||
b"test diff", | ||||
[b"foo"], | ||||
getfilectx, | ||||
ctxa.user(), | ||||
ctxa.date(), | ||||
) | ||||
|
r21836 | |||
|
r28738 | print(ctxb.status(ctxa)) | ||
|
r21837 | |||
# test performing a diff on a memctx | ||||
|
r38680 | diffopts = diffutil.diffallopts(repo.ui, {b'git': True}) | ||
|
r38582 | for d in ctxb.diff(ctxa, opts=diffopts): | ||
|
r37941 | printb(d, end=b'') | ||
|
r23700 | |||
|
r24180 | # test safeness and correctness of "ctx.status()" | ||
|
r28738 | print('= checking context.status():') | ||
|
r23700 | |||
# ancestor "wcctx ~ 2" | ||||
|
r37941 | actx2 = repo[b'.'] | ||
|
r23700 | |||
|
r37941 | repo.wwrite(b'bar-m', b'bar-m\n', b'') | ||
repo.wwrite(b'bar-r', b'bar-r\n', b'') | ||||
repo[None].add([b'bar-m', b'bar-r']) | ||||
repo.commit(text=b'add bar-m, bar-r', date=b"0 0") | ||||
|
r23700 | |||
# ancestor "wcctx ~ 1" | ||||
|
r37941 | actx1 = repo[b'.'] | ||
|
r23700 | |||
|
r37941 | repo.wwrite(b'bar-m', b'bar-m bar-m\n', b'') | ||
repo.wwrite(b'bar-a', b'bar-a\n', b'') | ||||
repo[None].add([b'bar-a']) | ||||
repo[None].forget([b'bar-r']) | ||||
|
r23700 | |||
# status at this point: | ||||
# M bar-m | ||||
# A bar-a | ||||
# R bar-r | ||||
# C foo | ||||
from mercurial import scmutil | ||||
|
r28738 | print('== checking workingctx.status:') | ||
|
r23700 | |||
wctx = repo[None] | ||||
|
r28738 | print('wctx._status=%s' % (str(wctx._status))) | ||
|
r23700 | |||
|
r28738 | print('=== with "pattern match":') | ||
|
r43346 | print( | ||
actx1.status(other=wctx, match=scmutil.matchfiles(repo, [b'bar-m', b'foo'])) | ||||
) | ||||
|
r28738 | print('wctx._status=%s' % (str(wctx._status))) | ||
|
r43346 | print( | ||
actx2.status(other=wctx, match=scmutil.matchfiles(repo, [b'bar-m', b'foo'])) | ||||
) | ||||
|
r28738 | print('wctx._status=%s' % (str(wctx._status))) | ||
|
r23709 | |||
|
r28738 | print('=== with "always match" and "listclean=True":') | ||
print(actx1.status(other=wctx, listclean=True)) | ||||
print('wctx._status=%s' % (str(wctx._status))) | ||||
print(actx2.status(other=wctx, listclean=True)) | ||||
print('wctx._status=%s' % (str(wctx._status))) | ||||
|
r23711 | |||
|
r28738 | print("== checking workingcommitctx.status:") | ||
|
r23711 | |||
|
r43346 | wcctx = context.workingcommitctx( | ||
repo, | ||||
scmutil.status([b'bar-m'], [b'bar-a'], [], [], [], [], []), | ||||
text=b'', | ||||
date=b'0 0', | ||||
) | ||||
|
r28738 | print('wcctx._status=%s' % (str(wcctx._status))) | ||
|
r23711 | |||
|
r28738 | print('=== with "always match":') | ||
print(actx1.status(other=wcctx)) | ||||
print('wcctx._status=%s' % (str(wcctx._status))) | ||||
print(actx2.status(other=wcctx)) | ||||
print('wcctx._status=%s' % (str(wcctx._status))) | ||||
|
r23711 | |||
|
r28738 | print('=== with "always match" and "listclean=True":') | ||
print(actx1.status(other=wcctx, listclean=True)) | ||||
print('wcctx._status=%s' % (str(wcctx._status))) | ||||
print(actx2.status(other=wcctx, listclean=True)) | ||||
print('wcctx._status=%s' % (str(wcctx._status))) | ||||
|
r23712 | |||
|
r28738 | print('=== with "pattern match":') | ||
|
r43346 | print( | ||
actx1.status( | ||||
other=wcctx, match=scmutil.matchfiles(repo, [b'bar-m', b'foo']) | ||||
) | ||||
) | ||||
|
r28738 | print('wcctx._status=%s' % (str(wcctx._status))) | ||
|
r43346 | print( | ||
actx2.status( | ||||
other=wcctx, match=scmutil.matchfiles(repo, [b'bar-m', b'foo']) | ||||
) | ||||
) | ||||
|
r28738 | print('wcctx._status=%s' % (str(wcctx._status))) | ||
|
r23712 | |||
|
r28738 | print('=== with "pattern match" and "listclean=True":') | ||
|
r43346 | print( | ||
actx1.status( | ||||
other=wcctx, | ||||
match=scmutil.matchfiles(repo, [b'bar-r', b'foo']), | ||||
listclean=True, | ||||
) | ||||
) | ||||
|
r28738 | print('wcctx._status=%s' % (str(wcctx._status))) | ||
|
r43346 | print( | ||
actx2.status( | ||||
other=wcctx, | ||||
match=scmutil.matchfiles(repo, [b'bar-r', b'foo']), | ||||
listclean=True, | ||||
) | ||||
) | ||||
|
r28738 | print('wcctx._status=%s' % (str(wcctx._status))) | ||
|
r32518 | |||
os.chdir('..') | ||||
# test manifestlog being changed | ||||
print('== commit with manifestlog invalidated') | ||||
|
r42223 | repo = hg.repository(ui, b'test2', create=1) | ||
|
r32518 | os.chdir('test2') | ||
# make some commits | ||||
for i in [b'1', b'2', b'3']: | ||||
with open(i, 'wb') as f: | ||||
f.write(i) | ||||
status = scmutil.status([], [i], [], [], [], [], []) | ||||
|
r43346 | ctx = context.workingcommitctx( | ||
repo, status, text=i, user=b'test@test.com', date=(0, 0) | ||||
) | ||||
ctx.p1().manifest() # side effect: cache manifestctx | ||||
|
r32518 | n = repo.commitctx(ctx) | ||
|
r37941 | printb(b'commit %s: %s' % (i, hex(n))) | ||
|
r32518 | |||
# touch 00manifest.i mtime so storecache could expire. | ||||
# repo.__dict__['manifestlog'] is deleted by transaction releasefn. | ||||
|
r37941 | st = repo.svfs.stat(b'00manifest.i') | ||
|
r43346 | repo.svfs.utime( | ||
b'00manifest.i', (st[stat.ST_MTIME] + 1, st[stat.ST_MTIME] + 1) | ||||
) | ||||
|
r32518 | |||
# read the file just committed | ||||
try: | ||||
if repo[n][i].data() != i: | ||||
print('data mismatch') | ||||
except Exception as ex: | ||||
print('cannot read data: %r' % ex) | ||||
|
r33672 | |||
|
r37941 | with repo.wlock(), repo.lock(), repo.transaction(b'test'): | ||
|
r33672 | with open(b'4', 'wb') as f: | ||
f.write(b'4') | ||||
|
r37941 | repo.dirstate.normal(b'4') | ||
repo.commit(b'4') | ||||
|
r33672 | revsbefore = len(repo.changelog) | ||
repo.invalidate(clearfilecache=True) | ||||
revsafter = len(repo.changelog) | ||||
if revsbefore != revsafter: | ||||
print('changeset lost by repo.invalidate()') | ||||