~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_mergetools.py

(jameinel) Allow 'bzr serve' to interpret SIGHUP as a graceful shutdown.
 (bug #795025) (John A Meinel)

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 re
 
19
import sys
 
20
import tempfile
 
21
 
 
22
from bzrlib import (
 
23
    mergetools,
 
24
    tests
 
25
)
 
26
from bzrlib.tests.features import backslashdir_feature
 
27
 
 
28
 
 
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')
 
34
        self.assertEqual(
 
35
            ['kdiff3',
 
36
             'test.txt.BASE',
 
37
             'test.txt.THIS',
 
38
             'test.txt.OTHER',
 
39
             '-o',
 
40
             'test.txt'],
 
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):
 
70
        self.build_tree(('test.txt', 'test.txt.BASE', 'test.txt.THIS',
 
71
                         'test.txt.OTHER'))
 
72
        cmd_list = ['some_tool', '{this_temp}']
 
73
        args, tmpfile = mergetools._subst_filename(cmd_list, 'test.txt')
 
74
        self.assertPathExists(tmpfile)
 
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):
 
84
        if sys.platform == 'win32':
 
85
            exe = 'cmd.exe'
 
86
        else:
 
87
            exe = 'sh'
 
88
        self.assertTrue(mergetools.check_availability(exe))
 
89
 
 
90
    def test_nonexistent(self):
 
91
        self.assertFalse(mergetools.check_availability('DOES NOT EXIST'))
 
92
 
 
93
    def test_non_executable(self):
 
94
        f, name = tempfile.mkstemp()
 
95
        try:
 
96
            self.log('temp filename: %s', name)
 
97
            self.assertFalse(mergetools.check_availability(name))
 
98
        finally:
 
99
            os.close(f)
 
100
            os.unlink(name)
 
101
 
 
102
 
 
103
class TestInvoke(tests.TestCaseInTempDir):
 
104
    def setUp(self):
 
105
        super(tests.TestCaseInTempDir, self).setUp()
 
106
        self._exe = None
 
107
        self._args = None
 
108
        self.build_tree_contents((
 
109
            ('test.txt', 'stuff'),
 
110
            ('test.txt.BASE', 'base stuff'),
 
111
            ('test.txt.THIS', 'this stuff'),
 
112
            ('test.txt.OTHER', 'other stuff'),
 
113
        ))
 
114
        
 
115
    def test_success(self):
 
116
        def dummy_invoker(exe, args, cleanup):
 
117
            self._exe = exe
 
118
            self._args = args
 
119
            cleanup(0)
 
120
            return 0
 
121
        retcode = mergetools.invoke('tool {result}', 'test.txt', dummy_invoker)
 
122
        self.assertEqual(0, retcode)
 
123
        self.assertEqual('tool', self._exe)
 
124
        self.assertEqual(['test.txt'], self._args)
 
125
    
 
126
    def test_failure(self):
 
127
        def dummy_invoker(exe, args, cleanup):
 
128
            self._exe = exe
 
129
            self._args = args
 
130
            cleanup(1)
 
131
            return 1
 
132
        retcode = mergetools.invoke('tool {result}', 'test.txt', dummy_invoker)
 
133
        self.assertEqual(1, retcode)
 
134
        self.assertEqual('tool', self._exe)
 
135
        self.assertEqual(['test.txt'], self._args)
 
136
 
 
137
    def test_success_tempfile(self):
 
138
        def dummy_invoker(exe, args, cleanup):
 
139
            self._exe = exe
 
140
            self._args = args
 
141
            self.assertPathExists(args[0])
 
142
            f = open(args[0], 'wt')
 
143
            f.write('temp stuff')
 
144
            f.close()
 
145
            cleanup(0)
 
146
            return 0
 
147
        retcode = mergetools.invoke('tool {this_temp}', 'test.txt',
 
148
                                    dummy_invoker)
 
149
        self.assertEqual(0, retcode)
 
150
        self.assertEqual('tool', self._exe)
 
151
        self.assertPathDoesNotExist(self._args[0])
 
152
        self.assertFileEqual('temp stuff', 'test.txt')
 
153
    
 
154
    def test_failure_tempfile(self):
 
155
        def dummy_invoker(exe, args, cleanup):
 
156
            self._exe = exe
 
157
            self._args = args
 
158
            self.assertPathExists(args[0])
 
159
            self.log(repr(args))
 
160
            f = open(args[0], 'wt')
 
161
            self.log(repr(f))
 
162
            f.write('temp stuff')
 
163
            f.close()
 
164
            cleanup(1)
 
165
            return 1
 
166
        retcode = mergetools.invoke('tool {this_temp}', 'test.txt',
 
167
                                    dummy_invoker)
 
168
        self.assertEqual(1, retcode)
 
169
        self.assertEqual('tool', self._exe)
 
170
        self.assertFileEqual('stuff', 'test.txt')