~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')
1694.2.2 by Martin Pool
Add test for diff --diff-prefix, which was previously untested.
33
        file('hello', 'wt').write('foo\n')
1185.50.44 by John Arbash Meinel
[patch] Robey Pointer: diff -r 1.. should diff against working tree.
34
        test.runbzr('add hello')
35
        test.runbzr('commit -m setup hello')
1694.2.2 by Martin Pool
Add test for diff --diff-prefix, which was previously untested.
36
        file('goodbye', 'wt').write('baz\n')
1185.50.44 by John Arbash Meinel
[patch] Robey Pointer: diff -r 1.. should diff against working tree.
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
1694.2.2 by Martin Pool
Add test for diff --diff-prefix, which was previously untested.
53
    def test_diff_prefix(self):
54
        self.make_example_branch()
55
        file('hello', 'wt').write('hello world!\n')
56
        out, err = self.runbzr('diff --diff-prefix old/:new/', retcode=1)
57
        self.assertEquals(err, '')
58
        self.assertEqualDiff(out, '''\
59
=== modified file 'old/hello'
60
--- old/hello\t
61
+++ new/hello\t
62
@@ -1,1 +1,1 @@
63
-foo
64
+hello world!
65
66
''')
67
1658.1.9 by Martin Pool
Give an error for bzr diff on an nonexistent file (Malone #3619)
68
    def test_diff_nonexistent(self):
69
        # Get an error from a file that does not exist at all
70
        # (Malone #3619)
71
        self.make_example_branch()
72
        out, err = self.runbzr('diff does-not-exist', retcode=3)
73
        self.assertContainsRe(err, 'not versioned.*does-not-exist')
74
1658.1.10 by Martin Pool
diff on unversiond files should give an error (Malone #3619)
75
    def test_diff_unversioned(self):
76
        # Get an error when diffing a non-versioned file.
77
        # (Malone #3619)
78
        self.make_example_branch()
79
        self.build_tree(['unversioned-file'])
80
        out, err = self.runbzr('diff unversioned-file', retcode=3)
81
        self.assertContainsRe(err, 'not versioned.*unversioned-file')
82
83
    # 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)
84
1551.2.13 by Aaron Bentley
Got diff working properly with checkouts
85
    def example_branches(self):
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
86
        self.build_tree(['branch1/', 'branch1/file'], line_endings='binary')
87
        self.capture('init branch1')
88
        self.capture('add branch1/file')
89
        self.run_bzr_captured(['commit', '-m', 'add file', 'branch1'])
90
        self.capture('branch branch1 branch2')
1185.50.44 by John Arbash Meinel
[patch] Robey Pointer: diff -r 1.. should diff against working tree.
91
        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.
92
        self.run_bzr_captured(['commit', '-m', 'update file', 'branch2'])
1551.2.13 by Aaron Bentley
Got diff working properly with checkouts
93
94
    def test_diff_branches(self):
95
        self.example_branches()
1185.50.44 by John Arbash Meinel
[patch] Robey Pointer: diff -r 1.. should diff against working tree.
96
        # should open branch1 and diff against branch2, 
97
        output = self.run_bzr_captured(['diff', '-r', 'branch:branch2', 
98
                                        'branch1'],
99
                                       retcode=1)
1694.2.1 by Martin Pool
Remove 'a/', 'b/' default prefixes on diff output.
100
        self.assertEquals(("=== modified file 'file'\n"
101
                           "--- file\t\n"
102
                           "+++ file\t\n"
1185.50.44 by John Arbash Meinel
[patch] Robey Pointer: diff -r 1.. should diff against working tree.
103
                           "@@ -1,1 +1,1 @@\n"
104
                           "-new content\n"
105
                           "+contents of branch1/file\n"
106
                           "\n", ''), output)
107
        output = self.run_bzr_captured(['diff', 'branch2', 'branch1'],
108
                                       retcode=1)
1694.2.1 by Martin Pool
Remove 'a/', 'b/' default prefixes on diff output.
109
        self.assertEqualDiff(("=== modified file 'file'\n"
110
                              "--- file\t\n"
111
                              "+++ file\t\n"
1185.50.44 by John Arbash Meinel
[patch] Robey Pointer: diff -r 1.. should diff against working tree.
112
                              "@@ -1,1 +1,1 @@\n"
113
                              "-new content\n"
114
                              "+contents of branch1/file\n"
115
                              "\n", ''), output)
116
1551.2.13 by Aaron Bentley
Got diff working properly with checkouts
117
    def example_branch2(self):
1185.50.44 by John Arbash Meinel
[patch] Robey Pointer: diff -r 1.. should diff against working tree.
118
        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.
119
        self.capture('init branch1')
120
        self.capture('add branch1/file1')
1185.50.44 by John Arbash Meinel
[patch] Robey Pointer: diff -r 1.. should diff against working tree.
121
        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.
122
        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.
123
        
124
        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.
125
        self.run_bzr_captured(['commit', '-m', 'second commit', 'branch1'])
1551.2.13 by Aaron Bentley
Got diff working properly with checkouts
126
127
    def test_diff_to_working_tree(self):
128
        self.example_branch2()
1185.50.44 by John Arbash Meinel
[patch] Robey Pointer: diff -r 1.. should diff against working tree.
129
        
130
        print >> open('branch1/file1', 'wb'), 'new line'
131
        output = self.run_bzr_captured(['diff', '-r', '1..', 'branch1'], retcode=1)
132
        self.assertTrue('\n-original line\n+new line\n' in output[0])
1551.2.13 by Aaron Bentley
Got diff working properly with checkouts
133
134
135
class TestCheckoutDiff(TestDiff):
136
1658.1.9 by Martin Pool
Give an error for bzr diff on an nonexistent file (Malone #3619)
137
    def make_example_branch(self):
138
        super(TestCheckoutDiff, self).make_example_branch()
1551.2.13 by Aaron Bentley
Got diff working properly with checkouts
139
        self.runbzr('checkout . checkout')
140
        os.chdir('checkout')
141
142
    def example_branch2(self):
143
        super(TestCheckoutDiff, self).example_branch2()
144
        os.mkdir('checkouts')
145
        self.runbzr('checkout branch1 checkouts/branch1')
146
        os.chdir('checkouts')
147
148
    def example_branches(self):
149
        super(TestCheckoutDiff, self).example_branches()
150
        os.mkdir('checkouts')
151
        self.runbzr('checkout branch1 checkouts/branch1')
152
        self.runbzr('checkout branch2 checkouts/branch2')
153
        os.chdir('checkouts')
1583.1.1 by Michael Ellerman
Change to -p1 format diffs. Update existing tests to cope, and add some
154
1658.1.9 by Martin Pool
Give an error for bzr diff on an nonexistent file (Malone #3619)
155
1583.1.1 by Michael Ellerman
Change to -p1 format diffs. Update existing tests to cope, and add some
156
class TestDiffLabels(TestDiff):
157
158
    def test_diff_label_removed(self):
1658.1.9 by Martin Pool
Give an error for bzr diff on an nonexistent file (Malone #3619)
159
        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
160
        self.runbzr('remove hello')
161
        diff = self.run_bzr_captured(['diff'], retcode=1)
1694.2.1 by Martin Pool
Remove 'a/', 'b/' default prefixes on diff output.
162
        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
163
164
    def test_diff_label_added(self):
1658.1.9 by Martin Pool
Give an error for bzr diff on an nonexistent file (Malone #3619)
165
        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
166
        file('barbar', 'wt').write('barbar')
167
        self.runbzr('add barbar')
168
        diff = self.run_bzr_captured(['diff'], retcode=1)
1694.2.1 by Martin Pool
Remove 'a/', 'b/' default prefixes on diff output.
169
        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
170
171
    def test_diff_label_modified(self):
1658.1.9 by Martin Pool
Give an error for bzr diff on an nonexistent file (Malone #3619)
172
        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
173
        file('hello', 'wt').write('barbar')
174
        diff = self.run_bzr_captured(['diff'], retcode=1)
1694.2.1 by Martin Pool
Remove 'a/', 'b/' default prefixes on diff output.
175
        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
176
177
    def test_diff_label_renamed(self):
1658.1.9 by Martin Pool
Give an error for bzr diff on an nonexistent file (Malone #3619)
178
        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
179
        self.runbzr('rename hello gruezi')
180
        diff = self.run_bzr_captured(['diff'], retcode=1)
1694.2.1 by Martin Pool
Remove 'a/', 'b/' default prefixes on diff output.
181
        self.assertTrue("=== renamed file 'hello' => 'gruezi'" in diff[0])