/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
1658.1.9 by Martin Pool
Give an error for bzr diff on an nonexistent file (Malone #3619)
1
# Copyright (C) 2005, 2006 by Canonical Ltd
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
2
#
1185.50.44 by John Arbash Meinel
[patch] Robey Pointer: diff -r 1.. should diff against working tree.
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
7
#
1185.50.44 by John Arbash Meinel
[patch] Robey Pointer: diff -r 1.. should diff against working tree.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
12
#
1185.50.44 by John Arbash Meinel
[patch] Robey Pointer: diff -r 1.. should diff against working tree.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
18
"""Black-box tests for bzr diff.
19
"""
20
21
import os
1740.2.5 by Aaron Bentley
Merge from bzr.dev
22
import re
1185.50.44 by John Arbash Meinel
[patch] Robey Pointer: diff -r 1.. should diff against working tree.
23
24
import bzrlib
25
from bzrlib.branch import Branch
1551.7.19 by Aaron Bentley
Always include working tree when calculating file ids for diff
26
from bzrlib import workingtree
1185.50.44 by John Arbash Meinel
[patch] Robey Pointer: diff -r 1.. should diff against working tree.
27
from bzrlib.tests.blackbox import ExternalBase
28
29
1740.2.5 by Aaron Bentley
Merge from bzr.dev
30
def subst_dates(string):
31
    """Replace date strings with constant values."""
32
    return re.sub(r'\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} [-\+]\d{4}',
33
                  'YYYY-MM-DD HH:MM:SS +ZZZZ', string)
34
35
1185.50.44 by John Arbash Meinel
[patch] Robey Pointer: diff -r 1.. should diff against working tree.
36
class TestDiff(ExternalBase):
1658.1.9 by Martin Pool
Give an error for bzr diff on an nonexistent file (Malone #3619)
37
38
    def make_example_branch(test):
1185.50.44 by John Arbash Meinel
[patch] Robey Pointer: diff -r 1.. should diff against working tree.
39
        # FIXME: copied from test_too_much -- share elsewhere?
40
        test.runbzr('init')
1711.7.14 by John Arbash Meinel
diff tests check exact texts, should use binary mode for files.
41
        file('hello', 'wb').write('foo\n')
1185.50.44 by John Arbash Meinel
[patch] Robey Pointer: diff -r 1.. should diff against working tree.
42
        test.runbzr('add hello')
43
        test.runbzr('commit -m setup hello')
1711.7.14 by John Arbash Meinel
diff tests check exact texts, should use binary mode for files.
44
        file('goodbye', 'wb').write('baz\n')
1185.50.44 by John Arbash Meinel
[patch] Robey Pointer: diff -r 1.. should diff against working tree.
45
        test.runbzr('add goodbye')
46
        test.runbzr('commit -m setup goodbye')
47
48
    def test_diff(self):
1658.1.9 by Martin Pool
Give an error for bzr diff on an nonexistent file (Malone #3619)
49
        self.make_example_branch()
1185.50.44 by John Arbash Meinel
[patch] Robey Pointer: diff -r 1.. should diff against working tree.
50
        file('hello', 'wt').write('hello world!')
51
        self.runbzr('commit -m fixing hello')
52
        output = self.runbzr('diff -r 2..3', backtick=1, retcode=1)
53
        self.assert_('\n+hello world!' in output)
54
        output = self.runbzr('diff -r last:3..last:1', backtick=1, retcode=1)
55
        self.assert_('\n+baz' in output)
56
        file('moo', 'wb').write('moo')
57
        self.runbzr('add moo')
58
        os.unlink('moo')
59
        self.runbzr('diff')
60
1694.2.2 by Martin Pool
Add test for diff --diff-prefix, which was previously untested.
61
    def test_diff_prefix(self):
1694.2.3 by Martin Pool
Add -p0, -p1 options for diff.
62
        """diff --prefix appends to filenames in output"""
63
        self.make_example_branch()
1711.7.14 by John Arbash Meinel
diff tests check exact texts, should use binary mode for files.
64
        file('hello', 'wb').write('hello world!\n')
1694.2.3 by Martin Pool
Add -p0, -p1 options for diff.
65
        out, err = self.runbzr('diff --prefix old/:new/', retcode=1)
66
        self.assertEquals(err, '')
1740.2.5 by Aaron Bentley
Merge from bzr.dev
67
        self.assertEqualDiff(subst_dates(out), '''\
1694.2.4 by Martin Pool
When a diff prefix is given, don't show it in === summary lines, only on the diffs themselves.
68
=== modified file 'hello'
1740.2.5 by Aaron Bentley
Merge from bzr.dev
69
--- old/hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
70
+++ new/hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
1694.2.3 by Martin Pool
Add -p0, -p1 options for diff.
71
@@ -1,1 +1,1 @@
72
-foo
73
+hello world!
74
75
''')
76
77
    def test_diff_p1(self):
78
        """diff -p1 produces lkml-style diffs"""
79
        self.make_example_branch()
1711.7.14 by John Arbash Meinel
diff tests check exact texts, should use binary mode for files.
80
        file('hello', 'wb').write('hello world!\n')
1694.2.3 by Martin Pool
Add -p0, -p1 options for diff.
81
        out, err = self.runbzr('diff -p1', retcode=1)
82
        self.assertEquals(err, '')
1740.2.5 by Aaron Bentley
Merge from bzr.dev
83
        self.assertEqualDiff(subst_dates(out), '''\
1694.2.4 by Martin Pool
When a diff prefix is given, don't show it in === summary lines, only on the diffs themselves.
84
=== modified file 'hello'
1740.2.5 by Aaron Bentley
Merge from bzr.dev
85
--- old/hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
86
+++ new/hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
1694.2.3 by Martin Pool
Add -p0, -p1 options for diff.
87
@@ -1,1 +1,1 @@
88
-foo
89
+hello world!
90
91
''')
92
93
    def test_diff_p0(self):
94
        """diff -p0 produces diffs with no prefix"""
95
        self.make_example_branch()
1711.7.14 by John Arbash Meinel
diff tests check exact texts, should use binary mode for files.
96
        file('hello', 'wb').write('hello world!\n')
1694.2.3 by Martin Pool
Add -p0, -p1 options for diff.
97
        out, err = self.runbzr('diff -p0', retcode=1)
98
        self.assertEquals(err, '')
1740.2.5 by Aaron Bentley
Merge from bzr.dev
99
        self.assertEqualDiff(subst_dates(out), '''\
1694.2.3 by Martin Pool
Add -p0, -p1 options for diff.
100
=== modified file 'hello'
1740.2.5 by Aaron Bentley
Merge from bzr.dev
101
--- hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
102
+++ hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
1694.2.2 by Martin Pool
Add test for diff --diff-prefix, which was previously untested.
103
@@ -1,1 +1,1 @@
104
-foo
105
+hello world!
106
107
''')
108
1658.1.9 by Martin Pool
Give an error for bzr diff on an nonexistent file (Malone #3619)
109
    def test_diff_nonexistent(self):
110
        # Get an error from a file that does not exist at all
111
        # (Malone #3619)
112
        self.make_example_branch()
113
        out, err = self.runbzr('diff does-not-exist', retcode=3)
114
        self.assertContainsRe(err, 'not versioned.*does-not-exist')
115
1658.1.10 by Martin Pool
diff on unversiond files should give an error (Malone #3619)
116
    def test_diff_unversioned(self):
117
        # Get an error when diffing a non-versioned file.
118
        # (Malone #3619)
119
        self.make_example_branch()
120
        self.build_tree(['unversioned-file'])
121
        out, err = self.runbzr('diff unversioned-file', retcode=3)
122
        self.assertContainsRe(err, 'not versioned.*unversioned-file')
123
124
    # TODO: What should diff say for a file deleted in working tree?
1658.1.9 by Martin Pool
Give an error for bzr diff on an nonexistent file (Malone #3619)
125
1551.2.13 by Aaron Bentley
Got diff working properly with checkouts
126
    def example_branches(self):
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
127
        self.build_tree(['branch1/', 'branch1/file'], line_endings='binary')
128
        self.capture('init branch1')
129
        self.capture('add branch1/file')
130
        self.run_bzr_captured(['commit', '-m', 'add file', 'branch1'])
131
        self.capture('branch branch1 branch2')
1185.50.44 by John Arbash Meinel
[patch] Robey Pointer: diff -r 1.. should diff against working tree.
132
        print >> open('branch2/file', 'wb'), 'new content'
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
133
        self.run_bzr_captured(['commit', '-m', 'update file', 'branch2'])
1551.2.13 by Aaron Bentley
Got diff working properly with checkouts
134
135
    def test_diff_branches(self):
136
        self.example_branches()
1185.50.44 by John Arbash Meinel
[patch] Robey Pointer: diff -r 1.. should diff against working tree.
137
        # should open branch1 and diff against branch2, 
1740.2.5 by Aaron Bentley
Merge from bzr.dev
138
        out, err = self.run_bzr_captured(['diff', '-r', 'branch:branch2', 
139
                                          'branch1'],
140
                                         retcode=1)
141
        self.assertEquals('', err)
142
        self.assertEquals("=== modified file 'file'\n"
143
                          "--- file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
144
                          "+++ file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
145
                          "@@ -1,1 +1,1 @@\n"
146
                          "-new content\n"
147
                          "+contents of branch1/file\n"
148
                          "\n", subst_dates(out))
1881.1.2 by Matthieu Moy
Formatting and style for the last patch.
149
        out, err = self.run_bzr_captured(['diff', 'branch2', 'branch1'],
1740.2.5 by Aaron Bentley
Merge from bzr.dev
150
                                         retcode=1)
151
        self.assertEquals('', err)
152
        self.assertEqualDiff("=== modified file 'file'\n"
153
                              "--- file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
154
                              "+++ file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
1185.50.44 by John Arbash Meinel
[patch] Robey Pointer: diff -r 1.. should diff against working tree.
155
                              "@@ -1,1 +1,1 @@\n"
156
                              "-new content\n"
157
                              "+contents of branch1/file\n"
1740.2.5 by Aaron Bentley
Merge from bzr.dev
158
                              "\n", subst_dates(out))
1185.50.44 by John Arbash Meinel
[patch] Robey Pointer: diff -r 1.. should diff against working tree.
159
1732.3.3 by Matthieu Moy
Testcases for revno:N:path
160
    def test_diff_revno_branches(self):
161
        self.example_branches()
162
        print >> open('branch2/file', 'wb'), 'even newer content'
1881.1.2 by Matthieu Moy
Formatting and style for the last patch.
163
        self.run_bzr_captured(['commit', '-m', 
164
                               'update file once more', 'branch2'])
1732.3.3 by Matthieu Moy
Testcases for revno:N:path
165
1881.1.2 by Matthieu Moy
Formatting and style for the last patch.
166
        out, err = self.run_bzr_captured(['diff', '-r',
167
                                          'revno:1:branch2..revno:1:branch1'],
1732.3.3 by Matthieu Moy
Testcases for revno:N:path
168
                                         retcode=0)
169
        self.assertEquals('', err)
170
        self.assertEquals('', out)
1881.1.2 by Matthieu Moy
Formatting and style for the last patch.
171
        out, err = self.run_bzr_captured(['diff', '-r', 
172
                                          'revno:2:branch2..revno:1:branch1'],
1732.3.3 by Matthieu Moy
Testcases for revno:N:path
173
                                         retcode=1)
174
        self.assertEquals('', err)
175
        self.assertEqualDiff("=== modified file 'file'\n"
176
                              "--- file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
177
                              "+++ file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
178
                              "@@ -1,1 +1,1 @@\n"
179
                              "-new content\n"
180
                              "+contents of branch1/file\n"
181
                              "\n", subst_dates(out))
182
1551.2.13 by Aaron Bentley
Got diff working properly with checkouts
183
    def example_branch2(self):
1185.50.44 by John Arbash Meinel
[patch] Robey Pointer: diff -r 1.. should diff against working tree.
184
        self.build_tree(['branch1/', 'branch1/file1'], line_endings='binary')
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
185
        self.capture('init branch1')
186
        self.capture('add branch1/file1')
1185.50.44 by John Arbash Meinel
[patch] Robey Pointer: diff -r 1.. should diff against working tree.
187
        print >> open('branch1/file1', 'wb'), 'original line'
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
188
        self.run_bzr_captured(['commit', '-m', 'first commit', 'branch1'])
1185.50.44 by John Arbash Meinel
[patch] Robey Pointer: diff -r 1.. should diff against working tree.
189
        
190
        print >> open('branch1/file1', 'wb'), 'repo line'
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
191
        self.run_bzr_captured(['commit', '-m', 'second commit', 'branch1'])
1551.2.13 by Aaron Bentley
Got diff working properly with checkouts
192
193
    def test_diff_to_working_tree(self):
194
        self.example_branch2()
1185.50.44 by John Arbash Meinel
[patch] Robey Pointer: diff -r 1.. should diff against working tree.
195
        
196
        print >> open('branch1/file1', 'wb'), 'new line'
1881.1.2 by Matthieu Moy
Formatting and style for the last patch.
197
        output = self.run_bzr_captured(['diff', '-r', '1..', 'branch1'],
198
                                       retcode=1)
1185.50.44 by John Arbash Meinel
[patch] Robey Pointer: diff -r 1.. should diff against working tree.
199
        self.assertTrue('\n-original line\n+new line\n' in output[0])
1551.2.13 by Aaron Bentley
Got diff working properly with checkouts
200
1551.7.19 by Aaron Bentley
Always include working tree when calculating file ids for diff
201
    def test_diff_across_rename(self):
202
        """The working tree path should always be considered for diffing"""
203
        self.make_example_branch()
204
        self.run_bzr('diff', '-r', '0..1', 'hello', retcode=1)
205
        wt = workingtree.WorkingTree.open_containing('.')[0]
206
        wt.rename_one('hello', 'hello1')
207
        self.run_bzr('diff', 'hello1', retcode=1)
208
        self.run_bzr('diff', '-r', '0..1', 'hello1', retcode=1)
209
1551.2.13 by Aaron Bentley
Got diff working properly with checkouts
210
211
class TestCheckoutDiff(TestDiff):
212
1658.1.9 by Martin Pool
Give an error for bzr diff on an nonexistent file (Malone #3619)
213
    def make_example_branch(self):
214
        super(TestCheckoutDiff, self).make_example_branch()
1551.2.13 by Aaron Bentley
Got diff working properly with checkouts
215
        self.runbzr('checkout . checkout')
216
        os.chdir('checkout')
217
218
    def example_branch2(self):
219
        super(TestCheckoutDiff, self).example_branch2()
220
        os.mkdir('checkouts')
221
        self.runbzr('checkout branch1 checkouts/branch1')
222
        os.chdir('checkouts')
223
224
    def example_branches(self):
225
        super(TestCheckoutDiff, self).example_branches()
226
        os.mkdir('checkouts')
227
        self.runbzr('checkout branch1 checkouts/branch1')
228
        self.runbzr('checkout branch2 checkouts/branch2')
229
        os.chdir('checkouts')
1583.1.1 by Michael Ellerman
Change to -p1 format diffs. Update existing tests to cope, and add some
230
1658.1.9 by Martin Pool
Give an error for bzr diff on an nonexistent file (Malone #3619)
231
1583.1.1 by Michael Ellerman
Change to -p1 format diffs. Update existing tests to cope, and add some
232
class TestDiffLabels(TestDiff):
233
234
    def test_diff_label_removed(self):
1658.1.9 by Martin Pool
Give an error for bzr diff on an nonexistent file (Malone #3619)
235
        super(TestDiffLabels, self).make_example_branch()
1583.1.1 by Michael Ellerman
Change to -p1 format diffs. Update existing tests to cope, and add some
236
        self.runbzr('remove hello')
237
        diff = self.run_bzr_captured(['diff'], retcode=1)
1694.2.1 by Martin Pool
Remove 'a/', 'b/' default prefixes on diff output.
238
        self.assertTrue("=== removed file 'hello'" in diff[0])
1583.1.1 by Michael Ellerman
Change to -p1 format diffs. Update existing tests to cope, and add some
239
240
    def test_diff_label_added(self):
1658.1.9 by Martin Pool
Give an error for bzr diff on an nonexistent file (Malone #3619)
241
        super(TestDiffLabels, self).make_example_branch()
1583.1.1 by Michael Ellerman
Change to -p1 format diffs. Update existing tests to cope, and add some
242
        file('barbar', 'wt').write('barbar')
243
        self.runbzr('add barbar')
244
        diff = self.run_bzr_captured(['diff'], retcode=1)
1694.2.1 by Martin Pool
Remove 'a/', 'b/' default prefixes on diff output.
245
        self.assertTrue("=== added file 'barbar'" in diff[0])
1583.1.1 by Michael Ellerman
Change to -p1 format diffs. Update existing tests to cope, and add some
246
247
    def test_diff_label_modified(self):
1658.1.9 by Martin Pool
Give an error for bzr diff on an nonexistent file (Malone #3619)
248
        super(TestDiffLabels, self).make_example_branch()
1583.1.1 by Michael Ellerman
Change to -p1 format diffs. Update existing tests to cope, and add some
249
        file('hello', 'wt').write('barbar')
250
        diff = self.run_bzr_captured(['diff'], retcode=1)
1694.2.1 by Martin Pool
Remove 'a/', 'b/' default prefixes on diff output.
251
        self.assertTrue("=== modified file 'hello'" in diff[0])
1583.1.1 by Michael Ellerman
Change to -p1 format diffs. Update existing tests to cope, and add some
252
253
    def test_diff_label_renamed(self):
1658.1.9 by Martin Pool
Give an error for bzr diff on an nonexistent file (Malone #3619)
254
        super(TestDiffLabels, self).make_example_branch()
1583.1.1 by Michael Ellerman
Change to -p1 format diffs. Update existing tests to cope, and add some
255
        self.runbzr('rename hello gruezi')
256
        diff = self.run_bzr_captured(['diff'], retcode=1)
1694.2.1 by Martin Pool
Remove 'a/', 'b/' default prefixes on diff output.
257
        self.assertTrue("=== renamed file 'hello' => 'gruezi'" in diff[0])