~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_hashcache.py

  • Committer: Robert Collins
  • Date: 2006-03-06 07:14:27 UTC
  • mto: (1594.2.4 integration)
  • mto: This revision was merged to the branch mainline in revision 1596.
  • Revision ID: robertc@robertcollins.net-20060306071427-359ef15c1d891e84
Add total_size to the revision_store api.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
 
from bzrlib.selftest import InTempDir
 
17
import os
 
18
import sha
 
19
import sys
 
20
import time
18
21
 
 
22
from bzrlib.errors import BzrError
 
23
from bzrlib.hashcache import HashCache
 
24
from bzrlib.tests import TestCaseInTempDir
19
25
 
20
26
 
21
27
def sha1(t):
22
 
    import sha
23
28
    return sha.new(t).hexdigest()
24
29
 
25
30
 
26
31
def pause():
27
 
    import time
 
32
    if False:
 
33
        return
 
34
    if sys.platform in ('win32', 'cygwin'):
 
35
        time.sleep(3)
 
36
        return
28
37
    # allow it to stabilize
29
38
    start = int(time.time())
30
39
    while int(time.time()) == start:
31
40
        time.sleep(0.2)
 
41
 
 
42
 
 
43
class FixThisError(Exception):
 
44
    pass
32
45
    
33
46
 
34
 
class TestHashCache(InTempDir):
 
47
class TestHashCache(TestCaseInTempDir):
35
48
 
36
49
    def test_hashcache(self):
37
50
        """Functional tests for hashcache"""
38
 
        from bzrlib.hashcache import HashCache
39
 
        import os
40
 
        import time
41
51
 
42
52
        # make a dummy bzr directory just to hold the cache
43
53
        os.mkdir('.bzr')
44
 
        hc = HashCache('.')
 
54
        hc = HashCache('.', '.bzr/stat-cache')
45
55
 
46
56
        file('foo', 'wb').write('hello')
47
57
        os.mkdir('subdir')
104
114
        hc.write()
105
115
        del hc
106
116
 
107
 
        hc = HashCache('.')
 
117
        hc = HashCache('.', '.bzr/stat-cache')
108
118
        hc.read()
109
119
 
110
120
        self.assertEquals(len(hc._cache), 2)
112
122
        self.assertEquals(hc.hit_count, 1)
113
123
        self.assertEquals(hc.miss_count, 0)
114
124
        self.assertEquals(hc.get_sha1('foo2'), sha1('new content'))
 
125
 
 
126
    def test_hashcache_raise(self):
 
127
        """check that hashcache can raise BzrError"""
 
128
 
 
129
        os.mkdir('.bzr')
 
130
        hc = HashCache('.', '.bzr/stat-cache')
 
131
        ok = False
 
132
 
 
133
        # make a best effort to create a weird kind of file
 
134
        funcs = (os.mkfifo, os.mknod)
 
135
        for func in funcs:
 
136
            try:
 
137
                func('a')
 
138
                ok = True
 
139
                break
 
140
            except FixThisError:
 
141
                pass
 
142
 
 
143
        if ok:
 
144
            self.assertRaises(BzrError, hc.get_sha1, 'a')
 
145
        else:
 
146
            raise BzrError("no weird file type could be created: extend this test case for your os")