~bzr-pqm/bzr/bzr.dev

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