~bzr-pqm/bzr/bzr.dev

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
1185.50.44 by John Arbash Meinel
[patch] Robey Pointer: diff -r 1.. should diff against working tree.
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
23
import bzrlib
24
from bzrlib.branch import Branch
25
from bzrlib.tests.blackbox import ExternalBase
26
27
28
class TestDiff(ExternalBase):
1658.1.9 by Martin Pool
Give an error for bzr diff on an nonexistent file (Malone #3619)
29
30
    def make_example_branch(test):
1185.50.44 by John Arbash Meinel
[patch] Robey Pointer: diff -r 1.. should diff against working tree.
31
        # FIXME: copied from test_too_much -- share elsewhere?
32
        test.runbzr('init')
33
        file('hello', 'wt').write('foo')
34
        test.runbzr('add hello')
35
        test.runbzr('commit -m setup hello')
36
        file('goodbye', 'wt').write('baz')
37
        test.runbzr('add goodbye')
38
        test.runbzr('commit -m setup goodbye')
39
40
    def test_diff(self):
1658.1.9 by Martin Pool
Give an error for bzr diff on an nonexistent file (Malone #3619)
41
        self.make_example_branch()
1185.50.44 by John Arbash Meinel
[patch] Robey Pointer: diff -r 1.. should diff against working tree.
42
        file('hello', 'wt').write('hello world!')
43
        self.runbzr('commit -m fixing hello')
44
        output = self.runbzr('diff -r 2..3', backtick=1, retcode=1)
45
        self.assert_('\n+hello world!' in output)
46
        output = self.runbzr('diff -r last:3..last:1', backtick=1, retcode=1)
47
        self.assert_('\n+baz' in output)
48
        file('moo', 'wb').write('moo')
49
        self.runbzr('add moo')
50
        os.unlink('moo')
51
        self.runbzr('diff')
52
1658.1.9 by Martin Pool
Give an error for bzr diff on an nonexistent file (Malone #3619)
53
    def test_diff_nonexistent(self):
54
        # Get an error from a file that does not exist at all
55
        # (Malone #3619)
56
        self.make_example_branch()
57
        out, err = self.runbzr('diff does-not-exist', retcode=3)
58
        self.assertContainsRe(err, 'not versioned.*does-not-exist')
59
1658.1.10 by Martin Pool
diff on unversiond files should give an error (Malone #3619)
60
    def test_diff_unversioned(self):
61
        # Get an error when diffing a non-versioned file.
62
        # (Malone #3619)
63
        self.make_example_branch()
64
        self.build_tree(['unversioned-file'])
65
        out, err = self.runbzr('diff unversioned-file', retcode=3)
66
        self.assertContainsRe(err, 'not versioned.*unversioned-file')
67
68
    # 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)
69
1551.2.13 by Aaron Bentley
Got diff working properly with checkouts
70
    def example_branches(self):
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
71
        self.build_tree(['branch1/', 'branch1/file'], line_endings='binary')
72
        self.capture('init branch1')
73
        self.capture('add branch1/file')
74
        self.run_bzr_captured(['commit', '-m', 'add file', 'branch1'])
75
        self.capture('branch branch1 branch2')
1185.50.44 by John Arbash Meinel
[patch] Robey Pointer: diff -r 1.. should diff against working tree.
76
        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.
77
        self.run_bzr_captured(['commit', '-m', 'update file', 'branch2'])
1551.2.13 by Aaron Bentley
Got diff working properly with checkouts
78
79
    def test_diff_branches(self):
80
        self.example_branches()
1185.50.44 by John Arbash Meinel
[patch] Robey Pointer: diff -r 1.. should diff against working tree.
81
        # should open branch1 and diff against branch2, 
82
        output = self.run_bzr_captured(['diff', '-r', 'branch:branch2', 
83
                                        'branch1'],
84
                                       retcode=1)
1583.1.1 by Michael Ellerman
Change to -p1 format diffs. Update existing tests to cope, and add some
85
        self.assertEquals(("=== modified file 'a/file'\n"
86
                           "--- a/file\t\n"
87
                           "+++ b/file\t\n"
1185.50.44 by John Arbash Meinel
[patch] Robey Pointer: diff -r 1.. should diff against working tree.
88
                           "@@ -1,1 +1,1 @@\n"
89
                           "-new content\n"
90
                           "+contents of branch1/file\n"
91
                           "\n", ''), output)
92
        output = self.run_bzr_captured(['diff', 'branch2', 'branch1'],
93
                                       retcode=1)
1583.1.1 by Michael Ellerman
Change to -p1 format diffs. Update existing tests to cope, and add some
94
        self.assertEqualDiff(("=== modified file 'a/file'\n"
95
                              "--- a/file\t\n"
96
                              "+++ b/file\t\n"
1185.50.44 by John Arbash Meinel
[patch] Robey Pointer: diff -r 1.. should diff against working tree.
97
                              "@@ -1,1 +1,1 @@\n"
98
                              "-new content\n"
99
                              "+contents of branch1/file\n"
100
                              "\n", ''), output)
101
1551.2.13 by Aaron Bentley
Got diff working properly with checkouts
102
    def example_branch2(self):
1185.50.44 by John Arbash Meinel
[patch] Robey Pointer: diff -r 1.. should diff against working tree.
103
        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.
104
        self.capture('init branch1')
105
        self.capture('add branch1/file1')
1185.50.44 by John Arbash Meinel
[patch] Robey Pointer: diff -r 1.. should diff against working tree.
106
        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.
107
        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.
108
        
109
        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.
110
        self.run_bzr_captured(['commit', '-m', 'second commit', 'branch1'])
1551.2.13 by Aaron Bentley
Got diff working properly with checkouts
111
112
    def test_diff_to_working_tree(self):
113
        self.example_branch2()
1185.50.44 by John Arbash Meinel
[patch] Robey Pointer: diff -r 1.. should diff against working tree.
114
        
115
        print >> open('branch1/file1', 'wb'), 'new line'
116
        output = self.run_bzr_captured(['diff', '-r', '1..', 'branch1'], retcode=1)
117
        self.assertTrue('\n-original line\n+new line\n' in output[0])
1551.2.13 by Aaron Bentley
Got diff working properly with checkouts
118
119
120
class TestCheckoutDiff(TestDiff):
121
1658.1.9 by Martin Pool
Give an error for bzr diff on an nonexistent file (Malone #3619)
122
    def make_example_branch(self):
123
        super(TestCheckoutDiff, self).make_example_branch()
1551.2.13 by Aaron Bentley
Got diff working properly with checkouts
124
        self.runbzr('checkout . checkout')
125
        os.chdir('checkout')
126
127
    def example_branch2(self):
128
        super(TestCheckoutDiff, self).example_branch2()
129
        os.mkdir('checkouts')
130
        self.runbzr('checkout branch1 checkouts/branch1')
131
        os.chdir('checkouts')
132
133
    def example_branches(self):
134
        super(TestCheckoutDiff, self).example_branches()
135
        os.mkdir('checkouts')
136
        self.runbzr('checkout branch1 checkouts/branch1')
137
        self.runbzr('checkout branch2 checkouts/branch2')
138
        os.chdir('checkouts')
1583.1.1 by Michael Ellerman
Change to -p1 format diffs. Update existing tests to cope, and add some
139
1658.1.9 by Martin Pool
Give an error for bzr diff on an nonexistent file (Malone #3619)
140
1583.1.1 by Michael Ellerman
Change to -p1 format diffs. Update existing tests to cope, and add some
141
class TestDiffLabels(TestDiff):
142
143
    def test_diff_label_removed(self):
1658.1.9 by Martin Pool
Give an error for bzr diff on an nonexistent file (Malone #3619)
144
        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
145
        self.runbzr('remove hello')
146
        diff = self.run_bzr_captured(['diff'], retcode=1)
147
        self.assertTrue("=== removed file 'a/hello'" in diff[0])
148
149
    def test_diff_label_added(self):
1658.1.9 by Martin Pool
Give an error for bzr diff on an nonexistent file (Malone #3619)
150
        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
151
        file('barbar', 'wt').write('barbar')
152
        self.runbzr('add barbar')
153
        diff = self.run_bzr_captured(['diff'], retcode=1)
154
        self.assertTrue("=== added file 'b/barbar'" in diff[0])
155
156
    def test_diff_label_modified(self):
1658.1.9 by Martin Pool
Give an error for bzr diff on an nonexistent file (Malone #3619)
157
        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
158
        file('hello', 'wt').write('barbar')
159
        diff = self.run_bzr_captured(['diff'], retcode=1)
160
        self.assertTrue("=== modified file 'a/hello'" in diff[0])
161
162
    def test_diff_label_renamed(self):
1658.1.9 by Martin Pool
Give an error for bzr diff on an nonexistent file (Malone #3619)
163
        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
164
        self.runbzr('rename hello gruezi')
165
        diff = self.run_bzr_captured(['diff'], retcode=1)
166
        self.assertTrue("=== renamed file 'a/hello' => 'b/gruezi'" in diff[0])