tools: don't mangle the path for tools, just exec directly
[platform/upstream/libxkbcommon.git] / tools / test-tool-option-parsing.py
1 #!/usr/bin/env python3
2 #
3 # Copyright © 2020 Red Hat, Inc.
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining a
6 # copy of this software and associated documentation files (the "Software"),
7 # to deal in the Software without restriction, including without limitation
8 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 # and/or sell copies of the Software, and to permit persons to whom the
10 # Software is furnished to do so, subject to the following conditions:
11 #
12 # The above copyright notice and this permission notice (including the next
13 # paragraph) shall be included in all copies or substantial portions of the
14 # Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19 # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 # DEALINGS IN THE SOFTWARE.
23
24 import itertools
25 import os
26 import resource
27 import sys
28 import subprocess
29 import logging
30
31 try:
32     import pytest
33 except ImportError:
34     print('Failed to import pytest. Skipping.', file=sys.stderr)
35     sys.exit(77)
36
37
38 logging.basicConfig(level=logging.DEBUG)
39 logger = logging.getLogger('test')
40 logger.setLevel(logging.DEBUG)
41
42 # Permutation of RMLVO that we use in multiple tests
43 rmlvos = [list(x) for x in itertools.permutations(
44     ['--rules=evdev', '--model=pc104',
45      '--layout=fr', '--options=eurosign:5']
46 )]
47
48
49 def _disable_coredump():
50     resource.setrlimit(resource.RLIMIT_CORE, (0, 0))
51
52
53 def run_command(args):
54     logger.debug('run command: {}'.format(' '.join(args)))
55
56     try:
57         p = subprocess.run(args, preexec_fn=_disable_coredump,
58                            capture_output=True, text=True,
59                            timeout=0.7)
60         return p.returncode, p.stdout, p.stderr
61     except subprocess.TimeoutExpired as e:
62         return 0, e.stdout, e.stderr
63
64
65 class XkbcliTool:
66     xkbcli_tool = 'xkbcli'
67     subtool = None
68
69     def __init__(self, subtool=None):
70         self.tool_path = "@MESON_BUILD_ROOT@"
71         self.subtool = subtool
72
73     def run_command(self, args):
74         if self.subtool is not None:
75             tool = '{}-{}'.format(self.xkbcli_tool, self.subtool)
76         else:
77             tool = self.xkbcli_tool
78         args = [os.path.join(self.tool_path, tool)] + args
79
80         return run_command(args)
81
82     def run_command_success(self, args):
83         rc, stdout, stderr = self.run_command(args)
84         assert rc == 0, (stdout, stderr)
85         return stdout, stderr
86
87     def run_command_invalid(self, args):
88         rc, stdout, stderr = self.run_command(args)
89         assert rc == 2, (rc, stdout, stderr)
90         return rc, stdout, stderr
91
92     def run_command_unrecognized_option(self, args):
93         rc, stdout, stderr = self.run_command(args)
94         assert rc == 2, (rc, stdout, stderr)
95         assert stdout.startswith('Usage') or stdout == ''
96         assert 'unrecognized option' in stderr
97
98     def run_command_missing_arg(self, args):
99         rc, stdout, stderr = self.run_command(args)
100         assert rc == 2, (rc, stdout, stderr)
101         assert stdout.startswith('Usage') or stdout == ''
102         assert 'requires an argument' in stderr
103
104
105 def get_tool(subtool=None):
106     return XkbcliTool(subtool)
107
108
109 def get_all_tools():
110     return [get_tool(x) for x in [None, 'list',
111                                   'compile-keymap',
112                                   'how-to-type',
113                                   'interactive-evdev',
114                                   'interactive-wayland',
115                                   'interactive-x11']]
116
117
118 @pytest.fixture
119 def xkbcli():
120     return get_tool()
121
122
123 @pytest.fixture
124 def xkbcli_list():
125     return get_tool('list')
126
127
128 @pytest.fixture
129 def xkbcli_how_to_type():
130     return get_tool('how-to-type')
131
132
133 @pytest.fixture
134 def xkbcli_compile_keymap():
135     return get_tool('compile-keymap')
136
137
138 @pytest.fixture
139 def xkbcli_interactive_evdev():
140     return get_tool('interactive-evdev')
141
142
143 @pytest.fixture
144 def xkbcli_interactive_x11():
145     return get_tool('interactive-x11')
146
147
148 @pytest.fixture
149 def xkbcli_interactive_wayland():
150     return get_tool('interactive-wayland')
151
152
153 # --help is supported by all tools
154 @pytest.mark.parametrize('tool', get_all_tools())
155 def test_help(tool):
156     stdout, stderr = tool.run_command_success(['--help'])
157     assert stdout.startswith('Usage:')
158     assert stderr == ''
159
160
161 # --foobar generates "Usage:" for all tools
162 @pytest.mark.parametrize('tool', get_all_tools())
163 def test_invalid_option(tool):
164     tool.run_command_unrecognized_option(['--foobar'])
165
166
167 # xkbcli --version
168 def test_xkbcli_version(xkbcli):
169     stdout, stderr = xkbcli.run_command_success(['--version'])
170     assert stdout.startswith('0')
171     assert stderr == ''
172
173
174 def test_xkbcli_too_many_args(xkbcli):
175     xkbcli.run_command_invalid(['a'] * 64)
176
177
178 @pytest.mark.parametrize('args', [['--verbose'],
179                                   ['--rmlvo'],
180                                   ['--kccgst'],
181                                   ['--verbose', '--rmlvo'],
182                                   ['--verbose', '--kccgst'],
183                                   ])
184 def test_compile_keymap_args(xkbcli_compile_keymap, args):
185     xkbcli_compile_keymap.run_command_success(args)
186
187
188 @pytest.mark.parametrize('rmlvos', rmlvos)
189 def test_compile_keymap_rmlvo(xkbcli_compile_keymap, rmlvos):
190     xkbcli_compile_keymap.run_command_success(rmlvos)
191
192
193 @pytest.mark.parametrize('args', [['--include', '.', '--include-defaults'],
194                                   ['--include', '/tmp', '--include-defaults'],
195                                   ])
196 def test_compile_keymap_include(xkbcli_compile_keymap, args):
197     # Succeeds thanks to include-defaults
198     xkbcli_compile_keymap.run_command_success(args)
199
200
201 def test_compile_keymap_include_invalid(xkbcli_compile_keymap):
202     # A non-directory is rejected by default
203     args = ['--include', '/proc/version']
204     rc, stdout, stderr = xkbcli_compile_keymap.run_command(args)
205     assert rc == 1, (stdout, stderr)
206     assert "There are no include paths to search" in stderr
207
208     # A non-existing directory is rejected by default
209     args = ['--include', '/tmp/does/not/exist']
210     rc, stdout, stderr = xkbcli_compile_keymap.run_command(args)
211     assert rc == 1, (stdout, stderr)
212     assert "There are no include paths to search" in stderr
213
214     # Valid dir, but missing files
215     args = ['--include', '/tmp']
216     rc, stdout, stderr = xkbcli_compile_keymap.run_command(args)
217     assert rc == 1, (stdout, stderr)
218     assert "Couldn't look up rules" in stderr
219
220
221 # Unicode codepoint conversions, we support whatever strtol does
222 @pytest.mark.parametrize('args', [['123'], ['0x123'], ['0123']])
223 def test_how_to_type(xkbcli_how_to_type, args):
224     xkbcli_how_to_type.run_command_success(args)
225
226
227 @pytest.mark.parametrize('rmlvos', rmlvos)
228 def test_how_to_type_rmlvo(xkbcli_how_to_type, rmlvos):
229     args = rmlvos + ['0x1234']
230     xkbcli_how_to_type.run_command_success(args)
231
232
233 @pytest.mark.parametrize('args', [['--verbose'],
234                                   ['-v'],
235                                   ['--verbose', '--load-exotic'],
236                                   ['--load-exotic'],
237                                   ['--ruleset=evdev'],
238                                   ['--ruleset=base'],
239                                   ])
240 def test_list_rmlvo(xkbcli_list, args):
241     xkbcli_list.run_command_success(args)
242
243
244 def test_list_rmlvo_includes(xkbcli_list):
245     args = ['/tmp/']
246     xkbcli_list.run_command_success(args)
247
248
249 def test_list_rmlvo_includes_invalid(xkbcli_list):
250     args = ['/proc/version']
251     rc, stdout, stderr = xkbcli_list.run_command(args)
252     assert rc == 1
253     assert "Failed to append include path" in stderr
254
255
256 def test_list_rmlvo_includes_no_defaults(xkbcli_list):
257     args = ['--skip-default-paths', '/tmp']
258     rc, stdout, stderr = xkbcli_list.run_command(args)
259     assert rc == 1
260     assert "Failed to parse XKB description" in stderr
261
262
263 @pytest.mark.skipif(not os.path.exists('/dev/input/event0'), reason='event node required')
264 @pytest.mark.skipif(not os.access('/dev/input/event0', os.R_OK), reason='insufficient permissions')
265 @pytest.mark.parametrize('rmlvos', rmlvos)
266 def test_interactive_evdev_rmlvo(xkbcli_interactive_evdev, rmlvos):
267     return
268     xkbcli_interactive_evdev.run_command_success(rmlvos)
269
270
271 @pytest.mark.skipif(not os.path.exists('/dev/input/event0'),
272                     reason='event node required')
273 @pytest.mark.skipif(not os.access('/dev/input/event0', os.R_OK),
274                     reason='insufficient permissions')
275 @pytest.mark.parametrize('args', [['--report-state-changes'],
276                                   ['--enable-compose'],
277                                   ['--consumed-mode=xkb'],
278                                   ['--consumed-mode=gtk'],
279                                   ])
280 def test_interactive_evdev(xkbcli_interactive_evdev, args):
281     # Note: --enable-compose fails if $prefix doesn't have the compose tables
282     # installed
283     xkbcli_interactive_evdev.run_command_success(args)
284
285
286 @pytest.mark.skipif(not os.getenv('DISPLAY'), reason='DISPLAY not set')
287 def test_interactive_x11(xkbcli_interactive_x11):
288     # To be filled in if we handle something other than --help
289     pass
290
291
292 @pytest.mark.skipif(not os.getenv('WAYLAND_DISPLAY'),
293                     reason='WAYLAND_DISPLAY not set')
294 def test_interactive_wayland(xkbcli_interactive_wayland):
295     # To be filled in if we handle something other than --help
296     pass
297
298
299 if __name__ == '__main__':
300     pytest.main(args=['@MESON_BUILD_ROOT@'])