~abentley/bzrtools/bzrtools.dev

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/python

import bzrlib.selftest

class ShelfTests(bzrlib.selftest.TestCaseInTempDir):
    ORIGINAL = '\n\nhello test world\n\n'
    MODIFIED = '\n\ngoodbye test world\n\n'
    DIFF_HEADER = "=== modified file 'test_file'\n"
    DIFF_1 = """--- test_file
+++ test_file
@@ -1,4 +1,4 @@
 
 
-hello test world
+goodbye test world
 
"""
    DIFF_2 = """--- test_file
+++ test_file
@@ -1,4 +1,4 @@
 
 
-goodbye test world
+hello test world
 
"""
    def test_shelf(self):
        from bzrlib.branch import Branch
        b = Branch.initialize('.')

        # Create a test file and commit it
        file('test_file', 'w').write(self.ORIGINAL)
        b.add('test_file')
        b.commit(message='add test_file')

        # Modify the test file
        file('test_file', 'w').write(self.MODIFIED)

        # Check the diff is right
        self.assertEqual(self.capture('diff'),
            self.DIFF_HEADER + self.DIFF_1 + '\n')

        # Shelve the changes
        self.run_bzr('shelve', '--all', retcode=True)

        # Make sure there is no diff anymore
        self.assertEqual(self.capture('diff'), '')

        # Make sure the file is actually back the way it was
        self.assertEqual(file('test_file').read(), self.ORIGINAL)

        # Check the shelf is right
        shelf = file('.bzr-shelf').read()
        self.assertEqual(shelf, self.DIFF_1)

        # Unshelve
        self.run_bzr('unshelve', retcode=True)

        # Check the diff is right again
        self.assertEqual(self.capture('diff'),
            self.DIFF_HEADER + self.DIFF_1 + '\n')

        # Make sure the file is back the way it should be
        self.assertEqual(file('test_file').read(), self.MODIFIED)

    def test_shelf_nothing_to_shelve(self):
        import os.path
        from bzrlib.branch import Branch
        b = Branch.initialize('.')

        # Create a test file and commit it
        file('test_file', 'w').write(self.ORIGINAL)
        b.add('test_file')
        b.commit(message='add test_file')

        # Shelve the changes
        self.run_bzr('shelve', '--all', retcode=True)

        if os.path.exists('.bzr-shelf'):
            self.fail("Shelf exists, but it shouldn't")

    def test_shelf_with_revision(self):
        from bzrlib.branch import Branch
        b = Branch.initialize('.')

        # Create a test file and commit it
        file('test_file', 'w').write(self.ORIGINAL)
        b.add('test_file')
        b.commit(message='add test_file')

        # Modify the test file and commit it
        file('test_file', 'w').write(self.MODIFIED)
        b.commit(message='update test_file')

        # Shelve the changes
        self.run_bzr('shelve', '-r', '1', '--all', retcode=True)

        # Check the diff is right
        self.assertEqual(self.capture('diff'),
            self.DIFF_HEADER + self.DIFF_2 + '\n')

        # Make sure the file is the way it should be
        self.assertEqual(file('test_file').read(), self.ORIGINAL)

        # Unshelve
        self.run_bzr('unshelve', retcode=True)

        # Make sure the file is back the way it should be
        self.assertEqual(file('test_file').read(), self.MODIFIED)

    def test_shelf_with_two_revisions(self):
        from bzrlib.branch import Branch
        b = Branch.initialize('.')

        cmd = 'shelve -r 1..2'
        (stdout, stderr) = self.run_bzr_captured(cmd.split(), retcode=None)

        self.assertEqual(stderr.split('\n')[0],
            'bzr: ERROR: shelve only accepts a single revision parameter.')