~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to tests/shelf_tests.py

  • Committer: Aaron Bentley
  • Date: 2006-07-13 13:25:20 UTC
  • Revision ID: abentley@panoramicfeedback.com-20060713132520-2fabfab51affb82f
Update clean-tree docs

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
import os.path
 
1
#!/usr/bin/python
 
2
 
2
3
from bzrlib.diff import _patch_header_date
3
4
import bzrlib.tests
4
 
 
5
 
from bzrlib.plugins.bzrtools.hunk_selector import (
6
 
    ShelveHunkSelector,
7
 
    UnshelveHunkSelector,
8
 
    )
9
 
from bzrlib.plugins.bzrtools.errors import NoColor
10
 
from bzrlib.plugins.bzrtools import cmd_shelf
11
 
 
 
5
import os.path
12
6
 
13
7
class ShelfTests(bzrlib.tests.TestCaseWithTransport):
14
8
    ORIGINAL = '\n\nhello test world\n\n'
34
28
"""
35
29
    def _check_diff(self, diff=DIFF_1, filename='test_file'):
36
30
        old_tree = self.tree.basis_tree()
37
 
        old_tree.lock_read()
38
 
        self.tree.lock_read()
39
 
        try:
40
 
            old_date = _patch_header_date(old_tree,
41
 
                                          old_tree.inventory.path2id(filename),
42
 
                                          filename)
43
 
            new_date = _patch_header_date(self.tree,
44
 
                self.tree.inventory.path2id(filename), filename)
45
 
        finally:
46
 
            self.tree.unlock()
47
 
            old_tree.unlock()
 
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)
48
37
        keys = { 'filename' : filename , 'old_date': old_date, 
49
38
                 'new_date': new_date}
50
39
        hdr  = self.DIFF_HEADER % keys
54
43
    def _check_shelf(self, idx, diff=DIFF_1, filename='test_file',
55
44
                     new_date=None):
56
45
        old_tree = self.tree.basis_tree()
57
 
        old_tree.lock_read()
58
 
        try:
59
 
            old_date = _patch_header_date(old_tree,
60
 
                                          old_tree.inventory.path2id(filename),
61
 
                                          filename)
62
 
        finally:
63
 
            old_tree.unlock()
 
46
        old_date = _patch_header_date(old_tree, 
 
47
                                      old_tree.inventory.path2id(filename),
 
48
                                      filename)
64
49
        diff = diff % { 'filename' : filename, 'old_date': old_date,
65
50
                        'new_date': new_date}
66
51
        shelf = open(os.path.join(self.tree.basedir,
77
62
    def __test_loop(self, count):
78
63
        self.tree = self.make_branch_and_tree('.')
79
64
        self.__create_and_add_test_file()
80
 
        for counter in range(count):
 
65
 
 
66
        while count > 0:
 
67
            count -= 1
 
68
 
81
69
            # Modify the test file
82
 
            # write in binary mode because on win32 line-endings should be
83
 
            # LF
 
70
            # write in binary mode because on win32 line-endings should be LF
84
71
            f = file('test_file', 'wb')
85
72
            f.write(self.MODIFIED)
86
73
            f.close()
87
74
 
88
75
            self._check_diff()
89
 
 
90
 
            self.tree.lock_write()
91
 
            try:
92
 
                new_date = _patch_header_date(self.tree,
93
 
                    self.tree.inventory.path2id('test_file'), 'test_file')
94
 
            finally:
95
 
                self.tree.unlock()
 
76
            
 
77
            new_date = _patch_header_date(self.tree, 
 
78
                self.tree.inventory.path2id('test_file'), 'test_file')
96
79
 
97
80
            # Shelve the changes
98
81
            self.run_bzr('shelve', '--all', retcode=0)
318
301
 
319
302
    def test_shelf_delete(self):
320
303
        self.tree = self.make_branch_and_tree('.')
321
 
        self.tree.lock_write()
322
 
        try:
323
 
            self.__create_and_add_test_file()
324
 
            self.__create_and_add_test_file(filename='test_file2')
325
 
 
326
 
            # Modify the test files
327
 
            # write in binary mode because on win32 line-endings should be LF
328
 
            f = file('test_file', 'wb')
329
 
            f.write(self.MODIFIED)
330
 
            f.close()
331
 
            f = file('test_file2', 'wb')
332
 
            f.write(self.MODIFIED)
333
 
            f.close()
334
 
            new_date = _patch_header_date(self.tree,
335
 
                self.tree.inventory.path2id('test_file'), 'test_file')
336
 
        finally:
337
 
            self.tree.unlock()
 
304
 
 
305
        self.__create_and_add_test_file()
 
306
        self.__create_and_add_test_file(filename='test_file2')
 
307
 
 
308
        # Modify the test files
 
309
        # write in binary mode because on win32 line-endings should be LF
 
310
        f = file('test_file', 'wb')
 
311
        f.write(self.MODIFIED)
 
312
        f.close()
 
313
        f = file('test_file2', 'wb')
 
314
        f.write(self.MODIFIED)
 
315
        f.close()
 
316
        new_date = _patch_header_date(self.tree, 
 
317
            self.tree.inventory.path2id('test_file'), 'test_file')
338
318
 
339
319
        # Shelve the changes
340
320
        self.run_bzr('shelve', '--all', 'test_file', retcode=0)
429
409
        self.run_bzr('shelf', 'ls', retcode=0)
430
410
 
431
411
        old_tree = self.tree.basis_tree()
432
 
        old_tree.lock_read()
433
 
        self.tree.lock_read()
434
 
        try:
435
 
            old_date = _patch_header_date(old_tree,
436
 
                old_tree.inventory.path2id('test_file'),
437
 
                                          'test_file')
438
 
            new_date = _patch_header_date(self.tree,
439
 
                self.tree.inventory.path2id('test_file'), 'test_file')
440
 
        finally:
441
 
            old_tree.unlock()
442
 
            self.tree.unlock()
 
412
        old_date = _patch_header_date(old_tree, 
 
413
                                      old_tree.inventory.path2id('test_file'),
 
414
                                      'test_file')
 
415
        new_date = _patch_header_date(self.tree, 
 
416
                                      self.tree.inventory.path2id('test_file'),
 
417
                                      'test_file')
443
418
        # Fake a -p0 shelved patch
444
419
        diff = self.DIFF_1 % { 'filename' : 'test_file', 'old_date': old_date,
445
420
                               'new_date' : new_date}
577
552
        # Hex and is cracky, so it shouldn't work
578
553
        stdout, error = self.run_bzr_captured(['unshelve', '0x00'], retcode=3)
579
554
        self.assertTrue("Invalid patch name '0x00'" in error)
580
 
 
581
 
    def test_color_hunk_selector(self):
582
 
        """Make sure NoColor is raised iff terminal.has_ansi_colors is False"""
583
 
        hs = ShelveHunkSelector([])
584
 
        hs = UnshelveHunkSelector([])
585
 
        try:
586
 
            from bzrlib.plugins.bzrtools import terminal
587
 
        except ImportError:
588
 
            from bzrtools import terminal
589
 
        old_has_ansi_colors = terminal.has_ansi_colors
590
 
        terminal.has_ansi_colors = lambda: False
591
 
        try:
592
 
            self.assertRaises(NoColor, ShelveHunkSelector, [], True)
593
 
            self.assertRaises(NoColor, UnshelveHunkSelector, [], True)
594
 
            terminal.has_ansi_colors = lambda: True
595
 
            hs = ShelveHunkSelector([], color=True)
596
 
            hs = UnshelveHunkSelector([], color=True)
597
 
        finally:
598
 
            terminal.has_ansi_colors = old_has_ansi_colors
599
 
 
600
 
    def test_no_color(self):
601
 
        """Ensure --no-color switch can be passed"""
602
 
        self.tree = self.make_branch_and_tree('.')
603
 
 
604
 
        subdir = 'subdir'
605
 
        os.mkdir(subdir)
606
 
        self.tree.add(subdir)
607
 
        os.chdir(subdir)
608
 
 
609
 
        self.__create_and_add_test_file()
610
 
 
611
 
        # Modify the test file
612
 
        # write in binary mode because on win32 line-endings should be LF
613
 
        f = file('test_file', 'wb')
614
 
        f.write(self.MODIFIED)
615
 
        f.close()
616
 
        stdout, error = self.run_bzr_captured(['shelve', '--all', 
617
 
                                               '--no-color'])
618
 
        stdout, error = self.run_bzr_captured(['unshelve', '--all', 
619
 
                                               '--no-color'])
620
 
 
621
 
    def test_shelf_help(self):
622
 
        self.assertContainsRe(cmd_shelf().help(),
623
 
                              'list\n.*List the patches on the current shelf')