~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_mergetools.py

  • Committer: Gordon Tyler
  • Date: 2011-01-20 04:44:14 UTC
  • mto: This revision was merged to the branch mainline in revision 5632.
  • Revision ID: gordon@doxxx.net-20110120044414-x8vislng3ukcfr1d
Simplified mergetools module down to functions which deal with command lines -- no MergeTool class.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
import tempfile
21
21
 
22
22
from bzrlib import (
23
 
    config,
24
23
    mergetools,
25
24
    tests
26
25
)
27
26
from bzrlib.tests.features import backslashdir_feature
28
27
 
29
28
 
30
 
class TestBasics(tests.TestCase):
31
 
 
32
 
    def setUp(self):
33
 
        super(TestBasics, self).setUp()
34
 
        self.tool = mergetools.MergeTool('sometool',
35
 
            '/path/to/tool --opt {base} -x {this} {other} --stuff {result}')
36
 
 
37
 
    def test_get_commandline(self):
38
 
        self.assertEqual(
39
 
            '/path/to/tool --opt {base} -x {this} {other} --stuff {result}',
40
 
            self.tool.command_line)
41
 
        
42
 
    def test_set_commandline(self):
43
 
        self.tool.command_line = "/path/to/tool blah"
44
 
        self.assertEqual("/path/to/tool blah", self.tool.command_line)
45
 
        self.assertEqual(['/path/to/tool', 'blah'], self.tool._cmd_list)
46
 
 
47
 
    def test_get_name(self):
48
 
        self.assertEqual('sometool', self.tool.name)
49
 
 
50
 
 
51
 
class TestUnicodeBasics(tests.TestCase):
52
 
 
53
 
    def setUp(self):
54
 
        super(TestUnicodeBasics, self).setUp()
55
 
        self.tool = mergetools.MergeTool(
56
 
            u'someb\u0414r',
57
 
            u'/path/to/b\u0414r --opt {base} -x {this} {other}'
58
 
            ' --stuff {result}')
59
 
 
60
 
    def test_get_commandline(self):
61
 
        self.assertEqual(
62
 
            u'/path/to/b\u0414r --opt {base} -x {this} {other}'
63
 
            ' --stuff {result}',
64
 
            self.tool.command_line)
65
 
 
66
 
    def test_get_name(self):
67
 
        self.assertEqual(u'someb\u0414r', self.tool.name)
68
 
 
69
 
 
70
 
class TestMergeToolOperations(tests.TestCaseInTempDir):
71
 
 
72
 
    def test_filename_substitution(self):
73
 
        def dummy_invoker(executable, args, cleanup):
74
 
            self._commandline = [executable] + args
75
 
            cleanup(0)
76
 
        mt = mergetools.MergeTool('kdiff3',
77
 
                                  'kdiff3 {base} {this} {other} -o {result}')
78
 
        mt.invoke('test.txt', dummy_invoker)
 
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')
79
34
        self.assertEqual(
80
35
            ['kdiff3',
81
36
             'test.txt.BASE',
83
38
             'test.txt.OTHER',
84
39
             '-o',
85
40
             'test.txt'],
86
 
            self._commandline)
87
 
        mt.invoke('file with space.txt', dummy_invoker)
88
 
        self.assertEqual(
89
 
            ['kdiff3',
90
 
             "file with space.txt.BASE",
91
 
             "file with space.txt.THIS",
92
 
             "file with space.txt.OTHER",
93
 
             '-o',
94
 
             "file with space.txt"],
95
 
            self._commandline)
96
 
        mt.invoke('file with "space and quotes".txt', dummy_invoker)
97
 
        self.assertEqual(
98
 
            ['kdiff3',
99
 
             "file with \"space and quotes\".txt.BASE",
100
 
             "file with \"space and quotes\".txt.THIS",
101
 
             "file with \"space and quotes\".txt.OTHER",
102
 
             '-o',
103
 
             "file with \"space and quotes\".txt"],
104
 
            self._commandline)
105
 
 
106
 
    def test_expand_commandline_tempfile(self):
107
 
        def dummy_invoker(executable, args, cleanup):
108
 
            self.assertEqual('some_tool', executable)
109
 
            self.failUnlessExists(args[0])
110
 
            cleanup(0)
111
 
            self._tmp_file = args[0]
 
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):
112
70
        self.build_tree(('test.txt', 'test.txt.BASE', 'test.txt.THIS',
113
71
                         'test.txt.OTHER'))
114
 
        mt = mergetools.MergeTool('some_tool', 'some_tool {this_temp}')
115
 
        mt.invoke('test.txt', dummy_invoker)
116
 
        self.failIfExists(self._tmp_file)
117
 
 
118
 
    def test_is_available_full_tool_path(self):
119
 
        mt = mergetools.MergeTool(None, sys.executable)
120
 
        self.assertTrue(mt.is_available())
121
 
 
122
 
    def test_is_available_tool_on_path(self):
123
 
        mt = mergetools.MergeTool(None, os.path.basename(sys.executable))
124
 
        self.assertTrue(mt.is_available())
125
 
 
126
 
    def test_is_available_nonexistent(self):
127
 
        mt = mergetools.MergeTool(None, "ThisExecutableShouldReallyNotExist")
128
 
        self.assertFalse(mt.is_available())
129
 
 
130
 
    def test_is_available_non_executable(self):
 
72
        cmd_list = ['some_tool', '{this_temp}']
 
73
        args, tmpfile = mergetools._subst_filename(cmd_list, 'test.txt')
 
74
        self.failUnlessExists(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
        self.assertTrue(mergetools.check_availability(
 
85
            os.path.basename(sys.executable)))
 
86
 
 
87
    def test_nonexistent(self):
 
88
        self.assertFalse(mergetools.check_availability('DOES NOT EXIST'))
 
89
 
 
90
    def test_non_executable(self):
131
91
        f, name = tempfile.mkstemp()
132
92
        try:
133
93
            self.log('temp filename: %s', name)
134
 
            mt = mergetools.MergeTool('temp', name)
135
 
            self.assertFalse(mt.is_available())
 
94
            self.assertFalse(mergetools.check_availability(name))
136
95
        finally:
137
96
            os.close(f)
138
97
            os.unlink(name)
 
98
 
 
99
 
 
100
class TestInvoke(tests.TestCaseInTempDir):
 
101
    def setUp(self):
 
102
        super(tests.TestCaseInTempDir, self).setUp()
 
103
        self._exe = None
 
104
        self._args = None
 
105
        self.build_tree_contents((
 
106
            ('test.txt', 'stuff'),
 
107
            ('test.txt.BASE', 'base stuff'),
 
108
            ('test.txt.THIS', 'this stuff'),
 
109
            ('test.txt.OTHER', 'other stuff'),
 
110
        ))
 
111
        
 
112
    def test_success(self):
 
113
        def dummy_invoker(exe, args, cleanup):
 
114
            self._exe = exe
 
115
            self._args = args
 
116
            cleanup(0)
 
117
            return 0
 
118
        retcode = mergetools.invoke('tool {result}', 'test.txt', dummy_invoker)
 
119
        self.assertEqual(0, retcode)
 
120
        self.assertEqual('tool', self._exe)
 
121
        self.assertEqual(['test.txt'], self._args)
 
122
    
 
123
    def test_failure(self):
 
124
        def dummy_invoker(exe, args, cleanup):
 
125
            self._exe = exe
 
126
            self._args = args
 
127
            cleanup(1)
 
128
            return 1
 
129
        retcode = mergetools.invoke('tool {result}', 'test.txt', dummy_invoker)
 
130
        self.assertEqual(1, retcode)
 
131
        self.assertEqual('tool', self._exe)
 
132
        self.assertEqual(['test.txt'], self._args)
 
133
 
 
134
    def test_success_tempfile(self):
 
135
        def dummy_invoker(exe, args, cleanup):
 
136
            self._exe = exe
 
137
            self._args = args
 
138
            self.failUnlessExists(args[0])
 
139
            f = open(args[0], 'wt')
 
140
            f.write('temp stuff')
 
141
            f.close()
 
142
            cleanup(0)
 
143
            return 0
 
144
        retcode = mergetools.invoke('tool {this_temp}', 'test.txt',
 
145
                                    dummy_invoker)
 
146
        self.assertEqual(0, retcode)
 
147
        self.assertEqual('tool', self._exe)
 
148
        self.failIfExists(self._args[0])
 
149
        self.assertFileEqual('temp stuff', 'test.txt')
 
150
    
 
151
    def test_failure_tempfile(self):
 
152
        def dummy_invoker(exe, args, cleanup):
 
153
            self._exe = exe
 
154
            self._args = args
 
155
            self.failUnlessExists(args[0])
 
156
            self.log(repr(args))
 
157
            f = open(args[0], 'wt')
 
158
            self.log(repr(f))
 
159
            f.write('temp stuff')
 
160
            f.close()
 
161
            cleanup(1)
 
162
            return 1
 
163
        retcode = mergetools.invoke('tool {this_temp}', 'test.txt',
 
164
                                    dummy_invoker)
 
165
        self.assertEqual(1, retcode)
 
166
        self.assertEqual('tool', self._exe)
 
167
        self.assertFileEqual('stuff', 'test.txt')