/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/tests/test_mergetools.py

  • Committer: Jelmer Vernooij
  • Date: 2020-04-05 19:11:34 UTC
  • mto: (7490.7.16 work)
  • mto: This revision was merged to the branch mainline in revision 7501.
  • Revision ID: jelmer@jelmer.uk-20200405191134-0aebh8ikiwygxma5
Populate the .gitignore file.

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 .. 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(
 
57
            cmd_list, '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_nonexistent(self):
 
82
        self.assertFalse(mergetools.check_availability('DOES NOT EXIST'))
 
83
 
 
84
    def test_non_executable(self):
 
85
        f, name = tempfile.mkstemp()
 
86
        try:
 
87
            self.log('temp filename: %s', name)
 
88
            self.assertFalse(mergetools.check_availability(name))
 
89
        finally:
 
90
            os.close(f)
 
91
            os.unlink(name)
 
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((
 
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'),
 
104
        ))
 
105
 
 
106
    def test_invoke_expands_exe_path(self):
 
107
        self.overrideEnv('PATH', os.path.dirname(sys.executable))
 
108
 
 
109
        def dummy_invoker(exe, args, cleanup):
 
110
            self._exe = exe
 
111
            self._args = args
 
112
            cleanup(0)
 
113
            return 0
 
114
        command = '%s {result}' % os.path.basename(sys.executable)
 
115
        retcode = mergetools.invoke(command, 'test.txt', dummy_invoker)
 
116
        self.assertEqual(0, retcode)
 
117
        self.assertEqual(sys.executable, self._exe)
 
118
        self.assertEqual(['test.txt'], self._args)
 
119
 
 
120
    def test_success(self):
 
121
        def dummy_invoker(exe, args, cleanup):
 
122
            self._exe = exe
 
123
            self._args = args
 
124
            cleanup(0)
 
125
            return 0
 
126
        retcode = mergetools.invoke('tool {result}', 'test.txt', dummy_invoker)
 
127
        self.assertEqual(0, retcode)
 
128
        self.assertEqual('tool', self._exe)
 
129
        self.assertEqual(['test.txt'], self._args)
 
130
 
 
131
    def test_failure(self):
 
132
        def dummy_invoker(exe, args, cleanup):
 
133
            self._exe = exe
 
134
            self._args = args
 
135
            cleanup(1)
 
136
            return 1
 
137
        retcode = mergetools.invoke('tool {result}', 'test.txt', dummy_invoker)
 
138
        self.assertEqual(1, retcode)
 
139
        self.assertEqual('tool', self._exe)
 
140
        self.assertEqual(['test.txt'], self._args)
 
141
 
 
142
    def test_success_tempfile(self):
 
143
        def dummy_invoker(exe, args, cleanup):
 
144
            self._exe = exe
 
145
            self._args = args
 
146
            self.assertPathExists(args[0])
 
147
            with open(args[0], 'wt') as f:
 
148
                f.write('temp stuff')
 
149
            cleanup(0)
 
150
            return 0
 
151
        retcode = mergetools.invoke('tool {this_temp}', 'test.txt',
 
152
                                    dummy_invoker)
 
153
        self.assertEqual(0, retcode)
 
154
        self.assertEqual('tool', self._exe)
 
155
        self.assertPathDoesNotExist(self._args[0])
 
156
        self.assertFileEqual(b'temp stuff', 'test.txt')
 
157
 
 
158
    def test_failure_tempfile(self):
 
159
        def dummy_invoker(exe, args, cleanup):
 
160
            self._exe = exe
 
161
            self._args = args
 
162
            self.assertPathExists(args[0])
 
163
            self.log(repr(args))
 
164
            with open(args[0], 'wt') as f:
 
165
                self.log(repr(f))
 
166
                f.write('temp stuff')
 
167
            cleanup(1)
 
168
            return 1
 
169
        retcode = mergetools.invoke('tool {this_temp}', 'test.txt',
 
170
                                    dummy_invoker)
 
171
        self.assertEqual(1, retcode)
 
172
        self.assertEqual('tool', self._exe)
 
173
        self.assertFileEqual(b'stuff', 'test.txt')