/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
846 by Martin Pool
- start adding refactored/simplified hash cache
1
# (C) 2005 Canonical Ltd
2
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
from bzrlib.selftest import InTempDir
18
19
20
21
def sha1(t):
22
    import sha
23
    return sha.new(t).hexdigest()
24
25
26
def pause():
27
    import time
28
    # allow it to stabilize
29
    start = int(time.time())
30
    while int(time.time()) == start:
31
        time.sleep(0.2)
32
    
33
34
859 by Martin Pool
- add HashCache.write and a simple test for it
35
class TestHashCache(InTempDir):
865 by Martin Pool
- cleaner 'modified 'command
36
    """Functional tests for hashcache"""
846 by Martin Pool
- start adding refactored/simplified hash cache
37
    def runTest(self):
38
        from bzrlib.hashcache import HashCache
39
        import os
40
        import time
41
42
        hc = HashCache('.')
43
44
        file('foo', 'wb').write('hello')
45
        os.mkdir('subdir')
46
        pause()
47
48
        self.assertEquals(hc.get_sha1('foo'),
49
                          'aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d')
50
        self.assertEquals(hc.miss_count, 1)
51
        self.assertEquals(hc.hit_count, 0)
52
53
        # check we hit without re-reading
54
        self.assertEquals(hc.get_sha1('foo'),
55
                          'aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d')
56
        self.assertEquals(hc.miss_count, 1)
57
        self.assertEquals(hc.hit_count, 1)
58
59
        # check again without re-reading
60
        self.assertEquals(hc.get_sha1('foo'),
61
                          'aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d')
62
        self.assertEquals(hc.miss_count, 1)
63
        self.assertEquals(hc.hit_count, 2)
64
65
        # write new file and make sure it is seen
66
        file('foo', 'wb').write('goodbye')
67
        pause()
68
        self.assertEquals(hc.get_sha1('foo'),
69
                          '3c8ec4874488f6090a157b014ce3397ca8e06d4f')
70
        self.assertEquals(hc.miss_count, 2)
71
72
        # quickly write new file of same size and make sure it is seen
73
        # this may rely on detection of timestamps that are too close
74
        # together to be safe
75
        file('foo', 'wb').write('g00dbye')
76
        self.assertEquals(hc.get_sha1('foo'),
77
                          sha1('g00dbye'))
863 by Martin Pool
- more hashcache tests
78
79
        file('foo2', 'wb').write('other file')
80
        self.assertEquals(hc.get_sha1('foo2'), sha1('other file'))
81
82
        os.remove('foo2')
83
        self.assertEquals(hc.get_sha1('foo2'), None)
84
85
        file('foo2', 'wb').write('new content')
86
        self.assertEquals(hc.get_sha1('foo2'), sha1('new content'))
846 by Martin Pool
- start adding refactored/simplified hash cache
87
88
        self.assertEquals(hc.get_sha1('subdir'), None)
848 by Martin Pool
doc
89
863 by Martin Pool
- more hashcache tests
90
        # it's likely neither are cached at the moment because they 
91
        # changed recently, but we can't be sure
861 by Martin Pool
- more hash-cache tests
92
        pause()
93
863 by Martin Pool
- more hashcache tests
94
        # should now be safe to cache it if we reread them
95
        self.assertEquals(hc.get_sha1('foo'), sha1('g00dbye'))
861 by Martin Pool
- more hash-cache tests
96
        self.assertEquals(len(hc._cache), 1)
863 by Martin Pool
- more hashcache tests
97
        self.assertEquals(hc.get_sha1('foo2'), sha1('new content'))
98
        self.assertEquals(len(hc._cache), 2)
861 by Martin Pool
- more hash-cache tests
99
100
        # write out, read back in and check that we don't need to
101
        # re-read any files
102
        hc.write('stat-cache')
103
        del hc
104
105
        hc = HashCache('.')
862 by Martin Pool
- code to re-read hashcache from file
106
        hc.read('stat-cache')
107
863 by Martin Pool
- more hashcache tests
108
        self.assertEquals(len(hc._cache), 2)
862 by Martin Pool
- code to re-read hashcache from file
109
        self.assertEquals(hc.get_sha1('foo'), sha1('g00dbye'))
110
        self.assertEquals(hc.hit_count, 1)
111
        self.assertEquals(hc.miss_count, 0)
863 by Martin Pool
- more hashcache tests
112
        self.assertEquals(hc.get_sha1('foo2'), sha1('new content'))
113
861 by Martin Pool
- more hash-cache tests
114
        
115
116
        
117