~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_mergetools.py

  • Committer: Patch Queue Manager
  • Date: 2016-02-01 19:56:05 UTC
  • mfrom: (6615.1.1 trunk)
  • Revision ID: pqm@pqm.ubuntu.com-20160201195605-o7rl92wf6uyum3fk
(vila) Open trunk again as 2.8b1 (Vincent Ladeuil)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2010 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
import os
 
18
import sys
 
19
import tempfile
 
20
 
 
21
from bzrlib import (
 
22
    mergetools,
 
23
    tests
 
24
)
 
25
 
 
26
 
 
27
class TestFilenameSubstitution(tests.TestCaseInTempDir):
 
28
 
 
29
    def test_simple_filename(self):
 
30
        cmd_list = ['kdiff3', '{base}', '{this}', '{other}', '-o', '{result}']
 
31
        args, tmpfile = mergetools._subst_filename(cmd_list, 'test.txt')
 
32
        self.assertEqual(
 
33
            ['kdiff3',
 
34
             'test.txt.BASE',
 
35
             'test.txt.THIS',
 
36
             'test.txt.OTHER',
 
37
             '-o',
 
38
             'test.txt'],
 
39
            args)
 
40
 
 
41
    def test_spaces(self):
 
42
        cmd_list = ['kdiff3', '{base}', '{this}', '{other}', '-o', '{result}']
 
43
        args, tmpfile = mergetools._subst_filename(cmd_list,
 
44
                                                   'file with space.txt')
 
45
        self.assertEqual(
 
46
            ['kdiff3',
 
47
             'file with space.txt.BASE',
 
48
             'file with space.txt.THIS',
 
49
             'file with space.txt.OTHER',
 
50
             '-o',
 
51
             'file with space.txt'],
 
52
            args)
 
53
 
 
54
    def test_spaces_and_quotes(self):
 
55
        cmd_list = ['kdiff3', '{base}', '{this}', '{other}', '-o', '{result}']
 
56
        args, tmpfile = mergetools._subst_filename(cmd_list,
 
57
            'file with "space and quotes".txt')
 
58
        self.assertEqual(
 
59
            ['kdiff3',
 
60
             'file with "space and quotes".txt.BASE',
 
61
             'file with "space and quotes".txt.THIS',
 
62
             'file with "space and quotes".txt.OTHER',
 
63
             '-o',
 
64
             'file with "space and quotes".txt'],
 
65
            args)
 
66
 
 
67
    def test_tempfile(self):
 
68
        self.build_tree(('test.txt', 'test.txt.BASE', 'test.txt.THIS',
 
69
                         'test.txt.OTHER'))
 
70
        cmd_list = ['some_tool', '{this_temp}']
 
71
        args, tmpfile = mergetools._subst_filename(cmd_list, 'test.txt')
 
72
        self.assertPathExists(tmpfile)
 
73
        os.remove(tmpfile)
 
74
 
 
75
 
 
76
class TestCheckAvailability(tests.TestCaseInTempDir):
 
77
 
 
78
    def test_full_path(self):
 
79
        self.assertTrue(mergetools.check_availability(sys.executable))
 
80
 
 
81
    def test_exe_on_path(self):
 
82
        self.assertTrue(mergetools.check_availability('python'))
 
83
 
 
84
    def test_nonexistent(self):
 
85
        self.assertFalse(mergetools.check_availability('DOES NOT EXIST'))
 
86
 
 
87
    def test_non_executable(self):
 
88
        f, name = tempfile.mkstemp()
 
89
        try:
 
90
            self.log('temp filename: %s', name)
 
91
            self.assertFalse(mergetools.check_availability(name))
 
92
        finally:
 
93
            os.close(f)
 
94
            os.unlink(name)
 
95
 
 
96
 
 
97
class TestInvoke(tests.TestCaseInTempDir):
 
98
    def setUp(self):
 
99
        super(tests.TestCaseInTempDir, self).setUp()
 
100
        self._exe = None
 
101
        self._args = None
 
102
        self.build_tree_contents((
 
103
            ('test.txt', 'stuff'),
 
104
            ('test.txt.BASE', 'base stuff'),
 
105
            ('test.txt.THIS', 'this stuff'),
 
106
            ('test.txt.OTHER', 'other stuff'),
 
107
        ))
 
108
 
 
109
    def test_invoke_expands_exe_path(self):
 
110
        self.overrideEnv('PATH', os.path.dirname(sys.executable))
 
111
        def dummy_invoker(exe, args, cleanup):
 
112
            self._exe = exe
 
113
            self._args = args
 
114
            cleanup(0)
 
115
            return 0
 
116
        command = '%s {result}' % os.path.basename(sys.executable)
 
117
        retcode = mergetools.invoke(command, 'test.txt', dummy_invoker)
 
118
        self.assertEqual(0, retcode)
 
119
        self.assertEqual(sys.executable, self._exe)
 
120
        self.assertEqual(['test.txt'], self._args)
 
121
 
 
122
    def test_success(self):
 
123
        def dummy_invoker(exe, args, cleanup):
 
124
            self._exe = exe
 
125
            self._args = args
 
126
            cleanup(0)
 
127
            return 0
 
128
        retcode = mergetools.invoke('tool {result}', 'test.txt', dummy_invoker)
 
129
        self.assertEqual(0, retcode)
 
130
        self.assertEqual('tool', self._exe)
 
131
        self.assertEqual(['test.txt'], self._args)
 
132
 
 
133
    def test_failure(self):
 
134
        def dummy_invoker(exe, args, cleanup):
 
135
            self._exe = exe
 
136
            self._args = args
 
137
            cleanup(1)
 
138
            return 1
 
139
        retcode = mergetools.invoke('tool {result}', 'test.txt', dummy_invoker)
 
140
        self.assertEqual(1, retcode)
 
141
        self.assertEqual('tool', self._exe)
 
142
        self.assertEqual(['test.txt'], self._args)
 
143
 
 
144
    def test_success_tempfile(self):
 
145
        def dummy_invoker(exe, args, cleanup):
 
146
            self._exe = exe
 
147
            self._args = args
 
148
            self.assertPathExists(args[0])
 
149
            f = open(args[0], 'wt')
 
150
            f.write('temp stuff')
 
151
            f.close()
 
152
            cleanup(0)
 
153
            return 0
 
154
        retcode = mergetools.invoke('tool {this_temp}', 'test.txt',
 
155
                                    dummy_invoker)
 
156
        self.assertEqual(0, retcode)
 
157
        self.assertEqual('tool', self._exe)
 
158
        self.assertPathDoesNotExist(self._args[0])
 
159
        self.assertFileEqual('temp stuff', 'test.txt')
 
160
 
 
161
    def test_failure_tempfile(self):
 
162
        def dummy_invoker(exe, args, cleanup):
 
163
            self._exe = exe
 
164
            self._args = args
 
165
            self.assertPathExists(args[0])
 
166
            self.log(repr(args))
 
167
            f = open(args[0], 'wt')
 
168
            self.log(repr(f))
 
169
            f.write('temp stuff')
 
170
            f.close()
 
171
            cleanup(1)
 
172
            return 1
 
173
        retcode = mergetools.invoke('tool {this_temp}', 'test.txt',
 
174
                                    dummy_invoker)
 
175
        self.assertEqual(1, retcode)
 
176
        self.assertEqual('tool', self._exe)
 
177
        self.assertFileEqual('stuff', 'test.txt')