~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/blackbox/test_diff.py

  • Committer: Robert Collins
  • Date: 2005-10-06 22:15:52 UTC
  • mfrom: (1185.13.2)
  • mto: This revision was merged to the branch mainline in revision 1420.
  • Revision ID: robertc@robertcollins.net-20051006221552-9b15c96fa504e0ad
mergeĀ fromĀ upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006 Canonical Ltd
2
 
#
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.
7
 
#
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.
12
 
#
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
22
 
import re
23
 
 
24
 
import bzrlib
25
 
from bzrlib import workingtree
26
 
from bzrlib.branch import Branch
27
 
from bzrlib.tests import TestSkipped
28
 
from bzrlib.tests.blackbox import ExternalBase
29
 
 
30
 
 
31
 
def subst_dates(string):
32
 
    """Replace date strings with constant values."""
33
 
    return re.sub(r'\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} [-\+]\d{4}',
34
 
                  'YYYY-MM-DD HH:MM:SS +ZZZZ', string)
35
 
 
36
 
 
37
 
class DiffBase(ExternalBase):
38
 
    """Base class with common setup method"""
39
 
 
40
 
    def make_example_branch(self):
41
 
        # FIXME: copied from test_too_much -- share elsewhere?
42
 
        tree = self.make_branch_and_tree('.')
43
 
        open('hello', 'wb').write('foo\n')
44
 
        tree.add(['hello'])
45
 
        tree.commit('setup')
46
 
        open('goodbye', 'wb').write('baz\n')
47
 
        tree.add(['goodbye'])
48
 
        tree.commit('setup')
49
 
 
50
 
 
51
 
class TestDiff(DiffBase):
52
 
 
53
 
    def test_diff(self):
54
 
        self.make_example_branch()
55
 
        file('hello', 'wt').write('hello world!')
56
 
        self.runbzr('commit -m fixing hello')
57
 
        output = self.runbzr('diff -r 2..3', backtick=1, retcode=1)
58
 
        self.assert_('\n+hello world!' in output)
59
 
        output = self.runbzr('diff -r last:3..last:1', backtick=1, retcode=1)
60
 
        self.assert_('\n+baz' in output)
61
 
        file('moo', 'wb').write('moo')
62
 
        self.runbzr('add moo')
63
 
        os.unlink('moo')
64
 
        self.runbzr('diff')
65
 
 
66
 
    def test_diff_prefix(self):
67
 
        """diff --prefix appends to filenames in output"""
68
 
        self.make_example_branch()
69
 
        file('hello', 'wb').write('hello world!\n')
70
 
        out, err = self.runbzr('diff --prefix old/:new/', retcode=1)
71
 
        self.assertEquals(err, '')
72
 
        self.assertEqualDiff(subst_dates(out), '''\
73
 
=== modified file 'hello'
74
 
--- old/hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
75
 
+++ new/hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
76
 
@@ -1,1 +1,1 @@
77
 
-foo
78
 
+hello world!
79
 
 
80
 
''')
81
 
 
82
 
    def test_diff_p1(self):
83
 
        """diff -p1 produces lkml-style diffs"""
84
 
        self.make_example_branch()
85
 
        file('hello', 'wb').write('hello world!\n')
86
 
        out, err = self.runbzr('diff -p1', retcode=1)
87
 
        self.assertEquals(err, '')
88
 
        self.assertEqualDiff(subst_dates(out), '''\
89
 
=== modified file 'hello'
90
 
--- old/hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
91
 
+++ new/hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
92
 
@@ -1,1 +1,1 @@
93
 
-foo
94
 
+hello world!
95
 
 
96
 
''')
97
 
 
98
 
    def test_diff_p0(self):
99
 
        """diff -p0 produces diffs with no prefix"""
100
 
        self.make_example_branch()
101
 
        file('hello', 'wb').write('hello world!\n')
102
 
        out, err = self.runbzr('diff -p0', retcode=1)
103
 
        self.assertEquals(err, '')
104
 
        self.assertEqualDiff(subst_dates(out), '''\
105
 
=== modified file 'hello'
106
 
--- hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
107
 
+++ hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
108
 
@@ -1,1 +1,1 @@
109
 
-foo
110
 
+hello world!
111
 
 
112
 
''')
113
 
 
114
 
    def test_diff_nonexistent(self):
115
 
        # Get an error from a file that does not exist at all
116
 
        # (Malone #3619)
117
 
        self.make_example_branch()
118
 
        out, err = self.runbzr('diff does-not-exist', retcode=3)
119
 
        self.assertContainsRe(err, 'not versioned.*does-not-exist')
120
 
 
121
 
    def test_diff_illegal_revision_specifiers(self):
122
 
        out, err = self.runbzr('diff -r 1..23..123', retcode=3)
123
 
        self.assertContainsRe(err, 'one or two revision specifiers')
124
 
 
125
 
    def test_diff_unversioned(self):
126
 
        # Get an error when diffing a non-versioned file.
127
 
        # (Malone #3619)
128
 
        self.make_example_branch()
129
 
        self.build_tree(['unversioned-file'])
130
 
        out, err = self.runbzr('diff unversioned-file', retcode=3)
131
 
        self.assertContainsRe(err, 'not versioned.*unversioned-file')
132
 
 
133
 
    # TODO: What should diff say for a file deleted in working tree?
134
 
 
135
 
    def example_branches(self):
136
 
        self.build_tree(['branch1/', 'branch1/file'], line_endings='binary')
137
 
        self.capture('init branch1')
138
 
        self.capture('add branch1/file')
139
 
        self.run_bzr_captured(['commit', '-m', 'add file', 'branch1'])
140
 
        self.capture('branch branch1 branch2')
141
 
        print >> open('branch2/file', 'wb'), 'new content'
142
 
        self.run_bzr_captured(['commit', '-m', 'update file', 'branch2'])
143
 
 
144
 
    def test_diff_branches(self):
145
 
        self.example_branches()
146
 
        # should open branch1 and diff against branch2, 
147
 
        out, err = self.run_bzr_captured(['diff', '-r', 'branch:branch2', 
148
 
                                          'branch1'],
149
 
                                         retcode=1)
150
 
        self.assertEquals('', err)
151
 
        self.assertEquals("=== modified file 'file'\n"
152
 
                          "--- file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
153
 
                          "+++ file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
154
 
                          "@@ -1,1 +1,1 @@\n"
155
 
                          "-new content\n"
156
 
                          "+contents of branch1/file\n"
157
 
                          "\n", subst_dates(out))
158
 
        out, err = self.run_bzr_captured(['diff', 'branch2', 'branch1'],
159
 
                                         retcode=1)
160
 
        self.assertEquals('', err)
161
 
        self.assertEqualDiff("=== modified file 'file'\n"
162
 
                              "--- file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
163
 
                              "+++ file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
164
 
                              "@@ -1,1 +1,1 @@\n"
165
 
                              "-new content\n"
166
 
                              "+contents of branch1/file\n"
167
 
                              "\n", subst_dates(out))
168
 
 
169
 
    def test_diff_revno_branches(self):
170
 
        self.example_branches()
171
 
        print >> open('branch2/file', 'wb'), 'even newer content'
172
 
        self.run_bzr_captured(['commit', '-m', 
173
 
                               'update file once more', 'branch2'])
174
 
 
175
 
        out, err = self.run_bzr_captured(['diff', '-r',
176
 
                                          'revno:1:branch2..revno:1:branch1'],
177
 
                                         retcode=0)
178
 
        self.assertEquals('', err)
179
 
        self.assertEquals('', out)
180
 
        out, err = self.run_bzr_captured(['diff', '-r', 
181
 
                                          'revno:2:branch2..revno:1:branch1'],
182
 
                                         retcode=1)
183
 
        self.assertEquals('', err)
184
 
        self.assertEqualDiff("=== modified file 'file'\n"
185
 
                              "--- file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
186
 
                              "+++ file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
187
 
                              "@@ -1,1 +1,1 @@\n"
188
 
                              "-new content\n"
189
 
                              "+contents of branch1/file\n"
190
 
                              "\n", subst_dates(out))
191
 
 
192
 
    def example_branch2(self):
193
 
        self.build_tree(['branch1/', 'branch1/file1'], line_endings='binary')
194
 
        self.capture('init branch1')
195
 
        self.capture('add branch1/file1')
196
 
        print >> open('branch1/file1', 'wb'), 'original line'
197
 
        self.run_bzr_captured(['commit', '-m', 'first commit', 'branch1'])
198
 
        
199
 
        print >> open('branch1/file1', 'wb'), 'repo line'
200
 
        self.run_bzr_captured(['commit', '-m', 'second commit', 'branch1'])
201
 
 
202
 
    def test_diff_to_working_tree(self):
203
 
        self.example_branch2()
204
 
        
205
 
        print >> open('branch1/file1', 'wb'), 'new line'
206
 
        output = self.run_bzr_captured(['diff', '-r', '1..', 'branch1'],
207
 
                                       retcode=1)
208
 
        self.assertTrue('\n-original line\n+new line\n' in output[0])
209
 
 
210
 
    def test_diff_across_rename(self):
211
 
        """The working tree path should always be considered for diffing"""
212
 
        self.make_example_branch()
213
 
        self.run_bzr('diff', '-r', '0..1', 'hello', retcode=1)
214
 
        wt = workingtree.WorkingTree.open_containing('.')[0]
215
 
        wt.rename_one('hello', 'hello1')
216
 
        self.run_bzr('diff', 'hello1', retcode=1)
217
 
        self.run_bzr('diff', '-r', '0..1', 'hello1', retcode=1)
218
 
 
219
 
 
220
 
class TestCheckoutDiff(TestDiff):
221
 
 
222
 
    def make_example_branch(self):
223
 
        super(TestCheckoutDiff, self).make_example_branch()
224
 
        self.runbzr('checkout . checkout')
225
 
        os.chdir('checkout')
226
 
 
227
 
    def example_branch2(self):
228
 
        super(TestCheckoutDiff, self).example_branch2()
229
 
        os.mkdir('checkouts')
230
 
        self.runbzr('checkout branch1 checkouts/branch1')
231
 
        os.chdir('checkouts')
232
 
 
233
 
    def example_branches(self):
234
 
        super(TestCheckoutDiff, self).example_branches()
235
 
        os.mkdir('checkouts')
236
 
        self.runbzr('checkout branch1 checkouts/branch1')
237
 
        self.runbzr('checkout branch2 checkouts/branch2')
238
 
        os.chdir('checkouts')
239
 
 
240
 
 
241
 
class TestDiffLabels(DiffBase):
242
 
 
243
 
    def test_diff_label_removed(self):
244
 
        super(TestDiffLabels, self).make_example_branch()
245
 
        self.runbzr('remove hello')
246
 
        diff = self.run_bzr_captured(['diff'], retcode=1)
247
 
        self.assertTrue("=== removed file 'hello'" in diff[0])
248
 
 
249
 
    def test_diff_label_added(self):
250
 
        super(TestDiffLabels, self).make_example_branch()
251
 
        file('barbar', 'wt').write('barbar')
252
 
        self.runbzr('add barbar')
253
 
        diff = self.run_bzr_captured(['diff'], retcode=1)
254
 
        self.assertTrue("=== added file 'barbar'" in diff[0])
255
 
 
256
 
    def test_diff_label_modified(self):
257
 
        super(TestDiffLabels, self).make_example_branch()
258
 
        file('hello', 'wt').write('barbar')
259
 
        diff = self.run_bzr_captured(['diff'], retcode=1)
260
 
        self.assertTrue("=== modified file 'hello'" in diff[0])
261
 
 
262
 
    def test_diff_label_renamed(self):
263
 
        super(TestDiffLabels, self).make_example_branch()
264
 
        self.runbzr('rename hello gruezi')
265
 
        diff = self.run_bzr_captured(['diff'], retcode=1)
266
 
        self.assertTrue("=== renamed file 'hello' => 'gruezi'" in diff[0])
267
 
 
268
 
 
269
 
class TestExternalDiff(DiffBase):
270
 
 
271
 
    def test_external_diff(self):
272
 
        """Test that we can spawn an external diff process"""
273
 
        # We have to use run_bzr_subprocess, because we need to
274
 
        # test writing directly to stdout, (there was a bug in
275
 
        # subprocess.py that we had to workaround).
276
 
        # However, if 'diff' may not be available
277
 
        self.make_example_branch()
278
 
        orig_progress = os.environ.get('BZR_PROGRESS_BAR')
279
 
        try:
280
 
            os.environ['BZR_PROGRESS_BAR'] = 'none'
281
 
            out, err = self.run_bzr_subprocess('diff', '-r', '1',
282
 
                                               '--diff-options', '-ub',
283
 
                                               universal_newlines=True,
284
 
                                               retcode=None)
285
 
        finally:
286
 
            if orig_progress is None:
287
 
                del os.environ['BZR_PROGRESS_BAR']
288
 
            else:
289
 
                os.environ['BZR_PROGRESS_BAR'] = orig_progress
290
 
            
291
 
        if 'Diff is not installed on this machine' in err:
292
 
            raise TestSkipped("No external 'diff' is available")
293
 
        self.assertEqual('', err)
294
 
        # We have to skip the stuff in the middle, because it depends
295
 
        # on time.time()
296
 
        self.assertStartsWith(out, "=== added file 'goodbye'\n"
297
 
                                   "--- goodbye\t1970-01-01 00:00:00 +0000\n"
298
 
                                   "+++ goodbye\t")
299
 
        self.assertEndsWith(out, "\n@@ -0,0 +1 @@\n"
300
 
                                 "+baz\n\n")
301
 
 
302
 
 
303
 
class TestDiffOutput(DiffBase):
304
 
 
305
 
    def test_diff_output(self):
306
 
        # check that output doesn't mangle line-endings
307
 
        self.make_example_branch()
308
 
        file('hello', 'wb').write('hello world!\n')
309
 
        output = self.run_bzr_subprocess('diff', retcode=1)[0]
310
 
        self.assert_('\n+hello world!\n' in output)