~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Martin Pool
  • Date: 2006-02-22 04:29:54 UTC
  • mfrom: (1566 +trunk)
  • mto: This revision was merged to the branch mainline in revision 1569.
  • Revision ID: mbp@sourcefrog.net-20060222042954-60333f08dd56a646
[merge] from bzr.dev before integration
Fix undefined ordering in sign_my_revisions breaking tests

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006 by Canonical Ltd
 
1
# Copyright (C) 2005 by Canonical Ltd
 
2
# -*- coding: utf-8 -*-
2
3
 
3
4
# This program is free software; you can redistribute it and/or modify
4
5
# it under the terms of the GNU General Public License as published by
19
20
"""
20
21
 
21
22
import os
22
 
import re
23
23
 
24
24
import bzrlib
25
25
from bzrlib.branch import Branch
26
 
from bzrlib import workingtree
27
26
from bzrlib.tests.blackbox import ExternalBase
28
27
 
29
28
 
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
 
 
36
29
class TestDiff(ExternalBase):
37
 
 
38
 
    def make_example_branch(test):
 
30
    def example_branch(test):
39
31
        # FIXME: copied from test_too_much -- share elsewhere?
40
32
        test.runbzr('init')
41
 
        file('hello', 'wb').write('foo\n')
 
33
        file('hello', 'wt').write('foo')
42
34
        test.runbzr('add hello')
43
35
        test.runbzr('commit -m setup hello')
44
 
        file('goodbye', 'wb').write('baz\n')
 
36
        file('goodbye', 'wt').write('baz')
45
37
        test.runbzr('add goodbye')
46
38
        test.runbzr('commit -m setup goodbye')
47
39
 
48
40
    def test_diff(self):
49
 
        self.make_example_branch()
 
41
        self.example_branch()
50
42
        file('hello', 'wt').write('hello world!')
51
43
        self.runbzr('commit -m fixing hello')
52
44
        output = self.runbzr('diff -r 2..3', backtick=1, retcode=1)
58
50
        os.unlink('moo')
59
51
        self.runbzr('diff')
60
52
 
61
 
    def test_diff_prefix(self):
62
 
        """diff --prefix appends to filenames in output"""
63
 
        self.make_example_branch()
64
 
        file('hello', 'wb').write('hello world!\n')
65
 
        out, err = self.runbzr('diff --prefix old/:new/', retcode=1)
66
 
        self.assertEquals(err, '')
67
 
        self.assertEqualDiff(subst_dates(out), '''\
68
 
=== modified file 'hello'
69
 
--- old/hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
70
 
+++ new/hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
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()
80
 
        file('hello', 'wb').write('hello world!\n')
81
 
        out, err = self.runbzr('diff -p1', retcode=1)
82
 
        self.assertEquals(err, '')
83
 
        self.assertEqualDiff(subst_dates(out), '''\
84
 
=== modified file 'hello'
85
 
--- old/hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
86
 
+++ new/hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
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()
96
 
        file('hello', 'wb').write('hello world!\n')
97
 
        out, err = self.runbzr('diff -p0', retcode=1)
98
 
        self.assertEquals(err, '')
99
 
        self.assertEqualDiff(subst_dates(out), '''\
100
 
=== modified file 'hello'
101
 
--- hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
102
 
+++ hello\tYYYY-MM-DD HH:MM:SS +ZZZZ
103
 
@@ -1,1 +1,1 @@
104
 
-foo
105
 
+hello world!
106
 
 
107
 
''')
108
 
 
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
 
 
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?
125
 
 
126
 
    def example_branches(self):
 
53
    def test_diff_branches(self):
127
54
        self.build_tree(['branch1/', 'branch1/file'], line_endings='binary')
128
55
        self.capture('init branch1')
129
56
        self.capture('add branch1/file')
131
58
        self.capture('branch branch1 branch2')
132
59
        print >> open('branch2/file', 'wb'), 'new content'
133
60
        self.run_bzr_captured(['commit', '-m', 'update file', 'branch2'])
134
 
 
135
 
    def test_diff_branches(self):
136
 
        self.example_branches()
137
61
        # should open branch1 and diff against branch2, 
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))
149
 
        out, ett = self.run_bzr_captured(['diff', 'branch2', 'branch1'],
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"
155
 
                              "@@ -1,1 +1,1 @@\n"
156
 
                              "-new content\n"
157
 
                              "+contents of branch1/file\n"
158
 
                              "\n", subst_dates(out))
159
 
 
160
 
    def test_diff_revno_branches(self):
161
 
        self.example_branches()
162
 
        print >> open('branch2/file', 'wb'), 'even newer content'
163
 
        self.run_bzr_captured(['commit', '-m', 'update file once more', 'branch2'])
164
 
 
165
 
        out, err = self.run_bzr_captured(['diff', '-r', 'revno:1:branch2..revno:1:branch1'],
166
 
                                         retcode=0)
167
 
        self.assertEquals('', err)
168
 
        self.assertEquals('', out)
169
 
        out, ett = self.run_bzr_captured(['diff', '-r', 'revno:2:branch2..revno:1:branch1'],
170
 
                                         retcode=1)
171
 
        self.assertEquals('', err)
172
 
        self.assertEqualDiff("=== modified file 'file'\n"
173
 
                              "--- file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
174
 
                              "+++ file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
175
 
                              "@@ -1,1 +1,1 @@\n"
176
 
                              "-new content\n"
177
 
                              "+contents of branch1/file\n"
178
 
                              "\n", subst_dates(out))
179
 
 
180
 
    def example_branch2(self):
 
62
        output = self.run_bzr_captured(['diff', '-r', 'branch:branch2', 
 
63
                                        'branch1'],
 
64
                                       retcode=1)
 
65
        self.assertEquals(("=== modified file 'file'\n"
 
66
                           "--- file\t\n"
 
67
                           "+++ file\t\n"
 
68
                           "@@ -1,1 +1,1 @@\n"
 
69
                           "-new content\n"
 
70
                           "+contents of branch1/file\n"
 
71
                           "\n", ''), output)
 
72
        output = self.run_bzr_captured(['diff', 'branch2', 'branch1'],
 
73
                                       retcode=1)
 
74
        self.assertEqualDiff(("=== modified file 'file'\n"
 
75
                              "--- file\t\n"
 
76
                              "+++ file\t\n"
 
77
                              "@@ -1,1 +1,1 @@\n"
 
78
                              "-new content\n"
 
79
                              "+contents of branch1/file\n"
 
80
                              "\n", ''), output)
 
81
 
 
82
    def test_diff_to_working_tree(self):
181
83
        self.build_tree(['branch1/', 'branch1/file1'], line_endings='binary')
182
84
        self.capture('init branch1')
183
85
        self.capture('add branch1/file1')
186
88
        
187
89
        print >> open('branch1/file1', 'wb'), 'repo line'
188
90
        self.run_bzr_captured(['commit', '-m', 'second commit', 'branch1'])
189
 
 
190
 
    def test_diff_to_working_tree(self):
191
 
        self.example_branch2()
192
91
        
193
92
        print >> open('branch1/file1', 'wb'), 'new line'
194
93
        output = self.run_bzr_captured(['diff', '-r', '1..', 'branch1'], retcode=1)
195
94
        self.assertTrue('\n-original line\n+new line\n' in output[0])
196
 
 
197
 
    def test_diff_across_rename(self):
198
 
        """The working tree path should always be considered for diffing"""
199
 
        self.make_example_branch()
200
 
        self.run_bzr('diff', '-r', '0..1', 'hello', retcode=1)
201
 
        wt = workingtree.WorkingTree.open_containing('.')[0]
202
 
        wt.rename_one('hello', 'hello1')
203
 
        self.run_bzr('diff', 'hello1', retcode=1)
204
 
        self.run_bzr('diff', '-r', '0..1', 'hello1', retcode=1)
205
 
 
206
 
 
207
 
class TestCheckoutDiff(TestDiff):
208
 
 
209
 
    def make_example_branch(self):
210
 
        super(TestCheckoutDiff, self).make_example_branch()
211
 
        self.runbzr('checkout . checkout')
212
 
        os.chdir('checkout')
213
 
 
214
 
    def example_branch2(self):
215
 
        super(TestCheckoutDiff, self).example_branch2()
216
 
        os.mkdir('checkouts')
217
 
        self.runbzr('checkout branch1 checkouts/branch1')
218
 
        os.chdir('checkouts')
219
 
 
220
 
    def example_branches(self):
221
 
        super(TestCheckoutDiff, self).example_branches()
222
 
        os.mkdir('checkouts')
223
 
        self.runbzr('checkout branch1 checkouts/branch1')
224
 
        self.runbzr('checkout branch2 checkouts/branch2')
225
 
        os.chdir('checkouts')
226
 
 
227
 
 
228
 
class TestDiffLabels(TestDiff):
229
 
 
230
 
    def test_diff_label_removed(self):
231
 
        super(TestDiffLabels, self).make_example_branch()
232
 
        self.runbzr('remove hello')
233
 
        diff = self.run_bzr_captured(['diff'], retcode=1)
234
 
        self.assertTrue("=== removed file 'hello'" in diff[0])
235
 
 
236
 
    def test_diff_label_added(self):
237
 
        super(TestDiffLabels, self).make_example_branch()
238
 
        file('barbar', 'wt').write('barbar')
239
 
        self.runbzr('add barbar')
240
 
        diff = self.run_bzr_captured(['diff'], retcode=1)
241
 
        self.assertTrue("=== added file 'barbar'" in diff[0])
242
 
 
243
 
    def test_diff_label_modified(self):
244
 
        super(TestDiffLabels, self).make_example_branch()
245
 
        file('hello', 'wt').write('barbar')
246
 
        diff = self.run_bzr_captured(['diff'], retcode=1)
247
 
        self.assertTrue("=== modified file 'hello'" in diff[0])
248
 
 
249
 
    def test_diff_label_renamed(self):
250
 
        super(TestDiffLabels, self).make_example_branch()
251
 
        self.runbzr('rename hello gruezi')
252
 
        diff = self.run_bzr_captured(['diff'], retcode=1)
253
 
        self.assertTrue("=== renamed file 'hello' => 'gruezi'" in diff[0])