tests/test-filecache.py
changeset 37900 b3ffa2faae04
parent 36781 ffa3026d4196
child 40424 7caf632e30c3
equal deleted inserted replaced
37899:2b3b6187c316 37900:b3ffa2faae04
     5 import sys
     5 import sys
     6 
     6 
     7 if subprocess.call(['python', '%s/hghave' % os.environ['TESTDIR'],
     7 if subprocess.call(['python', '%s/hghave' % os.environ['TESTDIR'],
     8                     'cacheable']):
     8                     'cacheable']):
     9     sys.exit(80)
     9     sys.exit(80)
       
    10 
       
    11 print_ = print
       
    12 def print(*args, **kwargs):
       
    13     """print() wrapper that flushes stdout buffers to avoid py3 buffer issues
       
    14 
       
    15     We could also just write directly to sys.stdout.buffer the way the
       
    16     ui object will, but this was easier for porting the test.
       
    17     """
       
    18     print_(*args, **kwargs)
       
    19     sys.stdout.flush()
    10 
    20 
    11 from mercurial import (
    21 from mercurial import (
    12     extensions,
    22     extensions,
    13     hg,
    23     hg,
    14     localrepo,
    24     localrepo,
    44         return 'string from function'
    54         return 'string from function'
    45 
    55 
    46     def invalidate(self):
    56     def invalidate(self):
    47         for k in self._filecache:
    57         for k in self._filecache:
    48             try:
    58             try:
    49                 delattr(self, k)
    59                 delattr(self, pycompat.sysstr(k))
    50             except AttributeError:
    60             except AttributeError:
    51                 pass
    61                 pass
    52 
    62 
    53 def basic(repo):
    63 def basic(repo):
    54     print("* neither file exists")
    64     print("* neither file exists")
    82     repo.cached
    92     repo.cached
    83 
    93 
    84     # atomic replace file, size doesn't change
    94     # atomic replace file, size doesn't change
    85     # hopefully st_mtime doesn't change as well so this doesn't use the cache
    95     # hopefully st_mtime doesn't change as well so this doesn't use the cache
    86     # because of inode change
    96     # because of inode change
    87     f = vfsmod.vfs('.')('x', 'w', atomictemp=True)
    97     f = vfsmod.vfs(b'.')(b'x', b'w', atomictemp=True)
    88     f.write('b')
    98     f.write(b'b')
    89     f.close()
    99     f.close()
    90 
   100 
    91     repo.invalidate()
   101     repo.invalidate()
    92     print("* file x changed inode")
   102     print("* file x changed inode")
    93     repo.cached
   103     repo.cached
   106     repo.invalidate()
   116     repo.invalidate()
   107     print("* file y changed size")
   117     print("* file y changed size")
   108     # should recreate the object
   118     # should recreate the object
   109     repo.cached
   119     repo.cached
   110 
   120 
   111     f = vfsmod.vfs('.')('y', 'w', atomictemp=True)
   121     f = vfsmod.vfs(b'.')(b'y', b'w', atomictemp=True)
   112     f.write('B')
   122     f.write(b'B')
   113     f.close()
   123     f.close()
   114 
   124 
   115     repo.invalidate()
   125     repo.invalidate()
   116     print("* file y changed inode")
   126     print("* file y changed inode")
   117     repo.cached
   127     repo.cached
   118 
   128 
   119     f = vfsmod.vfs('.')('x', 'w', atomictemp=True)
   129     f = vfsmod.vfs(b'.')(b'x', b'w', atomictemp=True)
   120     f.write('c')
   130     f.write(b'c')
   121     f.close()
   131     f.close()
   122     f = vfsmod.vfs('.')('y', 'w', atomictemp=True)
   132     f = vfsmod.vfs(b'.')(b'y', b'w', atomictemp=True)
   123     f.write('C')
   133     f.write(b'C')
   124     f.close()
   134     f.close()
   125 
   135 
   126     repo.invalidate()
   136     repo.invalidate()
   127     print("* both files changed inode")
   137     print("* both files changed inode")
   128     repo.cached
   138     repo.cached
   153     # test old behavior that caused filecached properties to go out of sync
   163     # test old behavior that caused filecached properties to go out of sync
   154     os.system('hg init && echo a >> a && hg ci -qAm.')
   164     os.system('hg init && echo a >> a && hg ci -qAm.')
   155     repo = hg.repository(uimod.ui.load())
   165     repo = hg.repository(uimod.ui.load())
   156     # first rollback clears the filecache, but changelog to stays in __dict__
   166     # first rollback clears the filecache, but changelog to stays in __dict__
   157     repo.rollback()
   167     repo.rollback()
   158     repo.commit('.')
   168     repo.commit(b'.')
   159     # second rollback comes along and touches the changelog externally
   169     # second rollback comes along and touches the changelog externally
   160     # (file is moved)
   170     # (file is moved)
   161     repo.rollback()
   171     repo.rollback()
   162     # but since changelog isn't under the filecache control anymore, we don't
   172     # but since changelog isn't under the filecache control anymore, we don't
   163     # see that it changed, and return the old changelog without reconstructing
   173     # see that it changed, and return the old changelog without reconstructing
   164     # it
   174     # it
   165     repo.commit('.')
   175     repo.commit(b'.')
   166 
   176 
   167 def setbeforeget(repo):
   177 def setbeforeget(repo):
   168     os.remove('x')
   178     os.remove('x')
   169     os.remove('y')
   179     os.remove('y')
   170     repo.cached = 'string set externally'
   180     repo.cached = 'string set externally'