~bzr-pqm/bzr/bzr.dev

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# Copyright (C) 2010 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

import os
import sys
import tempfile

from bzrlib import (
    mergetools,
    tests
)


class TestFilenameSubstitution(tests.TestCaseInTempDir):

    def test_simple_filename(self):
        cmd_list = ['kdiff3', '{base}', '{this}', '{other}', '-o', '{result}']
        args, tmpfile = mergetools._subst_filename(cmd_list, 'test.txt')
        self.assertEqual(
            ['kdiff3',
             'test.txt.BASE',
             'test.txt.THIS',
             'test.txt.OTHER',
             '-o',
             'test.txt'],
            args)

    def test_spaces(self):
        cmd_list = ['kdiff3', '{base}', '{this}', '{other}', '-o', '{result}']
        args, tmpfile = mergetools._subst_filename(cmd_list,
                                                   'file with space.txt')
        self.assertEqual(
            ['kdiff3',
             'file with space.txt.BASE',
             'file with space.txt.THIS',
             'file with space.txt.OTHER',
             '-o',
             'file with space.txt'],
            args)

    def test_spaces_and_quotes(self):
        cmd_list = ['kdiff3', '{base}', '{this}', '{other}', '-o', '{result}']
        args, tmpfile = mergetools._subst_filename(cmd_list,
            'file with "space and quotes".txt')
        self.assertEqual(
            ['kdiff3',
             'file with "space and quotes".txt.BASE',
             'file with "space and quotes".txt.THIS',
             'file with "space and quotes".txt.OTHER',
             '-o',
             'file with "space and quotes".txt'],
            args)

    def test_tempfile(self):
        self.build_tree(('test.txt', 'test.txt.BASE', 'test.txt.THIS',
                         'test.txt.OTHER'))
        cmd_list = ['some_tool', '{this_temp}']
        args, tmpfile = mergetools._subst_filename(cmd_list, 'test.txt')
        self.assertPathExists(tmpfile)
        os.remove(tmpfile)


class TestCheckAvailability(tests.TestCaseInTempDir):

    def test_full_path(self):
        self.assertTrue(mergetools.check_availability(sys.executable))

    def test_exe_on_path(self):
        self.assertTrue(mergetools.check_availability('python'))

    def test_nonexistent(self):
        self.assertFalse(mergetools.check_availability('DOES NOT EXIST'))

    def test_non_executable(self):
        f, name = tempfile.mkstemp()
        try:
            self.log('temp filename: %s', name)
            self.assertFalse(mergetools.check_availability(name))
        finally:
            os.close(f)
            os.unlink(name)


class TestInvoke(tests.TestCaseInTempDir):
    def setUp(self):
        super(tests.TestCaseInTempDir, self).setUp()
        self._exe = None
        self._args = None
        self.build_tree_contents((
            ('test.txt', 'stuff'),
            ('test.txt.BASE', 'base stuff'),
            ('test.txt.THIS', 'this stuff'),
            ('test.txt.OTHER', 'other stuff'),
        ))

    def test_invoke_expands_exe_path(self):
        self.overrideEnv('PATH', os.path.dirname(sys.executable))
        def dummy_invoker(exe, args, cleanup):
            self._exe = exe
            self._args = args
            cleanup(0)
            return 0
        command = '%s {result}' % os.path.basename(sys.executable)
        retcode = mergetools.invoke(command, 'test.txt', dummy_invoker)
        self.assertEqual(0, retcode)
        self.assertEqual(sys.executable, self._exe)
        self.assertEqual(['test.txt'], self._args)

    def test_success(self):
        def dummy_invoker(exe, args, cleanup):
            self._exe = exe
            self._args = args
            cleanup(0)
            return 0
        retcode = mergetools.invoke('tool {result}', 'test.txt', dummy_invoker)
        self.assertEqual(0, retcode)
        self.assertEqual('tool', self._exe)
        self.assertEqual(['test.txt'], self._args)

    def test_failure(self):
        def dummy_invoker(exe, args, cleanup):
            self._exe = exe
            self._args = args
            cleanup(1)
            return 1
        retcode = mergetools.invoke('tool {result}', 'test.txt', dummy_invoker)
        self.assertEqual(1, retcode)
        self.assertEqual('tool', self._exe)
        self.assertEqual(['test.txt'], self._args)

    def test_success_tempfile(self):
        def dummy_invoker(exe, args, cleanup):
            self._exe = exe
            self._args = args
            self.assertPathExists(args[0])
            f = open(args[0], 'wt')
            f.write('temp stuff')
            f.close()
            cleanup(0)
            return 0
        retcode = mergetools.invoke('tool {this_temp}', 'test.txt',
                                    dummy_invoker)
        self.assertEqual(0, retcode)
        self.assertEqual('tool', self._exe)
        self.assertPathDoesNotExist(self._args[0])
        self.assertFileEqual('temp stuff', 'test.txt')

    def test_failure_tempfile(self):
        def dummy_invoker(exe, args, cleanup):
            self._exe = exe
            self._args = args
            self.assertPathExists(args[0])
            self.log(repr(args))
            f = open(args[0], 'wt')
            self.log(repr(f))
            f.write('temp stuff')
            f.close()
            cleanup(1)
            return 1
        retcode = mergetools.invoke('tool {this_temp}', 'test.txt',
                                    dummy_invoker)
        self.assertEqual(1, retcode)
        self.assertEqual('tool', self._exe)
        self.assertFileEqual('stuff', 'test.txt')