~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Vincent Ladeuil
  • Date: 2010-03-02 10:21:39 UTC
  • mfrom: (4797.2.24 2.1)
  • mto: This revision was merged to the branch mainline in revision 5069.
  • Revision ID: v.ladeuil+lp@free.fr-20100302102139-b5cba7h6xu13mekg
Merge 2.1 into trunk including fixes for #331095, #507557, #185103, #524184 and #369501

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
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
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
 
18
18
"""Black-box tests for bzr diff.
21
21
import os
22
22
import re
23
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
 
24
from bzrlib import (
 
25
    tests,
 
26
    workingtree,
 
27
    )
29
28
 
30
29
 
31
30
def subst_dates(string):
34
33
                  'YYYY-MM-DD HH:MM:SS +ZZZZ', string)
35
34
 
36
35
 
37
 
class DiffBase(ExternalBase):
 
36
class DiffBase(tests.TestCaseWithTransport):
38
37
    """Base class with common setup method"""
39
38
 
40
39
    def make_example_branch(self):
126
125
        # Get an error from a file that does not exist at all
127
126
        # (Malone #3619)
128
127
        self.make_example_branch()
129
 
        out, err = self.run_bzr('diff does-not-exist', retcode=3)
130
 
        self.assertContainsRe(err, 'not versioned.*does-not-exist')
 
128
        out, err = self.run_bzr('diff does-not-exist', retcode=3,
 
129
            error_regexes=('not versioned.*does-not-exist',))
131
130
 
132
131
    def test_diff_illegal_revision_specifiers(self):
133
 
        out, err = self.run_bzr('diff -r 1..23..123', retcode=3)
134
 
        self.assertContainsRe(err, 'one or two revision specifiers')
 
132
        out, err = self.run_bzr('diff -r 1..23..123', retcode=3,
 
133
            error_regexes=('one or two revision specifiers',))
 
134
 
 
135
    def test_diff_nonexistent_revision(self):
 
136
        out, err = self.run_bzr('diff -r 123', retcode=3,
 
137
            error_regexes=("Requested revision: '123' does not "
 
138
                "exist in branch:",))
 
139
 
 
140
    def test_diff_nonexistent_dotted_revision(self):
 
141
        out, err = self.run_bzr('diff -r 1.1', retcode=3)
 
142
        self.assertContainsRe(err,
 
143
            "Requested revision: '1.1' does not exist in branch:")
 
144
 
 
145
    def test_diff_nonexistent_dotted_revision_change(self):
 
146
        out, err = self.run_bzr('diff -c 1.1', retcode=3)
 
147
        self.assertContainsRe(err,
 
148
            "Requested revision: '1.1' does not exist in branch:")
135
149
 
136
150
    def test_diff_unversioned(self):
137
151
        # Get an error when diffing a non-versioned file.
146
160
    def example_branches(self):
147
161
        branch1_tree = self.make_branch_and_tree('branch1')
148
162
        self.build_tree(['branch1/file'], line_endings='binary')
 
163
        self.build_tree(['branch1/file2'], line_endings='binary')
149
164
        branch1_tree.add('file')
150
 
        branch1_tree.commit(message='add file')
 
165
        branch1_tree.add('file2')
 
166
        branch1_tree.commit(message='add file and file2')
151
167
        branch2_tree = branch1_tree.bzrdir.sprout('branch2').open_workingtree()
152
168
        self.build_tree_contents([('branch2/file', 'new content\n')])
153
169
        branch2_tree.commit(message='update file')
154
170
        return branch1_tree, branch2_tree
155
171
 
156
 
    def test_diff_branches(self):
157
 
        self.example_branches()
158
 
        # should open branch1 and diff against branch2, 
159
 
        out, err = self.run_bzr('diff -r branch:branch2 branch1',
160
 
                                retcode=1)
 
172
    def check_b2_vs_b1(self, cmd):
 
173
        # Compare branch2 vs branch1 using cmd and check the result
 
174
        out, err = self.run_bzr(cmd, retcode=1)
161
175
        self.assertEquals('', err)
162
176
        self.assertEquals("=== modified file 'file'\n"
163
177
                          "--- file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
166
180
                          "-new content\n"
167
181
                          "+contents of branch1/file\n"
168
182
                          "\n", subst_dates(out))
169
 
        out, err = self.run_bzr('diff branch2 branch1',
170
 
                                         retcode=1)
 
183
 
 
184
    def check_b1_vs_b2(self, cmd):
 
185
        # Compare branch1 vs branch2 using cmd and check the result
 
186
        out, err = self.run_bzr(cmd, retcode=1)
171
187
        self.assertEquals('', err)
172
188
        self.assertEqualDiff("=== modified file 'file'\n"
173
189
                              "--- file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
174
190
                              "+++ file\tYYYY-MM-DD HH:MM:SS +ZZZZ\n"
175
191
                              "@@ -1,1 +1,1 @@\n"
176
 
                              "-new content\n"
177
 
                              "+contents of branch1/file\n"
 
192
                              "-contents of branch1/file\n"
 
193
                              "+new content\n"
178
194
                              "\n", subst_dates(out))
179
195
 
 
196
    def check_no_diffs(self, cmd):
 
197
        # Check that running cmd returns an empty diff
 
198
        out, err = self.run_bzr(cmd, retcode=0)
 
199
        self.assertEquals('', err)
 
200
        self.assertEquals('', out)
 
201
 
 
202
    def test_diff_branches(self):
 
203
        self.example_branches()
 
204
        # should open branch1 and diff against branch2,
 
205
        self.check_b2_vs_b1('diff -r branch:branch2 branch1')
 
206
        # Compare two working trees using various syntax forms
 
207
        self.check_b2_vs_b1('diff --old branch2 --new branch1')
 
208
        self.check_b2_vs_b1('diff --old branch2 branch1')
 
209
        self.check_b2_vs_b1('diff branch2 --new branch1')
 
210
        # Test with a selected file that was changed
 
211
        self.check_b2_vs_b1('diff --old branch2 --new branch1 file')
 
212
        self.check_b2_vs_b1('diff --old branch2 branch1/file')
 
213
        self.check_b2_vs_b1('diff branch2/file --new branch1')
 
214
        # Test with a selected file that was not changed
 
215
        self.check_no_diffs('diff --old branch2 --new branch1 file2')
 
216
        self.check_no_diffs('diff --old branch2 branch1/file2')
 
217
        self.check_no_diffs('diff branch2/file2 --new branch1')
 
218
 
 
219
    def test_diff_branches_no_working_trees(self):
 
220
        branch1_tree, branch2_tree = self.example_branches()
 
221
        # Compare a working tree to a branch without a WT
 
222
        dir1 = branch1_tree.bzrdir
 
223
        dir1.destroy_workingtree()
 
224
        self.assertFalse(dir1.has_workingtree())
 
225
        self.check_b2_vs_b1('diff --old branch2 --new branch1')
 
226
        self.check_b2_vs_b1('diff --old branch2 branch1')
 
227
        self.check_b2_vs_b1('diff branch2 --new branch1')
 
228
        # Compare a branch without a WT to one with a WT
 
229
        self.check_b1_vs_b2('diff --old branch1 --new branch2')
 
230
        self.check_b1_vs_b2('diff --old branch1 branch2')
 
231
        self.check_b1_vs_b2('diff branch1 --new branch2')
 
232
        # Compare a branch with a WT against another without a WT
 
233
        dir2 = branch2_tree.bzrdir
 
234
        dir2.destroy_workingtree()
 
235
        self.assertFalse(dir2.has_workingtree())
 
236
        self.check_b1_vs_b2('diff --old branch1 --new branch2')
 
237
        self.check_b1_vs_b2('diff --old branch1 branch2')
 
238
        self.check_b1_vs_b2('diff branch1 --new branch2')
 
239
 
180
240
    def test_diff_revno_branches(self):
181
241
        self.example_branches()
182
242
        branch2_tree = workingtree.WorkingTree.open_containing('branch2')[0]
213
273
        output = self.run_bzr('diff -r 1.. branch1', retcode=1)
214
274
        self.assertContainsRe(output[0], '\n\\-original line\n\\+new line\n')
215
275
 
 
276
    def test_diff_to_working_tree_in_subdir(self):
 
277
        self.example_branch2()
 
278
        self.build_tree_contents([('branch1/file1', 'new line')])
 
279
        os.mkdir('branch1/dir1')
 
280
        os.chdir('branch1/dir1')
 
281
        output = self.run_bzr('diff -r 1..', retcode=1)
 
282
        self.assertContainsRe(output[0], '\n\\-original line\n\\+new line\n')
 
283
 
216
284
    def test_diff_across_rename(self):
217
285
        """The working tree path should always be considered for diffing"""
218
286
        tree = self.make_example_branch()
221
289
        self.run_bzr('diff hello1', retcode=1)
222
290
        self.run_bzr('diff -r 0..1 hello1', retcode=1)
223
291
 
 
292
    def test_diff_to_branch_no_working_tree(self):
 
293
        branch1_tree = self.example_branch2()
 
294
        dir1 = branch1_tree.bzrdir
 
295
        dir1.destroy_workingtree()
 
296
        self.assertFalse(dir1.has_workingtree())
 
297
        output = self.run_bzr('diff -r 1.. branch1', retcode=1)
 
298
        self.assertContainsRe(output[0], '\n\\-original line\n\\+repo line\n')
 
299
 
224
300
 
225
301
class TestCheckoutDiff(TestDiff):
226
302
 
278
354
 
279
355
    def test_external_diff(self):
280
356
        """Test that we can spawn an external diff process"""
 
357
        self.disable_missing_extensions_warning()
281
358
        # We have to use run_bzr_subprocess, because we need to
282
359
        # test writing directly to stdout, (there was a bug in
283
360
        # subprocess.py that we had to workaround).
284
361
        # However, if 'diff' may not be available
285
362
        self.make_example_branch()
286
 
        orig_progress = os.environ.get('BZR_PROGRESS_BAR')
287
 
        try:
288
 
            os.environ['BZR_PROGRESS_BAR'] = 'none'
289
 
            out, err = self.run_bzr_subprocess('diff -r 1 --diff-options -ub',
290
 
                                               universal_newlines=True,
291
 
                                               retcode=None)
292
 
        finally:
293
 
            if orig_progress is None:
294
 
                del os.environ['BZR_PROGRESS_BAR']
295
 
            else:
296
 
                os.environ['BZR_PROGRESS_BAR'] = orig_progress
297
 
            
 
363
        # this will be automatically restored by the base bzr test class
 
364
        os.environ['BZR_PROGRESS_BAR'] = 'none'
 
365
        out, err = self.run_bzr_subprocess('diff -r 1 --diff-options -ub',
 
366
                                           universal_newlines=True,
 
367
                                           retcode=None)
298
368
        if 'Diff is not installed on this machine' in err:
299
 
            raise TestSkipped("No external 'diff' is available")
 
369
            raise tests.TestSkipped("No external 'diff' is available")
300
370
        self.assertEqual('', err)
301
371
        # We have to skip the stuff in the middle, because it depends
302
372
        # on time.time()