~abentley/bzrtools/bzrtools.dev

0.1.29 by Michael Ellerman
Add basic tests for shelve --all, and unshelve.
1
#!/usr/bin/python
2
0.1.41 by Michael Ellerman
Update tests for changes in bzr:
3
import bzrlib.tests
0.2.6 by Michael Ellerman
Make tests work with new bzr API, we can't use _transport anymore.
4
import os.path
0.1.29 by Michael Ellerman
Add basic tests for shelve --all, and unshelve.
5
0.2.26 by Michael Ellerman
Update tests for BzrDir changes, use test helpers to save future pain.
6
class ShelfTests(bzrlib.tests.TestCaseWithTransport):
0.1.29 by Michael Ellerman
Add basic tests for shelve --all, and unshelve.
7
    ORIGINAL = '\n\nhello test world\n\n'
8
    MODIFIED = '\n\ngoodbye test world\n\n'
9
    DIFF_HEADER = "=== modified file 'test_file'\n"
0.1.41 by Michael Ellerman
Update tests for changes in bzr:
10
    DIFF_1 = """--- test_file\t
11
+++ test_file\t
0.1.29 by Michael Ellerman
Add basic tests for shelve --all, and unshelve.
12
@@ -1,4 +1,4 @@
13
 
14
 
15
-hello test world
16
+goodbye test world
17
 
18
"""
0.1.41 by Michael Ellerman
Update tests for changes in bzr:
19
    DIFF_2 = """--- test_file\t
20
+++ test_file\t
0.1.40 by Michael Ellerman
Update test with revision to actually test the shelf worked properly.
21
@@ -1,4 +1,4 @@
22
 
23
 
24
-goodbye test world
25
+hello test world
26
 
27
"""
0.1.29 by Michael Ellerman
Add basic tests for shelve --all, and unshelve.
28
    def test_shelf(self):
0.1.42 by Michael Ellerman
Add tests for new shelf layout.
29
        self.__test_loop(1)
30
31
    def test_shelf_multi(self):
32
        self.__test_loop(10)
33
34
    def __test_loop(self, count):
0.2.26 by Michael Ellerman
Update tests for BzrDir changes, use test helpers to save future pain.
35
        tree = self.make_branch_and_tree('.')
36
        self.__create_and_add_test_file(tree)
0.1.29 by Michael Ellerman
Add basic tests for shelve --all, and unshelve.
37
0.1.42 by Michael Ellerman
Add tests for new shelf layout.
38
        while count > 0:
39
            count -= 1
40
41
            # Modify the test file
42
            file('test_file', 'w').write(self.MODIFIED)
43
44
            # Check the diff is right
45
            self.assertEqual(self.capture('diff', retcode=1),
46
                self.DIFF_HEADER + self.DIFF_1 + '\n')
47
48
            # Shelve the changes
0.1.62 by Michael Ellerman
Update tests for new return codes from error commands.
49
            self.run_bzr('shelve', retcode=0)
0.1.42 by Michael Ellerman
Add tests for new shelf layout.
50
51
            # Make sure there is no diff anymore
52
            self.assertEqual(self.capture('diff', retcode=0), '')
53
54
            # Make sure the file is actually back the way it was
55
            self.assertEqual(file('test_file').read(), self.ORIGINAL)
56
57
            # Check the shelf is right
0.2.26 by Michael Ellerman
Update tests for BzrDir changes, use test helpers to save future pain.
58
            shelf = open(os.path.join(tree.branch.base,
59
                        '.shelf/shelves/default/00')).read()
0.2.11 by Michael Ellerman
Fix test now that we have a message always.
60
            shelf = shelf[shelf.index('\n') + 1:] # skip the message
0.1.42 by Michael Ellerman
Add tests for new shelf layout.
61
            self.assertEqual(shelf, self.DIFF_1)
62
63
            # Unshelve
0.1.62 by Michael Ellerman
Update tests for new return codes from error commands.
64
            self.run_bzr('unshelve', retcode=0)
0.1.42 by Michael Ellerman
Add tests for new shelf layout.
65
66
            # Check the diff is right again
67
            self.assertEqual(self.capture('diff', retcode=1),
68
                self.DIFF_HEADER + self.DIFF_1 + '\n')
69
70
            # Make sure the file is back the way it should be
71
            self.assertEqual(file('test_file').read(), self.MODIFIED)
0.1.29 by Michael Ellerman
Add basic tests for shelve --all, and unshelve.
72
73
    def test_shelf_nothing_to_shelve(self):
74
        import os.path
0.2.26 by Michael Ellerman
Update tests for BzrDir changes, use test helpers to save future pain.
75
        tree = self.make_branch_and_tree('.')
76
        self.__create_and_add_test_file(tree)
0.1.29 by Michael Ellerman
Add basic tests for shelve --all, and unshelve.
77
78
        # Shelve the changes
0.1.68 by Michael Ellerman
Fix tests, now that we don't wrap exceptions we return 3 on error, not 1.
79
        self.run_bzr('shelve', retcode=3)
0.1.29 by Michael Ellerman
Add basic tests for shelve --all, and unshelve.
80
0.2.26 by Michael Ellerman
Update tests for BzrDir changes, use test helpers to save future pain.
81
        if os.path.exists(os.path.join(tree.branch.base,
82
                '.shelf/shelves/default/00')):
0.1.29 by Michael Ellerman
Add basic tests for shelve --all, and unshelve.
83
            self.fail("Shelf exists, but it shouldn't")
0.1.37 by Michael Ellerman
Add (failing) tests of revision argument for shelve.
84
0.2.26 by Michael Ellerman
Update tests for BzrDir changes, use test helpers to save future pain.
85
    def __create_and_add_test_file(self, tree):
86
        self.build_tree_contents([('test_file', self.ORIGINAL)])
87
        tree.add('test_file')
88
        tree.commit(message='add test_file')
89
0.1.37 by Michael Ellerman
Add (failing) tests of revision argument for shelve.
90
    def test_shelf_with_revision(self):
0.2.26 by Michael Ellerman
Update tests for BzrDir changes, use test helpers to save future pain.
91
        tree = self.make_branch_and_tree('.')
0.1.37 by Michael Ellerman
Add (failing) tests of revision argument for shelve.
92
0.2.26 by Michael Ellerman
Update tests for BzrDir changes, use test helpers to save future pain.
93
        self.__create_and_add_test_file(tree)
0.1.37 by Michael Ellerman
Add (failing) tests of revision argument for shelve.
94
95
        # Modify the test file and commit it
0.2.26 by Michael Ellerman
Update tests for BzrDir changes, use test helpers to save future pain.
96
        self.build_tree_contents([('test_file', self.MODIFIED)])
97
        tree.commit(message='update test_file')
0.1.37 by Michael Ellerman
Add (failing) tests of revision argument for shelve.
98
99
        # Shelve the changes
0.1.62 by Michael Ellerman
Update tests for new return codes from error commands.
100
        self.run_bzr('shelve', '-r', '1', retcode=0)
0.1.37 by Michael Ellerman
Add (failing) tests of revision argument for shelve.
101
0.1.40 by Michael Ellerman
Update test with revision to actually test the shelf worked properly.
102
        # Check the diff is right
0.1.41 by Michael Ellerman
Update tests for changes in bzr:
103
        self.assertEqual(self.capture('diff', retcode=1),
0.1.40 by Michael Ellerman
Update test with revision to actually test the shelf worked properly.
104
            self.DIFF_HEADER + self.DIFF_2 + '\n')
105
106
        # Make sure the file is the way it should be
107
        self.assertEqual(file('test_file').read(), self.ORIGINAL)
108
109
        # Unshelve
0.1.62 by Michael Ellerman
Update tests for new return codes from error commands.
110
        self.run_bzr('unshelve', retcode=0)
0.1.40 by Michael Ellerman
Update test with revision to actually test the shelf worked properly.
111
112
        # Make sure the file is back the way it should be
113
        self.assertEqual(file('test_file').read(), self.MODIFIED)
114
0.1.37 by Michael Ellerman
Add (failing) tests of revision argument for shelve.
115
    def test_shelf_with_two_revisions(self):
0.2.26 by Michael Ellerman
Update tests for BzrDir changes, use test helpers to save future pain.
116
        tree = self.make_branch_and_tree('.')
0.1.37 by Michael Ellerman
Add (failing) tests of revision argument for shelve.
117
118
        cmd = 'shelve -r 1..2'
119
        (stdout, stderr) = self.run_bzr_captured(cmd.split(), retcode=None)
120
121
        self.assertEqual(stderr.split('\n')[0],
0.1.66 by Michael Ellerman
Update tests for cleaner exceptions
122
            'bzr: ERROR: shelve only accepts a single revision parameter.')
0.1.72 by Michael Ellerman
Merge Dafydd Harries' fix for long filenames in diffstat
123
124
    def test_shelf_long_filename(self):
125
        """Regression test for diffstat with long filenames.
126
127
        Create a branch with two files, one of which has a long name. Commit.
128
        Modify both files. Shelve the file with the short name. If diffstat
129
        has regressed, it will generate a diffstat of the file with the long
130
        name, and break.
131
        """
132
        self.run_bzr('init')
133
        filename = 'a' * 80
134
        file(filename, 'wb').write('hello')
135
        file('foo', 'wb').write('bar')
136
        self.run_bzr('add')
137
        self.run_bzr('commit', '-m', 'added')
138
        file(filename, 'wb').write('goodbye')
139
        file('foo', 'wb').write('baz')
140
        self.run_bzr('shelve', 'foo', retcode=0)
141
0.2.27 by Michael Ellerman
Add basic test for shelf show
142
    def test_shelf_show_basic(self):
143
        tree = self.make_branch_and_tree('.')
144
        self.__create_and_add_test_file(tree)
0.2.28 by Michael Ellerman
More tests of shelf show
145
        self.__test_show(tree, '00')
0.2.27 by Michael Ellerman
Add basic test for shelf show
146
0.2.28 by Michael Ellerman
More tests of shelf show
147
    def __test_show(self, tree, patch):
0.2.27 by Michael Ellerman
Add basic test for shelf show
148
        # Modify the test file
0.2.28 by Michael Ellerman
More tests of shelf show
149
        self.build_tree_contents([('test_file', 'patch %s\n' % patch)])
0.2.27 by Michael Ellerman
Add basic test for shelf show
150
151
        # Shelve the changes
152
        self.run_bzr('shelve', retcode=0)
153
154
        # Make sure there is no diff anymore
155
        self.assertEqual(self.capture('diff', retcode=0), '')
156
157
        # Check the shelf is right
158
        shelf = open(os.path.join(tree.branch.base,
0.2.28 by Michael Ellerman
More tests of shelf show
159
                    '.shelf/shelves/default', patch)).read()
160
        self.assertTrue('patch %s' % patch in shelf)
161
162
        # Check the shown output is right
163
        shown = self.capture('shelf show %s' % patch, retcode=0)
164
        self.assertEqual(shown, shelf)
165
166
    def test_shelf_show_multi(self):
167
        tree = self.make_branch_and_tree('.')
168
        self.__create_and_add_test_file(tree)
169
        self.__test_show(tree, '00')
170
        self.__test_show(tree, '01')
171
        self.__test_show(tree, '02')
172
173
        # Now check we can show a previously shelved patch
174
        shelf = open(os.path.join(tree.branch.base,
0.2.27 by Michael Ellerman
Add basic test for shelf show
175
                    '.shelf/shelves/default/00')).read()
0.2.28 by Michael Ellerman
More tests of shelf show
176
        self.assertTrue('patch 00' in shelf)
0.2.27 by Michael Ellerman
Add basic test for shelf show
177
178
        # Check the shown output is right
179
        shown = self.capture('shelf show 00', retcode=0)
180
        self.assertEqual(shown, shelf)