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