]> arthur.barton.de Git - bup.git/blob - lib/bup/t/tindex.py
Remove a file created in a index test
[bup.git] / lib / bup / t / tindex.py
1 import os
2 import time
3 from bup import index
4 from bup.helpers import *
5 import bup.xstat as xstat
6 from wvtest import *
7
8 @wvtest
9 def index_basic():
10     cd = os.path.realpath('../../../t')
11     WVPASS(cd)
12     sd = os.path.realpath(cd + '/sampledata')
13     WVPASSEQ(index.realpath(cd + '/sampledata'), cd + '/sampledata')
14     WVPASSEQ(os.path.realpath(cd + '/sampledata/x'), sd + '/x')
15     WVPASSEQ(os.path.realpath(cd + '/sampledata/etc'), os.path.realpath('/etc'))
16     WVPASSEQ(index.realpath(cd + '/sampledata/etc'), sd + '/etc')
17
18
19 @wvtest
20 def index_writer():
21     unlink('index.tmp')
22     ds = xstat.stat('.')
23     fs = xstat.stat('tindex.py')
24     w = index.Writer('index.tmp')
25     w.add('/var/tmp/sporky', fs)
26     w.add('/etc/passwd', fs)
27     w.add('/etc/', ds)
28     w.add('/', ds)
29     w.close()
30
31
32 def dump(m):
33     for e in list(m):
34         print '%s%s %s' % (e.is_valid() and ' ' or 'M',
35                            e.is_fake() and 'F' or ' ',
36                            e.name)
37
38 def fake_validate(*l):
39     for i in l:
40         for e in i:
41             e.validate(0100644, index.FAKE_SHA)
42             e.repack()
43
44 def eget(l, ename):
45     for e in l:
46         if e.name == ename:
47             return e
48
49 @wvtest
50 def index_negative_timestamps():
51     # Makes 'foo' exist
52     f = file('foo', 'wb')
53     f.close()
54
55     # Dec 31, 1969
56     os.utime("foo", (-86400, -86400))
57     e = index.BlankNewEntry("foo")
58     e.from_stat(xstat.stat("foo"), time.time())
59     assert len(e.packed())
60     WVPASS()
61
62     # Jun 10, 1893
63     os.utime("foo", (-0x80000000, -0x80000000))
64     e = index.BlankNewEntry("foo")
65     e.from_stat(xstat.stat("foo"), time.time())
66     assert len(e.packed())
67     WVPASS()
68
69     unlink('foo')
70
71
72 @wvtest
73 def index_dirty():
74     unlink('index.tmp')
75     unlink('index2.tmp')
76     ds = xstat.stat('.')
77     fs = xstat.stat('tindex.py')
78     
79     w1 = index.Writer('index.tmp')
80     w1.add('/a/b/x', fs)
81     w1.add('/a/b/c', fs)
82     w1.add('/a/b/', ds)
83     w1.add('/a/', ds)
84     #w1.close()
85     WVPASS()
86
87     w2 = index.Writer('index2.tmp')
88     w2.add('/a/b/n/2', fs)
89     #w2.close()
90     WVPASS()
91
92     w3 = index.Writer('index3.tmp')
93     w3.add('/a/c/n/3', fs)
94     #w3.close()
95     WVPASS()
96
97     r1 = w1.new_reader()
98     r2 = w2.new_reader()
99     r3 = w3.new_reader()
100     WVPASS()
101
102     r1all = [e.name for e in r1]
103     WVPASSEQ(r1all,
104              ['/a/b/x', '/a/b/c', '/a/b/', '/a/', '/'])
105     r2all = [e.name for e in r2]
106     WVPASSEQ(r2all,
107              ['/a/b/n/2', '/a/b/n/', '/a/b/', '/a/', '/'])
108     r3all = [e.name for e in r3]
109     WVPASSEQ(r3all,
110              ['/a/c/n/3', '/a/c/n/', '/a/c/', '/a/', '/'])
111     all = [e.name for e in index.merge(r2, r1, r3)]
112     WVPASSEQ(all,
113              ['/a/c/n/3', '/a/c/n/', '/a/c/',
114               '/a/b/x', '/a/b/n/2', '/a/b/n/', '/a/b/c',
115               '/a/b/', '/a/', '/'])
116     fake_validate(r1)
117     dump(r1)
118
119     print [hex(e.flags) for e in r1]
120     WVPASSEQ([e.name for e in r1 if e.is_valid()], r1all)
121     WVPASSEQ([e.name for e in r1 if not e.is_valid()], [])
122     WVPASSEQ([e.name for e in index.merge(r2, r1, r3) if not e.is_valid()],
123              ['/a/c/n/3', '/a/c/n/', '/a/c/',
124               '/a/b/n/2', '/a/b/n/', '/a/b/', '/a/', '/'])
125
126     expect_invalid = ['/'] + r2all + r3all
127     expect_real = (set(r1all) - set(r2all) - set(r3all)) \
128                     | set(['/a/b/n/2', '/a/c/n/3'])
129     dump(index.merge(r2, r1, r3))
130     for e in index.merge(r2, r1, r3):
131         print e.name, hex(e.flags), e.ctime
132         eiv = e.name in expect_invalid
133         er  = e.name in expect_real
134         WVPASSEQ(eiv, not e.is_valid())
135         WVPASSEQ(er, e.is_real())
136     fake_validate(r2, r3)
137     dump(index.merge(r2, r1, r3))
138     WVPASSEQ([e.name for e in index.merge(r2, r1, r3) if not e.is_valid()], [])
139     
140     e = eget(index.merge(r2, r1, r3), '/a/b/c')
141     e.invalidate()
142     e.repack()
143     dump(index.merge(r2, r1, r3))
144     WVPASSEQ([e.name for e in index.merge(r2, r1, r3) if not e.is_valid()],
145              ['/a/b/c', '/a/b/', '/a/', '/'])