~abentley/bzrtools/bzrtools.dev

0.1.29 by Michael Ellerman
Add basic tests for shelve --all, and unshelve.
1
#!/usr/bin/python
2
3
import bzrlib.selftest
281 by Aaron Bentley
Handled whitespace branch names better
4
import os
0.1.29 by Michael Ellerman
Add basic tests for shelve --all, and unshelve.
5
class ShelfTests(bzrlib.selftest.TestCaseInTempDir):
6
    ORIGINAL = '\n\nhello test world\n\n'
7
    MODIFIED = '\n\ngoodbye test world\n\n'
8
    DIFF_HEADER = "=== modified file 'test_file'\n"
0.1.40 by Michael Ellerman
Update test with revision to actually test the shelf worked properly.
9
    DIFF_1 = """--- test_file
0.1.29 by Michael Ellerman
Add basic tests for shelve --all, and unshelve.
10
+++ test_file
11
@@ -1,4 +1,4 @@
12
 
13
 
14
-hello test world
15
+goodbye test world
16
 
17
"""
0.1.40 by Michael Ellerman
Update test with revision to actually test the shelf worked properly.
18
    DIFF_2 = """--- test_file
19
+++ test_file
20
@@ -1,4 +1,4 @@
21
 
22
 
23
-goodbye test world
24
+hello test world
25
 
26
"""
0.1.29 by Michael Ellerman
Add basic tests for shelve --all, and unshelve.
27
    def test_shelf(self):
28
        from bzrlib.branch import Branch
29
        b = Branch.initialize('.')
30
31
        # Create a test file and commit it
32
        file('test_file', 'w').write(self.ORIGINAL)
33
        b.add('test_file')
34
        b.commit(message='add test_file')
35
36
        # Modify the test file
37
        file('test_file', 'w').write(self.MODIFIED)
38
39
        # Check the diff is right
271 by Aaron Bentley
Cherry-picked Robert's diff and push fixes
40
        self.assertEqual(self.capture('diff', retcode=1),
0.1.40 by Michael Ellerman
Update test with revision to actually test the shelf worked properly.
41
            self.DIFF_HEADER + self.DIFF_1 + '\n')
0.1.29 by Michael Ellerman
Add basic tests for shelve --all, and unshelve.
42
43
        # Shelve the changes
281 by Aaron Bentley
Handled whitespace branch names better
44
        self.run_bzr('shelve', '--all', retcode=1)
0.1.29 by Michael Ellerman
Add basic tests for shelve --all, and unshelve.
45
46
        # Make sure there is no diff anymore
47
        self.assertEqual(self.capture('diff'), '')
48
49
        # Make sure the file is actually back the way it was
50
        self.assertEqual(file('test_file').read(), self.ORIGINAL)
51
52
        # Check the shelf is right
53
        shelf = file('.bzr-shelf').read()
0.1.40 by Michael Ellerman
Update test with revision to actually test the shelf worked properly.
54
        self.assertEqual(shelf, self.DIFF_1)
0.1.29 by Michael Ellerman
Add basic tests for shelve --all, and unshelve.
55
56
        # Unshelve
282 by Aaron Bentley
Fixed unshelve return code
57
        self.run_bzr('unshelve', retcode=1)
0.1.29 by Michael Ellerman
Add basic tests for shelve --all, and unshelve.
58
59
        # Check the diff is right again
271 by Aaron Bentley
Cherry-picked Robert's diff and push fixes
60
        self.assertEqual(self.capture('diff', retcode=1),
0.1.40 by Michael Ellerman
Update test with revision to actually test the shelf worked properly.
61
            self.DIFF_HEADER + self.DIFF_1 + '\n')
0.1.29 by Michael Ellerman
Add basic tests for shelve --all, and unshelve.
62
63
        # Make sure the file is back the way it should be
64
        self.assertEqual(file('test_file').read(), self.MODIFIED)
65
66
    def test_shelf_nothing_to_shelve(self):
67
        import os.path
68
        from bzrlib.branch import Branch
69
        b = Branch.initialize('.')
70
71
        # Create a test file and commit it
72
        file('test_file', 'w').write(self.ORIGINAL)
73
        b.add('test_file')
74
        b.commit(message='add test_file')
75
76
        # Shelve the changes
281 by Aaron Bentley
Handled whitespace branch names better
77
        self.run_bzr('shelve', '--all')
0.1.29 by Michael Ellerman
Add basic tests for shelve --all, and unshelve.
78
79
        if os.path.exists('.bzr-shelf'):
80
            self.fail("Shelf exists, but it shouldn't")
0.1.37 by Michael Ellerman
Add (failing) tests of revision argument for shelve.
81
82
    def test_shelf_with_revision(self):
83
        from bzrlib.branch import Branch
84
        b = Branch.initialize('.')
85
86
        # Create a test file and commit it
87
        file('test_file', 'w').write(self.ORIGINAL)
88
        b.add('test_file')
89
        b.commit(message='add test_file')
90
91
        # Modify the test file and commit it
92
        file('test_file', 'w').write(self.MODIFIED)
93
        b.commit(message='update test_file')
94
95
        # Shelve the changes
281 by Aaron Bentley
Handled whitespace branch names better
96
        self.run_bzr('shelve', '-r', '1', '--all', retcode=1)
0.1.37 by Michael Ellerman
Add (failing) tests of revision argument for shelve.
97
0.1.40 by Michael Ellerman
Update test with revision to actually test the shelf worked properly.
98
        # Check the diff is right
271 by Aaron Bentley
Cherry-picked Robert's diff and push fixes
99
        self.assertEqual(self.capture('diff', retcode=1),
0.1.40 by Michael Ellerman
Update test with revision to actually test the shelf worked properly.
100
            self.DIFF_HEADER + self.DIFF_2 + '\n')
101
102
        # Make sure the file is the way it should be
103
        self.assertEqual(file('test_file').read(), self.ORIGINAL)
104
105
        # Unshelve
282 by Aaron Bentley
Fixed unshelve return code
106
        self.run_bzr('unshelve', retcode=1)
0.1.40 by Michael Ellerman
Update test with revision to actually test the shelf worked properly.
107
108
        # Make sure the file is back the way it should be
109
        self.assertEqual(file('test_file').read(), self.MODIFIED)
110
0.1.37 by Michael Ellerman
Add (failing) tests of revision argument for shelve.
111
    def test_shelf_with_two_revisions(self):
112
        from bzrlib.branch import Branch
113
        b = Branch.initialize('.')
114
115
        cmd = 'shelve -r 1..2'
116
        (stdout, stderr) = self.run_bzr_captured(cmd.split(), retcode=None)
117
118
        self.assertEqual(stderr.split('\n')[0],
119
            'bzr: ERROR: shelve only accepts a single revision parameter.')
281 by Aaron Bentley
Handled whitespace branch names better
120
121
    def disabled_test_shelf_with_whitespace(self):
122
        """Shows that bzr doesn't handle whitespace well"""
123
        self.run_bzr('init')
124
        file('file\t name', 'wb').write('hello')
125
        self.run_bzr('add')
126
        self.run_bzr('commit', '-m', 'added')
127
        file('file\t name', 'wb').write('goodbye')
128
        self.run_bzr('shelve', '--all', 'file name')
129
130
    def test_whitespace_branches(self):
131
        os.mkdir('name with space')
132
        os.chdir('name with space')
133
        self.run_bzr('init')
134
        file('filename', 'wb').write('hello')
135
        self.run_bzr('add')
136
        self.run_bzr('commit', '-m', 'added')
137
        file('filename', 'wb').write('goodbye')
138
        self.run_bzr('shelve', '--all', 'filename', retcode=1)
139
        os.chdir('..')