test-context.py
211 lines
| 6.3 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, | ||
encoding, | ||||
Robert Stanca
|
r28735 | hg, | ||
Jun Wu
|
r32518 | scmutil, | ||
Yuya Nishihara
|
r28775 | ui as uimod, | ||
Robert Stanca
|
r28735 | ) | ||
Boris Feld
|
r38582 | from mercurial.utils import diffutil | ||
Thomas Arendsen Hein
|
r4110 | |||
Augie Fackler
|
r37941 | print_ = print | ||
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() | ||||
def printb(data, end=b'\n'): | ||||
out = getattr(sys.stdout, 'buffer', sys.stdout) | ||||
out.write(data + end) | ||||
out.flush() | ||||
Yuya Nishihara
|
r30559 | u = uimod.ui.load() | ||
Thomas Arendsen Hein
|
r4110 | |||
Augie Fackler
|
r37941 | repo = hg.repository(u, 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 | ||||
def filectxfn(repo, memctx, path): | ||||
Augie Fackler
|
r37941 | return context.memfilectx(repo, memctx, b"foo", b"") | ||
Martin Geisler
|
r14379 | |||
Augie Fackler
|
r37941 | 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 | ||||
def getfilectx(repo, memctx, f): | ||||
fctx = memctx.parents()[0][f] | ||||
data, flags = fctx.data(), fctx.flags() | ||||
Augie Fackler
|
r37941 | if f == b'foo': | ||
data += b'bar\n' | ||||
return context.memfilectx( | ||||
repo, memctx, f, data, b'l' in flags, b'x' in flags) | ||||
Sean Farley
|
r21836 | |||
Martin von Zweigbergk
|
r37320 | ctxa = repo[0] | ||
Augie Fackler
|
r37941 | ctxb = context.memctx(repo, [ctxa.node(), None], b"test diff", [b"foo"], | ||
Sean Farley
|
r21837 | 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 | ||||
Boris Feld
|
r38582 | diffopts = diffutil.diffopts(repo.ui, {'git': True}) | ||
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":') | ||
print(actx1.status(other=wctx, | ||||
Augie Fackler
|
r37941 | match=scmutil.matchfiles(repo, [b'bar-m', b'foo']))) | ||
Robert Stanca
|
r28738 | print('wctx._status=%s' % (str(wctx._status))) | ||
print(actx2.status(other=wctx, | ||||
Augie Fackler
|
r37941 | 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 | |||
wcctx = context.workingcommitctx(repo, | ||||
Augie Fackler
|
r37941 | scmutil.status([b'bar-m'], | ||
[b'bar-a'], | ||||
FUJIWARA Katsunori
|
r23711 | [], | ||
[], [], [], []), | ||||
Augie Fackler
|
r37941 | 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":') | ||
print(actx1.status(other=wcctx, | ||||
Augie Fackler
|
r37941 | match=scmutil.matchfiles(repo, [b'bar-m', b'foo']))) | ||
Robert Stanca
|
r28738 | print('wcctx._status=%s' % (str(wcctx._status))) | ||
print(actx2.status(other=wcctx, | ||||
Augie Fackler
|
r37941 | 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":') | ||
print(actx1.status(other=wcctx, | ||||
Augie Fackler
|
r37941 | match=scmutil.matchfiles(repo, [b'bar-r', b'foo']), | ||
Robert Stanca
|
r28738 | listclean=True)) | ||
print('wcctx._status=%s' % (str(wcctx._status))) | ||||
print(actx2.status(other=wcctx, | ||||
Augie Fackler
|
r37941 | match=scmutil.matchfiles(repo, [b'bar-r', b'foo']), | ||
Robert Stanca
|
r28738 | listclean=True)) | ||
print('wcctx._status=%s' % (str(wcctx._status))) | ||||
Jun Wu
|
r32518 | |||
os.chdir('..') | ||||
# test manifestlog being changed | ||||
print('== commit with manifestlog invalidated') | ||||
Augie Fackler
|
r37941 | repo = hg.repository(u, 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], [], [], [], [], []) | ||||
ctx = context.workingcommitctx(repo, status, text=i, user=b'test@test.com', | ||||
date=(0, 0)) | ||||
ctx.p1().manifest() # side effect: cache manifestctx | ||||
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') | ||
repo.svfs.utime(b'00manifest.i', | ||||
Augie Fackler
|
r36799 | (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()') | ||||