test-context.py
249 lines
| 6.1 KiB
| text/x-python
|
PythonLexer
/ tests / test-context.py
Robert Stanca
|
r28738 | from __future__ import absolute_import, print_function | ||
Thomas Arendsen Hein
|
r4110 | import os | ||
Augie Fackler
|
r36799 | import stat | ||
Augie Fackler
|
r37941 | import sys | ||
Jun Wu
|
r32518 | from mercurial.node import hex | ||
Robert Stanca
|
r28735 | from mercurial import ( | ||
Robert Stanca
|
r28738 | context, | ||
Yuya Nishihara
|
r38607 | diffutil, | ||
Robert Stanca
|
r28738 | encoding, | ||
Robert Stanca
|
r28735 | hg, | ||
Jun Wu
|
r32518 | scmutil, | ||
Yuya Nishihara
|
r28775 | ui as uimod, | ||
Robert Stanca
|
r28735 | ) | ||
Thomas Arendsen Hein
|
r4110 | |||
Augie Fackler
|
r37941 | print_ = print | ||
Augie Fackler
|
r43346 | |||
Augie Fackler
|
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() | ||||
Augie Fackler
|
r43346 | |||
Augie Fackler
|
r37941 | def printb(data, end=b'\n'): | ||
out = getattr(sys.stdout, 'buffer', sys.stdout) | ||||
out.write(data + end) | ||||
out.flush() | ||||
Augie Fackler
|
r43346 | |||
Martin von Zweigbergk
|
r42223 | ui = uimod.ui.load() | ||
Thomas Arendsen Hein
|
r4110 | |||
Martin von Zweigbergk
|
r42223 | repo = hg.repository(ui, b'test1', create=1) | ||
Thomas Arendsen Hein
|
r4110 | os.chdir('test1') | ||
# create 'foo' with fixed time stamp | ||||
FUJIWARA Katsunori
|
r23060 | f = open('foo', 'wb') | ||
timeless
|
r29187 | f.write(b'foo\n') | ||
Thomas Arendsen Hein
|
r4110 | f.close() | ||
os.utime('foo', (1000, 1000)) | ||||
# add+commit 'foo' | ||||
Augie Fackler
|
r37941 | repo[None].add([b'foo']) | ||
repo.commit(text=b'commit1', date=b"0 0") | ||||
Thomas Arendsen Hein
|
r4110 | |||
Augie Fackler
|
r37941 | d = repo[None][b'foo'].date() | ||
Matt Harbison
|
r27056 | if os.name == 'nt': | ||
Tristan Seligmann
|
r33797 | d = d[:2] | ||
print("workingfilectx.date = (%d, %d)" % d) | ||||
Martin Geisler
|
r14379 | |||
# test memctx with non-ASCII commit message | ||||
Augie Fackler
|
r43346 | |||
Martin Geisler
|
r14379 | def filectxfn(repo, memctx, path): | ||
Augie Fackler
|
r37941 | return context.memfilectx(repo, memctx, b"foo", b"") | ||
Martin Geisler
|
r14379 | |||
Augie Fackler
|
r43346 | |||
ctx = context.memctx( | ||||
repo, | ||||
[b'tip', None], | ||||
encoding.tolocal(b"Gr\xc3\xbcezi!"), | ||||
[b"foo"], | ||||
filectxfn, | ||||
) | ||||
Martin Geisler
|
r14379 | ctx.commit() | ||
for enc in "ASCII", "Latin-1", "UTF-8": | ||||
encoding.encoding = enc | ||||
Augie Fackler
|
r37941 | printb(b"%-8s: %s" % (enc.encode('ascii'), repo[b"tip"].description())) | ||
Sean Farley
|
r21836 | |||
# test performing a status | ||||
Augie Fackler
|
r43346 | |||
Sean Farley
|
r21836 | def getfilectx(repo, memctx, f): | ||
Martin von Zweigbergk
|
r41442 | fctx = memctx.p1()[f] | ||
Sean Farley
|
r21836 | data, flags = fctx.data(), fctx.flags() | ||
Augie Fackler
|
r37941 | if f == b'foo': | ||
data += b'bar\n' | ||||
return context.memfilectx( | ||||
Augie Fackler
|
r43346 | repo, memctx, f, data, b'l' in flags, b'x' in flags | ||
) | ||||
Sean Farley
|
r21836 | |||
Martin von Zweigbergk
|
r37320 | ctxa = repo[0] | ||
Augie Fackler
|
r43346 | ctxb = context.memctx( | ||
repo, | ||||
[ctxa.node(), None], | ||||
b"test diff", | ||||
[b"foo"], | ||||
getfilectx, | ||||
ctxa.user(), | ||||
ctxa.date(), | ||||
) | ||||
Sean Farley
|
r21836 | |||
Robert Stanca
|
r28738 | print(ctxb.status(ctxa)) | ||
Sean Farley
|
r21837 | |||
# test performing a diff on a memctx | ||||
Augie Fackler
|
r38680 | diffopts = diffutil.diffallopts(repo.ui, {b'git': True}) | ||
Boris Feld
|
r38582 | for d in ctxb.diff(ctxa, opts=diffopts): | ||
Augie Fackler
|
r37941 | printb(d, end=b'') | ||
FUJIWARA Katsunori
|
r23700 | |||
Mads Kiilerich
|
r24180 | # test safeness and correctness of "ctx.status()" | ||
Robert Stanca
|
r28738 | print('= checking context.status():') | ||
FUJIWARA Katsunori
|
r23700 | |||
# ancestor "wcctx ~ 2" | ||||
Augie Fackler
|
r37941 | actx2 = repo[b'.'] | ||
FUJIWARA Katsunori
|
r23700 | |||
Augie Fackler
|
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") | ||||
FUJIWARA Katsunori
|
r23700 | |||
# ancestor "wcctx ~ 1" | ||||
Augie Fackler
|
r37941 | actx1 = repo[b'.'] | ||
FUJIWARA Katsunori
|
r23700 | |||
Augie Fackler
|
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']) | ||||
FUJIWARA Katsunori
|
r23700 | |||
# status at this point: | ||||
# M bar-m | ||||
# A bar-a | ||||
# R bar-r | ||||
# C foo | ||||
from mercurial import scmutil | ||||
Robert Stanca
|
r28738 | print('== checking workingctx.status:') | ||
FUJIWARA Katsunori
|
r23700 | |||
wctx = repo[None] | ||||
Robert Stanca
|
r28738 | print('wctx._status=%s' % (str(wctx._status))) | ||
FUJIWARA Katsunori
|
r23700 | |||
Robert Stanca
|
r28738 | print('=== with "pattern match":') | ||
Augie Fackler
|
r43346 | print( | ||
actx1.status(other=wctx, match=scmutil.matchfiles(repo, [b'bar-m', b'foo'])) | ||||
) | ||||
Robert Stanca
|
r28738 | print('wctx._status=%s' % (str(wctx._status))) | ||
Augie Fackler
|
r43346 | print( | ||
actx2.status(other=wctx, match=scmutil.matchfiles(repo, [b'bar-m', b'foo'])) | ||||
) | ||||
Robert Stanca
|
r28738 | print('wctx._status=%s' % (str(wctx._status))) | ||
FUJIWARA Katsunori
|
r23709 | |||
Robert Stanca
|
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))) | ||||
FUJIWARA Katsunori
|
r23711 | |||
Robert Stanca
|
r28738 | print("== checking workingcommitctx.status:") | ||
FUJIWARA Katsunori
|
r23711 | |||
Augie Fackler
|
r43346 | wcctx = context.workingcommitctx( | ||
repo, | ||||
scmutil.status([b'bar-m'], [b'bar-a'], [], [], [], [], []), | ||||
text=b'', | ||||
date=b'0 0', | ||||
) | ||||
Robert Stanca
|
r28738 | print('wcctx._status=%s' % (str(wcctx._status))) | ||
FUJIWARA Katsunori
|
r23711 | |||
Robert Stanca
|
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))) | ||||
FUJIWARA Katsunori
|
r23711 | |||
Robert Stanca
|
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))) | ||||
FUJIWARA Katsunori
|
r23712 | |||
Robert Stanca
|
r28738 | print('=== with "pattern match":') | ||
Augie Fackler
|
r43346 | print( | ||
actx1.status( | ||||
other=wcctx, match=scmutil.matchfiles(repo, [b'bar-m', b'foo']) | ||||
) | ||||
) | ||||
Robert Stanca
|
r28738 | print('wcctx._status=%s' % (str(wcctx._status))) | ||
Augie Fackler
|
r43346 | print( | ||
actx2.status( | ||||
other=wcctx, match=scmutil.matchfiles(repo, [b'bar-m', b'foo']) | ||||
) | ||||
) | ||||
Robert Stanca
|
r28738 | print('wcctx._status=%s' % (str(wcctx._status))) | ||
FUJIWARA Katsunori
|
r23712 | |||
Robert Stanca
|
r28738 | print('=== with "pattern match" and "listclean=True":') | ||
Augie Fackler
|
r43346 | print( | ||
actx1.status( | ||||
other=wcctx, | ||||
match=scmutil.matchfiles(repo, [b'bar-r', b'foo']), | ||||
listclean=True, | ||||
) | ||||
) | ||||
Robert Stanca
|
r28738 | print('wcctx._status=%s' % (str(wcctx._status))) | ||
Augie Fackler
|
r43346 | print( | ||
actx2.status( | ||||
other=wcctx, | ||||
match=scmutil.matchfiles(repo, [b'bar-r', b'foo']), | ||||
listclean=True, | ||||
) | ||||
) | ||||
Robert Stanca
|
r28738 | print('wcctx._status=%s' % (str(wcctx._status))) | ||
Jun Wu
|
r32518 | |||
os.chdir('..') | ||||
# test manifestlog being changed | ||||
print('== commit with manifestlog invalidated') | ||||
Martin von Zweigbergk
|
r42223 | repo = hg.repository(ui, b'test2', create=1) | ||
Jun Wu
|
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], [], [], [], [], []) | ||||
Augie Fackler
|
r43346 | ctx = context.workingcommitctx( | ||
repo, status, text=i, user=b'test@test.com', date=(0, 0) | ||||
) | ||||
ctx.p1().manifest() # side effect: cache manifestctx | ||||
Jun Wu
|
r32518 | n = repo.commitctx(ctx) | ||
Augie Fackler
|
r37941 | printb(b'commit %s: %s' % (i, hex(n))) | ||
Jun Wu
|
r32518 | |||
# touch 00manifest.i mtime so storecache could expire. | ||||
# repo.__dict__['manifestlog'] is deleted by transaction releasefn. | ||||
Augie Fackler
|
r37941 | st = repo.svfs.stat(b'00manifest.i') | ||
Augie Fackler
|
r43346 | repo.svfs.utime( | ||
b'00manifest.i', (st[stat.ST_MTIME] + 1, st[stat.ST_MTIME] + 1) | ||||
) | ||||
Jun Wu
|
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) | ||||
Martin von Zweigbergk
|
r33672 | |||
Augie Fackler
|
r37941 | with repo.wlock(), repo.lock(), repo.transaction(b'test'): | ||
Martin von Zweigbergk
|
r33672 | with open(b'4', 'wb') as f: | ||
f.write(b'4') | ||||
Augie Fackler
|
r37941 | repo.dirstate.normal(b'4') | ||
repo.commit(b'4') | ||||
Martin von Zweigbergk
|
r33672 | revsbefore = len(repo.changelog) | ||
repo.invalidate(clearfilecache=True) | ||||
revsafter = len(repo.changelog) | ||||
if revsbefore != revsafter: | ||||
print('changeset lost by repo.invalidate()') | ||||