/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
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 sys
5321.1.107 by Gordon Tyler
Fixed is_available to properly test for executable-ness and added a test for this.
19
import tempfile
5321.1.69 by Gordon Tyler
Fixed line-endings to be Unix.
20
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
21
from .. import (
5321.1.69 by Gordon Tyler
Fixed line-endings to be Unix.
22
    mergetools,
23
    tests
24
)
25
26
5321.1.116 by Gordon Tyler
Simplified mergetools module down to functions which deal with command lines -- no MergeTool class.
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')
5321.1.69 by Gordon Tyler
Fixed line-endings to be Unix.
32
        self.assertEqual(
33
            ['kdiff3',
34
             'test.txt.BASE',
35
             'test.txt.THIS',
36
             'test.txt.OTHER',
37
             '-o',
38
             'test.txt'],
5321.1.116 by Gordon Tyler
Simplified mergetools module down to functions which deal with command lines -- no MergeTool class.
39
            args)
6511.2.1 by Jelmer Vernooij
Remove some unused imports.
40
5321.1.116 by Gordon Tyler
Simplified mergetools module down to functions which deal with command lines -- no MergeTool class.
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):
5321.1.69 by Gordon Tyler
Fixed line-endings to be Unix.
68
        self.build_tree(('test.txt', 'test.txt.BASE', 'test.txt.THIS',
69
                         'test.txt.OTHER'))
5321.1.116 by Gordon Tyler
Simplified mergetools module down to functions which deal with command lines -- no MergeTool class.
70
        cmd_list = ['some_tool', '{this_temp}']
71
        args, tmpfile = mergetools._subst_filename(cmd_list, 'test.txt')
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
72
        self.assertPathExists(tmpfile)
5321.1.116 by Gordon Tyler
Simplified mergetools module down to functions which deal with command lines -- no MergeTool class.
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_nonexistent(self):
82
        self.assertFalse(mergetools.check_availability('DOES NOT EXIST'))
83
84
    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.
85
        f, name = tempfile.mkstemp()
86
        try:
87
            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.
88
            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.
89
        finally:
90
            os.close(f)
91
            os.unlink(name)
5321.1.116 by Gordon Tyler
Simplified mergetools module down to functions which deal with command lines -- no MergeTool class.
92
93
94
class TestInvoke(tests.TestCaseInTempDir):
95
    def setUp(self):
96
        super(tests.TestCaseInTempDir, self).setUp()
97
        self._exe = None
98
        self._args = None
99
        self.build_tree_contents((
6855.4.1 by Jelmer Vernooij
Yet more bees.
100
            ('test.txt', b'stuff'),
101
            ('test.txt.BASE', b'base stuff'),
102
            ('test.txt.THIS', b'this stuff'),
103
            ('test.txt.OTHER', b'other stuff'),
5321.1.116 by Gordon Tyler
Simplified mergetools module down to functions which deal with command lines -- no MergeTool class.
104
        ))
6511.2.1 by Jelmer Vernooij
Remove some unused imports.
105
6437.44.1 by Gordon Tyler
Backport of fix for bug 939605 to bzr 2.5 series.
106
    def test_invoke_expands_exe_path(self):
107
        self.overrideEnv('PATH', os.path.dirname(sys.executable))
108
        def dummy_invoker(exe, args, cleanup):
109
            self._exe = exe
110
            self._args = args
111
            cleanup(0)
112
            return 0
113
        command = '%s {result}' % os.path.basename(sys.executable)
114
        retcode = mergetools.invoke(command, 'test.txt', dummy_invoker)
115
        self.assertEqual(0, retcode)
116
        self.assertEqual(sys.executable, self._exe)
117
        self.assertEqual(['test.txt'], self._args)
6511.2.1 by Jelmer Vernooij
Remove some unused imports.
118
5321.1.116 by Gordon Tyler
Simplified mergetools module down to functions which deal with command lines -- no MergeTool class.
119
    def test_success(self):
120
        def dummy_invoker(exe, args, cleanup):
121
            self._exe = exe
122
            self._args = args
123
            cleanup(0)
124
            return 0
125
        retcode = mergetools.invoke('tool {result}', 'test.txt', dummy_invoker)
126
        self.assertEqual(0, retcode)
127
        self.assertEqual('tool', self._exe)
128
        self.assertEqual(['test.txt'], self._args)
6511.2.1 by Jelmer Vernooij
Remove some unused imports.
129
5321.1.116 by Gordon Tyler
Simplified mergetools module down to functions which deal with command lines -- no MergeTool class.
130
    def test_failure(self):
131
        def dummy_invoker(exe, args, cleanup):
132
            self._exe = exe
133
            self._args = args
134
            cleanup(1)
135
            return 1
136
        retcode = mergetools.invoke('tool {result}', 'test.txt', dummy_invoker)
137
        self.assertEqual(1, retcode)
138
        self.assertEqual('tool', self._exe)
139
        self.assertEqual(['test.txt'], self._args)
140
141
    def test_success_tempfile(self):
142
        def dummy_invoker(exe, args, cleanup):
143
            self._exe = exe
144
            self._args = args
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
145
            self.assertPathExists(args[0])
6973.7.5 by Jelmer Vernooij
s/file/open.
146
            with open(args[0], 'wt') as f:
147
                f.write('temp stuff')
5321.1.116 by Gordon Tyler
Simplified mergetools module down to functions which deal with command lines -- no MergeTool class.
148
            cleanup(0)
149
            return 0
150
        retcode = mergetools.invoke('tool {this_temp}', 'test.txt',
151
                                    dummy_invoker)
152
        self.assertEqual(0, retcode)
153
        self.assertEqual('tool', self._exe)
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
154
        self.assertPathDoesNotExist(self._args[0])
6973.11.5 by Jelmer Vernooij
Update python3.passing.
155
        self.assertFileEqual(b'temp stuff', 'test.txt')
6511.2.1 by Jelmer Vernooij
Remove some unused imports.
156
5321.1.116 by Gordon Tyler
Simplified mergetools module down to functions which deal with command lines -- no MergeTool class.
157
    def test_failure_tempfile(self):
158
        def dummy_invoker(exe, args, cleanup):
159
            self._exe = exe
160
            self._args = args
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
161
            self.assertPathExists(args[0])
5321.1.116 by Gordon Tyler
Simplified mergetools module down to functions which deal with command lines -- no MergeTool class.
162
            self.log(repr(args))
6973.7.5 by Jelmer Vernooij
s/file/open.
163
            with open(args[0], 'wt') as f:
164
                self.log(repr(f))
165
                f.write('temp stuff')
5321.1.116 by Gordon Tyler
Simplified mergetools module down to functions which deal with command lines -- no MergeTool class.
166
            cleanup(1)
167
            return 1
168
        retcode = mergetools.invoke('tool {this_temp}', 'test.txt',
169
                                    dummy_invoker)
170
        self.assertEqual(1, retcode)
171
        self.assertEqual('tool', self._exe)
6973.11.5 by Jelmer Vernooij
Update python3.passing.
172
        self.assertFileEqual(b'stuff', 'test.txt')