~abentley/bzrtools/bzrtools.dev

0.1.29 by Michael Ellerman
Add basic tests for shelve --all, and unshelve.
1
#!/usr/bin/python
2
281 by Aaron Bentley
Handled whitespace branch names better
3
import os
147.4.24 by Robert Collins
Merge from Aaron.
4
291 by Aaron Bentley
Adjusted to selftest -> tests change
5
import bzrlib.tests
0.1.29 by Michael Ellerman
Add basic tests for shelve --all, and unshelve.
6
147.4.24 by Robert Collins
Merge from Aaron.
7
291 by Aaron Bentley
Adjusted to selftest -> tests change
8
class ShelfTests(bzrlib.tests.TestCaseInTempDir):
0.1.29 by Michael Ellerman
Add basic tests for shelve --all, and unshelve.
9
    ORIGINAL = '\n\nhello test world\n\n'
10
    MODIFIED = '\n\ngoodbye test world\n\n'
320 by Aaron Bentley
Updated to match new bzr diff behaviour
11
    DIFF_HEADER = "=== modified file 'a/test_file'\n"
12
    DIFF_1 = """--- a/test_file\t
13
+++ b/test_file\t
0.1.29 by Michael Ellerman
Add basic tests for shelve --all, and unshelve.
14
@@ -1,4 +1,4 @@
15
 
16
 
17
-hello test world
18
+goodbye test world
19
 
20
"""
320 by Aaron Bentley
Updated to match new bzr diff behaviour
21
    DIFF_2 = """--- a/test_file\t
22
+++ b/test_file\t
0.1.40 by Michael Ellerman
Update test with revision to actually test the shelf worked properly.
23
@@ -1,4 +1,4 @@
24
 
25
 
26
-goodbye test world
27
+hello test world
28
 
29
"""
0.1.29 by Michael Ellerman
Add basic tests for shelve --all, and unshelve.
30
    def test_shelf(self):
313 by Aaron Bentley
Updated to match API changes
31
        from bzrlib.bzrdir import BzrDir
32
        tree = BzrDir.create_standalone_workingtree('.')
0.1.29 by Michael Ellerman
Add basic tests for shelve --all, and unshelve.
33
34
        # Create a test file and commit it
35
        file('test_file', 'w').write(self.ORIGINAL)
313 by Aaron Bentley
Updated to match API changes
36
        tree.add('test_file')
37
        tree.commit(message='add test_file')
0.1.29 by Michael Ellerman
Add basic tests for shelve --all, and unshelve.
38
39
        # Modify the test file
40
        file('test_file', 'w').write(self.MODIFIED)
41
42
        # Check the diff is right
147.4.28 by Robert Collins
merge in storage api support.
43
        self.assertEqualDiff(self.capture('diff', retcode=1),
0.1.40 by Michael Ellerman
Update test with revision to actually test the shelf worked properly.
44
            self.DIFF_HEADER + self.DIFF_1 + '\n')
0.1.29 by Michael Ellerman
Add basic tests for shelve --all, and unshelve.
45
46
        # Shelve the changes
281 by Aaron Bentley
Handled whitespace branch names better
47
        self.run_bzr('shelve', '--all', retcode=1)
0.1.29 by Michael Ellerman
Add basic tests for shelve --all, and unshelve.
48
49
        # Make sure there is no diff anymore
50
        self.assertEqual(self.capture('diff'), '')
51
52
        # Make sure the file is actually back the way it was
147.4.28 by Robert Collins
merge in storage api support.
53
        self.assertEqualDiff(file('test_file').read(), self.ORIGINAL)
0.1.29 by Michael Ellerman
Add basic tests for shelve --all, and unshelve.
54
55
        # Check the shelf is right
56
        shelf = file('.bzr-shelf').read()
0.1.40 by Michael Ellerman
Update test with revision to actually test the shelf worked properly.
57
        self.assertEqual(shelf, self.DIFF_1)
0.1.29 by Michael Ellerman
Add basic tests for shelve --all, and unshelve.
58
59
        # Unshelve
282 by Aaron Bentley
Fixed unshelve return code
60
        self.run_bzr('unshelve', retcode=1)
0.1.29 by Michael Ellerman
Add basic tests for shelve --all, and unshelve.
61
62
        # Check the diff is right again
271 by Aaron Bentley
Cherry-picked Robert's diff and push fixes
63
        self.assertEqual(self.capture('diff', retcode=1),
0.1.40 by Michael Ellerman
Update test with revision to actually test the shelf worked properly.
64
            self.DIFF_HEADER + self.DIFF_1 + '\n')
0.1.29 by Michael Ellerman
Add basic tests for shelve --all, and unshelve.
65
66
        # Make sure the file is back the way it should be
67
        self.assertEqual(file('test_file').read(), self.MODIFIED)
68
69
    def test_shelf_nothing_to_shelve(self):
70
        import os.path
313 by Aaron Bentley
Updated to match API changes
71
        from bzrlib.bzrdir import BzrDir
72
        tree = BzrDir.create_standalone_workingtree('.')
0.1.29 by Michael Ellerman
Add basic tests for shelve --all, and unshelve.
73
74
        # Create a test file and commit it
75
        file('test_file', 'w').write(self.ORIGINAL)
313 by Aaron Bentley
Updated to match API changes
76
        tree.add('test_file')
77
        tree.commit(message='add test_file')
0.1.29 by Michael Ellerman
Add basic tests for shelve --all, and unshelve.
78
79
        # Shelve the changes
281 by Aaron Bentley
Handled whitespace branch names better
80
        self.run_bzr('shelve', '--all')
0.1.29 by Michael Ellerman
Add basic tests for shelve --all, and unshelve.
81
82
        if os.path.exists('.bzr-shelf'):
83
            self.fail("Shelf exists, but it shouldn't")
0.1.37 by Michael Ellerman
Add (failing) tests of revision argument for shelve.
84
85
    def test_shelf_with_revision(self):
313 by Aaron Bentley
Updated to match API changes
86
        from bzrlib.bzrdir import BzrDir
87
        tree = BzrDir.create_standalone_workingtree('.')
0.1.37 by Michael Ellerman
Add (failing) tests of revision argument for shelve.
88
89
        # Create a test file and commit it
90
        file('test_file', 'w').write(self.ORIGINAL)
313 by Aaron Bentley
Updated to match API changes
91
        tree.add('test_file')
92
        tree.commit(message='add test_file')
0.1.37 by Michael Ellerman
Add (failing) tests of revision argument for shelve.
93
94
        # Modify the test file and commit it
95
        file('test_file', 'w').write(self.MODIFIED)
313 by Aaron Bentley
Updated to match API changes
96
        tree.commit(message='update test_file')
0.1.37 by Michael Ellerman
Add (failing) tests of revision argument for shelve.
97
98
        # Shelve the changes
281 by Aaron Bentley
Handled whitespace branch names better
99
        self.run_bzr('shelve', '-r', '1', '--all', retcode=1)
0.1.37 by Michael Ellerman
Add (failing) tests of revision argument for shelve.
100
0.1.40 by Michael Ellerman
Update test with revision to actually test the shelf worked properly.
101
        # Check the diff is right
271 by Aaron Bentley
Cherry-picked Robert's diff and push fixes
102
        self.assertEqual(self.capture('diff', retcode=1),
0.1.40 by Michael Ellerman
Update test with revision to actually test the shelf worked properly.
103
            self.DIFF_HEADER + self.DIFF_2 + '\n')
104
105
        # Make sure the file is the way it should be
106
        self.assertEqual(file('test_file').read(), self.ORIGINAL)
107
108
        # Unshelve
282 by Aaron Bentley
Fixed unshelve return code
109
        self.run_bzr('unshelve', retcode=1)
0.1.40 by Michael Ellerman
Update test with revision to actually test the shelf worked properly.
110
111
        # Make sure the file is back the way it should be
112
        self.assertEqual(file('test_file').read(), self.MODIFIED)
113
0.1.37 by Michael Ellerman
Add (failing) tests of revision argument for shelve.
114
    def test_shelf_with_two_revisions(self):
313 by Aaron Bentley
Updated to match API changes
115
        from bzrlib.bzrdir import BzrDir
116
        tree = BzrDir.create_standalone_workingtree('.')
0.1.37 by Michael Ellerman
Add (failing) tests of revision argument for shelve.
117
118
        cmd = 'shelve -r 1..2'
147.4.28 by Robert Collins
merge in storage api support.
119
        (stdout, stderr) = self.run_bzr_captured(cmd.split(), retcode=3)
0.1.37 by Michael Ellerman
Add (failing) tests of revision argument for shelve.
120
121
        self.assertEqual(stderr.split('\n')[0],
295 by Aaron Bentley
Fixed test case failure
122
            'bzr: ERROR: shelve only accepts a single revision parameter.')
281 by Aaron Bentley
Handled whitespace branch names better
123
286 by Aaron Bentley
Applied tabs_in_diff_filenames patch
124
    def test_shelf_with_whitespace(self):
281 by Aaron Bentley
Handled whitespace branch names better
125
        """Shows that bzr doesn't handle whitespace well"""
126
        self.run_bzr('init')
286 by Aaron Bentley
Applied tabs_in_diff_filenames patch
127
        file('file name', 'wb').write('hello')
281 by Aaron Bentley
Handled whitespace branch names better
128
        self.run_bzr('add')
129
        self.run_bzr('commit', '-m', 'added')
286 by Aaron Bentley
Applied tabs_in_diff_filenames patch
130
        file('file name', 'wb').write('goodbye')
131
        self.run_bzr('shelve', '--all', 'file name', retcode=1)
281 by Aaron Bentley
Handled whitespace branch names better
132
133
    def test_whitespace_branches(self):
134
        os.mkdir('name with space')
135
        os.chdir('name with space')
136
        self.run_bzr('init')
137
        file('filename', 'wb').write('hello')
138
        self.run_bzr('add')
139
        self.run_bzr('commit', '-m', 'added')
140
        file('filename', 'wb').write('goodbye')
141
        self.run_bzr('shelve', '--all', 'filename', retcode=1)
142
        os.chdir('..')
296.1.1 by Dafydd Harries
Fix a bug in diffstat that causes an infinite loop on long filenames.
143
144
    def test_long_filename(self):
145
        """Regression test for diffstat with long filenames.
146
147
        Create a branch with two files, one of which has a long name. Commit.
148
        Modify both files. Shelve the file with the short name. If diffstat
149
        has regressed, it will generate a diffstat of the file with the long
150
        name, and break.
151
        """
152
        self.run_bzr('init')
153
        filename = 'a' * 80
154
        file(filename, 'wb').write('hello')
155
        file('foo', 'wb').write('bar')
156
        self.run_bzr('add')
157
        self.run_bzr('commit', '-m', 'added')
158
        file(filename, 'wb').write('goodbye')
159
        file('foo', 'wb').write('baz')
160
        self.run_bzr('shelve', '--all', 'foo', retcode=1)
161