~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to tests.py

  • Committer: Michael Ellerman
  • Date: 2006-06-13 02:41:36 UTC
  • mfrom: (0.1.119 shelf)
  • mto: (0.6.1 shelf-win32)
  • mto: This revision was merged to the branch mainline in revision 396.
  • Revision ID: michael@ellerman.id.au-20060613024136-cdec631489ca8074
Merge updates to tests from Aaron.

Fixes the tests to work with diffs that include dates. This ties us to
bzr as of revision 1743 (v0.9pre).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/python
2
2
 
 
3
from bzrlib.diff import _patch_header_date
3
4
import bzrlib.tests
4
5
import os.path
5
6
 
7
8
    ORIGINAL = '\n\nhello test world\n\n'
8
9
    MODIFIED = '\n\ngoodbye test world\n\n'
9
10
    DIFF_HEADER = "=== modified file '%(filename)s'\n"
10
 
    DIFF_1 = """--- %(filename)s\t
11
 
+++ %(filename)s\t
 
11
    DIFF_1 = """--- %(filename)s\t%(old_date)s
 
12
+++ %(filename)s\t%(new_date)s
12
13
@@ -1,4 +1,4 @@
13
14
 
14
15
 
16
17
+goodbye test world
17
18
 
18
19
"""
19
 
    DIFF_2 = """--- test_file\t
20
 
+++ test_file\t
 
20
    DIFF_2 = """--- test_file\t%(old_date)s
 
21
+++ test_file\t%(new_date)s
21
22
@@ -1,4 +1,4 @@
22
23
 
23
24
 
26
27
 
27
28
"""
28
29
    def _check_diff(self, diff=DIFF_1, filename='test_file'):
29
 
        keys = { 'filename' : filename }
 
30
        old_tree = self.tree.basis_tree()
 
31
        old_date = _patch_header_date(old_tree, 
 
32
                                      old_tree.inventory.path2id(filename),
 
33
                                      filename)
 
34
        new_date = _patch_header_date(self.tree, 
 
35
                                      self.tree.inventory.path2id(filename),
 
36
                                      filename)
 
37
        keys = { 'filename' : filename , 'old_date': old_date, 
 
38
                 'new_date': new_date}
30
39
        hdr  = self.DIFF_HEADER % keys
31
40
        diff = diff % keys
32
41
        self.assertEqual(self.capture('diff', retcode=1), hdr + diff + '\n')
33
42
 
34
 
    def _check_shelf(self, idx, diff=DIFF_1, filename='test_file'):
35
 
        diff = diff % { 'filename' : filename }
36
 
        shelf = open(os.path.join(self.tree.branch.base,
 
43
    def _check_shelf(self, idx, diff=DIFF_1, filename='test_file',
 
44
                     new_date=None):
 
45
        old_tree = self.tree.basis_tree()
 
46
        old_date = _patch_header_date(old_tree, 
 
47
                                      old_tree.inventory.path2id(filename),
 
48
                                      filename)
 
49
        diff = diff % { 'filename' : filename, 'old_date': old_date,
 
50
                        'new_date': new_date}
 
51
        shelf = open(os.path.join(self.tree.basedir,
37
52
                '.shelf/shelves/default/' + idx)).read()
38
53
        shelf = shelf[shelf.index('\n') + 1:] # skip the message
39
54
        self.assertEqual(shelf, diff)
55
70
            file('test_file', 'w').write(self.MODIFIED)
56
71
 
57
72
            self._check_diff()
 
73
            
 
74
            new_date = _patch_header_date(self.tree, 
 
75
                self.tree.inventory.path2id('test_file'), 'test_file')
58
76
 
59
77
            # Shelve the changes
60
78
            self.run_bzr('shelve', '--all', retcode=0)
65
83
            # Make sure the file is actually back the way it was
66
84
            self.assertEqual(file('test_file').read(), self.ORIGINAL)
67
85
 
68
 
            self._check_shelf('00')
 
86
            self._check_shelf('00', new_date=new_date)
69
87
 
70
88
            # Unshelve
71
89
            self.run_bzr('unshelve', '--all', retcode=0)
73
91
            self._check_diff()
74
92
 
75
93
            # Check the shelved patch was backed up
76
 
            self._check_shelf('00~')
 
94
            self._check_shelf('00~', new_date=new_date)
77
95
 
78
96
            # Make sure the file is back the way it should be
79
97
            self.assertEqual(file('test_file').read(), self.MODIFIED)
145
163
        self.assertEqual(self.capture('diff', retcode=0), '')
146
164
 
147
165
        # Check the shelf is right
148
 
        shelf = open(os.path.join(self.tree.branch.base,
 
166
        shelf = open(os.path.join(self.tree.basedir,
149
167
                    '.shelf/shelves/default', patch)).read()
150
168
        self.assertTrue('patch %s' % patch in shelf)
151
169
 
161
179
        self.__test_show(self.tree, '02')
162
180
 
163
181
        # Now check we can show a previously shelved patch
164
 
        shelf = open(os.path.join(self.tree.branch.base,
 
182
        shelf = open(os.path.join(self.tree.basedir,
165
183
                    '.shelf/shelves/default/00')).read()
166
184
        self.assertTrue('patch 00' in shelf)
167
185
 
177
195
        self.__test_show(self.tree, '02')
178
196
 
179
197
        # Check that not specifying at patch gets us the most recent
180
 
        shelf = open(os.path.join(self.tree.branch.base,
 
198
        shelf = open(os.path.join(self.tree.basedir,
181
199
                    '.shelf/shelves/default/02')).read()
182
200
        self.assertTrue('patch 02' in shelf)
183
201
 
202
220
        self.run_bzr('shelve', '--all', retcode=0)
203
221
 
204
222
        # Write an unapplyable patch into the shelf
205
 
        shelf = open(os.path.join(self.tree.branch.base,
 
223
        shelf = open(os.path.join(self.tree.basedir,
206
224
                    '.shelf/shelves/default/00'), 'w')
207
225
        shelf.write(self.DIFF_2)
208
226
        shelf.close()
211
229
        self.run_bzr('unshelve', '--all', retcode=3)
212
230
 
213
231
        # Make sure the patch is still there, eventhough it's broken
214
 
        shelf = open(os.path.join(self.tree.branch.base,
 
232
        shelf = open(os.path.join(self.tree.basedir,
215
233
                    '.shelf/shelves/default/00')).read()
216
234
        self.assertEqual(shelf, self.DIFF_2)
217
235
 
286
304
        # Modify the test files
287
305
        file('test_file', 'w').write(self.MODIFIED)
288
306
        file('test_file2', 'w').write(self.MODIFIED)
 
307
        new_date = _patch_header_date(self.tree, 
 
308
            self.tree.inventory.path2id('test_file'), 'test_file')
289
309
 
290
310
        # Shelve the changes
291
311
        self.run_bzr('shelve', '--all', 'test_file', retcode=0)
292
312
        self.run_bzr('shelve', '--all', 'test_file2', retcode=0)
293
313
 
294
 
        self._check_shelf('00')
 
314
        self._check_shelf('00', new_date=new_date)
295
315
 
296
316
        # Delete 00
297
317
        self.run_bzr('shelf', 'delete', '00', retcode=0)
302
322
        self.assertTrue(os.path.exists('.shelf/shelves/default/01'))
303
323
 
304
324
        # Check the backup is right
305
 
        self._check_shelf('00~')
 
325
        self._check_shelf('00~', new_date=new_date)
306
326
 
307
327
        # Check ls works
308
328
        lines = self.capture('shelf ls', retcode=0).split('\n')
379
399
        # Run a benign shelf command to setup .shelf for us
380
400
        self.run_bzr('shelf', 'ls', retcode=0)
381
401
 
 
402
        old_tree = self.tree.basis_tree()
 
403
        old_date = _patch_header_date(old_tree, 
 
404
                                      old_tree.inventory.path2id('test_file'),
 
405
                                      'test_file')
 
406
        new_date = _patch_header_date(self.tree, 
 
407
                                      self.tree.inventory.path2id('test_file'),
 
408
                                      'test_file')
382
409
        # Fake a -p0 shelved patch
383
 
        diff = self.DIFF_1 % { 'filename' : 'test_file' }
 
410
        diff = self.DIFF_1 % { 'filename' : 'test_file', 'old_date': old_date,
 
411
                               'new_date' : new_date}
384
412
        diff = diff.replace('--- ', '--- a/')
385
413
        diff = diff.replace('+++ ', '+++ b/')
386
414
        open('.shelf/shelves/default/00', 'w').write(diff)