test: ignore the real XDG_CONFIG_HOME during tests
[platform/upstream/libxkbcommon.git] / 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 import tempfile
31
32 try:
33     import pytest
34 except ImportError:
35     print('Failed to import pytest. Skipping.', file=sys.stderr)
36     sys.exit(77)
37
38
39 top_builddir = os.environ['top_builddir']
40
41 logging.basicConfig(level=logging.DEBUG)
42 logger = logging.getLogger('test')
43 logger.setLevel(logging.DEBUG)
44
45 # Permutation of RMLVO that we use in multiple tests
46 rmlvos = [list(x) for x in itertools.permutations(
47     ['--rules=evdev', '--model=pc104',
48      '--layout=fr', '--options=eurosign:5']
49 )]
50
51
52 def _disable_coredump():
53     resource.setrlimit(resource.RLIMIT_CORE, (0, 0))
54
55
56 def run_command(args):
57     logger.debug('run command: {}'.format(' '.join(args)))
58
59     try:
60         p = subprocess.run(args, preexec_fn=_disable_coredump,
61                            capture_output=True, text=True,
62                            timeout=0.7)
63         return p.returncode, p.stdout, p.stderr
64     except subprocess.TimeoutExpired as e:
65         return 0, e.stdout, e.stderr
66
67
68 class XkbcliTool:
69     xkbcli_tool = 'xkbcli'
70     subtool = None
71
72     def __init__(self, subtool=None):
73         self.tool_path = top_builddir
74         self.subtool = subtool
75
76     def run_command(self, args):
77         if self.subtool is not None:
78             tool = '{}-{}'.format(self.xkbcli_tool, self.subtool)
79         else:
80             tool = self.xkbcli_tool
81         args = [os.path.join(self.tool_path, tool)] + args
82
83         return run_command(args)
84
85     def run_command_success(self, args):
86         rc, stdout, stderr = self.run_command(args)
87         assert rc == 0, (stdout, stderr)
88         return stdout, stderr
89
90     def run_command_invalid(self, args):
91         rc, stdout, stderr = self.run_command(args)
92         assert rc == 2, (rc, stdout, stderr)
93         return rc, stdout, stderr
94
95     def run_command_unrecognized_option(self, args):
96         rc, stdout, stderr = self.run_command(args)
97         assert rc == 2, (rc, stdout, stderr)
98         assert stdout.startswith('Usage') or stdout == ''
99         assert 'unrecognized option' in stderr
100
101     def run_command_missing_arg(self, args):
102         rc, stdout, stderr = self.run_command(args)
103         assert rc == 2, (rc, stdout, stderr)
104         assert stdout.startswith('Usage') or stdout == ''
105         assert 'requires an argument' in stderr
106
107
108 def get_tool(subtool=None):
109     return XkbcliTool(subtool)
110
111
112 def get_all_tools():
113     return [get_tool(x) for x in [None, 'list',
114                                   'compile-keymap',
115                                   'how-to-type',
116                                   'interactive-evdev',
117                                   'interactive-wayland',
118                                   'interactive-x11']]
119
120
121 @pytest.fixture
122 def xkbcli():
123     return get_tool()
124
125
126 @pytest.fixture
127 def xkbcli_list():
128     return get_tool('list')
129
130
131 @pytest.fixture
132 def xkbcli_how_to_type():
133     return get_tool('how-to-type')
134
135
136 @pytest.fixture
137 def xkbcli_compile_keymap():
138     return get_tool('compile-keymap')
139
140
141 @pytest.fixture
142 def xkbcli_interactive_evdev():
143     return get_tool('interactive-evdev')
144
145
146 @pytest.fixture
147 def xkbcli_interactive_x11():
148     return get_tool('interactive-x11')
149
150
151 @pytest.fixture
152 def xkbcli_interactive_wayland():
153     return get_tool('interactive-wayland')
154
155
156 # --help is supported by all tools
157 @pytest.mark.parametrize('tool', get_all_tools())
158 def test_help(tool):
159     stdout, stderr = tool.run_command_success(['--help'])
160     assert stdout.startswith('Usage:')
161     assert stderr == ''
162
163
164 # --foobar generates "Usage:" for all tools
165 @pytest.mark.parametrize('tool', get_all_tools())
166 def test_invalid_option(tool):
167     tool.run_command_unrecognized_option(['--foobar'])
168
169
170 # xkbcli --version
171 def test_xkbcli_version(xkbcli):
172     stdout, stderr = xkbcli.run_command_success(['--version'])
173     assert stdout.startswith('0')
174     assert stderr == ''
175
176
177 def test_xkbcli_too_many_args(xkbcli):
178     xkbcli.run_command_invalid(['a'] * 64)
179
180
181 @pytest.mark.parametrize('args', [['--verbose'],
182                                   ['--rmlvo'],
183                                   # ['--kccgst'],
184                                   ['--verbose', '--rmlvo'],
185                                   # ['--verbose', '--kccgst'],
186                                   ])
187 def test_compile_keymap_args(xkbcli_compile_keymap, args):
188     xkbcli_compile_keymap.run_command_success(args)
189
190
191 @pytest.mark.parametrize('rmlvos', rmlvos)
192 def test_compile_keymap_rmlvo(xkbcli_compile_keymap, rmlvos):
193     xkbcli_compile_keymap.run_command_success(rmlvos)
194
195
196 @pytest.mark.parametrize('args', [['--include', '.', '--include-defaults'],
197                                   ['--include', '/tmp', '--include-defaults'],
198                                   ])
199 def test_compile_keymap_include(xkbcli_compile_keymap, args):
200     # Succeeds thanks to include-defaults
201     xkbcli_compile_keymap.run_command_success(args)
202
203
204 def test_compile_keymap_include_invalid(xkbcli_compile_keymap):
205     # A non-directory is rejected by default
206     args = ['--include', '/proc/version']
207     rc, stdout, stderr = xkbcli_compile_keymap.run_command(args)
208     assert rc == 1, (stdout, stderr)
209     assert "There are no include paths to search" in stderr
210
211     # A non-existing directory is rejected by default
212     args = ['--include', '/tmp/does/not/exist']
213     rc, stdout, stderr = xkbcli_compile_keymap.run_command(args)
214     assert rc == 1, (stdout, stderr)
215     assert "There are no include paths to search" in stderr
216
217     # Valid dir, but missing files
218     args = ['--include', '/tmp']
219     rc, stdout, stderr = xkbcli_compile_keymap.run_command(args)
220     assert rc == 1, (stdout, stderr)
221     assert "Couldn't look up rules" in stderr
222
223
224 # Unicode codepoint conversions, we support whatever strtol does
225 @pytest.mark.parametrize('args', [['123'], ['0x123'], ['0123']])
226 def test_how_to_type(xkbcli_how_to_type, args):
227     xkbcli_how_to_type.run_command_success(args)
228
229
230 @pytest.mark.parametrize('rmlvos', rmlvos)
231 def test_how_to_type_rmlvo(xkbcli_how_to_type, rmlvos):
232     args = rmlvos + ['0x1234']
233     xkbcli_how_to_type.run_command_success(args)
234
235
236 @pytest.mark.parametrize('args', [['--verbose'],
237                                   ['-v'],
238                                   ['--verbose', '--load-exotic'],
239                                   ['--load-exotic'],
240                                   ['--ruleset=evdev'],
241                                   ['--ruleset=base'],
242                                   ])
243 def test_list_rmlvo(xkbcli_list, args):
244     xkbcli_list.run_command_success(args)
245
246
247 def test_list_rmlvo_includes(xkbcli_list):
248     args = ['/tmp/']
249     xkbcli_list.run_command_success(args)
250
251
252 def test_list_rmlvo_includes_invalid(xkbcli_list):
253     args = ['/proc/version']
254     rc, stdout, stderr = xkbcli_list.run_command(args)
255     assert rc == 1
256     assert "Failed to append include path" in stderr
257
258
259 def test_list_rmlvo_includes_no_defaults(xkbcli_list):
260     args = ['--skip-default-paths', '/tmp']
261     rc, stdout, stderr = xkbcli_list.run_command(args)
262     assert rc == 1
263     assert "Failed to parse XKB description" in stderr
264
265
266 @pytest.mark.skipif(not os.path.exists('/dev/input/event0'), reason='event node required')
267 @pytest.mark.skipif(not os.access('/dev/input/event0', os.R_OK), reason='insufficient permissions')
268 @pytest.mark.parametrize('rmlvos', rmlvos)
269 def test_interactive_evdev_rmlvo(xkbcli_interactive_evdev, rmlvos):
270     return
271     xkbcli_interactive_evdev.run_command_success(rmlvos)
272
273
274 @pytest.mark.skipif(not os.path.exists('/dev/input/event0'),
275                     reason='event node required')
276 @pytest.mark.skipif(not os.access('/dev/input/event0', os.R_OK),
277                     reason='insufficient permissions')
278 @pytest.mark.parametrize('args', [['--report-state-changes'],
279                                   ['--enable-compose'],
280                                   ['--consumed-mode=xkb'],
281                                   ['--consumed-mode=gtk'],
282                                   ['--without-x11-offset'],
283                                   ])
284 def test_interactive_evdev(xkbcli_interactive_evdev, args):
285     # Note: --enable-compose fails if $prefix doesn't have the compose tables
286     # installed
287     xkbcli_interactive_evdev.run_command_success(args)
288
289
290 @pytest.mark.skipif(not os.getenv('DISPLAY'), reason='DISPLAY not set')
291 def test_interactive_x11(xkbcli_interactive_x11):
292     # To be filled in if we handle something other than --help
293     pass
294
295
296 @pytest.mark.skipif(not os.getenv('WAYLAND_DISPLAY'),
297                     reason='WAYLAND_DISPLAY not set')
298 def test_interactive_wayland(xkbcli_interactive_wayland):
299     # To be filled in if we handle something other than --help
300     pass
301
302
303 if __name__ == '__main__':
304     with tempfile.TemporaryDirectory() as tmpdir:
305         # libxkbcommon has fallbacks when XDG_CONFIG_HOME isn't set so we need
306         # to override it with a known (empty) directory. Otherwise our test
307         # behavior depends on the system the test is run on.
308         os.environ['XDG_CONFIG_HOME'] = tmpdir
309
310         sys.exit(pytest.main(args=[__file__]))